9690
|
1 |
(*<*)
|
|
2 |
theory Nested2 = Nested0:;
|
|
3 |
consts trev :: "('a,'b)term => ('a,'b)term";
|
|
4 |
(*>*)
|
|
5 |
|
|
6 |
text{*\noindent
|
|
7 |
The termintion condition is easily proved by induction:
|
|
8 |
*};
|
|
9 |
|
9754
|
10 |
lemma [simp]: "t \<in> set ts \<longrightarrow> size t < Suc(term_list_size ts)";
|
9690
|
11 |
by(induct_tac ts, auto);
|
|
12 |
(*<*)
|
|
13 |
recdef trev "measure size"
|
|
14 |
"trev (Var x) = Var x"
|
|
15 |
"trev (App f ts) = App f (rev(map trev ts))";
|
|
16 |
(*>*)
|
|
17 |
text{*\noindent
|
|
18 |
By making this theorem a simplification rule, \isacommand{recdef}
|
|
19 |
applies it automatically and the above definition of @{term"trev"}
|
|
20 |
succeeds now. As a reward for our effort, we can now prove the desired
|
|
21 |
lemma directly. The key is the fact that we no longer need the verbose
|
9754
|
22 |
induction schema for type @{name"term"} but the simpler one arising from
|
9690
|
23 |
@{term"trev"}:
|
|
24 |
*};
|
|
25 |
|
|
26 |
lemmas [cong] = map_cong;
|
|
27 |
lemma "trev(trev t) = t";
|
|
28 |
apply(induct_tac t rule:trev.induct);
|
|
29 |
txt{*\noindent
|
|
30 |
This leaves us with a trivial base case @{term"trev (trev (Var x)) = Var x"} and the step case
|
|
31 |
\begin{quote}
|
|
32 |
@{term[display,margin=60]"ALL t. t : set ts --> trev (trev t) = t ==> trev (trev (App f ts)) = App f ts"}
|
|
33 |
\end{quote}
|
|
34 |
both of which are solved by simplification:
|
|
35 |
*};
|
|
36 |
|
9721
|
37 |
by(simp_all add:rev_map sym[OF map_compose]);
|
9690
|
38 |
|
|
39 |
text{*\noindent
|
9721
|
40 |
If the proof of the induction step mystifies you, we recommend to go through
|
9754
|
41 |
the chain of simplification steps in detail; you will probably need the help of
|
|
42 |
@{name"trace_simp"}.
|
9721
|
43 |
%\begin{quote}
|
|
44 |
%{term[display]"trev(trev(App f ts))"}\\
|
|
45 |
%{term[display]"App f (rev(map trev (rev(map trev ts))))"}\\
|
|
46 |
%{term[display]"App f (map trev (rev(rev(map trev ts))))"}\\
|
|
47 |
%{term[display]"App f (map trev (map trev ts))"}\\
|
|
48 |
%{term[display]"App f (map (trev o trev) ts)"}\\
|
|
49 |
%{term[display]"App f (map (%x. x) ts)"}\\
|
|
50 |
%{term[display]"App f ts"}
|
|
51 |
%\end{quote}
|
9690
|
52 |
|
9754
|
53 |
The above definition of @{term"trev"} is superior to the one in
|
|
54 |
\S\ref{sec:nested-datatype} because it brings @{term"rev"} into play, about
|
|
55 |
which already know a lot, in particular @{prop"rev(rev xs) = xs"}.
|
9690
|
56 |
Thus this proof is a good example of an important principle:
|
|
57 |
\begin{quote}
|
|
58 |
\emph{Chose your definitions carefully\\
|
|
59 |
because they determine the complexity of your proofs.}
|
|
60 |
\end{quote}
|
|
61 |
|
9721
|
62 |
Let us now return to the question of how \isacommand{recdef} can come up with
|
|
63 |
sensible termination conditions in the presence of higher-order functions
|
|
64 |
like @{term"map"}. For a start, if nothing were known about @{term"map"},
|
|
65 |
@{term"map trev ts"} might apply @{term"trev"} to arbitrary terms, and thus
|
|
66 |
\isacommand{recdef} would try to prove the unprovable @{term"size t < Suc
|
9754
|
67 |
(term_list_size ts)"}, without any assumption about @{term"t"}. Therefore
|
9721
|
68 |
\isacommand{recdef} has been supplied with the congruence theorem
|
9754
|
69 |
@{name"map_cong"}:
|
9690
|
70 |
\begin{quote}
|
|
71 |
@{thm[display,margin=50]"map_cong"[no_vars]}
|
|
72 |
\end{quote}
|
9721
|
73 |
Its second premise expresses (indirectly) that the second argument of
|
|
74 |
@{term"map"} is only applied to elements of its third argument. Congruence
|
|
75 |
rules for other higher-order functions on lists would look very similar but
|
|
76 |
have not been proved yet because they were never needed. If you get into a
|
|
77 |
situation where you need to supply \isacommand{recdef} with new congruence
|
9690
|
78 |
rules, you can either append the line
|
|
79 |
\begin{ttbox}
|
|
80 |
congs <congruence rules>
|
|
81 |
\end{ttbox}
|
|
82 |
to the specific occurrence of \isacommand{recdef} or declare them globally:
|
|
83 |
\begin{ttbox}
|
|
84 |
lemmas [????????] = <congruence rules>
|
|
85 |
\end{ttbox}
|
|
86 |
|
|
87 |
Note that \isacommand{recdef} feeds on exactly the same \emph{kind} of
|
|
88 |
congruence rules as the simplifier (\S\ref{sec:simp-cong}) but that
|
|
89 |
declaring a congruence rule for the simplifier does not make it
|
|
90 |
available to \isacommand{recdef}, and vice versa. This is intentional.
|
|
91 |
*};
|
|
92 |
(*<*)end;(*>*)
|