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