doc-src/TutorialI/Recdef/document/Nested2.tex
author nipkow
Fri Sep 01 19:09:44 2000 +0200 (2000-09-01)
changeset 9792 bbefb6ce5cb2
parent 9754 a123a64cadeb
child 9834 109b11c4e77e
permissions -rw-r--r--
*** empty log message ***
nipkow@9722
     1
%
nipkow@9722
     2
\begin{isabellebody}%
nipkow@9690
     3
%
nipkow@9690
     4
\begin{isamarkuptext}%
nipkow@9690
     5
\noindent
nipkow@9690
     6
The termintion condition is easily proved by induction:%
nipkow@9690
     7
\end{isamarkuptext}%
nipkow@9754
     8
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}t\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ size\ t\ {\isacharless}\ Suc{\isacharparenleft}term{\isacharunderscore}list{\isacharunderscore}size\ ts{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@9698
     9
\isacommand{by}{\isacharparenleft}induct{\isacharunderscore}tac\ ts{\isacharcomma}\ auto{\isacharparenright}%
nipkow@9690
    10
\begin{isamarkuptext}%
nipkow@9690
    11
\noindent
nipkow@9690
    12
By making this theorem a simplification rule, \isacommand{recdef}
nipkow@9690
    13
applies it automatically and the above definition of \isa{trev}
nipkow@9690
    14
succeeds now. As a reward for our effort, we can now prove the desired
nipkow@9690
    15
lemma directly. The key is the fact that we no longer need the verbose
nipkow@9690
    16
induction schema for type \isa{term} but the simpler one arising from
nipkow@9690
    17
\isa{trev}:%
nipkow@9690
    18
\end{isamarkuptext}%
wenzelm@9698
    19
\isacommand{lemmas}\ {\isacharbrackleft}cong{\isacharbrackright}\ {\isacharequal}\ map{\isacharunderscore}cong\isanewline
wenzelm@9698
    20
\isacommand{lemma}\ {\isachardoublequote}trev{\isacharparenleft}trev\ t{\isacharparenright}\ {\isacharequal}\ t{\isachardoublequote}\isanewline
wenzelm@9698
    21
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ t\ rule{\isacharcolon}trev{\isachardot}induct{\isacharparenright}%
nipkow@9690
    22
\begin{isamarkuptxt}%
nipkow@9690
    23
\noindent
nipkow@9792
    24
This leaves us with a trivial base case \isa{trev\ {\isacharparenleft}trev\ {\isacharparenleft}Var\ x{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ Var\ x} and the step case
nipkow@9690
    25
\begin{quote}
nipkow@9690
    26
nipkow@9690
    27
\begin{isabelle}%
nipkow@9792
    28
{\isasymforall}t{\isachardot}\ t\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ trev\ {\isacharparenleft}trev\ t{\isacharparenright}\ {\isacharequal}\ t\ {\isasymLongrightarrow}\isanewline
nipkow@9792
    29
trev\ {\isacharparenleft}trev\ {\isacharparenleft}App\ f\ ts{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ App\ f\ ts
nipkow@9690
    30
\end{isabelle}%
nipkow@9690
    31
nipkow@9690
    32
\end{quote}
nipkow@9690
    33
both of which are solved by simplification:%
nipkow@9690
    34
\end{isamarkuptxt}%
nipkow@9721
    35
\isacommand{by}{\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}rev{\isacharunderscore}map\ sym{\isacharbrackleft}OF\ map{\isacharunderscore}compose{\isacharbrackright}{\isacharparenright}%
nipkow@9690
    36
\begin{isamarkuptext}%
nipkow@9690
    37
\noindent
nipkow@9721
    38
If the proof of the induction step mystifies you, we recommend to go through
nipkow@9754
    39
the chain of simplification steps in detail; you will probably need the help of
nipkow@9754
    40
\isa{trace{\isacharunderscore}simp}.
nipkow@9721
    41
%\begin{quote}
nipkow@9721
    42
%{term[display]"trev(trev(App f ts))"}\\
nipkow@9721
    43
%{term[display]"App f (rev(map trev (rev(map trev ts))))"}\\
nipkow@9721
    44
%{term[display]"App f (map trev (rev(rev(map trev ts))))"}\\
nipkow@9721
    45
%{term[display]"App f (map trev (map trev ts))"}\\
nipkow@9721
    46
%{term[display]"App f (map (trev o trev) ts)"}\\
nipkow@9721
    47
%{term[display]"App f (map (%x. x) ts)"}\\
nipkow@9721
    48
%{term[display]"App f ts"}
nipkow@9721
    49
%\end{quote}
nipkow@9690
    50
nipkow@9754
    51
The above definition of \isa{trev} is superior to the one in
nipkow@9754
    52
\S\ref{sec:nested-datatype} because it brings \isa{rev} into play, about
nipkow@9792
    53
which already know a lot, in particular \isa{rev\ {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ xs}.
nipkow@9690
    54
Thus this proof is a good example of an important principle:
nipkow@9690
    55
\begin{quote}
nipkow@9690
    56
\emph{Chose your definitions carefully\\
nipkow@9690
    57
because they determine the complexity of your proofs.}
nipkow@9690
    58
\end{quote}
nipkow@9690
    59
nipkow@9721
    60
Let us now return to the question of how \isacommand{recdef} can come up with
nipkow@9721
    61
sensible termination conditions in the presence of higher-order functions
nipkow@9721
    62
like \isa{map}. For a start, if nothing were known about \isa{map},
nipkow@9792
    63
\isa{map\ trev\ ts} might apply \isa{trev} to arbitrary terms, and thus
nipkow@9792
    64
\isacommand{recdef} would try to prove the unprovable \isa{size\ t\ {\isacharless}\ Suc\ {\isacharparenleft}term{\isacharunderscore}list{\isacharunderscore}size\ ts{\isacharparenright}}, without any assumption about \isa{t}.  Therefore
nipkow@9721
    65
\isacommand{recdef} has been supplied with the congruence theorem
nipkow@9754
    66
\isa{map{\isacharunderscore}cong}:
nipkow@9690
    67
\begin{quote}
nipkow@9690
    68
nipkow@9690
    69
\begin{isabelle}%
nipkow@9792
    70
{\isasymlbrakk}xs\ {\isacharequal}\ ys{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ x\ {\isasymin}\ set\ ys\ {\isasymLongrightarrow}\ f\ x\ {\isacharequal}\ g\ x{\isasymrbrakk}\isanewline
nipkow@9792
    71
{\isasymLongrightarrow}\ map\ f\ xs\ {\isacharequal}\ map\ g\ ys
nipkow@9690
    72
\end{isabelle}%
nipkow@9690
    73
nipkow@9690
    74
\end{quote}
nipkow@9721
    75
Its second premise expresses (indirectly) that the second argument of
nipkow@9721
    76
\isa{map} is only applied to elements of its third argument. Congruence
nipkow@9721
    77
rules for other higher-order functions on lists would look very similar but
nipkow@9721
    78
have not been proved yet because they were never needed. If you get into a
nipkow@9721
    79
situation where you need to supply \isacommand{recdef} with new congruence
nipkow@9690
    80
rules, you can either append the line
nipkow@9690
    81
\begin{ttbox}
nipkow@9690
    82
congs <congruence rules>
nipkow@9690
    83
\end{ttbox}
nipkow@9690
    84
to the specific occurrence of \isacommand{recdef} or declare them globally:
nipkow@9690
    85
\begin{ttbox}
nipkow@9690
    86
lemmas [????????] = <congruence rules>
nipkow@9690
    87
\end{ttbox}
nipkow@9690
    88
nipkow@9690
    89
Note that \isacommand{recdef} feeds on exactly the same \emph{kind} of
nipkow@9690
    90
congruence rules as the simplifier (\S\ref{sec:simp-cong}) but that
nipkow@9690
    91
declaring a congruence rule for the simplifier does not make it
nipkow@9690
    92
available to \isacommand{recdef}, and vice versa. This is intentional.%
nipkow@9690
    93
\end{isamarkuptext}%
nipkow@9722
    94
\end{isabellebody}%
nipkow@9690
    95
%%% Local Variables:
nipkow@9690
    96
%%% mode: latex
nipkow@9690
    97
%%% TeX-master: "root"
nipkow@9690
    98
%%% End: