author | haftmann |
Mon, 27 Sep 2010 16:27:31 +0200 | |
changeset 39745 | 3aa2bc9c5478 |
parent 39683 | f75a01ee6c41 |
child 40406 | 313a24b66a8d |
permissions | -rw-r--r-- |
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 |
% |
|
39745 | 136 |
\isadelimquotetypewriter |
28447 | 137 |
% |
39745 | 138 |
\endisadelimquotetypewriter |
28447 | 139 |
% |
39745 | 140 |
\isatagquotetypewriter |
28447 | 141 |
% |
142 |
\begin{isamarkuptext}% |
|
39683 | 143 |
structure\ Example\ {\isacharcolon}\ sig\isanewline |
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
144 |
\ \ val\ id\ {\isacharcolon}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
145 |
\ \ val\ fold\ {\isacharcolon}\ {\isacharparenleft}{\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}b\ {\isacharminus}{\isachargreater}\ {\isacharprime}b{\isacharparenright}\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ list\ {\isacharminus}{\isachargreater}\ {\isacharprime}b\ {\isacharminus}{\isachargreater}\ {\isacharprime}b\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
146 |
\ \ val\ rev\ {\isacharcolon}\ {\isacharprime}a\ list\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ list\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
147 |
\ \ datatype\ {\isacharprime}a\ queue\ {\isacharequal}\ AQueue\ of\ {\isacharprime}a\ list\ {\isacharasterisk}\ {\isacharprime}a\ list\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
148 |
\ \ val\ empty\ {\isacharcolon}\ {\isacharprime}a\ queue\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
149 |
\ \ val\ dequeue\ {\isacharcolon}\ {\isacharprime}a\ queue\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ option\ {\isacharasterisk}\ {\isacharprime}a\ queue\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
150 |
\ \ val\ enqueue\ {\isacharcolon}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ queue\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ queue\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
151 |
end\ {\isacharequal}\ struct\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
152 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
153 |
fun\ id\ x\ {\isacharequal}\ {\isacharparenleft}fn\ xa\ {\isacharequal}{\isachargreater}\ xa{\isacharparenright}\ x{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
154 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
155 |
fun\ fold\ f\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ id\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
156 |
\ \ {\isacharbar}\ fold\ f\ {\isacharparenleft}x\ {\isacharcolon}{\isacharcolon}\ xs{\isacharparenright}\ {\isacharequal}\ fold\ f\ xs\ o\ f\ x{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
157 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
158 |
fun\ rev\ xs\ {\isacharequal}\ fold\ {\isacharparenleft}fn\ a\ {\isacharequal}{\isachargreater}\ fn\ b\ {\isacharequal}{\isachargreater}\ a\ {\isacharcolon}{\isacharcolon}\ b{\isacharparenright}\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
159 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
160 |
datatype\ {\isacharprime}a\ queue\ {\isacharequal}\ AQueue\ of\ {\isacharprime}a\ list\ {\isacharasterisk}\ {\isacharprime}a\ list{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
161 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
162 |
val\ empty\ {\isacharcolon}\ {\isacharprime}a\ queue\ {\isacharequal}\ AQueue\ {\isacharparenleft}{\isacharbrackleft}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
163 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
164 |
fun\ dequeue\ {\isacharparenleft}AQueue\ {\isacharparenleft}{\isacharbrackleft}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}NONE{\isacharcomma}\ AQueue\ {\isacharparenleft}{\isacharbrackleft}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharparenright}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
165 |
\ \ {\isacharbar}\ dequeue\ {\isacharparenleft}AQueue\ {\isacharparenleft}xs{\isacharcomma}\ y\ {\isacharcolon}{\isacharcolon}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}SOME\ y{\isacharcomma}\ AQueue\ {\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isacharparenright}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
166 |
\ \ {\isacharbar}\ dequeue\ {\isacharparenleft}AQueue\ {\isacharparenleft}v\ {\isacharcolon}{\isacharcolon}\ va{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
167 |
\ \ \ \ let\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
168 |
\ \ \ \ \ \ val\ y\ {\isacharcolon}{\isacharcolon}\ ys\ {\isacharequal}\ rev\ {\isacharparenleft}v\ {\isacharcolon}{\isacharcolon}\ va{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
169 |
\ \ \ \ in\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
170 |
\ \ \ \ \ \ {\isacharparenleft}SOME\ y{\isacharcomma}\ AQueue\ {\isacharparenleft}{\isacharbrackleft}{\isacharbrackright}{\isacharcomma}\ ys{\isacharparenright}{\isacharparenright}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
171 |
\ \ \ \ end{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
172 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
173 |
fun\ enqueue\ x\ {\isacharparenleft}AQueue\ {\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharcolon}{\isacharcolon}\ xs{\isacharcomma}\ ys{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
174 |
\isanewline |
39683 | 175 |
end{\isacharsemicolon}\ {\isacharparenleft}{\isacharasterisk}struct\ Example{\isacharasterisk}{\isacharparenright}\isanewline% |
28447 | 176 |
\end{isamarkuptext}% |
177 |
\isamarkuptrue% |
|
178 |
% |
|
39745 | 179 |
\endisatagquotetypewriter |
180 |
{\isafoldquotetypewriter}% |
|
28447 | 181 |
% |
39745 | 182 |
\isadelimquotetypewriter |
28447 | 183 |
% |
39745 | 184 |
\endisadelimquotetypewriter |
28447 | 185 |
% |
186 |
\begin{isamarkuptext}% |
|
38505 | 187 |
\noindent The \indexdef{}{command}{export\_code}\hypertarget{command.export-code}{\hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}}} command takes a |
188 |
space-separated list of constants for which code shall be generated; |
|
189 |
anything else needed for those is added implicitly. Then follows a |
|
190 |
target language identifier and a freely chosen module name. A file |
|
191 |
name denotes the destination to store the generated code. Note that |
|
192 |
the semantics of the destination depends on the target language: for |
|
38813 | 193 |
\isa{SML}, \isa{OCaml} and \isa{Scala} it denotes a \emph{file}, |
194 |
for \isa{Haskell} it denotes a \emph{directory} where a file named as the |
|
38437 | 195 |
module name (with extension \isa{{\isachardot}hs}) is written:% |
28447 | 196 |
\end{isamarkuptext}% |
197 |
\isamarkuptrue% |
|
198 |
% |
|
28564 | 199 |
\isadelimquote |
28447 | 200 |
% |
28564 | 201 |
\endisadelimquote |
28447 | 202 |
% |
28564 | 203 |
\isatagquote |
28447 | 204 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
205 |
\ empty\ dequeue\ enqueue\ \isakeyword{in}\ Haskell\isanewline |
|
206 |
\ \ \isakeyword{module{\isacharunderscore}name}\ Example\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}% |
|
28564 | 207 |
\endisatagquote |
208 |
{\isafoldquote}% |
|
28447 | 209 |
% |
28564 | 210 |
\isadelimquote |
28447 | 211 |
% |
28564 | 212 |
\endisadelimquote |
28447 | 213 |
% |
214 |
\begin{isamarkuptext}% |
|
38437 | 215 |
\noindent This is the corresponding code:% |
28447 | 216 |
\end{isamarkuptext}% |
217 |
\isamarkuptrue% |
|
218 |
% |
|
39745 | 219 |
\isadelimquotetypewriter |
28447 | 220 |
% |
39745 | 221 |
\endisadelimquotetypewriter |
28447 | 222 |
% |
39745 | 223 |
\isatagquotetypewriter |
28447 | 224 |
% |
225 |
\begin{isamarkuptext}% |
|
39683 | 226 |
module\ Example\ where\ {\isacharbraceleft}\isanewline |
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
227 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
228 |
data\ Queue\ a\ {\isacharequal}\ AQueue\ {\isacharbrackleft}a{\isacharbrackright}\ {\isacharbrackleft}a{\isacharbrackright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
229 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
230 |
empty\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ Queue\ a{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
231 |
empty\ {\isacharequal}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
232 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
233 |
dequeue\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ Queue\ a\ {\isacharminus}{\isachargreater}\ {\isacharparenleft}Maybe\ a{\isacharcomma}\ Queue\ a{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
234 |
dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Nothing{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
235 |
dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharcolon}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Just\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
236 |
dequeue\ {\isacharparenleft}AQueue\ {\isacharparenleft}v\ {\isacharcolon}\ va{\isacharparenright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
237 |
\ \ let\ {\isacharbraceleft}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
238 |
\ \ \ \ {\isacharparenleft}y\ {\isacharcolon}\ ys{\isacharparenright}\ {\isacharequal}\ reverse\ {\isacharparenleft}v\ {\isacharcolon}\ va{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
239 |
\ \ {\isacharbraceright}\ in\ {\isacharparenleft}Just\ y{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
240 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
241 |
enqueue\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ a\ {\isacharminus}{\isachargreater}\ Queue\ a\ {\isacharminus}{\isachargreater}\ Queue\ a{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
242 |
enqueue\ x\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharcolon}\ xs{\isacharparenright}\ ys{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
243 |
\isanewline |
39683 | 244 |
{\isacharbraceright}\isanewline% |
28447 | 245 |
\end{isamarkuptext}% |
246 |
\isamarkuptrue% |
|
247 |
% |
|
39745 | 248 |
\endisatagquotetypewriter |
249 |
{\isafoldquotetypewriter}% |
|
28447 | 250 |
% |
39745 | 251 |
\isadelimquotetypewriter |
28447 | 252 |
% |
39745 | 253 |
\endisadelimquotetypewriter |
28447 | 254 |
% |
255 |
\begin{isamarkuptext}% |
|
38437 | 256 |
\noindent For more details about \hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}} see |
257 |
\secref{sec:further}.% |
|
28447 | 258 |
\end{isamarkuptext}% |
259 |
\isamarkuptrue% |
|
260 |
% |
|
38437 | 261 |
\isamarkupsubsection{Type classes% |
28447 | 262 |
} |
263 |
\isamarkuptrue% |
|
264 |
% |
|
265 |
\begin{isamarkuptext}% |
|
38437 | 266 |
Code can also be generated from type classes in a Haskell-like |
267 |
manner. For illustration here an example from abstract algebra:% |
|
268 |
\end{isamarkuptext}% |
|
269 |
\isamarkuptrue% |
|
270 |
% |
|
271 |
\isadelimquote |
|
272 |
% |
|
273 |
\endisadelimquote |
|
274 |
% |
|
275 |
\isatagquote |
|
276 |
\isacommand{class}\isamarkupfalse% |
|
277 |
\ semigroup\ {\isacharequal}\isanewline |
|
278 |
\ \ \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 |
|
279 |
\ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
280 |
\isanewline |
|
281 |
\isacommand{class}\isamarkupfalse% |
|
282 |
\ monoid\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline |
|
283 |
\ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
284 |
\ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline |
|
285 |
\ \ \ \ \isakeyword{and}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline |
|
286 |
\isanewline |
|
287 |
\isacommand{instantiation}\isamarkupfalse% |
|
288 |
\ nat\ {\isacharcolon}{\isacharcolon}\ monoid\isanewline |
|
289 |
\isakeyword{begin}\isanewline |
|
290 |
\isanewline |
|
291 |
\isacommand{primrec}\isamarkupfalse% |
|
292 |
\ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline |
|
293 |
\ \ \ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isasymotimes}\ n\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
294 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ n\ {\isacharplus}\ m\ {\isasymotimes}\ n{\isachardoublequoteclose}\isanewline |
|
295 |
\isanewline |
|
296 |
\isacommand{definition}\isamarkupfalse% |
|
297 |
\ neutral{\isacharunderscore}nat\ \isakeyword{where}\isanewline |
|
298 |
\ \ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline |
|
299 |
\isanewline |
|
300 |
\isacommand{lemma}\isamarkupfalse% |
|
301 |
\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharcolon}\isanewline |
|
302 |
\ \ \isakeyword{fixes}\ n\ m\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline |
|
303 |
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}{\isacharparenleft}n\ {\isacharplus}\ m{\isacharparenright}\ {\isasymotimes}\ q\ {\isacharequal}\ n\ {\isasymotimes}\ q\ {\isacharplus}\ m\ {\isasymotimes}\ q{\isachardoublequoteclose}\isanewline |
|
304 |
\ \ \isacommand{by}\isamarkupfalse% |
|
305 |
\ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline |
|
306 |
\isanewline |
|
307 |
\isacommand{instance}\isamarkupfalse% |
|
308 |
\ \isacommand{proof}\isamarkupfalse% |
|
309 |
\isanewline |
|
310 |
\ \ \isacommand{fix}\isamarkupfalse% |
|
311 |
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline |
|
312 |
\ \ \isacommand{show}\isamarkupfalse% |
|
313 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
314 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
|
315 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharparenright}\isanewline |
|
316 |
\ \ \isacommand{show}\isamarkupfalse% |
|
317 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline |
|
318 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
|
319 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline |
|
320 |
\ \ \isacommand{show}\isamarkupfalse% |
|
321 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline |
|
322 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
|
323 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline |
|
324 |
\isacommand{qed}\isamarkupfalse% |
|
325 |
\isanewline |
|
326 |
\isanewline |
|
327 |
\isacommand{end}\isamarkupfalse% |
|
328 |
% |
|
329 |
\endisatagquote |
|
330 |
{\isafoldquote}% |
|
331 |
% |
|
332 |
\isadelimquote |
|
333 |
% |
|
334 |
\endisadelimquote |
|
335 |
% |
|
336 |
\begin{isamarkuptext}% |
|
337 |
\noindent We define the natural operation of the natural numbers |
|
338 |
on monoids:% |
|
339 |
\end{isamarkuptext}% |
|
340 |
\isamarkuptrue% |
|
341 |
% |
|
342 |
\isadelimquote |
|
343 |
% |
|
344 |
\endisadelimquote |
|
345 |
% |
|
346 |
\isatagquote |
|
347 |
\isacommand{primrec}\isamarkupfalse% |
|
348 |
\ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\ pow\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
349 |
\ \ \ \ {\isachardoublequoteopen}pow\ {\isadigit{0}}\ a\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline |
|
350 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}pow\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ a\ {\isasymotimes}\ pow\ n\ a{\isachardoublequoteclose}% |
|
351 |
\endisatagquote |
|
352 |
{\isafoldquote}% |
|
353 |
% |
|
354 |
\isadelimquote |
|
355 |
% |
|
356 |
\endisadelimquote |
|
357 |
% |
|
358 |
\begin{isamarkuptext}% |
|
359 |
\noindent This we use to define the discrete exponentiation |
|
360 |
function:% |
|
361 |
\end{isamarkuptext}% |
|
362 |
\isamarkuptrue% |
|
363 |
% |
|
364 |
\isadelimquote |
|
365 |
% |
|
366 |
\endisadelimquote |
|
367 |
% |
|
368 |
\isatagquote |
|
369 |
\isacommand{definition}\isamarkupfalse% |
|
370 |
\ bexp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
371 |
\ \ {\isachardoublequoteopen}bexp\ n\ {\isacharequal}\ pow\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}% |
|
372 |
\endisatagquote |
|
373 |
{\isafoldquote}% |
|
374 |
% |
|
375 |
\isadelimquote |
|
376 |
% |
|
377 |
\endisadelimquote |
|
378 |
% |
|
379 |
\begin{isamarkuptext}% |
|
380 |
\noindent The corresponding code in Haskell uses that language's |
|
381 |
native classes:% |
|
382 |
\end{isamarkuptext}% |
|
383 |
\isamarkuptrue% |
|
384 |
% |
|
39745 | 385 |
\isadelimquotetypewriter |
38437 | 386 |
% |
39745 | 387 |
\endisadelimquotetypewriter |
38437 | 388 |
% |
39745 | 389 |
\isatagquotetypewriter |
38437 | 390 |
% |
391 |
\begin{isamarkuptext}% |
|
39683 | 392 |
module\ Example\ where\ {\isacharbraceleft}\isanewline |
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
393 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
394 |
data\ Nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ Nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
395 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
396 |
plus{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ Nat\ {\isacharminus}{\isachargreater}\ Nat\ {\isacharminus}{\isachargreater}\ Nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
397 |
plus{\isacharunderscore}nat\ {\isacharparenleft}Suc\ m{\isacharparenright}\ n\ {\isacharequal}\ plus{\isacharunderscore}nat\ m\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
398 |
plus{\isacharunderscore}nat\ Zero{\isacharunderscore}nat\ n\ {\isacharequal}\ n{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
399 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
400 |
class\ Semigroup\ a\ where\ {\isacharbraceleft}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
401 |
\ \ mult\ {\isacharcolon}{\isacharcolon}\ a\ {\isacharminus}{\isachargreater}\ a\ {\isacharminus}{\isachargreater}\ a{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
402 |
{\isacharbraceright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
403 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
404 |
class\ {\isacharparenleft}Semigroup\ a{\isacharparenright}\ {\isacharequal}{\isachargreater}\ Monoid\ a\ where\ {\isacharbraceleft}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
405 |
\ \ neutral\ {\isacharcolon}{\isacharcolon}\ a{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
406 |
{\isacharbraceright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
407 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
408 |
pow\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ {\isacharparenleft}Monoid\ a{\isacharparenright}\ {\isacharequal}{\isachargreater}\ Nat\ {\isacharminus}{\isachargreater}\ a\ {\isacharminus}{\isachargreater}\ a{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
409 |
pow\ Zero{\isacharunderscore}nat\ a\ {\isacharequal}\ neutral{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
410 |
pow\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ mult\ a\ {\isacharparenleft}pow\ n\ a{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
411 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
412 |
mult{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ Nat\ {\isacharminus}{\isachargreater}\ Nat\ {\isacharminus}{\isachargreater}\ Nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
413 |
mult{\isacharunderscore}nat\ Zero{\isacharunderscore}nat\ n\ {\isacharequal}\ Zero{\isacharunderscore}nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
414 |
mult{\isacharunderscore}nat\ {\isacharparenleft}Suc\ m{\isacharparenright}\ n\ {\isacharequal}\ plus{\isacharunderscore}nat\ n\ {\isacharparenleft}mult{\isacharunderscore}nat\ m\ n{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
415 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
416 |
instance\ Semigroup\ Nat\ where\ {\isacharbraceleft}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
417 |
\ \ mult\ {\isacharequal}\ mult{\isacharunderscore}nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
418 |
{\isacharbraceright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
419 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
420 |
neutral{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ Nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
421 |
neutral{\isacharunderscore}nat\ {\isacharequal}\ Suc\ Zero{\isacharunderscore}nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
422 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
423 |
instance\ Monoid\ Nat\ where\ {\isacharbraceleft}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
424 |
\ \ neutral\ {\isacharequal}\ neutral{\isacharunderscore}nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
425 |
{\isacharbraceright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
426 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
427 |
bexp\ {\isacharcolon}{\isacharcolon}\ Nat\ {\isacharminus}{\isachargreater}\ Nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
428 |
bexp\ n\ {\isacharequal}\ pow\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ Zero{\isacharunderscore}nat{\isacharparenright}{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
429 |
\isanewline |
39683 | 430 |
{\isacharbraceright}\isanewline% |
38437 | 431 |
\end{isamarkuptext}% |
432 |
\isamarkuptrue% |
|
433 |
% |
|
39745 | 434 |
\endisatagquotetypewriter |
435 |
{\isafoldquotetypewriter}% |
|
38437 | 436 |
% |
39745 | 437 |
\isadelimquotetypewriter |
38437 | 438 |
% |
39745 | 439 |
\endisadelimquotetypewriter |
38437 | 440 |
% |
441 |
\begin{isamarkuptext}% |
|
442 |
\noindent This is a convenient place to show how explicit dictionary |
|
443 |
construction manifests in generated code -- the same example in |
|
444 |
\isa{SML}:% |
|
445 |
\end{isamarkuptext}% |
|
446 |
\isamarkuptrue% |
|
447 |
% |
|
39745 | 448 |
\isadelimquotetypewriter |
38437 | 449 |
% |
39745 | 450 |
\endisadelimquotetypewriter |
38437 | 451 |
% |
39745 | 452 |
\isatagquotetypewriter |
38437 | 453 |
% |
454 |
\begin{isamarkuptext}% |
|
39683 | 455 |
structure\ Example\ {\isacharcolon}\ sig\isanewline |
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
456 |
\ \ datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
457 |
\ \ val\ plus{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
458 |
\ \ type\ {\isacharprime}a\ semigroup\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
459 |
\ \ val\ mult\ {\isacharcolon}\ {\isacharprime}a\ semigroup\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
460 |
\ \ type\ {\isacharprime}a\ monoid\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
461 |
\ \ val\ semigroup{\isacharunderscore}monoid\ {\isacharcolon}\ {\isacharprime}a\ monoid\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ semigroup\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
462 |
\ \ val\ neutral\ {\isacharcolon}\ {\isacharprime}a\ monoid\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
463 |
\ \ val\ pow\ {\isacharcolon}\ {\isacharprime}a\ monoid\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
464 |
\ \ val\ mult{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
465 |
\ \ val\ semigroup{\isacharunderscore}nat\ {\isacharcolon}\ nat\ semigroup\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
466 |
\ \ val\ neutral{\isacharunderscore}nat\ {\isacharcolon}\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
467 |
\ \ val\ monoid{\isacharunderscore}nat\ {\isacharcolon}\ nat\ monoid\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
468 |
\ \ val\ bexp\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
469 |
end\ {\isacharequal}\ struct\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
470 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
471 |
datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
472 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
473 |
fun\ plus{\isacharunderscore}nat\ {\isacharparenleft}Suc\ m{\isacharparenright}\ n\ {\isacharequal}\ plus{\isacharunderscore}nat\ m\ {\isacharparenleft}Suc\ n{\isacharparenright}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
474 |
\ \ {\isacharbar}\ plus{\isacharunderscore}nat\ Zero{\isacharunderscore}nat\ n\ {\isacharequal}\ n{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
475 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
476 |
type\ {\isacharprime}a\ semigroup\ {\isacharequal}\ {\isacharbraceleft}mult\ {\isacharcolon}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a{\isacharbraceright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
477 |
val\ mult\ {\isacharequal}\ {\isacharhash}mult\ {\isacharcolon}\ {\isacharprime}a\ semigroup\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
478 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
479 |
type\ {\isacharprime}a\ monoid\ {\isacharequal}\ {\isacharbraceleft}semigroup{\isacharunderscore}monoid\ {\isacharcolon}\ {\isacharprime}a\ semigroup{\isacharcomma}\ neutral\ {\isacharcolon}\ {\isacharprime}a{\isacharbraceright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
480 |
val\ semigroup{\isacharunderscore}monoid\ {\isacharequal}\ {\isacharhash}semigroup{\isacharunderscore}monoid\ {\isacharcolon}\ {\isacharprime}a\ monoid\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ semigroup{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
481 |
val\ neutral\ {\isacharequal}\ {\isacharhash}neutral\ {\isacharcolon}\ {\isacharprime}a\ monoid\ {\isacharminus}{\isachargreater}\ {\isacharprime}a{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
482 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
483 |
fun\ pow\ A{\isacharunderscore}\ Zero{\isacharunderscore}nat\ a\ {\isacharequal}\ neutral\ A{\isacharunderscore}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
484 |
\ \ {\isacharbar}\ pow\ A{\isacharunderscore}\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ mult\ {\isacharparenleft}semigroup{\isacharunderscore}monoid\ A{\isacharunderscore}{\isacharparenright}\ a\ {\isacharparenleft}pow\ A{\isacharunderscore}\ n\ a{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
485 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
486 |
fun\ mult{\isacharunderscore}nat\ Zero{\isacharunderscore}nat\ n\ {\isacharequal}\ Zero{\isacharunderscore}nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
487 |
\ \ {\isacharbar}\ mult{\isacharunderscore}nat\ {\isacharparenleft}Suc\ m{\isacharparenright}\ n\ {\isacharequal}\ plus{\isacharunderscore}nat\ n\ {\isacharparenleft}mult{\isacharunderscore}nat\ m\ n{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
488 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
489 |
val\ semigroup{\isacharunderscore}nat\ {\isacharequal}\ {\isacharbraceleft}mult\ {\isacharequal}\ mult{\isacharunderscore}nat{\isacharbraceright}\ {\isacharcolon}\ nat\ semigroup{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
490 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
491 |
val\ neutral{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharequal}\ Suc\ Zero{\isacharunderscore}nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
492 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
493 |
val\ monoid{\isacharunderscore}nat\ {\isacharequal}\ {\isacharbraceleft}semigroup{\isacharunderscore}monoid\ {\isacharequal}\ semigroup{\isacharunderscore}nat{\isacharcomma}\ neutral\ {\isacharequal}\ neutral{\isacharunderscore}nat{\isacharbraceright}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
494 |
\ \ {\isacharcolon}\ nat\ monoid{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
495 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
496 |
fun\ bexp\ n\ {\isacharequal}\ pow\ monoid{\isacharunderscore}nat\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ Zero{\isacharunderscore}nat{\isacharparenright}{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39210
diff
changeset
|
497 |
\isanewline |
39683 | 498 |
end{\isacharsemicolon}\ {\isacharparenleft}{\isacharasterisk}struct\ Example{\isacharasterisk}{\isacharparenright}\isanewline% |
38437 | 499 |
\end{isamarkuptext}% |
500 |
\isamarkuptrue% |
|
501 |
% |
|
39745 | 502 |
\endisatagquotetypewriter |
503 |
{\isafoldquotetypewriter}% |
|
38437 | 504 |
% |
39745 | 505 |
\isadelimquotetypewriter |
38437 | 506 |
% |
39745 | 507 |
\endisadelimquotetypewriter |
38437 | 508 |
% |
509 |
\begin{isamarkuptext}% |
|
510 |
\noindent Note the parameters with trailing underscore (\verb|A_|), which are the dictionary parameters.% |
|
511 |
\end{isamarkuptext}% |
|
512 |
\isamarkuptrue% |
|
513 |
% |
|
514 |
\isamarkupsubsection{How to continue from here% |
|
515 |
} |
|
516 |
\isamarkuptrue% |
|
517 |
% |
|
518 |
\begin{isamarkuptext}% |
|
519 |
What you have seen so far should be already enough in a lot of |
|
520 |
cases. If you are content with this, you can quit reading here. |
|
28447 | 521 |
|
38437 | 522 |
Anyway, to understand situations where problems occur or to increase |
523 |
the scope of code generation beyond default, it is necessary to gain |
|
524 |
some understanding how the code generator actually works: |
|
28447 | 525 |
|
526 |
\begin{itemize} |
|
527 |
||
38437 | 528 |
\item The foundations of the code generator are described in |
529 |
\secref{sec:foundations}. |
|
530 |
||
531 |
\item In particular \secref{sec:utterly_wrong} gives hints how to |
|
532 |
debug situations where code generation does not succeed as |
|
533 |
expected. |
|
28447 | 534 |
|
38437 | 535 |
\item The scope and quality of generated code can be increased |
536 |
dramatically by applying refinement techniques, which are |
|
537 |
introduced in \secref{sec:refinement}. |
|
28447 | 538 |
|
38437 | 539 |
\item Inductive predicates can be turned executable using an |
540 |
extension of the code generator \secref{sec:inductive}. |
|
541 |
||
542 |
\item You may want to skim over the more technical sections |
|
543 |
\secref{sec:adaptation} and \secref{sec:further}. |
|
544 |
||
545 |
\item For exhaustive syntax diagrams etc. you should visit the |
|
546 |
Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}. |
|
28447 | 547 |
|
548 |
\end{itemize} |
|
549 |
||
38437 | 550 |
\bigskip |
551 |
||
552 |
\begin{center}\fbox{\fbox{\begin{minipage}{8cm} |
|
553 |
||
554 |
\begin{center}\textit{Happy proving, happy hacking!}\end{center} |
|
555 |
||
556 |
\end{minipage}}}\end{center} |
|
557 |
||
558 |
\begin{warn} |
|
559 |
There is also a more ancient code generator in Isabelle by Stefan |
|
560 |
Berghofer \cite{Berghofer-Nipkow:2002}. Although its |
|
561 |
functionality is covered by the code generator presented here, it |
|
562 |
will sometimes show up as an artifact. In case of ambiguity, we |
|
563 |
will refer to the framework described here as \isa{generic\ code\ generator}, to the other as \isa{SML\ code\ generator}. |
|
564 |
\end{warn}% |
|
28447 | 565 |
\end{isamarkuptext}% |
566 |
\isamarkuptrue% |
|
567 |
% |
|
568 |
\isadelimtheory |
|
569 |
% |
|
570 |
\endisadelimtheory |
|
571 |
% |
|
572 |
\isatagtheory |
|
573 |
\isacommand{end}\isamarkupfalse% |
|
574 |
% |
|
575 |
\endisatagtheory |
|
576 |
{\isafoldtheory}% |
|
577 |
% |
|
578 |
\isadelimtheory |
|
579 |
% |
|
580 |
\endisadelimtheory |
|
581 |
\isanewline |
|
582 |
\end{isabellebody}% |
|
583 |
%%% Local Variables: |
|
584 |
%%% mode: latex |
|
585 |
%%% TeX-master: "root" |
|
586 |
%%% End: |