| 
9690
 | 
     1  | 
(*<*)
  | 
| 
 | 
     2  | 
theory Nested2 = Nested0:;
  | 
| 
 | 
     3  | 
(*>*)
  | 
| 
 | 
     4  | 
  | 
| 
 | 
     5  | 
text{*\noindent
 | 
| 
 | 
     6  | 
The termintion condition is easily proved by induction:
  | 
| 
 | 
     7  | 
*};
  | 
| 
 | 
     8  | 
  | 
| 
9754
 | 
     9  | 
lemma [simp]: "t \<in> set ts \<longrightarrow> size t < Suc(term_list_size ts)";
  | 
| 
9690
 | 
    10  | 
by(induct_tac ts, auto);
  | 
| 
 | 
    11  | 
(*<*)
  | 
| 
 | 
    12  | 
recdef trev "measure size"
  | 
| 
 | 
    13  | 
 "trev (Var x) = Var x"
  | 
| 
 | 
    14  | 
 "trev (App f ts) = App f (rev(map trev ts))";
  | 
| 
 | 
    15  | 
(*>*)
  | 
| 
 | 
    16  | 
text{*\noindent
 | 
| 
 | 
    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
  | 
| 
9792
 | 
    21  | 
induction schema for type @{text"term"} but the simpler one arising from
 | 
| 
9690
 | 
    22  | 
@{term"trev"}:
 | 
| 
 | 
    23  | 
*};
  | 
| 
 | 
    24  | 
  | 
| 
 | 
    25  | 
lemma "trev(trev t) = t";
  | 
| 
 | 
    26  | 
apply(induct_tac t rule:trev.induct);
  | 
| 
 | 
    27  | 
txt{*\noindent
 | 
| 
 | 
    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:
  | 
| 
 | 
    31  | 
*};
  | 
| 
 | 
    32  | 
  | 
| 
9933
 | 
    33  | 
by(simp_all add:rev_map sym[OF map_compose] cong:map_cong);
  | 
| 
9690
 | 
    34  | 
  | 
| 
 | 
    35  | 
text{*\noindent
 | 
| 
9721
 | 
    36  | 
If the proof of the induction step mystifies you, we recommend to go through
  | 
| 
9754
 | 
    37  | 
the chain of simplification steps in detail; you will probably need the help of
  | 
| 
9933
 | 
    38  | 
@{text"trace_simp"}. Theorem @{thm[source]map_cong} is discussed below.
 | 
| 
9721
 | 
    39  | 
%\begin{quote}
 | 
| 
 | 
    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"}
 | 
| 
 | 
    47  | 
%\end{quote}
 | 
| 
9690
 | 
    48  | 
  | 
| 
9754
 | 
    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"}.
 | 
| 
9690
 | 
    52  | 
Thus this proof is a good example of an important principle:
  | 
| 
 | 
    53  | 
\begin{quote}
 | 
| 
 | 
    54  | 
\emph{Chose your definitions carefully\\
 | 
| 
 | 
    55  | 
because they determine the complexity of your proofs.}
  | 
| 
 | 
    56  | 
\end{quote}
 | 
| 
 | 
    57  | 
  | 
| 
9721
 | 
    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
 | 
| 
9754
 | 
    63  | 
(term_list_size ts)"}, without any assumption about @{term"t"}.  Therefore
 | 
| 
9721
 | 
    64  | 
\isacommand{recdef} has been supplied with the congruence theorem
 | 
| 
9792
 | 
    65  | 
@{thm[source]map_cong}:
 | 
| 
9690
 | 
    66  | 
@{thm[display,margin=50]"map_cong"[no_vars]}
 | 
| 
9721
 | 
    67  | 
Its second premise expresses (indirectly) that the second argument of
  | 
| 
 | 
    68  | 
@{term"map"} is only applied to elements of its third argument. Congruence
 | 
| 
10212
 | 
    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
  | 
| 
9940
 | 
    72  | 
to the specific occurrence of \isacommand{recdef}
 | 
| 
 | 
    73  | 
*}
  | 
| 
 | 
    74  | 
(*<*)
  | 
| 
 | 
    75  | 
consts dummy :: "nat => nat"
  | 
| 
 | 
    76  | 
recdef dummy "{}"
 | 
| 
 | 
    77  | 
"dummy n = n"
  | 
| 
 | 
    78  | 
(*>*)
  | 
| 
10171
 | 
    79  | 
(hints recdef_cong: map_cong)
  | 
| 
9690
 | 
    80  | 
  | 
| 
9940
 | 
    81  | 
text{*\noindent
 | 
| 
 | 
    82  | 
or declare them globally
  | 
| 
10654
 | 
    83  | 
by giving them the \isaindexbold{recdef_cong} attribute as in
 | 
| 
9940
 | 
    84  | 
*}
  | 
| 
 | 
    85  | 
  | 
| 
 | 
    86  | 
declare map_cong[recdef_cong];
  | 
| 
 | 
    87  | 
  | 
| 
 | 
    88  | 
text{*
 | 
| 
10171
 | 
    89  | 
Note that the @{text cong} and @{text recdef_cong} attributes are
 | 
| 
9940
 | 
    90  | 
intentionally kept apart because they control different activities, namely
  | 
| 
10171
 | 
    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}.
 | 
| 
9933
 | 
    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.
  | 
| 
9690
 | 
    97  | 
*};
  | 
| 
 | 
    98  | 
(*<*)end;(*>*)
  |