2 theory Nested2 = Nested0:
5 lemma [simp]: "t \<in> set ts \<longrightarrow> size t < Suc(term_list_size ts)"
6 by(induct_tac ts, auto)
8 recdef trev "measure size"
10 "trev (App f ts) = App f (rev(map trev ts))"
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
21 lemma "trev(trev t) = t"
22 apply(induct_tac t rule:trev.induct)
24 @{subgoals[display,indent=0]}
25 Both the base case and the induction step fall to simplification:
28 by(simp_all add:rev_map sym[OF map_compose] cong:map_cong)
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.
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"}
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:
49 \emph{Chose your definitions carefully\\
50 because they determine the complexity of your proofs.}
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:
70 consts dummy :: "nat => nat"
74 (hints recdef_cong: map_cong)
77 Or you can declare them globally
78 by giving them the \attrdx{recdef_cong} attribute:
81 declare map_cong[recdef_cong]
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.