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

     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/doc-src/TutorialI/Recdef/document/Nested2.tex	Mon Aug 28 10:16:58 2000 +0200
2.3 @@ -0,0 +1,84 @@
2.4 +\begin{isabelle}%
2.5 +%
2.6 +\begin{isamarkuptext}%
2.7 +\noindent
2.8 +The termintion condition is easily proved by induction:%
2.9 +\end{isamarkuptext}%
2.10 +\isacommand{lemma}\ [simp]:\ {"}t\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ size\ t\ <\ Suc(term\_size\ ts){"}\isanewline
2.11 +\isacommand{by}(induct\_tac\ ts,\ auto)%
2.12 +\begin{isamarkuptext}%
2.13 +\noindent
2.14 +By making this theorem a simplification rule, \isacommand{recdef}
2.15 +applies it automatically and the above definition of \isa{trev}
2.16 +succeeds now. As a reward for our effort, we can now prove the desired
2.17 +lemma directly. The key is the fact that we no longer need the verbose
2.18 +induction schema for type \isa{term} but the simpler one arising from
2.19 +\isa{trev}:%
2.20 +\end{isamarkuptext}%
2.21 +\isacommand{lemmas}\ [cong]\ =\ map\_cong\isanewline
2.22 +\isacommand{lemma}\ {"}trev(trev\ t)\ =\ t{"}\isanewline
2.23 +\isacommand{apply}(induct\_tac\ t\ rule:trev.induct)%
2.24 +\begin{isamarkuptxt}%
2.25 +\noindent
2.26 +This leaves us with a trivial base case \isa{trev\ (trev\ (Var\ \mbox{x}))\ =\ Var\ \mbox{x}} and the step case
2.27 +\begin{quote}
2.28 +
2.29 +\begin{isabelle}%
2.30 +{\isasymforall}\mbox{t}.\ \mbox{t}\ {\isasymin}\ set\ \mbox{ts}\ {\isasymlongrightarrow}\ trev\ (trev\ \mbox{t})\ =\ \mbox{t}\ {\isasymLongrightarrow}\isanewline
2.31 +trev\ (trev\ (App\ \mbox{f}\ \mbox{ts}))\ =\ App\ \mbox{f}\ \mbox{ts}
2.32 +\end{isabelle}%
2.33 +
2.34 +\end{quote}
2.35 +both of which are solved by simplification:%
2.36 +\end{isamarkuptxt}%
2.37 +\isacommand{by}(simp\_all\ del:map\_compose\ add:sym[OF\ map\_compose]\ rev\_map)%
2.38 +\begin{isamarkuptext}%
2.39 +\noindent
2.40 +If this surprises you, see Datatype/Nested2......
2.41 +
2.42 +The above definition of \isa{trev} is superior to the one in \S\ref{sec:nested-datatype}
2.43 +because it brings \isa{rev} into play, about which already know a lot, in particular
2.44 +\isa{rev\ (rev\ \mbox{xs})\ =\ \mbox{xs}}.
2.45 +Thus this proof is a good example of an important principle:
2.46 +\begin{quote}
2.48 +because they determine the complexity of your proofs.}
2.49 +\end{quote}
2.50 +
2.51 +Let us now return to the question of how \isacommand{recdef} can come up with sensible termination
2.52 +conditions in the presence of higher-order functions like \isa{map}. For a start, if nothing
2.53 +were known about \isa{map}, \isa{map\ trev\ \mbox{ts}} might apply \isa{trev} to arbitrary terms,
2.54 +and thus \isacommand{recdef} would try to prove the unprovable
2.55 +\isa{size\ \mbox{t}\ <\ Suc\ (term\_size\ \mbox{ts})}, without any assumption about \isa{t}.
2.56 +Therefore \isacommand{recdef} has been supplied with the congruence theorem \isa{map\_cong}:
2.57 +\begin{quote}
2.58 +
2.59 +\begin{isabelle}%
2.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
2.61 +{\isasymLongrightarrow}\ map\ \mbox{f}\ \mbox{xs}\ =\ map\ \mbox{g}\ \mbox{ys}
2.62 +\end{isabelle}%
2.63 +
2.64 +\end{quote}
2.65 +Its second premise expresses (indirectly) that the second argument of \isa{map} is only applied
2.66 +to elements of its third argument. Congruence rules for other higher-order functions on lists would
2.67 +look very similar but have not been proved yet because they were never needed.
2.68 +If you get into a situation where you need to supply \isacommand{recdef} with new congruence
2.69 +rules, you can either append the line
2.70 +\begin{ttbox}
2.71 +congs <congruence rules>
2.72 +\end{ttbox}
2.73 +to the specific occurrence of \isacommand{recdef} or declare them globally:
2.74 +\begin{ttbox}
2.75 +lemmas [????????] = <congruence rules>
2.76 +\end{ttbox}
2.77 +
2.78 +Note that \isacommand{recdef} feeds on exactly the same \emph{kind} of
2.79 +congruence rules as the simplifier (\S\ref{sec:simp-cong}) but that
2.80 +declaring a congruence rule for the simplifier does not make it
2.81 +available to \isacommand{recdef}, and vice versa. This is intentional.%
2.82 +\end{isamarkuptext}%
2.83 +\end{isabelle}%
2.84 +%%% Local Variables:
2.85 +%%% mode: latex
2.86 +%%% TeX-master: "root"
2.87 +%%% End: