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