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