doc-src/TutorialI/Recdef/Nested2.thy
author nipkow
Thu, 13 Dec 2001 17:57:55 +0100
changeset 12491 e28870d8b223
parent 11494 23a118849801
child 12815 1f073030b97a
permissions -rw-r--r--
*** empty log message ***
     1 (*<*)
     2 theory Nested2 = Nested0:
     3 (*>*)
     4 
     5 lemma [simp]: "t \<in> set ts \<longrightarrow> size t < Suc(term_list_size ts)"
     6 by(induct_tac ts, auto)
     7 (*<*)
     8 recdef trev "measure size"
     9  "trev (Var x) = Var x"
    10  "trev (App f ts) = App f (rev(map trev ts))"
    11 (*>*)
    12 text{*\noindent
    13 By making this theorem a simplification rule, \isacommand{recdef}
    14 applies it automatically and the definition of @{term"trev"}
    15 succeeds now. As a reward for our effort, we can now prove the desired
    16 lemma directly.  We no longer need the verbose
    17 induction schema for type @{text"term"} and can use the simpler one arising from
    18 @{term"trev"}:
    19 *}
    20 
    21 lemma "trev(trev t) = t"
    22 apply(induct_tac t rule:trev.induct)
    23 txt{*
    24 @{subgoals[display,indent=0]}
    25 Both the base case and the induction step fall to simplification:
    26 *}
    27 
    28 by(simp_all add:rev_map sym[OF map_compose] cong:map_cong)
    29 
    30 text{*\noindent
    31 If the proof of the induction step mystifies you, we recommend that you go through
    32 the chain of simplification steps in detail; you will probably need the help of
    33 @{text"trace_simp"}. Theorem @{thm[source]map_cong} is discussed below.
    34 %\begin{quote}
    35 %{term[display]"trev(trev(App f ts))"}\\
    36 %{term[display]"App f (rev(map trev (rev(map trev ts))))"}\\
    37 %{term[display]"App f (map trev (rev(rev(map trev ts))))"}\\
    38 %{term[display]"App f (map trev (map trev ts))"}\\
    39 %{term[display]"App f (map (trev o trev) ts)"}\\
    40 %{term[display]"App f (map (%x. x) ts)"}\\
    41 %{term[display]"App f ts"}
    42 %\end{quote}
    43 
    44 The definition of @{term"trev"} above is superior to the one in
    45 \S\ref{sec:nested-datatype} because it uses @{term"rev"}
    46 and lets us use existing facts such as \hbox{@{prop"rev(rev xs) = xs"}}.
    47 Thus this proof is a good example of an important principle:
    48 \begin{quote}
    49 \emph{Chose your definitions carefully\\
    50 because they determine the complexity of your proofs.}
    51 \end{quote}
    52 
    53 Let us now return to the question of how \isacommand{recdef} can come up with
    54 sensible termination conditions in the presence of higher-order functions
    55 like @{term"map"}. For a start, if nothing were known about @{term"map"}, then
    56 @{term"map trev ts"} might apply @{term"trev"} to arbitrary terms, and thus
    57 \isacommand{recdef} would try to prove the unprovable @{term"size t < Suc
    58 (term_list_size ts)"}, without any assumption about @{term"t"}.  Therefore
    59 \isacommand{recdef} has been supplied with the congruence theorem
    60 @{thm[source]map_cong}:
    61 @{thm[display,margin=50]"map_cong"[no_vars]}
    62 Its second premise expresses that in @{term"map f xs"},
    63 function @{term"f"} is only applied to elements of list @{term"xs"}.  Congruence
    64 rules for other higher-order functions on lists are similar.  If you get
    65 into a situation where you need to supply \isacommand{recdef} with new
    66 congruence rules, you can append a hint after the end of
    67 the recursion equations:
    68 *}
    69 (*<*)
    70 consts dummy :: "nat => nat"
    71 recdef dummy "{}"
    72 "dummy n = n"
    73 (*>*)
    74 (hints recdef_cong: map_cong)
    75 
    76 text{*\noindent
    77 Or you can declare them globally
    78 by giving them the \attrdx{recdef_cong} attribute:
    79 *}
    80 
    81 declare map_cong[recdef_cong]
    82 
    83 text{*
    84 The @{text cong} and @{text recdef_cong} attributes are
    85 intentionally kept apart because they control different activities, namely
    86 simplification and making recursive definitions.
    87 % The local @{text cong} in
    88 % the hints section of \isacommand{recdef} is merely short for @{text recdef_cong}.
    89 %The simplifier's congruence rules cannot be used by recdef.
    90 %For example the weak congruence rules for if and case would prevent
    91 %recdef from generating sensible termination conditions.
    92 *}
    93 (*<*)end(*>*)