author | haftmann |
Mon, 27 Sep 2010 16:27:31 +0200 | |
changeset 39745 | 3aa2bc9c5478 |
parent 39682 | 066e2d4d0de8 |
child 40406 | 313a24b66a8d |
permissions | -rw-r--r-- |
37614 | 1 |
% |
2 |
\begin{isabellebody}% |
|
3 |
\def\isabellecontext{Inductive{\isacharunderscore}Predicate}% |
|
4 |
% |
|
5 |
\isadelimtheory |
|
6 |
% |
|
7 |
\endisadelimtheory |
|
8 |
% |
|
9 |
\isatagtheory |
|
10 |
\isacommand{theory}\isamarkupfalse% |
|
11 |
\ Inductive{\isacharunderscore}Predicate\isanewline |
|
12 |
\isakeyword{imports}\ Setup\isanewline |
|
38441 | 13 |
\isakeyword{begin}\isanewline |
14 |
% |
|
37614 | 15 |
\endisatagtheory |
16 |
{\isafoldtheory}% |
|
17 |
% |
|
18 |
\isadelimtheory |
|
19 |
% |
|
20 |
\endisadelimtheory |
|
21 |
% |
|
38441 | 22 |
\isadeliminvisible |
23 |
% |
|
24 |
\endisadeliminvisible |
|
25 |
% |
|
26 |
\isataginvisible |
|
27 |
% |
|
28 |
\endisataginvisible |
|
29 |
{\isafoldinvisible}% |
|
30 |
% |
|
31 |
\isadeliminvisible |
|
32 |
% |
|
33 |
\endisadeliminvisible |
|
34 |
% |
|
38437 | 35 |
\isamarkupsection{Inductive Predicates \label{sec:inductive}% |
37614 | 36 |
} |
37 |
\isamarkuptrue% |
|
38 |
% |
|
39 |
\begin{isamarkuptext}% |
|
38509 | 40 |
The \isa{predicate\ compiler} is an extension of the code generator |
38441 | 41 |
which turns inductive specifications into equational ones, from |
42 |
which in turn executable code can be generated. The mechanisms of |
|
43 |
this compiler are described in detail in |
|
44 |
\cite{Berghofer-Bulwahn-Haftmann:2009:TPHOL}. |
|
45 |
||
46 |
Consider the simple predicate \isa{append} given by these two |
|
47 |
introduction rules:% |
|
37614 | 48 |
\end{isamarkuptext}% |
49 |
\isamarkuptrue% |
|
50 |
% |
|
51 |
\isadelimquote |
|
52 |
% |
|
53 |
\endisadelimquote |
|
54 |
% |
|
55 |
\isatagquote |
|
56 |
% |
|
57 |
\begin{isamarkuptext}% |
|
38441 | 58 |
\isa{append\ {\isacharbrackleft}{\isacharbrackright}\ ys\ ys} \\ |
59 |
\isa{append\ xs\ ys\ zs\ {\isasymLongrightarrow}\ append\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys\ {\isacharparenleft}x\ {\isacharhash}\ zs{\isacharparenright}}% |
|
37614 | 60 |
\end{isamarkuptext}% |
61 |
\isamarkuptrue% |
|
62 |
% |
|
63 |
\endisatagquote |
|
64 |
{\isafoldquote}% |
|
65 |
% |
|
66 |
\isadelimquote |
|
67 |
% |
|
68 |
\endisadelimquote |
|
69 |
% |
|
70 |
\begin{isamarkuptext}% |
|
71 |
\noindent To invoke the compiler, simply use \indexdef{}{command}{code\_pred}\hypertarget{command.code-pred}{\hyperlink{command.code-pred}{\mbox{\isa{\isacommand{code{\isacharunderscore}pred}}}}}:% |
|
72 |
\end{isamarkuptext}% |
|
73 |
\isamarkuptrue% |
|
74 |
% |
|
75 |
\isadelimquote |
|
76 |
% |
|
77 |
\endisadelimquote |
|
78 |
% |
|
79 |
\isatagquote |
|
80 |
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse% |
|
81 |
\ append\ \isacommand{{\isachardot}}\isamarkupfalse% |
|
82 |
% |
|
83 |
\endisatagquote |
|
84 |
{\isafoldquote}% |
|
85 |
% |
|
86 |
\isadelimquote |
|
87 |
% |
|
88 |
\endisadelimquote |
|
89 |
% |
|
90 |
\begin{isamarkuptext}% |
|
38441 | 91 |
\noindent The \hyperlink{command.code-pred}{\mbox{\isa{\isacommand{code{\isacharunderscore}pred}}}} command takes the name of the |
92 |
inductive predicate and then you put a period to discharge a trivial |
|
93 |
correctness proof. The compiler infers possible modes for the |
|
94 |
predicate and produces the derived code equations. Modes annotate |
|
95 |
which (parts of the) arguments are to be taken as input, and which |
|
96 |
output. Modes are similar to types, but use the notation \isa{i} |
|
97 |
for input and \isa{o} for output. |
|
37614 | 98 |
|
38441 | 99 |
For \isa{append}, the compiler can infer the following modes: |
100 |
\begin{itemize} |
|
101 |
\item \isa{i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool} |
|
102 |
\item \isa{i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool} |
|
103 |
\item \isa{o\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool} |
|
104 |
\end{itemize} |
|
105 |
You can compute sets of predicates using \indexdef{}{command}{values}\hypertarget{command.values}{\hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}}}:% |
|
37614 | 106 |
\end{isamarkuptext}% |
107 |
\isamarkuptrue% |
|
108 |
% |
|
109 |
\isadelimquote |
|
110 |
% |
|
111 |
\endisadelimquote |
|
112 |
% |
|
113 |
\isatagquote |
|
114 |
\isacommand{values}\isamarkupfalse% |
|
115 |
\ {\isachardoublequoteopen}{\isacharbraceleft}zs{\isachardot}\ append\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{1}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}{\isadigit{2}}{\isacharcomma}{\isadigit{3}}{\isacharbrackright}\ {\isacharbrackleft}{\isadigit{4}}{\isacharcomma}{\isadigit{5}}{\isacharbrackright}\ zs{\isacharbraceright}{\isachardoublequoteclose}% |
|
116 |
\endisatagquote |
|
117 |
{\isafoldquote}% |
|
118 |
% |
|
119 |
\isadelimquote |
|
120 |
% |
|
121 |
\endisadelimquote |
|
122 |
% |
|
123 |
\begin{isamarkuptext}% |
|
124 |
\noindent outputs \isa{{\isacharbraceleft}{\isacharbrackleft}{\isadigit{1}}{\isacharcomma}\ {\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharcomma}\ {\isadigit{4}}{\isacharcomma}\ {\isadigit{5}}{\isacharbrackright}{\isacharbraceright}}, and% |
|
125 |
\end{isamarkuptext}% |
|
126 |
\isamarkuptrue% |
|
127 |
% |
|
128 |
\isadelimquote |
|
129 |
% |
|
130 |
\endisadelimquote |
|
131 |
% |
|
132 |
\isatagquote |
|
133 |
\isacommand{values}\isamarkupfalse% |
|
134 |
\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardot}\ append\ xs\ ys\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{2}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}{\isadigit{3}}{\isacharbrackright}{\isacharbraceright}{\isachardoublequoteclose}% |
|
135 |
\endisatagquote |
|
136 |
{\isafoldquote}% |
|
137 |
% |
|
138 |
\isadelimquote |
|
139 |
% |
|
140 |
\endisadelimquote |
|
141 |
% |
|
142 |
\begin{isamarkuptext}% |
|
143 |
\noindent outputs \isa{{\isacharbraceleft}{\isacharparenleft}{\isacharbrackleft}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharbrackright}{\isacharparenright}{\isacharcomma}\ {\isacharparenleft}{\isacharbrackleft}{\isadigit{2}}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isadigit{3}}{\isacharbrackright}{\isacharparenright}{\isacharcomma}\ {\isacharparenleft}{\isacharbrackleft}{\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharbraceright}}.% |
|
144 |
\end{isamarkuptext}% |
|
145 |
\isamarkuptrue% |
|
146 |
% |
|
147 |
\begin{isamarkuptext}% |
|
38441 | 148 |
\noindent If you are only interested in the first elements of the |
149 |
set comprehension (with respect to a depth-first search on the |
|
150 |
introduction rules), you can pass an argument to \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} |
|
151 |
to specify the number of elements you want:% |
|
37614 | 152 |
\end{isamarkuptext}% |
153 |
\isamarkuptrue% |
|
154 |
% |
|
155 |
\isadelimquote |
|
156 |
% |
|
157 |
\endisadelimquote |
|
158 |
% |
|
159 |
\isatagquote |
|
160 |
\isacommand{values}\isamarkupfalse% |
|
38441 | 161 |
\ {\isadigit{1}}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardot}\ append\ xs\ ys\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{1}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}\ {\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharcomma}\ {\isadigit{4}}{\isacharbrackright}{\isacharbraceright}{\isachardoublequoteclose}\isanewline |
37614 | 162 |
\isacommand{values}\isamarkupfalse% |
38441 | 163 |
\ {\isadigit{3}}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardot}\ append\ xs\ ys\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{1}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}\ {\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharcomma}\ {\isadigit{4}}{\isacharbrackright}{\isacharbraceright}{\isachardoublequoteclose}% |
37614 | 164 |
\endisatagquote |
165 |
{\isafoldquote}% |
|
166 |
% |
|
167 |
\isadelimquote |
|
168 |
% |
|
169 |
\endisadelimquote |
|
170 |
% |
|
171 |
\begin{isamarkuptext}% |
|
172 |
\noindent The \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} command can only compute set |
|
38441 | 173 |
comprehensions for which a mode has been inferred. |
37614 | 174 |
|
38441 | 175 |
The code equations for a predicate are made available as theorems with |
176 |
the suffix \isa{equation}, and can be inspected with:% |
|
37614 | 177 |
\end{isamarkuptext}% |
178 |
\isamarkuptrue% |
|
179 |
% |
|
180 |
\isadelimquote |
|
181 |
% |
|
182 |
\endisadelimquote |
|
183 |
% |
|
184 |
\isatagquote |
|
185 |
\isacommand{thm}\isamarkupfalse% |
|
186 |
\ append{\isachardot}equation% |
|
187 |
\endisatagquote |
|
188 |
{\isafoldquote}% |
|
189 |
% |
|
190 |
\isadelimquote |
|
191 |
% |
|
192 |
\endisadelimquote |
|
193 |
% |
|
194 |
\begin{isamarkuptext}% |
|
195 |
\noindent More advanced options are described in the following subsections.% |
|
196 |
\end{isamarkuptext}% |
|
197 |
\isamarkuptrue% |
|
198 |
% |
|
38441 | 199 |
\isamarkupsubsection{Alternative names for functions% |
37614 | 200 |
} |
201 |
\isamarkuptrue% |
|
202 |
% |
|
203 |
\begin{isamarkuptext}% |
|
38441 | 204 |
By default, the functions generated from a predicate are named after |
205 |
the predicate with the mode mangled into the name (e.g., \isa{append{\isacharunderscore}i{\isacharunderscore}i{\isacharunderscore}o}). You can specify your own names as follows:% |
|
37614 | 206 |
\end{isamarkuptext}% |
207 |
\isamarkuptrue% |
|
208 |
% |
|
209 |
\isadelimquote |
|
210 |
% |
|
211 |
\endisadelimquote |
|
212 |
% |
|
213 |
\isatagquote |
|
214 |
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse% |
|
38813 | 215 |
\ {\isacharparenleft}modes{\isacharcolon}\ i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool\ as\ concat{\isacharcomma}\isanewline |
216 |
\ \ o\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool\ as\ split{\isacharcomma}\isanewline |
|
217 |
\ \ i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool\ as\ suffix{\isacharparenright}\ append\ \isacommand{{\isachardot}}\isamarkupfalse% |
|
37614 | 218 |
% |
219 |
\endisatagquote |
|
220 |
{\isafoldquote}% |
|
221 |
% |
|
222 |
\isadelimquote |
|
223 |
% |
|
224 |
\endisadelimquote |
|
225 |
% |
|
38441 | 226 |
\isamarkupsubsection{Alternative introduction rules% |
37614 | 227 |
} |
228 |
\isamarkuptrue% |
|
229 |
% |
|
230 |
\begin{isamarkuptext}% |
|
38441 | 231 |
Sometimes the introduction rules of an predicate are not executable |
232 |
because they contain non-executable constants or specific modes |
|
233 |
could not be inferred. It is also possible that the introduction |
|
234 |
rules yield a function that loops forever due to the execution in a |
|
235 |
depth-first search manner. Therefore, you can declare alternative |
|
236 |
introduction rules for predicates with the attribute \hyperlink{attribute.code-pred-intro}{\mbox{\isa{code{\isacharunderscore}pred{\isacharunderscore}intro}}}. For example, the transitive closure is defined |
|
237 |
by:% |
|
37614 | 238 |
\end{isamarkuptext}% |
239 |
\isamarkuptrue% |
|
240 |
% |
|
241 |
\isadelimquote |
|
242 |
% |
|
243 |
\endisadelimquote |
|
244 |
% |
|
245 |
\isatagquote |
|
246 |
% |
|
247 |
\begin{isamarkuptext}% |
|
39682
066e2d4d0de8
avoid fragile tranclp syntax; corrected resolution; corrected typo
haftmann
parents:
39068
diff
changeset
|
248 |
\isa{{\isachardoublequote}r\ a\ b\ {\isasymLongrightarrow}\ tranclp\ r\ a\ b{\isachardoublequote}}\\ |
066e2d4d0de8
avoid fragile tranclp syntax; corrected resolution; corrected typo
haftmann
parents:
39068
diff
changeset
|
249 |
\isa{{\isachardoublequote}tranclp\ r\ a\ b\ {\isasymLongrightarrow}\ r\ b\ c\ {\isasymLongrightarrow}\ tranclp\ r\ a\ c{\isachardoublequote}}% |
37614 | 250 |
\end{isamarkuptext}% |
251 |
\isamarkuptrue% |
|
252 |
% |
|
253 |
\endisatagquote |
|
254 |
{\isafoldquote}% |
|
255 |
% |
|
256 |
\isadelimquote |
|
257 |
% |
|
258 |
\endisadelimquote |
|
259 |
% |
|
260 |
\begin{isamarkuptext}% |
|
38441 | 261 |
\noindent These rules do not suit well for executing the transitive |
262 |
closure with the mode \isa{{\isacharparenleft}i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool}, as |
|
263 |
the second rule will cause an infinite loop in the recursive call. |
|
264 |
This can be avoided using the following alternative rules which are |
|
265 |
declared to the predicate compiler by the attribute \hyperlink{attribute.code-pred-intro}{\mbox{\isa{code{\isacharunderscore}pred{\isacharunderscore}intro}}}:% |
|
37614 | 266 |
\end{isamarkuptext}% |
267 |
\isamarkuptrue% |
|
268 |
% |
|
269 |
\isadelimquote |
|
270 |
% |
|
271 |
\endisadelimquote |
|
272 |
% |
|
273 |
\isatagquote |
|
274 |
\isacommand{lemma}\isamarkupfalse% |
|
275 |
\ {\isacharbrackleft}code{\isacharunderscore}pred{\isacharunderscore}intro{\isacharbrackright}{\isacharcolon}\isanewline |
|
39682
066e2d4d0de8
avoid fragile tranclp syntax; corrected resolution; corrected typo
haftmann
parents:
39068
diff
changeset
|
276 |
\ \ {\isachardoublequoteopen}r\ a\ b\ {\isasymLongrightarrow}\ tranclp\ r\ a\ b{\isachardoublequoteclose}\isanewline |
066e2d4d0de8
avoid fragile tranclp syntax; corrected resolution; corrected typo
haftmann
parents:
39068
diff
changeset
|
277 |
\ \ {\isachardoublequoteopen}r\ a\ b\ {\isasymLongrightarrow}\ tranclp\ r\ b\ c\ {\isasymLongrightarrow}\ tranclp\ r\ a\ c{\isachardoublequoteclose}\isanewline |
37614 | 278 |
\isacommand{by}\isamarkupfalse% |
279 |
\ auto% |
|
280 |
\endisatagquote |
|
281 |
{\isafoldquote}% |
|
282 |
% |
|
283 |
\isadelimquote |
|
284 |
% |
|
285 |
\endisadelimquote |
|
286 |
% |
|
287 |
\begin{isamarkuptext}% |
|
38441 | 288 |
\noindent After declaring all alternative rules for the transitive |
289 |
closure, you invoke \hyperlink{command.code-pred}{\mbox{\isa{\isacommand{code{\isacharunderscore}pred}}}} as usual. As you have |
|
290 |
declared alternative rules for the predicate, you are urged to prove |
|
291 |
that these introduction rules are complete, i.e., that you can |
|
292 |
derive an elimination rule for the alternative rules:% |
|
37614 | 293 |
\end{isamarkuptext}% |
294 |
\isamarkuptrue% |
|
295 |
% |
|
296 |
\isadelimquote |
|
297 |
% |
|
298 |
\endisadelimquote |
|
299 |
% |
|
300 |
\isatagquote |
|
301 |
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse% |
|
302 |
\ tranclp\isanewline |
|
303 |
\isacommand{proof}\isamarkupfalse% |
|
304 |
\ {\isacharminus}\isanewline |
|
305 |
\ \ \isacommand{case}\isamarkupfalse% |
|
306 |
\ tranclp\isanewline |
|
307 |
\ \ \isacommand{from}\isamarkupfalse% |
|
39682
066e2d4d0de8
avoid fragile tranclp syntax; corrected resolution; corrected typo
haftmann
parents:
39068
diff
changeset
|
308 |
\ this\ converse{\isacharunderscore}tranclpE\ {\isacharbrackleft}OF\ tranclp{\isachardot}prems{\isacharbrackright}\ \isacommand{show}\isamarkupfalse% |
37614 | 309 |
\ thesis\ \isacommand{by}\isamarkupfalse% |
310 |
\ metis\isanewline |
|
311 |
\isacommand{qed}\isamarkupfalse% |
|
312 |
% |
|
313 |
\endisatagquote |
|
314 |
{\isafoldquote}% |
|
315 |
% |
|
316 |
\isadelimquote |
|
317 |
% |
|
318 |
\endisadelimquote |
|
319 |
% |
|
320 |
\begin{isamarkuptext}% |
|
38441 | 321 |
\noindent Alternative rules can also be used for constants that have |
322 |
not been defined inductively. For example, the lexicographic order |
|
323 |
which is defined as:% |
|
37614 | 324 |
\end{isamarkuptext}% |
325 |
\isamarkuptrue% |
|
326 |
% |
|
327 |
\isadelimquote |
|
328 |
% |
|
329 |
\endisadelimquote |
|
330 |
% |
|
331 |
\isatagquote |
|
332 |
% |
|
333 |
\begin{isamarkuptext}% |
|
334 |
\begin{isabelle}% |
|
335 |
lexord\ r\ {\isacharequal}\isanewline |
|
336 |
{\isacharbraceleft}{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}{\isachardot}\isanewline |
|
337 |
\isaindent{\ }{\isasymexists}a\ v{\isachardot}\ y\ {\isacharequal}\ x\ {\isacharat}\ a\ {\isacharhash}\ v\ {\isasymor}\isanewline |
|
338 |
\isaindent{\ {\isasymexists}a\ v{\isachardot}\ }{\isacharparenleft}{\isasymexists}u\ a\ b\ v\ w{\isachardot}\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}\ {\isasymin}\ r\ {\isasymand}\ x\ {\isacharequal}\ u\ {\isacharat}\ a\ {\isacharhash}\ v\ {\isasymand}\ y\ {\isacharequal}\ u\ {\isacharat}\ b\ {\isacharhash}\ w{\isacharparenright}{\isacharbraceright}% |
|
339 |
\end{isabelle}% |
|
340 |
\end{isamarkuptext}% |
|
341 |
\isamarkuptrue% |
|
342 |
% |
|
343 |
\endisatagquote |
|
344 |
{\isafoldquote}% |
|
345 |
% |
|
346 |
\isadelimquote |
|
347 |
% |
|
348 |
\endisadelimquote |
|
349 |
% |
|
350 |
\begin{isamarkuptext}% |
|
38441 | 351 |
\noindent To make it executable, you can derive the following two |
352 |
rules and prove the elimination rule:% |
|
37614 | 353 |
\end{isamarkuptext}% |
354 |
\isamarkuptrue% |
|
355 |
% |
|
356 |
\isadelimquote |
|
357 |
% |
|
358 |
\endisadelimquote |
|
359 |
% |
|
360 |
\isatagquote |
|
361 |
\isacommand{lemma}\isamarkupfalse% |
|
362 |
\ {\isacharbrackleft}code{\isacharunderscore}pred{\isacharunderscore}intro{\isacharbrackright}{\isacharcolon}\isanewline |
|
363 |
\ \ {\isachardoublequoteopen}append\ xs\ {\isacharparenleft}a\ {\isacharhash}\ v{\isacharparenright}\ ys\ {\isasymLongrightarrow}\ lexord\ r\ {\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
364 |
\isacommand{lemma}\isamarkupfalse% |
|
365 |
\ {\isacharbrackleft}code{\isacharunderscore}pred{\isacharunderscore}intro{\isacharbrackright}{\isacharcolon}\isanewline |
|
366 |
\ \ {\isachardoublequoteopen}append\ u\ {\isacharparenleft}a\ {\isacharhash}\ v{\isacharparenright}\ xs\ {\isasymLongrightarrow}\ append\ u\ {\isacharparenleft}b\ {\isacharhash}\ w{\isacharparenright}\ ys\ {\isasymLongrightarrow}\ r\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}\isanewline |
|
367 |
\ \ {\isasymLongrightarrow}\ lexord\ r\ {\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
368 |
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse% |
|
369 |
\ lexord% |
|
370 |
\endisatagquote |
|
371 |
{\isafoldquote}% |
|
372 |
% |
|
373 |
\isadelimquote |
|
374 |
% |
|
375 |
\endisadelimquote |
|
376 |
% |
|
38441 | 377 |
\isamarkupsubsection{Options for values% |
37614 | 378 |
} |
379 |
\isamarkuptrue% |
|
380 |
% |
|
381 |
\begin{isamarkuptext}% |
|
38441 | 382 |
In the presence of higher-order predicates, multiple modes for some |
383 |
predicate could be inferred that are not disambiguated by the |
|
384 |
pattern of the set comprehension. To disambiguate the modes for the |
|
385 |
arguments of a predicate, you can state the modes explicitly in the |
|
386 |
\hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} command. Consider the simple predicate \isa{succ}:% |
|
37614 | 387 |
\end{isamarkuptext}% |
388 |
\isamarkuptrue% |
|
38441 | 389 |
% |
390 |
\isadelimquote |
|
391 |
% |
|
392 |
\endisadelimquote |
|
393 |
% |
|
394 |
\isatagquote |
|
37614 | 395 |
\isacommand{inductive}\isamarkupfalse% |
38441 | 396 |
\ succ\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
37614 | 397 |
\ \ {\isachardoublequoteopen}succ\ {\isadigit{0}}\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
398 |
{\isacharbar}\ {\isachardoublequoteopen}succ\ x\ y\ {\isasymLongrightarrow}\ succ\ {\isacharparenleft}Suc\ x{\isacharparenright}\ {\isacharparenleft}Suc\ y{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
399 |
\isanewline |
|
400 |
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse% |
|
38441 | 401 |
\ succ\ \isacommand{{\isachardot}}\isamarkupfalse% |
37614 | 402 |
% |
38441 | 403 |
\endisatagquote |
404 |
{\isafoldquote}% |
|
37614 | 405 |
% |
38441 | 406 |
\isadelimquote |
37614 | 407 |
% |
38441 | 408 |
\endisadelimquote |
37614 | 409 |
% |
410 |
\begin{isamarkuptext}% |
|
38441 | 411 |
\noindent For this, the predicate compiler can infer modes \isa{o\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool}, \isa{i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool}, \isa{o\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool} and |
412 |
\isa{i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool}. The invocation of \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} |
|
413 |
\isa{{\isacharbraceleft}n{\isachardot}\ tranclp\ succ\ {\isadigit{1}}{\isadigit{0}}\ n{\isacharbraceright}} loops, as multiple modes for the |
|
414 |
predicate \isa{succ} are possible and here the first mode \isa{o\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool} is chosen. To choose another mode for the argument, |
|
415 |
you can declare the mode for the argument between the \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} and the number of elements.% |
|
37614 | 416 |
\end{isamarkuptext}% |
417 |
\isamarkuptrue% |
|
418 |
% |
|
419 |
\isadelimquote |
|
420 |
% |
|
421 |
\endisadelimquote |
|
422 |
% |
|
423 |
\isatagquote |
|
424 |
\isacommand{values}\isamarkupfalse% |
|
39068 | 425 |
\ {\isacharbrackleft}mode{\isacharcolon}\ i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool{\isacharbrackright}\ {\isadigit{1}}\ {\isachardoublequoteopen}{\isacharbraceleft}n{\isachardot}\ tranclp\ succ\ {\isadigit{1}}{\isadigit{0}}\ n{\isacharbraceright}{\isachardoublequoteclose}\ \isanewline |
37614 | 426 |
\isacommand{values}\isamarkupfalse% |
39068 | 427 |
\ {\isacharbrackleft}mode{\isacharcolon}\ o\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool{\isacharbrackright}\ {\isadigit{1}}\ {\isachardoublequoteopen}{\isacharbraceleft}n{\isachardot}\ tranclp\ succ\ n\ {\isadigit{1}}{\isadigit{0}}{\isacharbraceright}{\isachardoublequoteclose}% |
37614 | 428 |
\endisatagquote |
429 |
{\isafoldquote}% |
|
430 |
% |
|
431 |
\isadelimquote |
|
432 |
% |
|
433 |
\endisadelimquote |
|
434 |
% |
|
38441 | 435 |
\isamarkupsubsection{Embedding into functional code within Isabelle/HOL% |
37614 | 436 |
} |
437 |
\isamarkuptrue% |
|
438 |
% |
|
439 |
\begin{isamarkuptext}% |
|
38441 | 440 |
To embed the computation of an inductive predicate into functions |
441 |
that are defined in Isabelle/HOL, you have a number of options: |
|
442 |
||
443 |
\begin{itemize} |
|
444 |
||
445 |
\item You want to use the first-order predicate with the mode |
|
446 |
where all arguments are input. Then you can use the predicate directly, e.g. |
|
447 |
||
448 |
\begin{quote} |
|
449 |
\isa{valid{\isacharunderscore}suffix\ ys\ zs\ {\isacharequal}} \\ |
|
450 |
\isa{{\isacharparenleft}if\ append\ {\isacharbrackleft}Suc\ {\isadigit{0}}{\isacharcomma}\ {\isadigit{2}}{\isacharbrackright}\ ys\ zs\ then\ Some\ ys\ else\ None{\isacharparenright}} |
|
451 |
\end{quote} |
|
452 |
||
453 |
\item If you know that the execution returns only one value (it is |
|
454 |
deterministic), then you can use the combinator \isa{Predicate{\isachardot}the}, e.g., a functional concatenation of lists is |
|
455 |
defined with |
|
456 |
||
457 |
\begin{quote} |
|
458 |
\isa{functional{\isacharunderscore}concat\ xs\ ys\ {\isacharequal}\ Predicate{\isachardot}the\ {\isacharparenleft}append{\isacharunderscore}i{\isacharunderscore}i{\isacharunderscore}o\ xs\ ys{\isacharparenright}} |
|
459 |
\end{quote} |
|
460 |
||
461 |
Note that if the evaluation does not return a unique value, it |
|
462 |
raises a run-time error \isa{not{\isacharunderscore}unique}. |
|
463 |
||
464 |
\end{itemize}% |
|
37614 | 465 |
\end{isamarkuptext}% |
466 |
\isamarkuptrue% |
|
467 |
% |
|
38441 | 468 |
\isamarkupsubsection{Further Examples% |
37614 | 469 |
} |
470 |
\isamarkuptrue% |
|
471 |
% |
|
472 |
\begin{isamarkuptext}% |
|
473 |
Further examples for compiling inductive predicates can be found in |
|
39682
066e2d4d0de8
avoid fragile tranclp syntax; corrected resolution; corrected typo
haftmann
parents:
39068
diff
changeset
|
474 |
the \isa{HOL{\isacharslash}ex{\isacharslash}Predicate{\isacharunderscore}Compile{\isacharunderscore}ex{\isachardot}thy} theory file. There are |
38441 | 475 |
also some examples in the Archive of Formal Proofs, notably in the |
476 |
\isa{POPLmark{\isacharminus}deBruijn} and the \isa{FeatherweightJava} |
|
477 |
sessions.% |
|
37614 | 478 |
\end{isamarkuptext}% |
479 |
\isamarkuptrue% |
|
480 |
% |
|
481 |
\isadelimtheory |
|
482 |
% |
|
483 |
\endisadelimtheory |
|
484 |
% |
|
485 |
\isatagtheory |
|
486 |
\isacommand{end}\isamarkupfalse% |
|
487 |
% |
|
488 |
\endisatagtheory |
|
489 |
{\isafoldtheory}% |
|
490 |
% |
|
491 |
\isadelimtheory |
|
492 |
% |
|
493 |
\endisadelimtheory |
|
494 |
\isanewline |
|
495 |
\end{isabellebody}% |
|
496 |
%%% Local Variables: |
|
497 |
%%% mode: latex |
|
498 |
%%% TeX-master: "root" |
|
499 |
%%% End: |