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 |
|
|
10 |
lemma [simp]: "t \\<in> set ts \\<longrightarrow> size t < Suc(term_size ts)";
|
|
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
|
|
22 |
induction schema for type \isa{term} but the simpler one arising from
|
|
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 |
|
|
37 |
by(simp_all del:map_compose add:sym[OF map_compose] rev_map);
|
|
38 |
|
|
39 |
text{*\noindent
|
|
40 |
If this surprises you, see Datatype/Nested2......
|
|
41 |
|
|
42 |
The above definition of @{term"trev"} is superior to the one in \S\ref{sec:nested-datatype}
|
|
43 |
because it brings @{term"rev"} into play, about which already know a lot, in particular
|
|
44 |
@{prop"rev(rev xs) = xs"}.
|
|
45 |
Thus this proof is a good example of an important principle:
|
|
46 |
\begin{quote}
|
|
47 |
\emph{Chose your definitions carefully\\
|
|
48 |
because they determine the complexity of your proofs.}
|
|
49 |
\end{quote}
|
|
50 |
|
|
51 |
Let us now return to the question of how \isacommand{recdef} can come up with sensible termination
|
|
52 |
conditions in the presence of higher-order functions like @{term"map"}. For a start, if nothing
|
|
53 |
were known about @{term"map"}, @{term"map trev ts"} might apply @{term"trev"} to arbitrary terms,
|
|
54 |
and thus \isacommand{recdef} would try to prove the unprovable
|
|
55 |
@{term"size t < Suc (term_size ts)"}, without any assumption about \isa{t}.
|
|
56 |
Therefore \isacommand{recdef} has been supplied with the congruence theorem \isa{map\_cong}:
|
|
57 |
\begin{quote}
|
|
58 |
@{thm[display,margin=50]"map_cong"[no_vars]}
|
|
59 |
\end{quote}
|
|
60 |
Its second premise expresses (indirectly) that the second argument of @{term"map"} is only applied
|
|
61 |
to elements of its third argument. Congruence rules for other higher-order functions on lists would
|
|
62 |
look very similar but have not been proved yet because they were never needed.
|
|
63 |
If you get into a situation where you need to supply \isacommand{recdef} with new congruence
|
|
64 |
rules, you can either append the line
|
|
65 |
\begin{ttbox}
|
|
66 |
congs <congruence rules>
|
|
67 |
\end{ttbox}
|
|
68 |
to the specific occurrence of \isacommand{recdef} or declare them globally:
|
|
69 |
\begin{ttbox}
|
|
70 |
lemmas [????????] = <congruence rules>
|
|
71 |
\end{ttbox}
|
|
72 |
|
|
73 |
Note that \isacommand{recdef} feeds on exactly the same \emph{kind} of
|
|
74 |
congruence rules as the simplifier (\S\ref{sec:simp-cong}) but that
|
|
75 |
declaring a congruence rule for the simplifier does not make it
|
|
76 |
available to \isacommand{recdef}, and vice versa. This is intentional.
|
|
77 |
*};
|
|
78 |
(*<*)end;(*>*)
|