2 theory Nested2 = Nested0:;
6 The termintion condition is easily proved by induction:
9 lemma [simp]: "t \<in> set ts \<longrightarrow> size t < Suc(term_list_size ts)";
10 by(induct_tac ts, auto);
12 recdef trev "measure size"
13 "trev (Var x) = Var x"
14 "trev (App f ts) = App f (rev(map trev ts))";
17 By making this theorem a simplification rule, \isacommand{recdef}
18 applies it automatically and the above definition of @{term"trev"}
19 succeeds now. As a reward for our effort, we can now prove the desired
20 lemma directly. The key is the fact that we no longer need the verbose
21 induction schema for type @{text"term"} but the simpler one arising from
25 lemma "trev(trev t) = t";
26 apply(induct_tac t rule:trev.induct);
28 This leaves us with a trivial base case @{term"trev (trev (Var x)) = Var x"} and the step case
29 @{term[display,margin=60]"ALL t. t : set ts --> trev (trev t) = t ==> trev (trev (App f ts)) = App f ts"}
30 both of which are solved by simplification:
33 by(simp_all add:rev_map sym[OF map_compose] cong:map_cong);
36 If the proof of the induction step mystifies you, we recommend to go through
37 the chain of simplification steps in detail; you will probably need the help of
38 @{text"trace_simp"}. Theorem @{thm[source]map_cong} is discussed below.
40 %{term[display]"trev(trev(App f ts))"}\\
41 %{term[display]"App f (rev(map trev (rev(map trev ts))))"}\\
42 %{term[display]"App f (map trev (rev(rev(map trev ts))))"}\\
43 %{term[display]"App f (map trev (map trev ts))"}\\
44 %{term[display]"App f (map (trev o trev) ts)"}\\
45 %{term[display]"App f (map (%x. x) ts)"}\\
46 %{term[display]"App f ts"}
49 The above definition of @{term"trev"} is superior to the one in
50 \S\ref{sec:nested-datatype} because it brings @{term"rev"} into play, about
51 which already know a lot, in particular @{prop"rev(rev xs) = xs"}.
52 Thus this proof is a good example of an important principle:
54 \emph{Chose your definitions carefully\\
55 because they determine the complexity of your proofs.}
58 Let us now return to the question of how \isacommand{recdef} can come up with
59 sensible termination conditions in the presence of higher-order functions
60 like @{term"map"}. For a start, if nothing were known about @{term"map"},
61 @{term"map trev ts"} might apply @{term"trev"} to arbitrary terms, and thus
62 \isacommand{recdef} would try to prove the unprovable @{term"size t < Suc
63 (term_list_size ts)"}, without any assumption about @{term"t"}. Therefore
64 \isacommand{recdef} has been supplied with the congruence theorem
65 @{thm[source]map_cong}:
66 @{thm[display,margin=50]"map_cong"[no_vars]}
67 Its second premise expresses (indirectly) that the second argument of
68 @{term"map"} is only applied to elements of its third argument. Congruence
69 rules for other higher-order functions on lists look very similar. If you get
70 into a situation where you need to supply \isacommand{recdef} with new
71 congruence rules, you can either append a hint locally
72 to the specific occurrence of \isacommand{recdef}
75 consts dummy :: "nat => nat"
79 (hints recdef_cong: map_cong)
82 or declare them globally
83 by giving them the \isaindexbold{recdef_cong} attribute as in
86 declare map_cong[recdef_cong];
89 Note that the @{text cong} and @{text recdef_cong} attributes are
90 intentionally kept apart because they control different activities, namely
91 simplification and making recursive definitions.
92 % The local @{text cong} in
93 % the hints section of \isacommand{recdef} is merely short for @{text recdef_cong}.
94 %The simplifier's congruence rules cannot be used by recdef.
95 %For example the weak congruence rules for if and case would prevent
96 %recdef from generating sensible termination conditions.