doc-src/TutorialI/Recdef/Nested2.thy
author nipkow
Mon, 09 Oct 2000 10:18:21 +0200
changeset 10171 59d6633835fa
parent 9940 102f2430cef9
child 10186 499637e8f2c6
permissions -rw-r--r--
*** empty log message ***
nipkow@9690
     1
(*<*)
nipkow@9690
     2
theory Nested2 = Nested0:;
nipkow@9690
     3
consts trev  :: "('a,'b)term => ('a,'b)term";
nipkow@9690
     4
(*>*)
nipkow@9690
     5
nipkow@9690
     6
text{*\noindent
nipkow@9690
     7
The termintion condition is easily proved by induction:
nipkow@9690
     8
*};
nipkow@9690
     9
nipkow@9754
    10
lemma [simp]: "t \<in> set ts \<longrightarrow> size t < Suc(term_list_size ts)";
nipkow@9690
    11
by(induct_tac ts, auto);
nipkow@9690
    12
(*<*)
nipkow@9690
    13
recdef trev "measure size"
nipkow@9690
    14
 "trev (Var x) = Var x"
nipkow@9690
    15
 "trev (App f ts) = App f (rev(map trev ts))";
nipkow@9690
    16
(*>*)
nipkow@9690
    17
text{*\noindent
nipkow@9690
    18
By making this theorem a simplification rule, \isacommand{recdef}
nipkow@9690
    19
applies it automatically and the above definition of @{term"trev"}
nipkow@9690
    20
succeeds now. As a reward for our effort, we can now prove the desired
nipkow@9690
    21
lemma directly. The key is the fact that we no longer need the verbose
nipkow@9792
    22
induction schema for type @{text"term"} but the simpler one arising from
nipkow@9690
    23
@{term"trev"}:
nipkow@9690
    24
*};
nipkow@9690
    25
nipkow@9690
    26
lemma "trev(trev t) = t";
nipkow@9690
    27
apply(induct_tac t rule:trev.induct);
nipkow@9690
    28
txt{*\noindent
nipkow@9690
    29
This leaves us with a trivial base case @{term"trev (trev (Var x)) = Var x"} and the step case
nipkow@9690
    30
@{term[display,margin=60]"ALL t. t : set ts --> trev (trev t) = t ==> trev (trev (App f ts)) = App f ts"}
nipkow@9690
    31
both of which are solved by simplification:
nipkow@9690
    32
*};
nipkow@9690
    33
nipkow@9933
    34
by(simp_all add:rev_map sym[OF map_compose] cong:map_cong);
nipkow@9690
    35
nipkow@9690
    36
text{*\noindent
nipkow@9721
    37
If the proof of the induction step mystifies you, we recommend to go through
nipkow@9754
    38
the chain of simplification steps in detail; you will probably need the help of
nipkow@9933
    39
@{text"trace_simp"}. Theorem @{thm[source]map_cong} is discussed below.
nipkow@9721
    40
%\begin{quote}
nipkow@9721
    41
%{term[display]"trev(trev(App f ts))"}\\
nipkow@9721
    42
%{term[display]"App f (rev(map trev (rev(map trev ts))))"}\\
nipkow@9721
    43
%{term[display]"App f (map trev (rev(rev(map trev ts))))"}\\
nipkow@9721
    44
%{term[display]"App f (map trev (map trev ts))"}\\
nipkow@9721
    45
%{term[display]"App f (map (trev o trev) ts)"}\\
nipkow@9721
    46
%{term[display]"App f (map (%x. x) ts)"}\\
nipkow@9721
    47
%{term[display]"App f ts"}
nipkow@9721
    48
%\end{quote}
nipkow@9690
    49
nipkow@9754
    50
The above definition of @{term"trev"} is superior to the one in
nipkow@9754
    51
\S\ref{sec:nested-datatype} because it brings @{term"rev"} into play, about
nipkow@9754
    52
which already know a lot, in particular @{prop"rev(rev xs) = xs"}.
nipkow@9690
    53
Thus this proof is a good example of an important principle:
nipkow@9690
    54
\begin{quote}
nipkow@9690
    55
\emph{Chose your definitions carefully\\
nipkow@9690
    56
because they determine the complexity of your proofs.}
nipkow@9690
    57
\end{quote}
nipkow@9690
    58
nipkow@9721
    59
Let us now return to the question of how \isacommand{recdef} can come up with
nipkow@9721
    60
sensible termination conditions in the presence of higher-order functions
nipkow@9721
    61
like @{term"map"}. For a start, if nothing were known about @{term"map"},
nipkow@9721
    62
@{term"map trev ts"} might apply @{term"trev"} to arbitrary terms, and thus
nipkow@9721
    63
\isacommand{recdef} would try to prove the unprovable @{term"size t < Suc
nipkow@9754
    64
(term_list_size ts)"}, without any assumption about @{term"t"}.  Therefore
nipkow@9721
    65
\isacommand{recdef} has been supplied with the congruence theorem
nipkow@9792
    66
@{thm[source]map_cong}:
nipkow@9690
    67
@{thm[display,margin=50]"map_cong"[no_vars]}
nipkow@9721
    68
Its second premise expresses (indirectly) that the second argument of
nipkow@9721
    69
@{term"map"} is only applied to elements of its third argument. Congruence
nipkow@9721
    70
rules for other higher-order functions on lists would look very similar but
nipkow@9721
    71
have not been proved yet because they were never needed. If you get into a
nipkow@9721
    72
situation where you need to supply \isacommand{recdef} with new congruence
nipkow@9940
    73
rules, you can either append a hint locally
nipkow@9940
    74
to the specific occurrence of \isacommand{recdef}
nipkow@9940
    75
*}
nipkow@9940
    76
(*<*)
nipkow@9940
    77
consts dummy :: "nat => nat"
nipkow@9940
    78
recdef dummy "{}"
nipkow@9940
    79
"dummy n = n"
nipkow@9940
    80
(*>*)
nipkow@10171
    81
(hints recdef_cong: map_cong)
nipkow@9690
    82
nipkow@9940
    83
text{*\noindent
nipkow@9940
    84
or declare them globally
nipkow@9940
    85
by giving them the @{text recdef_cong} attribute as in
nipkow@9940
    86
*}
nipkow@9940
    87
nipkow@9940
    88
declare map_cong[recdef_cong];
nipkow@9940
    89
nipkow@9940
    90
text{*
nipkow@10171
    91
Note that the @{text cong} and @{text recdef_cong} attributes are
nipkow@9940
    92
intentionally kept apart because they control different activities, namely
nipkow@10171
    93
simplification and making recursive definitions.
nipkow@10171
    94
% The local @{text cong} in
nipkow@10171
    95
% the hints section of \isacommand{recdef} is merely short for @{text recdef_cong}.
nipkow@9933
    96
%The simplifier's congruence rules cannot be used by recdef.
nipkow@9933
    97
%For example the weak congruence rules for if and case would prevent
nipkow@9933
    98
%recdef from generating sensible termination conditions.
nipkow@9690
    99
*};
nipkow@9690
   100
(*<*)end;(*>*)