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