doc-src/TutorialI/Recdef/document/Nested2.tex
author nipkow
Tue, 07 May 2002 15:03:50 +0200
changeset 13111 2d6782e71702
parent 12815 1f073030b97a
child 13758 ee898d32de21
permissions -rw-r--r--
*** empty log message ***
nipkow@9722
     1
%
nipkow@9722
     2
\begin{isabellebody}%
wenzelm@10267
     3
\def\isabellecontext{Nested{\isadigit{2}}}%
nipkow@12491
     4
\isanewline
wenzelm@11866
     5
\isamarkupfalse%
nipkow@9754
     6
\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@11866
     7
\isamarkupfalse%
wenzelm@11866
     8
\isacommand{by}{\isacharparenleft}induct{\isacharunderscore}tac\ ts{\isacharcomma}\ auto{\isacharparenright}\isamarkupfalse%
wenzelm@11866
     9
\isamarkupfalse%
wenzelm@11866
    10
%
nipkow@9690
    11
\begin{isamarkuptext}%
nipkow@9690
    12
\noindent
nipkow@9690
    13
By making this theorem a simplification rule, \isacommand{recdef}
paulson@10878
    14
applies it automatically and the definition of \isa{trev}
nipkow@9690
    15
succeeds now. As a reward for our effort, we can now prove the desired
paulson@10878
    16
lemma directly.  We no longer need the verbose
paulson@10878
    17
induction schema for type \isa{term} and can use the simpler one arising from
nipkow@9690
    18
\isa{trev}:%
nipkow@9690
    19
\end{isamarkuptext}%
wenzelm@11866
    20
\isamarkuptrue%
wenzelm@9698
    21
\isacommand{lemma}\ {\isachardoublequote}trev{\isacharparenleft}trev\ t{\isacharparenright}\ {\isacharequal}\ t{\isachardoublequote}\isanewline
wenzelm@11866
    22
\isamarkupfalse%
wenzelm@12815
    23
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ t\ rule{\isacharcolon}\ trev{\isachardot}induct{\isacharparenright}\isamarkupfalse%
wenzelm@11866
    24
%
nipkow@9690
    25
\begin{isamarkuptxt}%
nipkow@9690
    26
\begin{isabelle}%
nipkow@11196
    27
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}x{\isachardot}\ trev\ {\isacharparenleft}trev\ {\isacharparenleft}Var\ x{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ Var\ x\isanewline
nipkow@11196
    28
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}f\ ts{\isachardot}\isanewline
nipkow@11196
    29
\isaindent{\ {\isadigit{2}}{\isachardot}\ \ \ \ }{\isasymforall}x{\isachardot}\ x\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ trev\ {\isacharparenleft}trev\ x{\isacharparenright}\ {\isacharequal}\ x\ {\isasymLongrightarrow}\isanewline
nipkow@11196
    30
\isaindent{\ {\isadigit{2}}{\isachardot}\ \ \ \ }trev\ {\isacharparenleft}trev\ {\isacharparenleft}App\ f\ ts{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ App\ f\ ts%
wenzelm@9924
    31
\end{isabelle}
nipkow@11196
    32
Both the base case and the induction step fall to simplification:%
nipkow@9690
    33
\end{isamarkuptxt}%
wenzelm@11866
    34
\isamarkuptrue%
wenzelm@12815
    35
\isacommand{by}{\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ rev{\isacharunderscore}map\ sym{\isacharbrackleft}OF\ map{\isacharunderscore}compose{\isacharbrackright}\ cong{\isacharcolon}\ map{\isacharunderscore}cong{\isacharparenright}\isamarkupfalse%
wenzelm@11866
    36
%
nipkow@9690
    37
\begin{isamarkuptext}%
nipkow@9690
    38
\noindent
paulson@10878
    39
If the proof of the induction step mystifies you, we recommend that you go through
nipkow@9754
    40
the chain of simplification steps in detail; you will probably need the help of
nipkow@9933
    41
\isa{trace{\isacharunderscore}simp}. Theorem \isa{map{\isacharunderscore}cong} is discussed below.
nipkow@9721
    42
%\begin{quote}
nipkow@9721
    43
%{term[display]"trev(trev(App f ts))"}\\
nipkow@9721
    44
%{term[display]"App f (rev(map trev (rev(map trev ts))))"}\\
nipkow@9721
    45
%{term[display]"App f (map trev (rev(rev(map trev ts))))"}\\
nipkow@9721
    46
%{term[display]"App f (map trev (map trev ts))"}\\
nipkow@9721
    47
%{term[display]"App f (map (trev o trev) ts)"}\\
nipkow@9721
    48
%{term[display]"App f (map (%x. x) ts)"}\\
nipkow@9721
    49
%{term[display]"App f ts"}
nipkow@9721
    50
%\end{quote}
nipkow@9690
    51
paulson@10878
    52
The definition of \isa{trev} above is superior to the one in
paulson@10878
    53
\S\ref{sec:nested-datatype} because it uses \isa{rev}
paulson@10878
    54
and lets us use existing facts such as \hbox{\isa{rev\ {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ xs}}.
nipkow@9690
    55
Thus this proof is a good example of an important principle:
nipkow@9690
    56
\begin{quote}
nipkow@9690
    57
\emph{Chose your definitions carefully\\
nipkow@9690
    58
because they determine the complexity of your proofs.}
nipkow@9690
    59
\end{quote}
nipkow@9690
    60
nipkow@9721
    61
Let us now return to the question of how \isacommand{recdef} can come up with
nipkow@9721
    62
sensible termination conditions in the presence of higher-order functions
paulson@11494
    63
like \isa{map}. For a start, if nothing were known about \isa{map}, then
nipkow@9792
    64
\isa{map\ trev\ ts} might apply \isa{trev} to arbitrary terms, and thus
nipkow@9792
    65
\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
    66
\isacommand{recdef} has been supplied with the congruence theorem
nipkow@9754
    67
\isa{map{\isacharunderscore}cong}:
nipkow@9690
    68
\begin{isabelle}%
nipkow@10696
    69
\ \ \ \ \ {\isasymlbrakk}xs\ {\isacharequal}\ ys{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ x\ {\isasymin}\ set\ ys\ {\isasymLongrightarrow}\ f\ x\ {\isacharequal}\ g\ x{\isasymrbrakk}\isanewline
wenzelm@10950
    70
\isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ map\ f\ xs\ {\isacharequal}\ map\ g\ ys%
wenzelm@9924
    71
\end{isabelle}
paulson@11494
    72
Its second premise expresses that in \isa{map\ f\ xs},
paulson@11494
    73
function \isa{f} is only applied to elements of list \isa{xs}.  Congruence
paulson@11494
    74
rules for other higher-order functions on lists are similar.  If you get
nipkow@10212
    75
into a situation where you need to supply \isacommand{recdef} with new
paulson@11494
    76
congruence rules, you can append a hint after the end of
nipkow@13111
    77
the recursion equations:\cmmdx{hints}%
nipkow@9940
    78
\end{isamarkuptext}%
wenzelm@11866
    79
\isamarkuptrue%
wenzelm@11866
    80
\isamarkupfalse%
wenzelm@11866
    81
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}cong{\isacharcolon}\ map{\isacharunderscore}cong{\isacharparenright}\isamarkupfalse%
wenzelm@11866
    82
%
nipkow@9940
    83
\begin{isamarkuptext}%
nipkow@9940
    84
\noindent
paulson@11494
    85
Or you can declare them globally
paulson@11494
    86
by giving them the \attrdx{recdef_cong} attribute:%
nipkow@9940
    87
\end{isamarkuptext}%
wenzelm@11866
    88
\isamarkuptrue%
wenzelm@11866
    89
\isacommand{declare}\ map{\isacharunderscore}cong{\isacharbrackleft}recdef{\isacharunderscore}cong{\isacharbrackright}\isamarkupfalse%
wenzelm@11866
    90
%
nipkow@9940
    91
\begin{isamarkuptext}%
paulson@11494
    92
The \isa{cong} and \isa{recdef{\isacharunderscore}cong} attributes are
nipkow@9940
    93
intentionally kept apart because they control different activities, namely
nipkow@10171
    94
simplification and making recursive definitions.
nipkow@9933
    95
%The simplifier's congruence rules cannot be used by recdef.
nipkow@9933
    96
%For example the weak congruence rules for if and case would prevent
nipkow@9933
    97
%recdef from generating sensible termination conditions.%
nipkow@9690
    98
\end{isamarkuptext}%
wenzelm@11866
    99
\isamarkuptrue%
wenzelm@11866
   100
\isamarkupfalse%
nipkow@9722
   101
\end{isabellebody}%
nipkow@9690
   102
%%% Local Variables:
nipkow@9690
   103
%%% mode: latex
nipkow@9690
   104
%%% TeX-master: "root"
nipkow@9690
   105
%%% End: