doc-src/TutorialI/Recdef/document/Nested2.tex
author wenzelm
Tue Aug 29 12:08:20 2000 +0200 (2000-08-29)
changeset 9719 c753196599f9
parent 9698 f0740137a65d
child 9721 7e51c9f3d5a0
permissions -rw-r--r--
updated;
wenzelm@9719
     1
%
wenzelm@9719
     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}%
wenzelm@9698
     8
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}t\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ size\ t\ {\isacharless}\ Suc{\isacharparenleft}term{\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
wenzelm@9698
    24
This leaves us with a trivial base case \isa{trev\ {\isacharparenleft}trev\ {\isacharparenleft}Var\ \mbox{x}{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ Var\ \mbox{x}} and the step case
nipkow@9690
    25
\begin{quote}
nipkow@9690
    26
nipkow@9690
    27
\begin{isabelle}%
wenzelm@9698
    28
{\isasymforall}\mbox{t}{\isachardot}\ \mbox{t}\ {\isasymin}\ set\ \mbox{ts}\ {\isasymlongrightarrow}\ trev\ {\isacharparenleft}trev\ \mbox{t}{\isacharparenright}\ {\isacharequal}\ \mbox{t}\ {\isasymLongrightarrow}\isanewline
wenzelm@9698
    29
trev\ {\isacharparenleft}trev\ {\isacharparenleft}App\ \mbox{f}\ \mbox{ts}{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ App\ \mbox{f}\ \mbox{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}%
wenzelm@9698
    35
\isacommand{by}{\isacharparenleft}simp{\isacharunderscore}all\ del{\isacharcolon}map{\isacharunderscore}compose\ add{\isacharcolon}sym{\isacharbrackleft}OF\ map{\isacharunderscore}compose{\isacharbrackright}\ rev{\isacharunderscore}map{\isacharparenright}%
nipkow@9690
    36
\begin{isamarkuptext}%
nipkow@9690
    37
\noindent
nipkow@9690
    38
If this surprises you, see Datatype/Nested2......
nipkow@9690
    39
nipkow@9690
    40
The above definition of \isa{trev} is superior to the one in \S\ref{sec:nested-datatype}
nipkow@9690
    41
because it brings \isa{rev} into play, about which already know a lot, in particular
wenzelm@9698
    42
\isa{rev\ {\isacharparenleft}rev\ \mbox{xs}{\isacharparenright}\ {\isacharequal}\ \mbox{xs}}.
nipkow@9690
    43
Thus this proof is a good example of an important principle:
nipkow@9690
    44
\begin{quote}
nipkow@9690
    45
\emph{Chose your definitions carefully\\
nipkow@9690
    46
because they determine the complexity of your proofs.}
nipkow@9690
    47
\end{quote}
nipkow@9690
    48
nipkow@9690
    49
Let us now return to the question of how \isacommand{recdef} can come up with sensible termination
nipkow@9690
    50
conditions in the presence of higher-order functions like \isa{map}. For a start, if nothing
nipkow@9690
    51
were known about \isa{map}, \isa{map\ trev\ \mbox{ts}} might apply \isa{trev} to arbitrary terms,
nipkow@9690
    52
and thus \isacommand{recdef} would try to prove the unprovable
wenzelm@9698
    53
\isa{size\ \mbox{t}\ {\isacharless}\ Suc\ {\isacharparenleft}term{\isacharunderscore}size\ \mbox{ts}{\isacharparenright}}, without any assumption about \isa{t}.
nipkow@9690
    54
Therefore \isacommand{recdef} has been supplied with the congruence theorem \isa{map\_cong}: 
nipkow@9690
    55
\begin{quote}
nipkow@9690
    56
nipkow@9690
    57
\begin{isabelle}%
wenzelm@9698
    58
{\isasymlbrakk}\mbox{xs}\ {\isacharequal}\ \mbox{ys}{\isacharsemicolon}\ {\isasymAnd}\mbox{x}{\isachardot}\ \mbox{x}\ {\isasymin}\ set\ \mbox{ys}\ {\isasymLongrightarrow}\ \mbox{f}\ \mbox{x}\ {\isacharequal}\ \mbox{g}\ \mbox{x}{\isasymrbrakk}\isanewline
wenzelm@9698
    59
{\isasymLongrightarrow}\ map\ \mbox{f}\ \mbox{xs}\ {\isacharequal}\ map\ \mbox{g}\ \mbox{ys}
nipkow@9690
    60
\end{isabelle}%
nipkow@9690
    61
nipkow@9690
    62
\end{quote}
nipkow@9690
    63
Its second premise expresses (indirectly) that the second argument of \isa{map} is only applied
nipkow@9690
    64
to elements of its third argument. Congruence rules for other higher-order functions on lists would
nipkow@9690
    65
look very similar but have not been proved yet because they were never needed.
nipkow@9690
    66
If you get into a situation where you need to supply \isacommand{recdef} with new congruence
nipkow@9690
    67
rules, you can either append the line
nipkow@9690
    68
\begin{ttbox}
nipkow@9690
    69
congs <congruence rules>
nipkow@9690
    70
\end{ttbox}
nipkow@9690
    71
to the specific occurrence of \isacommand{recdef} or declare them globally:
nipkow@9690
    72
\begin{ttbox}
nipkow@9690
    73
lemmas [????????] = <congruence rules>
nipkow@9690
    74
\end{ttbox}
nipkow@9690
    75
nipkow@9690
    76
Note that \isacommand{recdef} feeds on exactly the same \emph{kind} of
nipkow@9690
    77
congruence rules as the simplifier (\S\ref{sec:simp-cong}) but that
nipkow@9690
    78
declaring a congruence rule for the simplifier does not make it
nipkow@9690
    79
available to \isacommand{recdef}, and vice versa. This is intentional.%
nipkow@9690
    80
\end{isamarkuptext}%
wenzelm@9719
    81
\end{isabellebody}%
nipkow@9690
    82
%%% Local Variables:
nipkow@9690
    83
%%% mode: latex
nipkow@9690
    84
%%% TeX-master: "root"
nipkow@9690
    85
%%% End: