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