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