28447
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{Program}%
|
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
%
|
|
7 |
\endisadelimtheory
|
|
8 |
%
|
|
9 |
\isatagtheory
|
|
10 |
\isacommand{theory}\isamarkupfalse%
|
|
11 |
\ Program\isanewline
|
|
12 |
\isakeyword{imports}\ Introduction\isanewline
|
|
13 |
\isakeyword{begin}%
|
|
14 |
\endisatagtheory
|
|
15 |
{\isafoldtheory}%
|
|
16 |
%
|
|
17 |
\isadelimtheory
|
|
18 |
%
|
|
19 |
\endisadelimtheory
|
|
20 |
%
|
|
21 |
\isamarkupsection{Turning Theories into Programs \label{sec:program}%
|
|
22 |
}
|
|
23 |
\isamarkuptrue%
|
|
24 |
%
|
|
25 |
\isamarkupsubsection{The \isa{Isabelle{\isacharslash}HOL} default setup%
|
|
26 |
}
|
|
27 |
\isamarkuptrue%
|
|
28 |
%
|
|
29 |
\begin{isamarkuptext}%
|
|
30 |
We have already seen how by default equations stemming from
|
|
31 |
\hyperlink{command.definition}{\mbox{\isa{\isacommand{definition}}}}/\hyperlink{command.primrec}{\mbox{\isa{\isacommand{primrec}}}}/\hyperlink{command.fun}{\mbox{\isa{\isacommand{fun}}}}
|
|
32 |
statements are used for code generation. This default behaviour
|
|
33 |
can be changed, e.g. by providing different defining equations.
|
|
34 |
All kinds of customization shown in this section is \emph{safe}
|
|
35 |
in the sense that the user does not have to worry about
|
|
36 |
correctness -- all programs generatable that way are partially
|
|
37 |
correct.%
|
|
38 |
\end{isamarkuptext}%
|
|
39 |
\isamarkuptrue%
|
|
40 |
%
|
|
41 |
\isamarkupsubsection{Selecting code equations%
|
|
42 |
}
|
|
43 |
\isamarkuptrue%
|
|
44 |
%
|
|
45 |
\begin{isamarkuptext}%
|
|
46 |
Coming back to our introductory example, we
|
|
47 |
could provide an alternative defining equations for \isa{dequeue}
|
|
48 |
explicitly:%
|
|
49 |
\end{isamarkuptext}%
|
|
50 |
\isamarkuptrue%
|
|
51 |
%
|
|
52 |
\isadelimquoteme
|
|
53 |
%
|
|
54 |
\endisadelimquoteme
|
|
55 |
%
|
|
56 |
\isatagquoteme
|
|
57 |
\isacommand{lemma}\isamarkupfalse%
|
|
58 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
59 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
|
28456
|
60 |
\ \ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
|
|
61 |
\ \ \ \ \ \ \ else\ dequeue\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
28447
|
62 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline
|
|
63 |
\ \ \ \ \ {\isacharparenleft}Some\ y{\isacharcomma}\ Queue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
64 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
65 |
\ {\isacharparenleft}cases\ xs{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\ {\isacharparenleft}cases\ {\isachardoublequoteopen}rev\ xs{\isachardoublequoteclose}{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}%
|
|
66 |
\endisatagquoteme
|
|
67 |
{\isafoldquoteme}%
|
|
68 |
%
|
|
69 |
\isadelimquoteme
|
|
70 |
%
|
|
71 |
\endisadelimquoteme
|
|
72 |
%
|
|
73 |
\begin{isamarkuptext}%
|
|
74 |
\noindent The annotation \isa{{\isacharbrackleft}code\ func{\isacharbrackright}} is an \isa{Isar}
|
|
75 |
\isa{attribute} which states that the given theorems should be
|
|
76 |
considered as defining equations for a \isa{fun} statement --
|
|
77 |
the corresponding constant is determined syntactically. The resulting code:%
|
|
78 |
\end{isamarkuptext}%
|
|
79 |
\isamarkuptrue%
|
|
80 |
%
|
|
81 |
\isadelimquoteme
|
|
82 |
%
|
|
83 |
\endisadelimquoteme
|
|
84 |
%
|
|
85 |
\isatagquoteme
|
|
86 |
%
|
|
87 |
\begin{isamarkuptext}%
|
|
88 |
\isaverbatim%
|
|
89 |
\noindent%
|
|
90 |
\verb|dequeue :: forall a. Queue a -> (Maybe a, Queue a);|\newline%
|
|
91 |
\verb|dequeue (Queue xs (y : ys)) = (Just y, Queue xs ys);|\newline%
|
|
92 |
\verb|dequeue (Queue xs []) =|\newline%
|
|
93 |
\verb| (if nulla xs then (Nothing, Queue [] [])|\newline%
|
28456
|
94 |
\verb| else dequeue (Queue [] (rev xs)));|%
|
28447
|
95 |
\end{isamarkuptext}%
|
|
96 |
\isamarkuptrue%
|
|
97 |
%
|
|
98 |
\endisatagquoteme
|
|
99 |
{\isafoldquoteme}%
|
|
100 |
%
|
|
101 |
\isadelimquoteme
|
|
102 |
%
|
|
103 |
\endisadelimquoteme
|
|
104 |
%
|
|
105 |
\begin{isamarkuptext}%
|
|
106 |
\noindent You may note that the equality test \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} has been
|
|
107 |
replaced by the predicate \isa{null\ xs}. This is due to the default
|
|
108 |
setup in the \qn{preprocessor} to be discussed further below (\secref{sec:preproc}).
|
|
109 |
|
|
110 |
Changing the default constructor set of datatypes is also
|
|
111 |
possible but rarely desired in practice. See \secref{sec:datatypes} for an example.
|
|
112 |
|
|
113 |
As told in \secref{sec:concept}, code generation is based
|
|
114 |
on a structured collection of code theorems.
|
|
115 |
For explorative purpose, this collection
|
|
116 |
may be inspected using the \hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} command:%
|
|
117 |
\end{isamarkuptext}%
|
|
118 |
\isamarkuptrue%
|
|
119 |
%
|
|
120 |
\isadelimquoteme
|
|
121 |
%
|
|
122 |
\endisadelimquoteme
|
|
123 |
%
|
|
124 |
\isatagquoteme
|
|
125 |
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
|
|
126 |
\ dequeue%
|
|
127 |
\endisatagquoteme
|
|
128 |
{\isafoldquoteme}%
|
|
129 |
%
|
|
130 |
\isadelimquoteme
|
|
131 |
%
|
|
132 |
\endisadelimquoteme
|
|
133 |
%
|
|
134 |
\begin{isamarkuptext}%
|
|
135 |
\noindent prints a table with \emph{all} defining equations
|
|
136 |
for \isa{dequeue}, including
|
|
137 |
\emph{all} defining equations those equations depend
|
|
138 |
on recursively.
|
|
139 |
|
|
140 |
Similarly, the \hyperlink{command.code-deps}{\mbox{\isa{\isacommand{code{\isacharunderscore}deps}}}} command shows a graph
|
|
141 |
visualising dependencies between defining equations.%
|
|
142 |
\end{isamarkuptext}%
|
|
143 |
\isamarkuptrue%
|
|
144 |
%
|
|
145 |
\isamarkupsubsection{\isa{class} and \isa{instantiation}%
|
|
146 |
}
|
|
147 |
\isamarkuptrue%
|
|
148 |
%
|
|
149 |
\begin{isamarkuptext}%
|
|
150 |
Concerning type classes and code generation, let us examine an example
|
|
151 |
from abstract algebra:%
|
|
152 |
\end{isamarkuptext}%
|
|
153 |
\isamarkuptrue%
|
|
154 |
%
|
|
155 |
\isadelimquoteme
|
|
156 |
%
|
|
157 |
\endisadelimquoteme
|
|
158 |
%
|
|
159 |
\isatagquoteme
|
|
160 |
\isacommand{class}\isamarkupfalse%
|
|
161 |
\ semigroup\ {\isacharequal}\ type\ {\isacharplus}\isanewline
|
|
162 |
\ \ \isakeyword{fixes}\ mult\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}{\isasymotimes}{\isachardoublequoteclose}\ {\isadigit{7}}{\isadigit{0}}{\isacharparenright}\isanewline
|
|
163 |
\ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
164 |
\isanewline
|
|
165 |
\isacommand{class}\isamarkupfalse%
|
|
166 |
\ monoid\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline
|
|
167 |
\ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
168 |
\ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
|
|
169 |
\ \ \ \ \isakeyword{and}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
|
|
170 |
\isanewline
|
|
171 |
\isacommand{instantiation}\isamarkupfalse%
|
|
172 |
\ nat\ {\isacharcolon}{\isacharcolon}\ monoid\isanewline
|
|
173 |
\isakeyword{begin}\isanewline
|
|
174 |
\isanewline
|
|
175 |
\isacommand{primrec}\isamarkupfalse%
|
|
176 |
\ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline
|
|
177 |
\ \ \ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isasymotimes}\ n\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
178 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ n\ {\isacharplus}\ m\ {\isasymotimes}\ n{\isachardoublequoteclose}\isanewline
|
|
179 |
\isanewline
|
|
180 |
\isacommand{definition}\isamarkupfalse%
|
|
181 |
\ neutral{\isacharunderscore}nat\ \isakeyword{where}\isanewline
|
|
182 |
\ \ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
|
|
183 |
\isanewline
|
|
184 |
\isacommand{lemma}\isamarkupfalse%
|
|
185 |
\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharcolon}\isanewline
|
|
186 |
\ \ \isakeyword{fixes}\ n\ m\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
|
|
187 |
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}{\isacharparenleft}n\ {\isacharplus}\ m{\isacharparenright}\ {\isasymotimes}\ q\ {\isacharequal}\ n\ {\isasymotimes}\ q\ {\isacharplus}\ m\ {\isasymotimes}\ q{\isachardoublequoteclose}\isanewline
|
|
188 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
189 |
\ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
|
|
190 |
\isanewline
|
|
191 |
\isacommand{instance}\isamarkupfalse%
|
|
192 |
\ \isacommand{proof}\isamarkupfalse%
|
|
193 |
\isanewline
|
|
194 |
\ \ \isacommand{fix}\isamarkupfalse%
|
|
195 |
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
|
|
196 |
\ \ \isacommand{show}\isamarkupfalse%
|
|
197 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
198 |
\ \ \ \ \isacommand{by}\isamarkupfalse%
|
|
199 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharparenright}\isanewline
|
|
200 |
\ \ \isacommand{show}\isamarkupfalse%
|
|
201 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
|
|
202 |
\ \ \ \ \isacommand{by}\isamarkupfalse%
|
|
203 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
|
|
204 |
\ \ \isacommand{show}\isamarkupfalse%
|
|
205 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
|
|
206 |
\ \ \ \ \isacommand{by}\isamarkupfalse%
|
|
207 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
|
|
208 |
\isacommand{qed}\isamarkupfalse%
|
|
209 |
\isanewline
|
|
210 |
\isanewline
|
|
211 |
\isacommand{end}\isamarkupfalse%
|
|
212 |
%
|
|
213 |
\endisatagquoteme
|
|
214 |
{\isafoldquoteme}%
|
|
215 |
%
|
|
216 |
\isadelimquoteme
|
|
217 |
%
|
|
218 |
\endisadelimquoteme
|
|
219 |
%
|
|
220 |
\begin{isamarkuptext}%
|
|
221 |
\noindent We define the natural operation of the natural numbers
|
|
222 |
on monoids:%
|
|
223 |
\end{isamarkuptext}%
|
|
224 |
\isamarkuptrue%
|
|
225 |
%
|
|
226 |
\isadelimquoteme
|
|
227 |
%
|
|
228 |
\endisadelimquoteme
|
|
229 |
%
|
|
230 |
\isatagquoteme
|
|
231 |
\isacommand{primrec}\isamarkupfalse%
|
|
232 |
\ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\ pow\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
233 |
\ \ \ \ {\isachardoublequoteopen}pow\ {\isadigit{0}}\ a\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
|
|
234 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}pow\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ a\ {\isasymotimes}\ pow\ n\ a{\isachardoublequoteclose}%
|
|
235 |
\endisatagquoteme
|
|
236 |
{\isafoldquoteme}%
|
|
237 |
%
|
|
238 |
\isadelimquoteme
|
|
239 |
%
|
|
240 |
\endisadelimquoteme
|
|
241 |
%
|
|
242 |
\begin{isamarkuptext}%
|
|
243 |
\noindent This we use to define the discrete exponentiation function:%
|
|
244 |
\end{isamarkuptext}%
|
|
245 |
\isamarkuptrue%
|
|
246 |
%
|
|
247 |
\isadelimquoteme
|
|
248 |
%
|
|
249 |
\endisadelimquoteme
|
|
250 |
%
|
|
251 |
\isatagquoteme
|
|
252 |
\isacommand{definition}\isamarkupfalse%
|
|
253 |
\ bexp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
254 |
\ \ {\isachardoublequoteopen}bexp\ n\ {\isacharequal}\ pow\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
|
|
255 |
\endisatagquoteme
|
|
256 |
{\isafoldquoteme}%
|
|
257 |
%
|
|
258 |
\isadelimquoteme
|
|
259 |
%
|
|
260 |
\endisadelimquoteme
|
|
261 |
%
|
|
262 |
\begin{isamarkuptext}%
|
|
263 |
\noindent The corresponding code:%
|
|
264 |
\end{isamarkuptext}%
|
|
265 |
\isamarkuptrue%
|
|
266 |
%
|
|
267 |
\isadelimquoteme
|
|
268 |
%
|
|
269 |
\endisadelimquoteme
|
|
270 |
%
|
|
271 |
\isatagquoteme
|
|
272 |
%
|
|
273 |
\begin{isamarkuptext}%
|
|
274 |
\isaverbatim%
|
|
275 |
\noindent%
|
|
276 |
\verb|module Example where {|\newline%
|
|
277 |
\newline%
|
|
278 |
\newline%
|
|
279 |
\verb|data Nat = Suc Nat |\verb,|,\verb| Zero_nat;|\newline%
|
|
280 |
\newline%
|
|
281 |
\verb|class Semigroup a where {|\newline%
|
|
282 |
\verb| mult :: a -> a -> a;|\newline%
|
|
283 |
\verb|};|\newline%
|
|
284 |
\newline%
|
|
285 |
\verb|class (Semigroup a) => Monoid a where {|\newline%
|
|
286 |
\verb| neutral :: a;|\newline%
|
|
287 |
\verb|};|\newline%
|
|
288 |
\newline%
|
|
289 |
\verb|pow :: forall a. (Monoid a) => Nat -> a -> a;|\newline%
|
|
290 |
\verb|pow (Suc n) a = mult a (pow n a);|\newline%
|
|
291 |
\verb|pow Zero_nat a = neutral;|\newline%
|
|
292 |
\newline%
|
|
293 |
\verb|plus_nat :: Nat -> Nat -> Nat;|\newline%
|
|
294 |
\verb|plus_nat (Suc m) n = plus_nat m (Suc n);|\newline%
|
|
295 |
\verb|plus_nat Zero_nat n = n;|\newline%
|
|
296 |
\newline%
|
|
297 |
\verb|neutral_nat :: Nat;|\newline%
|
|
298 |
\verb|neutral_nat = Suc Zero_nat;|\newline%
|
|
299 |
\newline%
|
|
300 |
\verb|mult_nat :: Nat -> Nat -> Nat;|\newline%
|
|
301 |
\verb|mult_nat (Suc m) n = plus_nat n (mult_nat m n);|\newline%
|
|
302 |
\verb|mult_nat Zero_nat n = Zero_nat;|\newline%
|
|
303 |
\newline%
|
|
304 |
\verb|instance Semigroup Nat where {|\newline%
|
|
305 |
\verb| mult = mult_nat;|\newline%
|
|
306 |
\verb|};|\newline%
|
|
307 |
\newline%
|
|
308 |
\verb|instance Monoid Nat where {|\newline%
|
|
309 |
\verb| neutral = neutral_nat;|\newline%
|
|
310 |
\verb|};|\newline%
|
|
311 |
\newline%
|
|
312 |
\verb|bexp :: Nat -> Nat;|\newline%
|
|
313 |
\verb|bexp n = pow n (Suc (Suc Zero_nat));|\newline%
|
|
314 |
\newline%
|
|
315 |
\verb|}|%
|
|
316 |
\end{isamarkuptext}%
|
|
317 |
\isamarkuptrue%
|
|
318 |
%
|
|
319 |
\endisatagquoteme
|
|
320 |
{\isafoldquoteme}%
|
|
321 |
%
|
|
322 |
\isadelimquoteme
|
|
323 |
%
|
|
324 |
\endisadelimquoteme
|
|
325 |
%
|
|
326 |
\begin{isamarkuptext}%
|
|
327 |
\noindent This is a convenient place to show how explicit dictionary construction
|
|
328 |
manifests in generated code (here, the same example in \isa{SML}):%
|
|
329 |
\end{isamarkuptext}%
|
|
330 |
\isamarkuptrue%
|
|
331 |
%
|
|
332 |
\isadelimquoteme
|
|
333 |
%
|
|
334 |
\endisadelimquoteme
|
|
335 |
%
|
|
336 |
\isatagquoteme
|
|
337 |
%
|
|
338 |
\begin{isamarkuptext}%
|
|
339 |
\isaverbatim%
|
|
340 |
\noindent%
|
|
341 |
\verb|structure Example = |\newline%
|
|
342 |
\verb|struct|\newline%
|
|
343 |
\newline%
|
|
344 |
\verb|datatype nat = Suc of nat |\verb,|,\verb| Zero_nat;|\newline%
|
|
345 |
\newline%
|
|
346 |
\verb|type 'a semigroup = {mult : 'a -> 'a -> 'a};|\newline%
|
|
347 |
\verb|fun mult (A_:'a semigroup) = #mult A_;|\newline%
|
|
348 |
\newline%
|
|
349 |
\verb|type 'a monoid = {Program__semigroup_monoid : 'a semigroup, neutral : 'a};|\newline%
|
|
350 |
\verb|fun semigroup_monoid (A_:'a monoid) = #Program__semigroup_monoid A_;|\newline%
|
|
351 |
\verb|fun neutral (A_:'a monoid) = #neutral A_;|\newline%
|
|
352 |
\newline%
|
|
353 |
\verb|fun pow A_ (Suc n) a = mult (semigroup_monoid A_) a (pow A_ n a)|\newline%
|
|
354 |
\verb| |\verb,|,\verb| pow A_ Zero_nat a = neutral A_;|\newline%
|
|
355 |
\newline%
|
|
356 |
\verb|fun plus_nat (Suc m) n = plus_nat m (Suc n)|\newline%
|
|
357 |
\verb| |\verb,|,\verb| plus_nat Zero_nat n = n;|\newline%
|
|
358 |
\newline%
|
|
359 |
\verb|val neutral_nat : nat = Suc Zero_nat;|\newline%
|
|
360 |
\newline%
|
|
361 |
\verb|fun mult_nat (Suc m) n = plus_nat n (mult_nat m n)|\newline%
|
|
362 |
\verb| |\verb,|,\verb| mult_nat Zero_nat n = Zero_nat;|\newline%
|
|
363 |
\newline%
|
|
364 |
\verb|val semigroup_nat = {mult = mult_nat} : nat semigroup;|\newline%
|
|
365 |
\newline%
|
|
366 |
\verb|val monoid_nat =|\newline%
|
|
367 |
\verb| {Program__semigroup_monoid = semigroup_nat, neutral = neutral_nat} :|\newline%
|
|
368 |
\verb| nat monoid;|\newline%
|
|
369 |
\newline%
|
|
370 |
\verb|fun bexp n = pow monoid_nat n (Suc (Suc Zero_nat));|\newline%
|
|
371 |
\newline%
|
|
372 |
\verb|end; (*struct Example*)|%
|
|
373 |
\end{isamarkuptext}%
|
|
374 |
\isamarkuptrue%
|
|
375 |
%
|
|
376 |
\endisatagquoteme
|
|
377 |
{\isafoldquoteme}%
|
|
378 |
%
|
|
379 |
\isadelimquoteme
|
|
380 |
%
|
|
381 |
\endisadelimquoteme
|
|
382 |
%
|
|
383 |
\begin{isamarkuptext}%
|
|
384 |
\noindent Note the parameters with trailing underscore (\verb|A_|)
|
|
385 |
which are the dictionary parameters.%
|
|
386 |
\end{isamarkuptext}%
|
|
387 |
\isamarkuptrue%
|
|
388 |
%
|
|
389 |
\isamarkupsubsection{The preprocessor \label{sec:preproc}%
|
|
390 |
}
|
|
391 |
\isamarkuptrue%
|
|
392 |
%
|
|
393 |
\begin{isamarkuptext}%
|
|
394 |
Before selected function theorems are turned into abstract
|
|
395 |
code, a chain of definitional transformation steps is carried
|
|
396 |
out: \emph{preprocessing}. In essence, the preprocessor
|
|
397 |
consists of two components: a \emph{simpset} and \emph{function transformers}.
|
|
398 |
|
|
399 |
The \emph{simpset} allows to employ the full generality of the Isabelle
|
|
400 |
simplifier. Due to the interpretation of theorems
|
|
401 |
as defining equations, rewrites are applied to the right
|
|
402 |
hand side and the arguments of the left hand side of an
|
|
403 |
equation, but never to the constant heading the left hand side.
|
|
404 |
An important special case are \emph{inline theorems} which may be
|
|
405 |
declared and undeclared using the
|
|
406 |
\emph{code inline} or \emph{code inline del} attribute respectively.
|
|
407 |
|
|
408 |
Some common applications:%
|
|
409 |
\end{isamarkuptext}%
|
|
410 |
\isamarkuptrue%
|
|
411 |
%
|
|
412 |
\begin{itemize}
|
|
413 |
%
|
|
414 |
\begin{isamarkuptext}%
|
|
415 |
\item replacing non-executable constructs by executable ones:%
|
|
416 |
\end{isamarkuptext}%
|
|
417 |
\isamarkuptrue%
|
|
418 |
%
|
|
419 |
\isadelimquoteme
|
|
420 |
%
|
|
421 |
\endisadelimquoteme
|
|
422 |
%
|
|
423 |
\isatagquoteme
|
|
424 |
\isacommand{lemma}\isamarkupfalse%
|
|
425 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
|
|
426 |
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
427 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
|
|
428 |
\endisatagquoteme
|
|
429 |
{\isafoldquoteme}%
|
|
430 |
%
|
|
431 |
\isadelimquoteme
|
|
432 |
%
|
|
433 |
\endisadelimquoteme
|
|
434 |
%
|
|
435 |
\begin{isamarkuptext}%
|
|
436 |
\item eliminating superfluous constants:%
|
|
437 |
\end{isamarkuptext}%
|
|
438 |
\isamarkuptrue%
|
|
439 |
%
|
|
440 |
\isadelimquoteme
|
|
441 |
%
|
|
442 |
\endisadelimquoteme
|
|
443 |
%
|
|
444 |
\isatagquoteme
|
|
445 |
\isacommand{lemma}\isamarkupfalse%
|
|
446 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
|
|
447 |
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
448 |
\ simp%
|
|
449 |
\endisatagquoteme
|
|
450 |
{\isafoldquoteme}%
|
|
451 |
%
|
|
452 |
\isadelimquoteme
|
|
453 |
%
|
|
454 |
\endisadelimquoteme
|
|
455 |
%
|
|
456 |
\begin{isamarkuptext}%
|
|
457 |
\item replacing executable but inconvenient constructs:%
|
|
458 |
\end{isamarkuptext}%
|
|
459 |
\isamarkuptrue%
|
|
460 |
%
|
|
461 |
\isadelimquoteme
|
|
462 |
%
|
|
463 |
\endisadelimquoteme
|
|
464 |
%
|
|
465 |
\isatagquoteme
|
|
466 |
\isacommand{lemma}\isamarkupfalse%
|
|
467 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
|
|
468 |
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
469 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
|
|
470 |
\endisatagquoteme
|
|
471 |
{\isafoldquoteme}%
|
|
472 |
%
|
|
473 |
\isadelimquoteme
|
|
474 |
%
|
|
475 |
\endisadelimquoteme
|
|
476 |
%
|
|
477 |
\end{itemize}
|
|
478 |
%
|
|
479 |
\begin{isamarkuptext}%
|
|
480 |
\noindent \emph{Function transformers} provide a very general interface,
|
|
481 |
transforming a list of function theorems to another
|
|
482 |
list of function theorems, provided that neither the heading
|
|
483 |
constant nor its type change. The \isa{{\isadigit{0}}} / \isa{Suc}
|
|
484 |
pattern elimination implemented in
|
|
485 |
theory \isa{Efficient{\isacharunderscore}Nat} (see \secref{eff_nat}) uses this
|
|
486 |
interface.
|
|
487 |
|
|
488 |
\noindent The current setup of the preprocessor may be inspected using
|
|
489 |
the \hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command.
|
|
490 |
\hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} provides a convenient
|
|
491 |
mechanism to inspect the impact of a preprocessor setup
|
|
492 |
on defining equations.
|
|
493 |
|
|
494 |
\begin{warn}
|
|
495 |
The attribute \emph{code unfold}
|
|
496 |
associated with the \isa{SML\ code\ generator} also applies to
|
|
497 |
the \isa{generic\ code\ generator}:
|
|
498 |
\emph{code unfold} implies \emph{code inline}.
|
|
499 |
\end{warn}%
|
|
500 |
\end{isamarkuptext}%
|
|
501 |
\isamarkuptrue%
|
|
502 |
%
|
|
503 |
\isamarkupsubsection{Datatypes \label{sec:datatypes}%
|
|
504 |
}
|
|
505 |
\isamarkuptrue%
|
|
506 |
%
|
|
507 |
\begin{isamarkuptext}%
|
|
508 |
Conceptually, any datatype is spanned by a set of
|
|
509 |
\emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n}
|
|
510 |
where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is excactly the set of \emph{all}
|
|
511 |
type variables in \isa{{\isasymtau}}. The HOL datatype package
|
|
512 |
by default registers any new datatype in the table
|
|
513 |
of datatypes, which may be inspected using
|
|
514 |
the \hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command.
|
|
515 |
|
|
516 |
In some cases, it may be convenient to alter or
|
|
517 |
extend this table; as an example, we will develop an alternative
|
|
518 |
representation of natural numbers as binary digits, whose
|
|
519 |
size does increase logarithmically with its value, not linear
|
|
520 |
\footnote{Indeed, the \hyperlink{theory.Efficient-Nat}{\mbox{\isa{Efficient{\isacharunderscore}Nat}}} theory (see \ref{eff_nat})
|
|
521 |
does something similar}. First, the digit representation:%
|
|
522 |
\end{isamarkuptext}%
|
|
523 |
\isamarkuptrue%
|
|
524 |
%
|
|
525 |
\isadelimquoteme
|
|
526 |
%
|
|
527 |
\endisadelimquoteme
|
|
528 |
%
|
|
529 |
\isatagquoteme
|
|
530 |
\isacommand{definition}\isamarkupfalse%
|
|
531 |
\ Dig{\isadigit{0}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
532 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ n\ {\isacharequal}\ {\isadigit{2}}\ {\isacharasterisk}\ n{\isachardoublequoteclose}\isanewline
|
|
533 |
\isanewline
|
|
534 |
\isacommand{definition}\isamarkupfalse%
|
|
535 |
\ \ Dig{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
536 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ n\ {\isacharequal}\ Suc\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n{\isacharparenright}{\isachardoublequoteclose}%
|
|
537 |
\endisatagquoteme
|
|
538 |
{\isafoldquoteme}%
|
|
539 |
%
|
|
540 |
\isadelimquoteme
|
|
541 |
%
|
|
542 |
\endisadelimquoteme
|
|
543 |
%
|
|
544 |
\begin{isamarkuptext}%
|
|
545 |
\noindent We will use these two \qt{digits} to represent natural numbers
|
|
546 |
in binary digits, e.g.:%
|
|
547 |
\end{isamarkuptext}%
|
|
548 |
\isamarkuptrue%
|
|
549 |
%
|
|
550 |
\isadelimquoteme
|
|
551 |
%
|
|
552 |
\endisadelimquoteme
|
|
553 |
%
|
|
554 |
\isatagquoteme
|
|
555 |
\isacommand{lemma}\isamarkupfalse%
|
|
556 |
\ {\isadigit{4}}{\isadigit{2}}{\isacharcolon}\ {\isachardoublequoteopen}{\isadigit{4}}{\isadigit{2}}\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}Dig{\isadigit{1}}\ {\isacharparenleft}Dig{\isadigit{0}}\ {\isacharparenleft}Dig{\isadigit{1}}\ {\isacharparenleft}Dig{\isadigit{0}}\ {\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
557 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
558 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
|
|
559 |
\endisatagquoteme
|
|
560 |
{\isafoldquoteme}%
|
|
561 |
%
|
|
562 |
\isadelimquoteme
|
|
563 |
%
|
|
564 |
\endisadelimquoteme
|
|
565 |
%
|
|
566 |
\begin{isamarkuptext}%
|
|
567 |
\noindent Of course we also have to provide proper code equations for
|
|
568 |
the operations, e.g. \isa{op\ {\isacharplus}}:%
|
|
569 |
\end{isamarkuptext}%
|
|
570 |
\isamarkuptrue%
|
|
571 |
%
|
|
572 |
\isadelimquoteme
|
|
573 |
%
|
|
574 |
\endisadelimquoteme
|
|
575 |
%
|
|
576 |
\isatagquoteme
|
|
577 |
\isacommand{lemma}\isamarkupfalse%
|
|
578 |
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
579 |
\ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
|
|
580 |
\ \ {\isachardoublequoteopen}m\ {\isacharplus}\ {\isadigit{0}}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
|
|
581 |
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ n{\isachardoublequoteclose}\isanewline
|
|
582 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{1}}\ m{\isachardoublequoteclose}\isanewline
|
|
583 |
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
584 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
585 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
586 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
587 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
588 |
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
589 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
590 |
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
|
|
591 |
\endisatagquoteme
|
|
592 |
{\isafoldquoteme}%
|
|
593 |
%
|
|
594 |
\isadelimquoteme
|
|
595 |
%
|
|
596 |
\endisadelimquoteme
|
|
597 |
%
|
|
598 |
\begin{isamarkuptext}%
|
|
599 |
\noindent We then instruct the code generator to view \isa{{\isadigit{0}}},
|
|
600 |
\isa{{\isadigit{1}}}, \isa{Dig{\isadigit{0}}} and \isa{Dig{\isadigit{1}}} as
|
|
601 |
datatype constructors:%
|
|
602 |
\end{isamarkuptext}%
|
|
603 |
\isamarkuptrue%
|
|
604 |
%
|
|
605 |
\isadelimquoteme
|
|
606 |
%
|
|
607 |
\endisadelimquoteme
|
|
608 |
%
|
|
609 |
\isatagquoteme
|
|
610 |
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
|
|
611 |
\ {\isachardoublequoteopen}{\isadigit{0}}{\isasymColon}nat{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isadigit{1}}{\isasymColon}nat{\isachardoublequoteclose}\ Dig{\isadigit{0}}\ Dig{\isadigit{1}}%
|
|
612 |
\endisatagquoteme
|
|
613 |
{\isafoldquoteme}%
|
|
614 |
%
|
|
615 |
\isadelimquoteme
|
|
616 |
%
|
|
617 |
\endisadelimquoteme
|
|
618 |
%
|
|
619 |
\begin{isamarkuptext}%
|
|
620 |
\noindent For the former constructor \isa{Suc}, we provide a code
|
|
621 |
equation and remove some parts of the default code generator setup
|
|
622 |
which are an obstacle here:%
|
|
623 |
\end{isamarkuptext}%
|
|
624 |
\isamarkuptrue%
|
|
625 |
%
|
|
626 |
\isadelimquoteme
|
|
627 |
%
|
|
628 |
\endisadelimquoteme
|
|
629 |
%
|
|
630 |
\isatagquoteme
|
|
631 |
\isacommand{lemma}\isamarkupfalse%
|
|
632 |
\ Suc{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
633 |
\ \ {\isachardoublequoteopen}Suc\ n\ {\isacharequal}\ n\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
|
|
634 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
635 |
\ simp\isanewline
|
|
636 |
\isanewline
|
|
637 |
\isacommand{declare}\isamarkupfalse%
|
|
638 |
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline\ del{\isacharbrackright}\isanewline
|
|
639 |
\isacommand{declare}\isamarkupfalse%
|
|
640 |
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}%
|
|
641 |
\endisatagquoteme
|
|
642 |
{\isafoldquoteme}%
|
|
643 |
%
|
|
644 |
\isadelimquoteme
|
|
645 |
%
|
|
646 |
\endisadelimquoteme
|
|
647 |
%
|
|
648 |
\begin{isamarkuptext}%
|
|
649 |
\noindent This yields the following code:%
|
|
650 |
\end{isamarkuptext}%
|
|
651 |
\isamarkuptrue%
|
|
652 |
%
|
|
653 |
\isadelimquoteme
|
|
654 |
%
|
|
655 |
\endisadelimquoteme
|
|
656 |
%
|
|
657 |
\isatagquoteme
|
|
658 |
%
|
|
659 |
\begin{isamarkuptext}%
|
|
660 |
\isaverbatim%
|
|
661 |
\noindent%
|
|
662 |
\verb|structure Example = |\newline%
|
|
663 |
\verb|struct|\newline%
|
|
664 |
\newline%
|
|
665 |
\verb|datatype nat = Dig1 of nat |\verb,|,\verb| Dig0 of nat |\verb,|,\verb| One_nat |\verb,|,\verb| Zero_nat;|\newline%
|
|
666 |
\newline%
|
|
667 |
\verb|fun plus_nat (Dig1 m) (Dig1 n) = Dig0 (plus_nat (plus_nat m n) One_nat)|\newline%
|
|
668 |
\verb| |\verb,|,\verb| plus_nat (Dig1 m) (Dig0 n) = Dig1 (plus_nat m n)|\newline%
|
|
669 |
\verb| |\verb,|,\verb| plus_nat (Dig0 m) (Dig1 n) = Dig1 (plus_nat m n)|\newline%
|
|
670 |
\verb| |\verb,|,\verb| plus_nat (Dig0 m) (Dig0 n) = Dig0 (plus_nat m n)|\newline%
|
|
671 |
\verb| |\verb,|,\verb| plus_nat (Dig1 m) One_nat = Dig0 (plus_nat m One_nat)|\newline%
|
|
672 |
\verb| |\verb,|,\verb| plus_nat One_nat (Dig1 n) = Dig0 (plus_nat n One_nat)|\newline%
|
|
673 |
\verb| |\verb,|,\verb| plus_nat (Dig0 m) One_nat = Dig1 m|\newline%
|
|
674 |
\verb| |\verb,|,\verb| plus_nat One_nat (Dig0 n) = Dig1 n|\newline%
|
|
675 |
\verb| |\verb,|,\verb| plus_nat m Zero_nat = m|\newline%
|
|
676 |
\verb| |\verb,|,\verb| plus_nat Zero_nat n = n;|\newline%
|
|
677 |
\newline%
|
|
678 |
\verb|end; (*struct Example*)|%
|
|
679 |
\end{isamarkuptext}%
|
|
680 |
\isamarkuptrue%
|
|
681 |
%
|
|
682 |
\endisatagquoteme
|
|
683 |
{\isafoldquoteme}%
|
|
684 |
%
|
|
685 |
\isadelimquoteme
|
|
686 |
%
|
|
687 |
\endisadelimquoteme
|
|
688 |
%
|
|
689 |
\begin{isamarkuptext}%
|
|
690 |
\noindent From this example, it can be easily glimpsed that using own constructor sets
|
|
691 |
is a little delicate since it changes the set of valid patterns for values
|
|
692 |
of that type. Without going into much detail, here some practical hints:
|
|
693 |
|
|
694 |
\begin{itemize}
|
|
695 |
\item When changing the constructor set for datatypes, take care to
|
|
696 |
provide an alternative for the \isa{case} combinator (e.g.~by replacing
|
|
697 |
it using the preprocessor).
|
|
698 |
\item Values in the target language need not to be normalised -- different
|
|
699 |
values in the target language may represent the same value in the
|
|
700 |
logic (e.g. \isa{Dig{\isadigit{1}}\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}}).
|
|
701 |
\item Usually, a good methodology to deal with the subtleties of pattern
|
|
702 |
matching is to see the type as an abstract type: provide a set
|
|
703 |
of operations which operate on the concrete representation of the type,
|
|
704 |
and derive further operations by combinations of these primitive ones,
|
|
705 |
without relying on a particular representation.
|
|
706 |
\end{itemize}%
|
|
707 |
\end{isamarkuptext}%
|
|
708 |
\isamarkuptrue%
|
|
709 |
%
|
|
710 |
\isadeliminvisible
|
|
711 |
%
|
|
712 |
\endisadeliminvisible
|
|
713 |
%
|
|
714 |
\isataginvisible
|
|
715 |
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
|
|
716 |
\ {\isachardoublequoteopen}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isachardoublequoteclose}\ Suc\isanewline
|
|
717 |
\isacommand{declare}\isamarkupfalse%
|
|
718 |
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\isanewline
|
|
719 |
\isacommand{declare}\isamarkupfalse%
|
|
720 |
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline{\isacharbrackright}\isanewline
|
|
721 |
\isacommand{declare}\isamarkupfalse%
|
|
722 |
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func{\isacharbrackright}\ \isanewline
|
|
723 |
\isacommand{lemma}\isamarkupfalse%
|
|
724 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ {\isacharparenleft}n\ {\isasymColon}\ nat{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
725 |
\ simp%
|
|
726 |
\endisataginvisible
|
|
727 |
{\isafoldinvisible}%
|
|
728 |
%
|
|
729 |
\isadeliminvisible
|
|
730 |
%
|
|
731 |
\endisadeliminvisible
|
|
732 |
%
|
|
733 |
\isamarkupsubsection{Equality and wellsortedness%
|
|
734 |
}
|
|
735 |
\isamarkuptrue%
|
|
736 |
%
|
|
737 |
\begin{isamarkuptext}%
|
|
738 |
Surely you have already noticed how equality is treated
|
|
739 |
by the code generator:%
|
|
740 |
\end{isamarkuptext}%
|
|
741 |
\isamarkuptrue%
|
|
742 |
%
|
|
743 |
\isadelimquoteme
|
|
744 |
%
|
|
745 |
\endisadelimquoteme
|
|
746 |
%
|
|
747 |
\isatagquoteme
|
|
748 |
\isacommand{primrec}\isamarkupfalse%
|
|
749 |
\ collect{\isacharunderscore}duplicates\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
750 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
|
|
751 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
|
|
752 |
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
|
|
753 |
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
|
|
754 |
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
|
|
755 |
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}%
|
|
756 |
\endisatagquoteme
|
|
757 |
{\isafoldquoteme}%
|
|
758 |
%
|
|
759 |
\isadelimquoteme
|
|
760 |
%
|
|
761 |
\endisadelimquoteme
|
|
762 |
%
|
|
763 |
\begin{isamarkuptext}%
|
|
764 |
\noindent The membership test during preprocessing is rewritten,
|
|
765 |
resulting in \isa{op\ mem}, which itself
|
|
766 |
performs an explicit equality check.%
|
|
767 |
\end{isamarkuptext}%
|
|
768 |
\isamarkuptrue%
|
|
769 |
%
|
|
770 |
\isadelimquoteme
|
|
771 |
%
|
|
772 |
\endisadelimquoteme
|
|
773 |
%
|
|
774 |
\isatagquoteme
|
|
775 |
%
|
|
776 |
\begin{isamarkuptext}%
|
|
777 |
\isaverbatim%
|
|
778 |
\noindent%
|
|
779 |
\verb|structure Example = |\newline%
|
|
780 |
\verb|struct|\newline%
|
|
781 |
\newline%
|
|
782 |
\verb|type 'a eq = {eq : 'a -> 'a -> bool};|\newline%
|
|
783 |
\verb|fun eq (A_:'a eq) = #eq A_;|\newline%
|
|
784 |
\newline%
|
|
785 |
\verb|fun eqop A_ a b = eq A_ a b;|\newline%
|
|
786 |
\newline%
|
|
787 |
\verb|fun member A_ x (y :: ys) = (if eqop A_ y x then true else member A_ x ys)|\newline%
|
|
788 |
\verb| |\verb,|,\verb| member A_ x [] = false;|\newline%
|
|
789 |
\newline%
|
|
790 |
\verb|fun collect_duplicates A_ xs ys (z :: zs) =|\newline%
|
|
791 |
\verb| (if member A_ z xs|\newline%
|
|
792 |
\verb| then (if member A_ z ys then collect_duplicates A_ xs ys zs|\newline%
|
|
793 |
\verb| else collect_duplicates A_ xs (z :: ys) zs)|\newline%
|
|
794 |
\verb| else collect_duplicates A_ (z :: xs) (z :: ys) zs)|\newline%
|
|
795 |
\verb| |\verb,|,\verb| collect_duplicates A_ xs ys [] = xs;|\newline%
|
|
796 |
\newline%
|
|
797 |
\verb|end; (*struct Example*)|%
|
|
798 |
\end{isamarkuptext}%
|
|
799 |
\isamarkuptrue%
|
|
800 |
%
|
|
801 |
\endisatagquoteme
|
|
802 |
{\isafoldquoteme}%
|
|
803 |
%
|
|
804 |
\isadelimquoteme
|
|
805 |
%
|
|
806 |
\endisadelimquoteme
|
|
807 |
%
|
|
808 |
\begin{isamarkuptext}%
|
|
809 |
\noindent Obviously, polymorphic equality is implemented the Haskell
|
|
810 |
way using a type class. How is this achieved? HOL introduces
|
|
811 |
an explicit class \isa{eq} with a corresponding operation
|
|
812 |
\isa{eq{\isacharunderscore}class{\isachardot}eq} such that \isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharequal}\ op\ {\isacharequal}}.
|
|
813 |
The preprocessing framework does the rest by propagating the
|
|
814 |
\isa{eq} constraints through all dependent defining equations.
|
|
815 |
For datatypes, instances of \isa{eq} are implicitly derived
|
|
816 |
when possible. For other types, you may instantiate \isa{eq}
|
|
817 |
manually like any other type class.
|
|
818 |
|
|
819 |
Though this \isa{eq} class is designed to get rarely in
|
|
820 |
the way, a subtlety
|
|
821 |
enters the stage when definitions of overloaded constants
|
|
822 |
are dependent on operational equality. For example, let
|
|
823 |
us define a lexicographic ordering on tuples
|
|
824 |
(also see theory \hyperlink{theory.Product-ord}{\mbox{\isa{Product{\isacharunderscore}ord}}}):%
|
|
825 |
\end{isamarkuptext}%
|
|
826 |
\isamarkuptrue%
|
|
827 |
%
|
|
828 |
\isadelimquoteme
|
|
829 |
%
|
|
830 |
\endisadelimquoteme
|
|
831 |
%
|
|
832 |
\isatagquoteme
|
|
833 |
\isacommand{instantiation}\isamarkupfalse%
|
|
834 |
\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}order{\isacharcomma}\ order{\isacharparenright}\ order\isanewline
|
|
835 |
\isakeyword{begin}\isanewline
|
|
836 |
\isanewline
|
|
837 |
\isacommand{definition}\isamarkupfalse%
|
|
838 |
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}{\isacharcolon}\isanewline
|
|
839 |
\ \ {\isachardoublequoteopen}x\ {\isasymle}\ y\ {\isasymlongleftrightarrow}\ fst\ x\ {\isacharless}\ fst\ y\ {\isasymor}\ fst\ x\ {\isacharequal}\ fst\ y\ {\isasymand}\ snd\ x\ {\isasymle}\ snd\ y{\isachardoublequoteclose}\isanewline
|
|
840 |
\isanewline
|
|
841 |
\isacommand{definition}\isamarkupfalse%
|
|
842 |
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}{\isacharcolon}\isanewline
|
|
843 |
\ \ {\isachardoublequoteopen}x\ {\isacharless}\ y\ {\isasymlongleftrightarrow}\ fst\ x\ {\isacharless}\ fst\ y\ {\isasymor}\ fst\ x\ {\isacharequal}\ fst\ y\ {\isasymand}\ snd\ x\ {\isacharless}\ snd\ y{\isachardoublequoteclose}\isanewline
|
|
844 |
\isanewline
|
|
845 |
\isacommand{instance}\isamarkupfalse%
|
|
846 |
\ \isacommand{proof}\isamarkupfalse%
|
|
847 |
\isanewline
|
|
848 |
\isacommand{qed}\isamarkupfalse%
|
|
849 |
\ {\isacharparenleft}auto\ simp{\isacharcolon}\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}prod{\isacharunderscore}def\ intro{\isacharcolon}\ order{\isacharunderscore}less{\isacharunderscore}trans{\isacharparenright}\isanewline
|
|
850 |
\isanewline
|
|
851 |
\isacommand{end}\isamarkupfalse%
|
|
852 |
\isanewline
|
|
853 |
\isanewline
|
|
854 |
\isacommand{lemma}\isamarkupfalse%
|
|
855 |
\ order{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
856 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}order{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
|
|
857 |
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
|
|
858 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}order{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
|
|
859 |
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
|
|
860 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
861 |
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def{\isacharparenright}%
|
|
862 |
\endisatagquoteme
|
|
863 |
{\isafoldquoteme}%
|
|
864 |
%
|
|
865 |
\isadelimquoteme
|
|
866 |
%
|
|
867 |
\endisadelimquoteme
|
|
868 |
%
|
|
869 |
\begin{isamarkuptext}%
|
|
870 |
\noindent Then code generation will fail. Why? The definition
|
|
871 |
of \isa{op\ {\isasymle}} depends on equality on both arguments,
|
|
872 |
which are polymorphic and impose an additional \isa{eq}
|
|
873 |
class constraint, which the preprocessor does not propagate
|
|
874 |
(for technical reasons).
|
|
875 |
|
|
876 |
The solution is to add \isa{eq} explicitly to the first sort arguments in the
|
|
877 |
code theorems:%
|
|
878 |
\end{isamarkuptext}%
|
|
879 |
\isamarkuptrue%
|
|
880 |
%
|
|
881 |
\isadelimquoteme
|
|
882 |
%
|
|
883 |
\endisadelimquoteme
|
|
884 |
%
|
|
885 |
\isatagquoteme
|
|
886 |
\isacommand{lemma}\isamarkupfalse%
|
|
887 |
\ order{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
888 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}order{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
|
|
889 |
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
|
|
890 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}order{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
|
|
891 |
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
|
|
892 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
893 |
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def{\isacharparenright}%
|
|
894 |
\endisatagquoteme
|
|
895 |
{\isafoldquoteme}%
|
|
896 |
%
|
|
897 |
\isadelimquoteme
|
|
898 |
%
|
|
899 |
\endisadelimquoteme
|
|
900 |
%
|
|
901 |
\begin{isamarkuptext}%
|
|
902 |
\noindent Then code generation succeeds:%
|
|
903 |
\end{isamarkuptext}%
|
|
904 |
\isamarkuptrue%
|
|
905 |
%
|
|
906 |
\isadelimquoteme
|
|
907 |
%
|
|
908 |
\endisadelimquoteme
|
|
909 |
%
|
|
910 |
\isatagquoteme
|
|
911 |
%
|
|
912 |
\begin{isamarkuptext}%
|
|
913 |
\isaverbatim%
|
|
914 |
\noindent%
|
|
915 |
\verb|structure Example = |\newline%
|
|
916 |
\verb|struct|\newline%
|
|
917 |
\newline%
|
|
918 |
\verb|type 'a eq = {eq : 'a -> 'a -> bool};|\newline%
|
|
919 |
\verb|fun eq (A_:'a eq) = #eq A_;|\newline%
|
|
920 |
\newline%
|
|
921 |
\verb|type 'a ord = {less_eq : 'a -> 'a -> bool, less : 'a -> 'a -> bool};|\newline%
|
|
922 |
\verb|fun less_eq (A_:'a ord) = #less_eq A_;|\newline%
|
|
923 |
\verb|fun less (A_:'a ord) = #less A_;|\newline%
|
|
924 |
\newline%
|
|
925 |
\verb|fun eqop A_ a b = eq A_ a b;|\newline%
|
|
926 |
\newline%
|
|
927 |
\verb|type 'a preorder = {Orderings__ord_preorder : 'a ord};|\newline%
|
|
928 |
\verb|fun ord_preorder (A_:'a preorder) = #Orderings__ord_preorder A_;|\newline%
|
|
929 |
\newline%
|
|
930 |
\verb|type 'a order = {Orderings__preorder_order : 'a preorder};|\newline%
|
|
931 |
\verb|fun preorder_order (A_:'a order) = #Orderings__preorder_order A_;|\newline%
|
|
932 |
\newline%
|
|
933 |
\verb|fun less_eqa (A1_, A2_) B_ (x1, y1) (x2, y2) =|\newline%
|
|
934 |
\verb| less ((ord_preorder o preorder_order) A2_) x1 x2 orelse|\newline%
|
|
935 |
\verb| eqop A1_ x1 x2 andalso|\newline%
|
|
936 |
\verb| less_eq ((ord_preorder o preorder_order) B_) y1 y2|\newline%
|
|
937 |
\verb| |\verb,|,\verb| less_eqa (A1_, A2_) B_ (x1, y1) (x2, y2) =|\newline%
|
|
938 |
\verb| less ((ord_preorder o preorder_order) A2_) x1 x2 orelse|\newline%
|
|
939 |
\verb| eqop A1_ x1 x2 andalso|\newline%
|
|
940 |
\verb| less_eq ((ord_preorder o preorder_order) B_) y1 y2;|\newline%
|
|
941 |
\newline%
|
|
942 |
\verb|end; (*struct Example*)|%
|
|
943 |
\end{isamarkuptext}%
|
|
944 |
\isamarkuptrue%
|
|
945 |
%
|
|
946 |
\endisatagquoteme
|
|
947 |
{\isafoldquoteme}%
|
|
948 |
%
|
|
949 |
\isadelimquoteme
|
|
950 |
%
|
|
951 |
\endisadelimquoteme
|
|
952 |
%
|
|
953 |
\begin{isamarkuptext}%
|
|
954 |
In some cases, the automatically derived defining equations
|
|
955 |
for equality on a particular type may not be appropriate.
|
|
956 |
As example, watch the following datatype representing
|
|
957 |
monomorphic parametric types (where type constructors
|
|
958 |
are referred to by natural numbers):%
|
|
959 |
\end{isamarkuptext}%
|
|
960 |
\isamarkuptrue%
|
|
961 |
%
|
|
962 |
\isadelimquoteme
|
|
963 |
%
|
|
964 |
\endisadelimquoteme
|
|
965 |
%
|
|
966 |
\isatagquoteme
|
|
967 |
\isacommand{datatype}\isamarkupfalse%
|
|
968 |
\ monotype\ {\isacharequal}\ Mono\ nat\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}%
|
|
969 |
\endisatagquoteme
|
|
970 |
{\isafoldquoteme}%
|
|
971 |
%
|
|
972 |
\isadelimquoteme
|
|
973 |
%
|
|
974 |
\endisadelimquoteme
|
|
975 |
%
|
|
976 |
\isadelimproof
|
|
977 |
%
|
|
978 |
\endisadelimproof
|
|
979 |
%
|
|
980 |
\isatagproof
|
|
981 |
%
|
|
982 |
\endisatagproof
|
|
983 |
{\isafoldproof}%
|
|
984 |
%
|
|
985 |
\isadelimproof
|
|
986 |
%
|
|
987 |
\endisadelimproof
|
|
988 |
%
|
|
989 |
\begin{isamarkuptext}%
|
|
990 |
Then code generation for SML would fail with a message
|
|
991 |
that the generated code contains illegal mutual dependencies:
|
|
992 |
the theorem \isa{Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymequiv}\ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ typargs{\isadigit{1}}\ {\isacharequal}\ typargs{\isadigit{2}}} already requires the
|
|
993 |
instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires
|
|
994 |
\isa{Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymequiv}\ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ typargs{\isadigit{1}}\ {\isacharequal}\ typargs{\isadigit{2}}}; Haskell has no problem with mutually
|
|
995 |
recursive \isa{inst} and \isa{fun} definitions,
|
|
996 |
but the SML serializer does not support this.
|
|
997 |
|
|
998 |
In such cases, you have to provide your own equality equations
|
|
999 |
involving auxiliary constants. In our case,
|
|
1000 |
\isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:%
|
|
1001 |
\end{isamarkuptext}%
|
|
1002 |
\isamarkuptrue%
|
|
1003 |
%
|
|
1004 |
\isadelimquoteme
|
|
1005 |
%
|
|
1006 |
\endisadelimquoteme
|
|
1007 |
%
|
|
1008 |
\isatagquoteme
|
|
1009 |
\isacommand{lemma}\isamarkupfalse%
|
|
1010 |
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
1011 |
\ \ {\isachardoublequoteopen}eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
|
|
1012 |
\ \ \ \ \ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ list{\isacharunderscore}all{\isadigit{2}}\ eq{\isacharunderscore}class{\isachardot}eq\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}{\isachardoublequoteclose}\isanewline
|
|
1013 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
1014 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ eq\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}%
|
|
1015 |
\endisatagquoteme
|
|
1016 |
{\isafoldquoteme}%
|
|
1017 |
%
|
|
1018 |
\isadelimquoteme
|
|
1019 |
%
|
|
1020 |
\endisadelimquoteme
|
|
1021 |
%
|
|
1022 |
\begin{isamarkuptext}%
|
|
1023 |
\noindent does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:%
|
|
1024 |
\end{isamarkuptext}%
|
|
1025 |
\isamarkuptrue%
|
|
1026 |
%
|
|
1027 |
\isadelimquoteme
|
|
1028 |
%
|
|
1029 |
\endisadelimquoteme
|
|
1030 |
%
|
|
1031 |
\isatagquoteme
|
|
1032 |
%
|
|
1033 |
\begin{isamarkuptext}%
|
|
1034 |
\isaverbatim%
|
|
1035 |
\noindent%
|
|
1036 |
\verb|structure Example = |\newline%
|
|
1037 |
\verb|struct|\newline%
|
|
1038 |
\newline%
|
|
1039 |
\verb|type 'a eq = {eq : 'a -> 'a -> bool};|\newline%
|
|
1040 |
\verb|fun eq (A_:'a eq) = #eq A_;|\newline%
|
|
1041 |
\newline%
|
|
1042 |
\verb|datatype nat = Suc of nat |\verb,|,\verb| Zero_nat;|\newline%
|
|
1043 |
\newline%
|
|
1044 |
\verb|fun eqop A_ a b = eq A_ a b;|\newline%
|
|
1045 |
\newline%
|
|
1046 |
\verb|fun null (x :: xs) = false|\newline%
|
|
1047 |
\verb| |\verb,|,\verb| null [] = true;|\newline%
|
|
1048 |
\newline%
|
|
1049 |
\verb|fun eq_nat (Suc a) Zero_nat = false|\newline%
|
|
1050 |
\verb| |\verb,|,\verb| eq_nat Zero_nat (Suc a) = false|\newline%
|
|
1051 |
\verb| |\verb,|,\verb| eq_nat (Suc nat) (Suc nat') = eq_nat nat nat'|\newline%
|
|
1052 |
\verb| |\verb,|,\verb| eq_nat Zero_nat Zero_nat = true;|\newline%
|
|
1053 |
\newline%
|
|
1054 |
\verb|val eq_nata = {eq = eq_nat} : nat eq;|\newline%
|
|
1055 |
\newline%
|
|
1056 |
\verb|datatype monotype = Mono of nat * monotype list;|\newline%
|
|
1057 |
\newline%
|
|
1058 |
\verb|fun list_all2 p (x :: xs) (y :: ys) = p x y andalso list_all2 p xs ys|\newline%
|
|
1059 |
\verb| |\verb,|,\verb| list_all2 p xs [] = null xs|\newline%
|
|
1060 |
\verb| |\verb,|,\verb| list_all2 p [] ys = null ys;|\newline%
|
|
1061 |
\newline%
|
|
1062 |
\verb|fun eq_monotype (Mono (tyco1, typargs1)) (Mono (tyco2, typargs2)) =|\newline%
|
|
1063 |
\verb| eqop eq_nata tyco1 tyco2 andalso|\newline%
|
|
1064 |
\verb| list_all2 eq_monotype typargs1 typargs2;|\newline%
|
|
1065 |
\newline%
|
|
1066 |
\verb|end; (*struct Example*)|%
|
|
1067 |
\end{isamarkuptext}%
|
|
1068 |
\isamarkuptrue%
|
|
1069 |
%
|
|
1070 |
\endisatagquoteme
|
|
1071 |
{\isafoldquoteme}%
|
|
1072 |
%
|
|
1073 |
\isadelimquoteme
|
|
1074 |
%
|
|
1075 |
\endisadelimquoteme
|
|
1076 |
%
|
|
1077 |
\isamarkupsubsection{Partiality%
|
|
1078 |
}
|
|
1079 |
\isamarkuptrue%
|
|
1080 |
%
|
|
1081 |
\begin{isamarkuptext}%
|
|
1082 |
\hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}, examples: maps%
|
|
1083 |
\end{isamarkuptext}%
|
|
1084 |
\isamarkuptrue%
|
|
1085 |
%
|
|
1086 |
\isadelimtheory
|
|
1087 |
%
|
|
1088 |
\endisadelimtheory
|
|
1089 |
%
|
|
1090 |
\isatagtheory
|
|
1091 |
\isacommand{end}\isamarkupfalse%
|
|
1092 |
%
|
|
1093 |
\endisatagtheory
|
|
1094 |
{\isafoldtheory}%
|
|
1095 |
%
|
|
1096 |
\isadelimtheory
|
|
1097 |
%
|
|
1098 |
\endisadelimtheory
|
|
1099 |
\isanewline
|
|
1100 |
\end{isabellebody}%
|
|
1101 |
%%% Local Variables:
|
|
1102 |
%%% mode: latex
|
|
1103 |
%%% TeX-master: "root"
|
|
1104 |
%%% End:
|