28447
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{Introduction}%
|
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
%
|
|
7 |
\endisadelimtheory
|
|
8 |
%
|
|
9 |
\isatagtheory
|
|
10 |
\isacommand{theory}\isamarkupfalse%
|
|
11 |
\ Introduction\isanewline
|
|
12 |
\isakeyword{imports}\ Setup\isanewline
|
|
13 |
\isakeyword{begin}%
|
|
14 |
\endisatagtheory
|
|
15 |
{\isafoldtheory}%
|
|
16 |
%
|
|
17 |
\isadelimtheory
|
|
18 |
%
|
|
19 |
\endisadelimtheory
|
|
20 |
%
|
38405
|
21 |
\isamarkupsection{Introduction%
|
|
22 |
}
|
|
23 |
\isamarkuptrue%
|
|
24 |
%
|
38437
|
25 |
\begin{isamarkuptext}%
|
|
26 |
This tutorial introduces the code generator facilities of \isa{Isabelle{\isacharslash}HOL}. It allows to turn (a certain class of) HOL
|
|
27 |
specifications into corresponding executable code in the programming
|
38813
|
28 |
languages \isa{SML} \cite{SML}, \isa{OCaml} \cite{OCaml},
|
|
29 |
\isa{Haskell} \cite{haskell-revised-report} and \isa{Scala}
|
|
30 |
\cite{scala-overview-tech-report}.
|
38437
|
31 |
|
|
32 |
To profit from this tutorial, some familiarity and experience with
|
|
33 |
\hyperlink{theory.HOL}{\mbox{\isa{HOL}}} \cite{isa-tutorial} and its basic theories is assumed.%
|
|
34 |
\end{isamarkuptext}%
|
38405
|
35 |
\isamarkuptrue%
|
|
36 |
%
|
38437
|
37 |
\isamarkupsubsection{Code generation principle: shallow embedding \label{sec:principle}%
|
28447
|
38 |
}
|
|
39 |
\isamarkuptrue%
|
|
40 |
%
|
|
41 |
\begin{isamarkuptext}%
|
38437
|
42 |
The key concept for understanding Isabelle's code generation is
|
|
43 |
\emph{shallow embedding}: logical entities like constants, types and
|
|
44 |
classes are identified with corresponding entities in the target
|
|
45 |
language. In particular, the carrier of a generated program's
|
|
46 |
semantics are \emph{equational theorems} from the logic. If we view
|
|
47 |
a generated program as an implementation of a higher-order rewrite
|
|
48 |
system, then every rewrite step performed by the program can be
|
|
49 |
simulated in the logic, which guarantees partial correctness
|
|
50 |
\cite{Haftmann-Nipkow:2010:code}.%
|
28447
|
51 |
\end{isamarkuptext}%
|
|
52 |
\isamarkuptrue%
|
|
53 |
%
|
38437
|
54 |
\isamarkupsubsection{A quick start with the Isabelle/HOL toolbox \label{sec:queue_example}%
|
28447
|
55 |
}
|
|
56 |
\isamarkuptrue%
|
|
57 |
%
|
|
58 |
\begin{isamarkuptext}%
|
38505
|
59 |
In a HOL theory, the \indexdef{}{command}{datatype}\hypertarget{command.datatype}{\hyperlink{command.datatype}{\mbox{\isa{\isacommand{datatype}}}}} and \indexdef{}{command}{definition}\hypertarget{command.definition}{\hyperlink{command.definition}{\mbox{\isa{\isacommand{definition}}}}}/\indexdef{}{command}{primrec}\hypertarget{command.primrec}{\hyperlink{command.primrec}{\mbox{\isa{\isacommand{primrec}}}}}/\indexdef{}{command}{fun}\hypertarget{command.fun}{\hyperlink{command.fun}{\mbox{\isa{\isacommand{fun}}}}} declarations
|
|
60 |
form the core of a functional programming language. By default
|
|
61 |
equational theorems stemming from those are used for generated code,
|
|
62 |
therefore \qt{naive} code generation can proceed without further
|
|
63 |
ado.
|
28447
|
64 |
|
|
65 |
For example, here a simple \qt{implementation} of amortised queues:%
|
|
66 |
\end{isamarkuptext}%
|
|
67 |
\isamarkuptrue%
|
|
68 |
%
|
28564
|
69 |
\isadelimquote
|
28447
|
70 |
%
|
28564
|
71 |
\endisadelimquote
|
28447
|
72 |
%
|
28564
|
73 |
\isatagquote
|
28447
|
74 |
\isacommand{datatype}\isamarkupfalse%
|
29798
|
75 |
\ {\isacharprime}a\ queue\ {\isacharequal}\ AQueue\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
|
28447
|
76 |
\isanewline
|
|
77 |
\isacommand{definition}\isamarkupfalse%
|
|
78 |
\ empty\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
29798
|
79 |
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
28447
|
80 |
\isanewline
|
|
81 |
\isacommand{primrec}\isamarkupfalse%
|
|
82 |
\ enqueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
29798
|
83 |
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys{\isachardoublequoteclose}\isanewline
|
28447
|
84 |
\isanewline
|
|
85 |
\isacommand{fun}\isamarkupfalse%
|
|
86 |
\ dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ option\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
29798
|
87 |
\ \ \ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
88 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
89 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
|
38437
|
90 |
\ \ \ \ \ \ {\isacharparenleft}case\ rev\ xs\ of\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\ %
|
28564
|
91 |
\endisatagquote
|
|
92 |
{\isafoldquote}%
|
28447
|
93 |
%
|
28564
|
94 |
\isadelimquote
|
28447
|
95 |
%
|
28564
|
96 |
\endisadelimquote
|
28447
|
97 |
%
|
38437
|
98 |
\isadeliminvisible
|
|
99 |
%
|
|
100 |
\endisadeliminvisible
|
|
101 |
%
|
|
102 |
\isataginvisible
|
|
103 |
%
|
|
104 |
\endisataginvisible
|
|
105 |
{\isafoldinvisible}%
|
|
106 |
%
|
|
107 |
\isadeliminvisible
|
|
108 |
%
|
|
109 |
\endisadeliminvisible
|
|
110 |
%
|
28447
|
111 |
\begin{isamarkuptext}%
|
|
112 |
\noindent Then we can generate code e.g.~for \isa{SML} as follows:%
|
|
113 |
\end{isamarkuptext}%
|
|
114 |
\isamarkuptrue%
|
|
115 |
%
|
28564
|
116 |
\isadelimquote
|
28447
|
117 |
%
|
28564
|
118 |
\endisadelimquote
|
28447
|
119 |
%
|
28564
|
120 |
\isatagquote
|
28447
|
121 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
|
|
122 |
\ empty\ dequeue\ enqueue\ \isakeyword{in}\ SML\isanewline
|
|
123 |
\ \ \isakeyword{module{\isacharunderscore}name}\ Example\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}example{\isachardot}ML{\isachardoublequoteclose}%
|
28564
|
124 |
\endisatagquote
|
|
125 |
{\isafoldquote}%
|
28447
|
126 |
%
|
28564
|
127 |
\isadelimquote
|
28447
|
128 |
%
|
28564
|
129 |
\endisadelimquote
|
28447
|
130 |
%
|
|
131 |
\begin{isamarkuptext}%
|
|
132 |
\noindent resulting in the following code:%
|
|
133 |
\end{isamarkuptext}%
|
|
134 |
\isamarkuptrue%
|
|
135 |
%
|
28564
|
136 |
\isadelimquote
|
28447
|
137 |
%
|
28564
|
138 |
\endisadelimquote
|
28447
|
139 |
%
|
28564
|
140 |
\isatagquote
|
28447
|
141 |
%
|
|
142 |
\begin{isamarkuptext}%
|
28727
|
143 |
\isatypewriter%
|
28447
|
144 |
\noindent%
|
34155
|
145 |
\hspace*{0pt}structure Example :~sig\\
|
38460
|
146 |
\hspace*{0pt} ~val id :~'a -> 'a\\
|
|
147 |
\hspace*{0pt} ~val fold :~('a -> 'b -> 'b) -> 'a list -> 'b -> 'b\\
|
34155
|
148 |
\hspace*{0pt} ~val rev :~'a list -> 'a list\\
|
34179
|
149 |
\hspace*{0pt} ~datatype 'a queue = AQueue of 'a list * 'a list\\
|
34155
|
150 |
\hspace*{0pt} ~val empty :~'a queue\\
|
|
151 |
\hspace*{0pt} ~val dequeue :~'a queue -> 'a option * 'a queue\\
|
|
152 |
\hspace*{0pt} ~val enqueue :~'a -> 'a queue -> 'a queue\\
|
|
153 |
\hspace*{0pt}end = struct\\
|
28714
|
154 |
\hspace*{0pt}\\
|
38460
|
155 |
\hspace*{0pt}fun id x = (fn xa => xa) x;\\
|
28714
|
156 |
\hspace*{0pt}\\
|
38460
|
157 |
\hspace*{0pt}fun fold f [] = id\\
|
|
158 |
\hspace*{0pt} ~| fold f (x ::~xs) = fold f xs o f x;\\
|
|
159 |
\hspace*{0pt}\\
|
|
160 |
\hspace*{0pt}fun rev xs = fold (fn a => fn b => a ::~b) xs [];\\
|
28714
|
161 |
\hspace*{0pt}\\
|
34179
|
162 |
\hspace*{0pt}datatype 'a queue = AQueue of 'a list * 'a list;\\
|
28714
|
163 |
\hspace*{0pt}\\
|
34179
|
164 |
\hspace*{0pt}val empty :~'a queue = AQueue ([],~[]);\\
|
28714
|
165 |
\hspace*{0pt}\\
|
34179
|
166 |
\hspace*{0pt}fun dequeue (AQueue ([],~[])) = (NONE,~AQueue ([],~[]))\\
|
|
167 |
\hspace*{0pt} ~| dequeue (AQueue (xs,~y ::~ys)) = (SOME y,~AQueue (xs,~ys))\\
|
|
168 |
\hspace*{0pt} ~| dequeue (AQueue (v ::~va,~[])) =\\
|
28714
|
169 |
\hspace*{0pt} ~~~let\\
|
34179
|
170 |
\hspace*{0pt} ~~~~~val y ::~ys = rev (v ::~va);\\
|
28714
|
171 |
\hspace*{0pt} ~~~in\\
|
34179
|
172 |
\hspace*{0pt} ~~~~~(SOME y,~AQueue ([],~ys))\\
|
28714
|
173 |
\hspace*{0pt} ~~~end;\\
|
|
174 |
\hspace*{0pt}\\
|
34179
|
175 |
\hspace*{0pt}fun enqueue x (AQueue (xs,~ys)) = AQueue (x ::~xs,~ys);\\
|
28714
|
176 |
\hspace*{0pt}\\
|
29297
|
177 |
\hspace*{0pt}end;~(*struct Example*)%
|
28447
|
178 |
\end{isamarkuptext}%
|
|
179 |
\isamarkuptrue%
|
|
180 |
%
|
28564
|
181 |
\endisatagquote
|
|
182 |
{\isafoldquote}%
|
28447
|
183 |
%
|
28564
|
184 |
\isadelimquote
|
28447
|
185 |
%
|
28564
|
186 |
\endisadelimquote
|
28447
|
187 |
%
|
|
188 |
\begin{isamarkuptext}%
|
38505
|
189 |
\noindent The \indexdef{}{command}{export\_code}\hypertarget{command.export-code}{\hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}}} command takes a
|
|
190 |
space-separated list of constants for which code shall be generated;
|
|
191 |
anything else needed for those is added implicitly. Then follows a
|
|
192 |
target language identifier and a freely chosen module name. A file
|
|
193 |
name denotes the destination to store the generated code. Note that
|
|
194 |
the semantics of the destination depends on the target language: for
|
38813
|
195 |
\isa{SML}, \isa{OCaml} and \isa{Scala} it denotes a \emph{file},
|
|
196 |
for \isa{Haskell} it denotes a \emph{directory} where a file named as the
|
38437
|
197 |
module name (with extension \isa{{\isachardot}hs}) is written:%
|
28447
|
198 |
\end{isamarkuptext}%
|
|
199 |
\isamarkuptrue%
|
|
200 |
%
|
28564
|
201 |
\isadelimquote
|
28447
|
202 |
%
|
28564
|
203 |
\endisadelimquote
|
28447
|
204 |
%
|
28564
|
205 |
\isatagquote
|
28447
|
206 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
|
|
207 |
\ empty\ dequeue\ enqueue\ \isakeyword{in}\ Haskell\isanewline
|
|
208 |
\ \ \isakeyword{module{\isacharunderscore}name}\ Example\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}%
|
28564
|
209 |
\endisatagquote
|
|
210 |
{\isafoldquote}%
|
28447
|
211 |
%
|
28564
|
212 |
\isadelimquote
|
28447
|
213 |
%
|
28564
|
214 |
\endisadelimquote
|
28447
|
215 |
%
|
|
216 |
\begin{isamarkuptext}%
|
38437
|
217 |
\noindent This is the corresponding code:%
|
28447
|
218 |
\end{isamarkuptext}%
|
|
219 |
\isamarkuptrue%
|
|
220 |
%
|
28564
|
221 |
\isadelimquote
|
28447
|
222 |
%
|
28564
|
223 |
\endisadelimquote
|
28447
|
224 |
%
|
28564
|
225 |
\isatagquote
|
28447
|
226 |
%
|
|
227 |
\begin{isamarkuptext}%
|
28727
|
228 |
\isatypewriter%
|
28447
|
229 |
\noindent%
|
28714
|
230 |
\hspace*{0pt}module Example where {\char123}\\
|
|
231 |
\hspace*{0pt}\\
|
29798
|
232 |
\hspace*{0pt}data Queue a = AQueue [a] [a];\\
|
28714
|
233 |
\hspace*{0pt}\\
|
29297
|
234 |
\hspace*{0pt}empty ::~forall a.~Queue a;\\
|
29798
|
235 |
\hspace*{0pt}empty = AQueue [] [];\\
|
28714
|
236 |
\hspace*{0pt}\\
|
29297
|
237 |
\hspace*{0pt}dequeue ::~forall a.~Queue a -> (Maybe a,~Queue a);\\
|
34179
|
238 |
\hspace*{0pt}dequeue (AQueue [] []) = (Nothing,~AQueue [] []);\\
|
|
239 |
\hspace*{0pt}dequeue (AQueue xs (y :~ys)) = (Just y,~AQueue xs ys);\\
|
|
240 |
\hspace*{0pt}dequeue (AQueue (v :~va) []) =\\
|
31848
|
241 |
\hspace*{0pt} ~let {\char123}\\
|
37610
|
242 |
\hspace*{0pt} ~~~(y :~ys) = reverse (v :~va);\\
|
31848
|
243 |
\hspace*{0pt} ~{\char125}~in (Just y,~AQueue [] ys);\\
|
28714
|
244 |
\hspace*{0pt}\\
|
29297
|
245 |
\hspace*{0pt}enqueue ::~forall a.~a -> Queue a -> Queue a;\\
|
34179
|
246 |
\hspace*{0pt}enqueue x (AQueue xs ys) = AQueue (x :~xs) ys;\\
|
28714
|
247 |
\hspace*{0pt}\\
|
|
248 |
\hspace*{0pt}{\char125}%
|
28447
|
249 |
\end{isamarkuptext}%
|
|
250 |
\isamarkuptrue%
|
|
251 |
%
|
28564
|
252 |
\endisatagquote
|
|
253 |
{\isafoldquote}%
|
28447
|
254 |
%
|
28564
|
255 |
\isadelimquote
|
28447
|
256 |
%
|
28564
|
257 |
\endisadelimquote
|
28447
|
258 |
%
|
|
259 |
\begin{isamarkuptext}%
|
38437
|
260 |
\noindent For more details about \hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}} see
|
|
261 |
\secref{sec:further}.%
|
28447
|
262 |
\end{isamarkuptext}%
|
|
263 |
\isamarkuptrue%
|
|
264 |
%
|
38437
|
265 |
\isamarkupsubsection{Type classes%
|
28447
|
266 |
}
|
|
267 |
\isamarkuptrue%
|
|
268 |
%
|
|
269 |
\begin{isamarkuptext}%
|
38437
|
270 |
Code can also be generated from type classes in a Haskell-like
|
|
271 |
manner. For illustration here an example from abstract algebra:%
|
|
272 |
\end{isamarkuptext}%
|
|
273 |
\isamarkuptrue%
|
|
274 |
%
|
|
275 |
\isadelimquote
|
|
276 |
%
|
|
277 |
\endisadelimquote
|
|
278 |
%
|
|
279 |
\isatagquote
|
|
280 |
\isacommand{class}\isamarkupfalse%
|
|
281 |
\ semigroup\ {\isacharequal}\isanewline
|
|
282 |
\ \ \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
|
|
283 |
\ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
284 |
\isanewline
|
|
285 |
\isacommand{class}\isamarkupfalse%
|
|
286 |
\ monoid\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline
|
|
287 |
\ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
288 |
\ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
|
|
289 |
\ \ \ \ \isakeyword{and}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
|
|
290 |
\isanewline
|
|
291 |
\isacommand{instantiation}\isamarkupfalse%
|
|
292 |
\ nat\ {\isacharcolon}{\isacharcolon}\ monoid\isanewline
|
|
293 |
\isakeyword{begin}\isanewline
|
|
294 |
\isanewline
|
|
295 |
\isacommand{primrec}\isamarkupfalse%
|
|
296 |
\ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline
|
|
297 |
\ \ \ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isasymotimes}\ n\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
298 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ n\ {\isacharplus}\ m\ {\isasymotimes}\ n{\isachardoublequoteclose}\isanewline
|
|
299 |
\isanewline
|
|
300 |
\isacommand{definition}\isamarkupfalse%
|
|
301 |
\ neutral{\isacharunderscore}nat\ \isakeyword{where}\isanewline
|
|
302 |
\ \ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
|
|
303 |
\isanewline
|
|
304 |
\isacommand{lemma}\isamarkupfalse%
|
|
305 |
\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharcolon}\isanewline
|
|
306 |
\ \ \isakeyword{fixes}\ n\ m\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
|
|
307 |
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}{\isacharparenleft}n\ {\isacharplus}\ m{\isacharparenright}\ {\isasymotimes}\ q\ {\isacharequal}\ n\ {\isasymotimes}\ q\ {\isacharplus}\ m\ {\isasymotimes}\ q{\isachardoublequoteclose}\isanewline
|
|
308 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
309 |
\ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
|
|
310 |
\isanewline
|
|
311 |
\isacommand{instance}\isamarkupfalse%
|
|
312 |
\ \isacommand{proof}\isamarkupfalse%
|
|
313 |
\isanewline
|
|
314 |
\ \ \isacommand{fix}\isamarkupfalse%
|
|
315 |
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
|
|
316 |
\ \ \isacommand{show}\isamarkupfalse%
|
|
317 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
318 |
\ \ \ \ \isacommand{by}\isamarkupfalse%
|
|
319 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharparenright}\isanewline
|
|
320 |
\ \ \isacommand{show}\isamarkupfalse%
|
|
321 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
|
|
322 |
\ \ \ \ \isacommand{by}\isamarkupfalse%
|
|
323 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
|
|
324 |
\ \ \isacommand{show}\isamarkupfalse%
|
|
325 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
|
|
326 |
\ \ \ \ \isacommand{by}\isamarkupfalse%
|
|
327 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
|
|
328 |
\isacommand{qed}\isamarkupfalse%
|
|
329 |
\isanewline
|
|
330 |
\isanewline
|
|
331 |
\isacommand{end}\isamarkupfalse%
|
|
332 |
%
|
|
333 |
\endisatagquote
|
|
334 |
{\isafoldquote}%
|
|
335 |
%
|
|
336 |
\isadelimquote
|
|
337 |
%
|
|
338 |
\endisadelimquote
|
|
339 |
%
|
|
340 |
\begin{isamarkuptext}%
|
|
341 |
\noindent We define the natural operation of the natural numbers
|
|
342 |
on monoids:%
|
|
343 |
\end{isamarkuptext}%
|
|
344 |
\isamarkuptrue%
|
|
345 |
%
|
|
346 |
\isadelimquote
|
|
347 |
%
|
|
348 |
\endisadelimquote
|
|
349 |
%
|
|
350 |
\isatagquote
|
|
351 |
\isacommand{primrec}\isamarkupfalse%
|
|
352 |
\ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\ pow\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
353 |
\ \ \ \ {\isachardoublequoteopen}pow\ {\isadigit{0}}\ a\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
|
|
354 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}pow\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ a\ {\isasymotimes}\ pow\ n\ a{\isachardoublequoteclose}%
|
|
355 |
\endisatagquote
|
|
356 |
{\isafoldquote}%
|
|
357 |
%
|
|
358 |
\isadelimquote
|
|
359 |
%
|
|
360 |
\endisadelimquote
|
|
361 |
%
|
|
362 |
\begin{isamarkuptext}%
|
|
363 |
\noindent This we use to define the discrete exponentiation
|
|
364 |
function:%
|
|
365 |
\end{isamarkuptext}%
|
|
366 |
\isamarkuptrue%
|
|
367 |
%
|
|
368 |
\isadelimquote
|
|
369 |
%
|
|
370 |
\endisadelimquote
|
|
371 |
%
|
|
372 |
\isatagquote
|
|
373 |
\isacommand{definition}\isamarkupfalse%
|
|
374 |
\ bexp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
375 |
\ \ {\isachardoublequoteopen}bexp\ n\ {\isacharequal}\ pow\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
|
|
376 |
\endisatagquote
|
|
377 |
{\isafoldquote}%
|
|
378 |
%
|
|
379 |
\isadelimquote
|
|
380 |
%
|
|
381 |
\endisadelimquote
|
|
382 |
%
|
|
383 |
\begin{isamarkuptext}%
|
|
384 |
\noindent The corresponding code in Haskell uses that language's
|
|
385 |
native classes:%
|
|
386 |
\end{isamarkuptext}%
|
|
387 |
\isamarkuptrue%
|
|
388 |
%
|
|
389 |
\isadelimquote
|
|
390 |
%
|
|
391 |
\endisadelimquote
|
|
392 |
%
|
|
393 |
\isatagquote
|
|
394 |
%
|
|
395 |
\begin{isamarkuptext}%
|
|
396 |
\isatypewriter%
|
|
397 |
\noindent%
|
|
398 |
\hspace*{0pt}module Example where {\char123}\\
|
|
399 |
\hspace*{0pt}\\
|
|
400 |
\hspace*{0pt}data Nat = Zero{\char95}nat | Suc Nat;\\
|
|
401 |
\hspace*{0pt}\\
|
|
402 |
\hspace*{0pt}plus{\char95}nat ::~Nat -> Nat -> Nat;\\
|
|
403 |
\hspace*{0pt}plus{\char95}nat (Suc m) n = plus{\char95}nat m (Suc n);\\
|
|
404 |
\hspace*{0pt}plus{\char95}nat Zero{\char95}nat n = n;\\
|
|
405 |
\hspace*{0pt}\\
|
|
406 |
\hspace*{0pt}class Semigroup a where {\char123}\\
|
|
407 |
\hspace*{0pt} ~mult ::~a -> a -> a;\\
|
|
408 |
\hspace*{0pt}{\char125};\\
|
|
409 |
\hspace*{0pt}\\
|
|
410 |
\hspace*{0pt}class (Semigroup a) => Monoid a where {\char123}\\
|
|
411 |
\hspace*{0pt} ~neutral ::~a;\\
|
|
412 |
\hspace*{0pt}{\char125};\\
|
|
413 |
\hspace*{0pt}\\
|
|
414 |
\hspace*{0pt}pow ::~forall a.~(Monoid a) => Nat -> a -> a;\\
|
|
415 |
\hspace*{0pt}pow Zero{\char95}nat a = neutral;\\
|
|
416 |
\hspace*{0pt}pow (Suc n) a = mult a (pow n a);\\
|
|
417 |
\hspace*{0pt}\\
|
|
418 |
\hspace*{0pt}mult{\char95}nat ::~Nat -> Nat -> Nat;\\
|
|
419 |
\hspace*{0pt}mult{\char95}nat Zero{\char95}nat n = Zero{\char95}nat;\\
|
|
420 |
\hspace*{0pt}mult{\char95}nat (Suc m) n = plus{\char95}nat n (mult{\char95}nat m n);\\
|
|
421 |
\hspace*{0pt}\\
|
|
422 |
\hspace*{0pt}neutral{\char95}nat ::~Nat;\\
|
|
423 |
\hspace*{0pt}neutral{\char95}nat = Suc Zero{\char95}nat;\\
|
|
424 |
\hspace*{0pt}\\
|
|
425 |
\hspace*{0pt}instance Semigroup Nat where {\char123}\\
|
|
426 |
\hspace*{0pt} ~mult = mult{\char95}nat;\\
|
|
427 |
\hspace*{0pt}{\char125};\\
|
|
428 |
\hspace*{0pt}\\
|
|
429 |
\hspace*{0pt}instance Monoid Nat where {\char123}\\
|
|
430 |
\hspace*{0pt} ~neutral = neutral{\char95}nat;\\
|
|
431 |
\hspace*{0pt}{\char125};\\
|
|
432 |
\hspace*{0pt}\\
|
|
433 |
\hspace*{0pt}bexp ::~Nat -> Nat;\\
|
|
434 |
\hspace*{0pt}bexp n = pow n (Suc (Suc Zero{\char95}nat));\\
|
|
435 |
\hspace*{0pt}\\
|
|
436 |
\hspace*{0pt}{\char125}%
|
|
437 |
\end{isamarkuptext}%
|
|
438 |
\isamarkuptrue%
|
|
439 |
%
|
|
440 |
\endisatagquote
|
|
441 |
{\isafoldquote}%
|
|
442 |
%
|
|
443 |
\isadelimquote
|
|
444 |
%
|
|
445 |
\endisadelimquote
|
|
446 |
%
|
|
447 |
\begin{isamarkuptext}%
|
|
448 |
\noindent This is a convenient place to show how explicit dictionary
|
|
449 |
construction manifests in generated code -- the same example in
|
|
450 |
\isa{SML}:%
|
|
451 |
\end{isamarkuptext}%
|
|
452 |
\isamarkuptrue%
|
|
453 |
%
|
|
454 |
\isadelimquote
|
|
455 |
%
|
|
456 |
\endisadelimquote
|
|
457 |
%
|
|
458 |
\isatagquote
|
|
459 |
%
|
|
460 |
\begin{isamarkuptext}%
|
|
461 |
\isatypewriter%
|
|
462 |
\noindent%
|
|
463 |
\hspace*{0pt}structure Example :~sig\\
|
|
464 |
\hspace*{0pt} ~datatype nat = Zero{\char95}nat | Suc of nat\\
|
|
465 |
\hspace*{0pt} ~val plus{\char95}nat :~nat -> nat -> nat\\
|
|
466 |
\hspace*{0pt} ~type 'a semigroup\\
|
|
467 |
\hspace*{0pt} ~val mult :~'a semigroup -> 'a -> 'a -> 'a\\
|
|
468 |
\hspace*{0pt} ~type 'a monoid\\
|
|
469 |
\hspace*{0pt} ~val semigroup{\char95}monoid :~'a monoid -> 'a semigroup\\
|
|
470 |
\hspace*{0pt} ~val neutral :~'a monoid -> 'a\\
|
|
471 |
\hspace*{0pt} ~val pow :~'a monoid -> nat -> 'a -> 'a\\
|
|
472 |
\hspace*{0pt} ~val mult{\char95}nat :~nat -> nat -> nat\\
|
|
473 |
\hspace*{0pt} ~val neutral{\char95}nat :~nat\\
|
|
474 |
\hspace*{0pt} ~val semigroup{\char95}nat :~nat semigroup\\
|
|
475 |
\hspace*{0pt} ~val monoid{\char95}nat :~nat monoid\\
|
|
476 |
\hspace*{0pt} ~val bexp :~nat -> nat\\
|
|
477 |
\hspace*{0pt}end = struct\\
|
|
478 |
\hspace*{0pt}\\
|
|
479 |
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\
|
|
480 |
\hspace*{0pt}\\
|
|
481 |
\hspace*{0pt}fun plus{\char95}nat (Suc m) n = plus{\char95}nat m (Suc n)\\
|
|
482 |
\hspace*{0pt} ~| plus{\char95}nat Zero{\char95}nat n = n;\\
|
|
483 |
\hspace*{0pt}\\
|
|
484 |
\hspace*{0pt}type 'a semigroup = {\char123}mult :~'a -> 'a -> 'a{\char125};\\
|
|
485 |
\hspace*{0pt}val mult = {\char35}mult :~'a semigroup -> 'a -> 'a -> 'a;\\
|
|
486 |
\hspace*{0pt}\\
|
|
487 |
\hspace*{0pt}type 'a monoid = {\char123}semigroup{\char95}monoid :~'a semigroup,~neutral :~'a{\char125};\\
|
|
488 |
\hspace*{0pt}val semigroup{\char95}monoid = {\char35}semigroup{\char95}monoid :~'a monoid -> 'a semigroup;\\
|
|
489 |
\hspace*{0pt}val neutral = {\char35}neutral :~'a monoid -> 'a;\\
|
|
490 |
\hspace*{0pt}\\
|
|
491 |
\hspace*{0pt}fun pow A{\char95}~Zero{\char95}nat a = neutral A{\char95}\\
|
|
492 |
\hspace*{0pt} ~| pow A{\char95}~(Suc n) a = mult (semigroup{\char95}monoid A{\char95}) a (pow A{\char95}~n a);\\
|
|
493 |
\hspace*{0pt}\\
|
|
494 |
\hspace*{0pt}fun mult{\char95}nat Zero{\char95}nat n = Zero{\char95}nat\\
|
|
495 |
\hspace*{0pt} ~| mult{\char95}nat (Suc m) n = plus{\char95}nat n (mult{\char95}nat m n);\\
|
|
496 |
\hspace*{0pt}\\
|
|
497 |
\hspace*{0pt}val neutral{\char95}nat :~nat = Suc Zero{\char95}nat;\\
|
|
498 |
\hspace*{0pt}\\
|
|
499 |
\hspace*{0pt}val semigroup{\char95}nat = {\char123}mult = mult{\char95}nat{\char125}~:~nat semigroup;\\
|
|
500 |
\hspace*{0pt}\\
|
|
501 |
\hspace*{0pt}val monoid{\char95}nat = {\char123}semigroup{\char95}monoid = semigroup{\char95}nat,~neutral = neutral{\char95}nat{\char125}\\
|
|
502 |
\hspace*{0pt} ~:~nat monoid;\\
|
|
503 |
\hspace*{0pt}\\
|
|
504 |
\hspace*{0pt}fun bexp n = pow monoid{\char95}nat n (Suc (Suc Zero{\char95}nat));\\
|
|
505 |
\hspace*{0pt}\\
|
|
506 |
\hspace*{0pt}end;~(*struct Example*)%
|
|
507 |
\end{isamarkuptext}%
|
|
508 |
\isamarkuptrue%
|
|
509 |
%
|
|
510 |
\endisatagquote
|
|
511 |
{\isafoldquote}%
|
|
512 |
%
|
|
513 |
\isadelimquote
|
|
514 |
%
|
|
515 |
\endisadelimquote
|
|
516 |
%
|
|
517 |
\begin{isamarkuptext}%
|
|
518 |
\noindent Note the parameters with trailing underscore (\verb|A_|), which are the dictionary parameters.%
|
|
519 |
\end{isamarkuptext}%
|
|
520 |
\isamarkuptrue%
|
|
521 |
%
|
|
522 |
\isamarkupsubsection{How to continue from here%
|
|
523 |
}
|
|
524 |
\isamarkuptrue%
|
|
525 |
%
|
|
526 |
\begin{isamarkuptext}%
|
|
527 |
What you have seen so far should be already enough in a lot of
|
|
528 |
cases. If you are content with this, you can quit reading here.
|
28447
|
529 |
|
38437
|
530 |
Anyway, to understand situations where problems occur or to increase
|
|
531 |
the scope of code generation beyond default, it is necessary to gain
|
|
532 |
some understanding how the code generator actually works:
|
28447
|
533 |
|
|
534 |
\begin{itemize}
|
|
535 |
|
38437
|
536 |
\item The foundations of the code generator are described in
|
|
537 |
\secref{sec:foundations}.
|
|
538 |
|
|
539 |
\item In particular \secref{sec:utterly_wrong} gives hints how to
|
|
540 |
debug situations where code generation does not succeed as
|
|
541 |
expected.
|
28447
|
542 |
|
38437
|
543 |
\item The scope and quality of generated code can be increased
|
|
544 |
dramatically by applying refinement techniques, which are
|
|
545 |
introduced in \secref{sec:refinement}.
|
28447
|
546 |
|
38437
|
547 |
\item Inductive predicates can be turned executable using an
|
|
548 |
extension of the code generator \secref{sec:inductive}.
|
|
549 |
|
|
550 |
\item You may want to skim over the more technical sections
|
|
551 |
\secref{sec:adaptation} and \secref{sec:further}.
|
|
552 |
|
|
553 |
\item For exhaustive syntax diagrams etc. you should visit the
|
|
554 |
Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}.
|
28447
|
555 |
|
|
556 |
\end{itemize}
|
|
557 |
|
38437
|
558 |
\bigskip
|
|
559 |
|
|
560 |
\begin{center}\fbox{\fbox{\begin{minipage}{8cm}
|
|
561 |
|
|
562 |
\begin{center}\textit{Happy proving, happy hacking!}\end{center}
|
|
563 |
|
|
564 |
\end{minipage}}}\end{center}
|
|
565 |
|
|
566 |
\begin{warn}
|
|
567 |
There is also a more ancient code generator in Isabelle by Stefan
|
|
568 |
Berghofer \cite{Berghofer-Nipkow:2002}. Although its
|
|
569 |
functionality is covered by the code generator presented here, it
|
|
570 |
will sometimes show up as an artifact. In case of ambiguity, we
|
|
571 |
will refer to the framework described here as \isa{generic\ code\ generator}, to the other as \isa{SML\ code\ generator}.
|
|
572 |
\end{warn}%
|
28447
|
573 |
\end{isamarkuptext}%
|
|
574 |
\isamarkuptrue%
|
|
575 |
%
|
|
576 |
\isadelimtheory
|
|
577 |
%
|
|
578 |
\endisadelimtheory
|
|
579 |
%
|
|
580 |
\isatagtheory
|
|
581 |
\isacommand{end}\isamarkupfalse%
|
|
582 |
%
|
|
583 |
\endisatagtheory
|
|
584 |
{\isafoldtheory}%
|
|
585 |
%
|
|
586 |
\isadelimtheory
|
|
587 |
%
|
|
588 |
\endisadelimtheory
|
|
589 |
\isanewline
|
|
590 |
\end{isabellebody}%
|
|
591 |
%%% Local Variables:
|
|
592 |
%%% mode: latex
|
|
593 |
%%% TeX-master: "root"
|
|
594 |
%%% End:
|