doc-src/TutorialI/Recdef/document/Nested2.tex
changeset 9690 50f22b1b136a
child 9698 f0740137a65d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/doc-src/TutorialI/Recdef/document/Nested2.tex	Mon Aug 28 10:16:58 2000 +0200
     1.3 @@ -0,0 +1,84 @@
     1.4 +\begin{isabelle}%
     1.5 +%
     1.6 +\begin{isamarkuptext}%
     1.7 +\noindent
     1.8 +The termintion condition is easily proved by induction:%
     1.9 +\end{isamarkuptext}%
    1.10 +\isacommand{lemma}\ [simp]:\ {"}t\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ size\ t\ <\ Suc(term\_size\ ts){"}\isanewline
    1.11 +\isacommand{by}(induct\_tac\ ts,\ auto)%
    1.12 +\begin{isamarkuptext}%
    1.13 +\noindent
    1.14 +By making this theorem a simplification rule, \isacommand{recdef}
    1.15 +applies it automatically and the above definition of \isa{trev}
    1.16 +succeeds now. As a reward for our effort, we can now prove the desired
    1.17 +lemma directly. The key is the fact that we no longer need the verbose
    1.18 +induction schema for type \isa{term} but the simpler one arising from
    1.19 +\isa{trev}:%
    1.20 +\end{isamarkuptext}%
    1.21 +\isacommand{lemmas}\ [cong]\ =\ map\_cong\isanewline
    1.22 +\isacommand{lemma}\ {"}trev(trev\ t)\ =\ t{"}\isanewline
    1.23 +\isacommand{apply}(induct\_tac\ t\ rule:trev.induct)%
    1.24 +\begin{isamarkuptxt}%
    1.25 +\noindent
    1.26 +This leaves us with a trivial base case \isa{trev\ (trev\ (Var\ \mbox{x}))\ =\ Var\ \mbox{x}} and the step case
    1.27 +\begin{quote}
    1.28 +
    1.29 +\begin{isabelle}%
    1.30 +{\isasymforall}\mbox{t}.\ \mbox{t}\ {\isasymin}\ set\ \mbox{ts}\ {\isasymlongrightarrow}\ trev\ (trev\ \mbox{t})\ =\ \mbox{t}\ {\isasymLongrightarrow}\isanewline
    1.31 +trev\ (trev\ (App\ \mbox{f}\ \mbox{ts}))\ =\ App\ \mbox{f}\ \mbox{ts}
    1.32 +\end{isabelle}%
    1.33 +
    1.34 +\end{quote}
    1.35 +both of which are solved by simplification:%
    1.36 +\end{isamarkuptxt}%
    1.37 +\isacommand{by}(simp\_all\ del:map\_compose\ add:sym[OF\ map\_compose]\ rev\_map)%
    1.38 +\begin{isamarkuptext}%
    1.39 +\noindent
    1.40 +If this surprises you, see Datatype/Nested2......
    1.41 +
    1.42 +The above definition of \isa{trev} is superior to the one in \S\ref{sec:nested-datatype}
    1.43 +because it brings \isa{rev} into play, about which already know a lot, in particular
    1.44 +\isa{rev\ (rev\ \mbox{xs})\ =\ \mbox{xs}}.
    1.45 +Thus this proof is a good example of an important principle:
    1.46 +\begin{quote}
    1.47 +\emph{Chose your definitions carefully\\
    1.48 +because they determine the complexity of your proofs.}
    1.49 +\end{quote}
    1.50 +
    1.51 +Let us now return to the question of how \isacommand{recdef} can come up with sensible termination
    1.52 +conditions in the presence of higher-order functions like \isa{map}. For a start, if nothing
    1.53 +were known about \isa{map}, \isa{map\ trev\ \mbox{ts}} might apply \isa{trev} to arbitrary terms,
    1.54 +and thus \isacommand{recdef} would try to prove the unprovable
    1.55 +\isa{size\ \mbox{t}\ <\ Suc\ (term\_size\ \mbox{ts})}, without any assumption about \isa{t}.
    1.56 +Therefore \isacommand{recdef} has been supplied with the congruence theorem \isa{map\_cong}: 
    1.57 +\begin{quote}
    1.58 +
    1.59 +\begin{isabelle}%
    1.60 +{\isasymlbrakk}\mbox{xs}\ =\ \mbox{ys};\ {\isasymAnd}\mbox{x}.\ \mbox{x}\ {\isasymin}\ set\ \mbox{ys}\ {\isasymLongrightarrow}\ \mbox{f}\ \mbox{x}\ =\ \mbox{g}\ \mbox{x}{\isasymrbrakk}\isanewline
    1.61 +{\isasymLongrightarrow}\ map\ \mbox{f}\ \mbox{xs}\ =\ map\ \mbox{g}\ \mbox{ys}
    1.62 +\end{isabelle}%
    1.63 +
    1.64 +\end{quote}
    1.65 +Its second premise expresses (indirectly) that the second argument of \isa{map} is only applied
    1.66 +to elements of its third argument. Congruence rules for other higher-order functions on lists would
    1.67 +look very similar but have not been proved yet because they were never needed.
    1.68 +If you get into a situation where you need to supply \isacommand{recdef} with new congruence
    1.69 +rules, you can either append the line
    1.70 +\begin{ttbox}
    1.71 +congs <congruence rules>
    1.72 +\end{ttbox}
    1.73 +to the specific occurrence of \isacommand{recdef} or declare them globally:
    1.74 +\begin{ttbox}
    1.75 +lemmas [????????] = <congruence rules>
    1.76 +\end{ttbox}
    1.77 +
    1.78 +Note that \isacommand{recdef} feeds on exactly the same \emph{kind} of
    1.79 +congruence rules as the simplifier (\S\ref{sec:simp-cong}) but that
    1.80 +declaring a congruence rule for the simplifier does not make it
    1.81 +available to \isacommand{recdef}, and vice versa. This is intentional.%
    1.82 +\end{isamarkuptext}%
    1.83 +\end{isabelle}%
    1.84 +%%% Local Variables:
    1.85 +%%% mode: latex
    1.86 +%%% TeX-master: "root"
    1.87 +%%% End: