doc-src/IsarAdvanced/Codegen/Thy/document/Introduction.tex
author wenzelm
Tue, 14 Oct 2008 15:16:11 +0200
changeset 28586 d238b83ba3fc
parent 28564 1358b1ddd915
child 28635 cc53d2ab0170
permissions -rw-r--r--
renamed kill_all to kill, in conformance with atp_kill command; simplified/unified treatment of preferences; check_thread_manager: CRITICAL due to global ref; goal addressing via Thm.cprem_of; reduced NJ basis library stuff to bare minimum;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28447
haftmann
parents:
diff changeset
     1
%
haftmann
parents:
diff changeset
     2
\begin{isabellebody}%
haftmann
parents:
diff changeset
     3
\def\isabellecontext{Introduction}%
haftmann
parents:
diff changeset
     4
%
haftmann
parents:
diff changeset
     5
\isadelimtheory
haftmann
parents:
diff changeset
     6
%
haftmann
parents:
diff changeset
     7
\endisadelimtheory
haftmann
parents:
diff changeset
     8
%
haftmann
parents:
diff changeset
     9
\isatagtheory
haftmann
parents:
diff changeset
    10
\isacommand{theory}\isamarkupfalse%
haftmann
parents:
diff changeset
    11
\ Introduction\isanewline
haftmann
parents:
diff changeset
    12
\isakeyword{imports}\ Setup\isanewline
haftmann
parents:
diff changeset
    13
\isakeyword{begin}%
haftmann
parents:
diff changeset
    14
\endisatagtheory
haftmann
parents:
diff changeset
    15
{\isafoldtheory}%
haftmann
parents:
diff changeset
    16
%
haftmann
parents:
diff changeset
    17
\isadelimtheory
haftmann
parents:
diff changeset
    18
%
haftmann
parents:
diff changeset
    19
\endisadelimtheory
haftmann
parents:
diff changeset
    20
%
haftmann
parents:
diff changeset
    21
\isamarkupchapter{Code generation from \isa{Isabelle{\isacharslash}HOL} theories%
haftmann
parents:
diff changeset
    22
}
haftmann
parents:
diff changeset
    23
\isamarkuptrue%
haftmann
parents:
diff changeset
    24
%
haftmann
parents:
diff changeset
    25
\isamarkupsection{Introduction and Overview%
haftmann
parents:
diff changeset
    26
}
haftmann
parents:
diff changeset
    27
\isamarkuptrue%
haftmann
parents:
diff changeset
    28
%
haftmann
parents:
diff changeset
    29
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
    30
This tutorial introduces a generic code generator for the
haftmann
parents:
diff changeset
    31
  \isa{Isabelle} system.
haftmann
parents:
diff changeset
    32
  Generic in the sense that the
haftmann
parents:
diff changeset
    33
  \qn{target language} for which code shall ultimately be
haftmann
parents:
diff changeset
    34
  generated is not fixed but may be an arbitrary state-of-the-art
haftmann
parents:
diff changeset
    35
  functional programming language (currently, the implementation
haftmann
parents:
diff changeset
    36
  supports \isa{SML} \cite{SML}, \isa{OCaml} \cite{OCaml} and \isa{Haskell}
haftmann
parents:
diff changeset
    37
  \cite{haskell-revised-report}).
haftmann
parents:
diff changeset
    38
haftmann
parents:
diff changeset
    39
  Conceptually the code generator framework is part
haftmann
parents:
diff changeset
    40
  of Isabelle's \hyperlink{theory.Pure}{\mbox{\isa{Pure}}} meta logic framework; the logic
haftmann
parents:
diff changeset
    41
  \hyperlink{theory.HOL}{\mbox{\isa{HOL}}} which is an extension of \hyperlink{theory.Pure}{\mbox{\isa{Pure}}}
haftmann
parents:
diff changeset
    42
  already comes with a reasonable framework setup and thus provides
haftmann
parents:
diff changeset
    43
  a good working horse for raising code-generation-driven
haftmann
parents:
diff changeset
    44
  applications.  So, we assume some familiarity and experience
haftmann
parents:
diff changeset
    45
  with the ingredients of the \hyperlink{theory.HOL}{\mbox{\isa{HOL}}} distribution theories.
haftmann
parents:
diff changeset
    46
  (see also \cite{isa-tutorial}).
haftmann
parents:
diff changeset
    47
haftmann
parents:
diff changeset
    48
  The code generator aims to be usable with no further ado
haftmann
parents:
diff changeset
    49
  in most cases while allowing for detailed customisation.
haftmann
parents:
diff changeset
    50
  This manifests in the structure of this tutorial: after a short
haftmann
parents:
diff changeset
    51
  conceptual introduction with an example (\secref{sec:intro}),
haftmann
parents:
diff changeset
    52
  we discuss the generic customisation facilities (\secref{sec:program}).
haftmann
parents:
diff changeset
    53
  A further section (\secref{sec:adaption}) is dedicated to the matter of
haftmann
parents:
diff changeset
    54
  \qn{adaption} to specific target language environments.  After some
haftmann
parents:
diff changeset
    55
  further issues (\secref{sec:further}) we conclude with an overview
haftmann
parents:
diff changeset
    56
  of some ML programming interfaces (\secref{sec:ml}).
haftmann
parents:
diff changeset
    57
haftmann
parents:
diff changeset
    58
  \begin{warn}
haftmann
parents:
diff changeset
    59
    Ultimately, the code generator which this tutorial deals with
haftmann
parents:
diff changeset
    60
    is supposed to replace the existing code generator
haftmann
parents:
diff changeset
    61
    by Stefan Berghofer \cite{Berghofer-Nipkow:2002}.
haftmann
parents:
diff changeset
    62
    So, for the moment, there are two distinct code generators
haftmann
parents:
diff changeset
    63
    in Isabelle.  In case of ambiguity, we will refer to the framework
haftmann
parents:
diff changeset
    64
    described here as \isa{generic\ code\ generator}, to the
haftmann
parents:
diff changeset
    65
    other as \isa{SML\ code\ generator}.
haftmann
parents:
diff changeset
    66
    Also note that while the framework itself is
haftmann
parents:
diff changeset
    67
    object-logic independent, only \hyperlink{theory.HOL}{\mbox{\isa{HOL}}} provides a reasonable
haftmann
parents:
diff changeset
    68
    framework setup.    
haftmann
parents:
diff changeset
    69
  \end{warn}%
haftmann
parents:
diff changeset
    70
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    71
\isamarkuptrue%
haftmann
parents:
diff changeset
    72
%
haftmann
parents:
diff changeset
    73
\isamarkupsubsection{Code generation via shallow embedding \label{sec:intro}%
haftmann
parents:
diff changeset
    74
}
haftmann
parents:
diff changeset
    75
\isamarkuptrue%
haftmann
parents:
diff changeset
    76
%
haftmann
parents:
diff changeset
    77
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
    78
The key concept for understanding \isa{Isabelle}'s code generation is
haftmann
parents:
diff changeset
    79
  \emph{shallow embedding}, i.e.~logical entities like constants, types and
haftmann
parents:
diff changeset
    80
  classes are identified with corresponding concepts in the target language.
haftmann
parents:
diff changeset
    81
haftmann
parents:
diff changeset
    82
  Inside \hyperlink{theory.HOL}{\mbox{\isa{HOL}}}, the \hyperlink{command.datatype}{\mbox{\isa{\isacommand{datatype}}}} and
haftmann
parents:
diff changeset
    83
  \hyperlink{command.definition}{\mbox{\isa{\isacommand{definition}}}}/\hyperlink{command.primrec}{\mbox{\isa{\isacommand{primrec}}}}/\hyperlink{command.fun}{\mbox{\isa{\isacommand{fun}}}} declarations form
haftmann
parents:
diff changeset
    84
  the core of a functional programming language.  The default code generator setup
haftmann
parents:
diff changeset
    85
  allows to turn those into functional programs immediately.
haftmann
parents:
diff changeset
    86
  This means that \qt{naive} code generation can proceed without further ado.
haftmann
parents:
diff changeset
    87
  For example, here a simple \qt{implementation} of amortised queues:%
haftmann
parents:
diff changeset
    88
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    89
\isamarkuptrue%
haftmann
parents:
diff changeset
    90
%
28564
haftmann
parents: 28447
diff changeset
    91
\isadelimquote
28447
haftmann
parents:
diff changeset
    92
%
28564
haftmann
parents: 28447
diff changeset
    93
\endisadelimquote
28447
haftmann
parents:
diff changeset
    94
%
28564
haftmann
parents: 28447
diff changeset
    95
\isatagquote
28447
haftmann
parents:
diff changeset
    96
\isacommand{datatype}\isamarkupfalse%
haftmann
parents:
diff changeset
    97
\ {\isacharprime}a\ queue\ {\isacharequal}\ Queue\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
    98
\isanewline
haftmann
parents:
diff changeset
    99
\isacommand{definition}\isamarkupfalse%
haftmann
parents:
diff changeset
   100
\ empty\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann
parents:
diff changeset
   101
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   102
\isanewline
haftmann
parents:
diff changeset
   103
\isacommand{primrec}\isamarkupfalse%
haftmann
parents:
diff changeset
   104
\ enqueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann
parents:
diff changeset
   105
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}Queue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ Queue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   106
\isanewline
haftmann
parents:
diff changeset
   107
\isacommand{fun}\isamarkupfalse%
haftmann
parents:
diff changeset
   108
\ dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ option\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann
parents:
diff changeset
   109
\ \ \ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}None{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   110
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ y{\isacharcomma}\ Queue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   111
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
haftmann
parents:
diff changeset
   112
\ \ \ \ \ \ {\isacharparenleft}case\ rev\ xs\ of\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ {\isacharparenleft}Some\ y{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
28564
haftmann
parents: 28447
diff changeset
   113
\endisatagquote
haftmann
parents: 28447
diff changeset
   114
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   115
%
28564
haftmann
parents: 28447
diff changeset
   116
\isadelimquote
28447
haftmann
parents:
diff changeset
   117
%
28564
haftmann
parents: 28447
diff changeset
   118
\endisadelimquote
28447
haftmann
parents:
diff changeset
   119
%
haftmann
parents:
diff changeset
   120
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   121
\noindent Then we can generate code e.g.~for \isa{SML} as follows:%
haftmann
parents:
diff changeset
   122
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   123
\isamarkuptrue%
haftmann
parents:
diff changeset
   124
%
28564
haftmann
parents: 28447
diff changeset
   125
\isadelimquote
28447
haftmann
parents:
diff changeset
   126
%
28564
haftmann
parents: 28447
diff changeset
   127
\endisadelimquote
28447
haftmann
parents:
diff changeset
   128
%
28564
haftmann
parents: 28447
diff changeset
   129
\isatagquote
28447
haftmann
parents:
diff changeset
   130
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann
parents:
diff changeset
   131
\ empty\ dequeue\ enqueue\ \isakeyword{in}\ SML\isanewline
haftmann
parents:
diff changeset
   132
\ \ \isakeyword{module{\isacharunderscore}name}\ Example\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}example{\isachardot}ML{\isachardoublequoteclose}%
28564
haftmann
parents: 28447
diff changeset
   133
\endisatagquote
haftmann
parents: 28447
diff changeset
   134
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   135
%
28564
haftmann
parents: 28447
diff changeset
   136
\isadelimquote
28447
haftmann
parents:
diff changeset
   137
%
28564
haftmann
parents: 28447
diff changeset
   138
\endisadelimquote
28447
haftmann
parents:
diff changeset
   139
%
haftmann
parents:
diff changeset
   140
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   141
\noindent resulting in the following code:%
haftmann
parents:
diff changeset
   142
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   143
\isamarkuptrue%
haftmann
parents:
diff changeset
   144
%
28564
haftmann
parents: 28447
diff changeset
   145
\isadelimquote
28447
haftmann
parents:
diff changeset
   146
%
28564
haftmann
parents: 28447
diff changeset
   147
\endisadelimquote
28447
haftmann
parents:
diff changeset
   148
%
28564
haftmann
parents: 28447
diff changeset
   149
\isatagquote
28447
haftmann
parents:
diff changeset
   150
%
haftmann
parents:
diff changeset
   151
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   152
\isaverbatim%
haftmann
parents:
diff changeset
   153
\noindent%
haftmann
parents:
diff changeset
   154
\verb|structure Example = |\newline%
haftmann
parents:
diff changeset
   155
\verb|struct|\newline%
haftmann
parents:
diff changeset
   156
\newline%
haftmann
parents:
diff changeset
   157
\verb|fun foldl f a [] = a|\newline%
haftmann
parents:
diff changeset
   158
\verb|  |\verb,|,\verb| foldl f a (x :: xs) = foldl f (f a x) xs;|\newline%
haftmann
parents:
diff changeset
   159
\newline%
haftmann
parents:
diff changeset
   160
\verb|fun rev xs = foldl (fn xsa => fn x => x :: xsa) [] xs;|\newline%
haftmann
parents:
diff changeset
   161
\newline%
haftmann
parents:
diff changeset
   162
\verb|fun list_case f1 f2 (a :: lista) = f2 a lista|\newline%
haftmann
parents:
diff changeset
   163
\verb|  |\verb,|,\verb| list_case f1 f2 [] = f1;|\newline%
haftmann
parents:
diff changeset
   164
\newline%
haftmann
parents:
diff changeset
   165
\verb|datatype 'a queue = Queue of 'a list * 'a list;|\newline%
haftmann
parents:
diff changeset
   166
\newline%
haftmann
parents:
diff changeset
   167
\verb|val empty : 'a queue = Queue ([], []);|\newline%
haftmann
parents:
diff changeset
   168
\newline%
haftmann
parents:
diff changeset
   169
\verb|fun dequeue (Queue ([], [])) = (NONE, Queue ([], []))|\newline%
haftmann
parents:
diff changeset
   170
\verb|  |\verb,|,\verb| dequeue (Queue (xs, y :: ys)) = (SOME y, Queue (xs, ys))|\newline%
haftmann
parents:
diff changeset
   171
\verb|  |\verb,|,\verb| dequeue (Queue (v :: va, [])) =|\newline%
haftmann
parents:
diff changeset
   172
\verb|    let|\newline%
haftmann
parents:
diff changeset
   173
\verb|      val y :: ys = rev (v :: va);|\newline%
haftmann
parents:
diff changeset
   174
\verb|    in|\newline%
haftmann
parents:
diff changeset
   175
\verb|      (SOME y, Queue ([], ys))|\newline%
haftmann
parents:
diff changeset
   176
\verb|    end;|\newline%
haftmann
parents:
diff changeset
   177
\newline%
haftmann
parents:
diff changeset
   178
\verb|fun enqueue x (Queue (xs, ys)) = Queue (x :: xs, ys);|\newline%
haftmann
parents:
diff changeset
   179
\newline%
haftmann
parents:
diff changeset
   180
\verb|end; (*struct Example*)|%
haftmann
parents:
diff changeset
   181
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   182
\isamarkuptrue%
haftmann
parents:
diff changeset
   183
%
28564
haftmann
parents: 28447
diff changeset
   184
\endisatagquote
haftmann
parents: 28447
diff changeset
   185
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   186
%
28564
haftmann
parents: 28447
diff changeset
   187
\isadelimquote
28447
haftmann
parents:
diff changeset
   188
%
28564
haftmann
parents: 28447
diff changeset
   189
\endisadelimquote
28447
haftmann
parents:
diff changeset
   190
%
haftmann
parents:
diff changeset
   191
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   192
\noindent The \hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}} command takes a space-separated list of
haftmann
parents:
diff changeset
   193
  constants for which code shall be generated;  anything else needed for those
haftmann
parents:
diff changeset
   194
  is added implicitly.  Then follows a target language identifier
haftmann
parents:
diff changeset
   195
  (\isa{SML}, \isa{OCaml} or \isa{Haskell}) and a freely chosen module name.
haftmann
parents:
diff changeset
   196
  A file name denotes the destination to store the generated code.  Note that
haftmann
parents:
diff changeset
   197
  the semantics of the destination depends on the target language:  for
haftmann
parents:
diff changeset
   198
  \isa{SML} and \isa{OCaml} it denotes a \emph{file}, for \isa{Haskell}
haftmann
parents:
diff changeset
   199
  it denotes a \emph{directory} where a file named as the module name
haftmann
parents:
diff changeset
   200
  (with extension \isa{{\isachardot}hs}) is written:%
haftmann
parents:
diff changeset
   201
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   202
\isamarkuptrue%
haftmann
parents:
diff changeset
   203
%
28564
haftmann
parents: 28447
diff changeset
   204
\isadelimquote
28447
haftmann
parents:
diff changeset
   205
%
28564
haftmann
parents: 28447
diff changeset
   206
\endisadelimquote
28447
haftmann
parents:
diff changeset
   207
%
28564
haftmann
parents: 28447
diff changeset
   208
\isatagquote
28447
haftmann
parents:
diff changeset
   209
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann
parents:
diff changeset
   210
\ empty\ dequeue\ enqueue\ \isakeyword{in}\ Haskell\isanewline
haftmann
parents:
diff changeset
   211
\ \ \isakeyword{module{\isacharunderscore}name}\ Example\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}%
28564
haftmann
parents: 28447
diff changeset
   212
\endisatagquote
haftmann
parents: 28447
diff changeset
   213
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   214
%
28564
haftmann
parents: 28447
diff changeset
   215
\isadelimquote
28447
haftmann
parents:
diff changeset
   216
%
28564
haftmann
parents: 28447
diff changeset
   217
\endisadelimquote
28447
haftmann
parents:
diff changeset
   218
%
haftmann
parents:
diff changeset
   219
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   220
\noindent This is how the corresponding code in \isa{Haskell} looks like:%
haftmann
parents:
diff changeset
   221
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   222
\isamarkuptrue%
haftmann
parents:
diff changeset
   223
%
28564
haftmann
parents: 28447
diff changeset
   224
\isadelimquote
28447
haftmann
parents:
diff changeset
   225
%
28564
haftmann
parents: 28447
diff changeset
   226
\endisadelimquote
28447
haftmann
parents:
diff changeset
   227
%
28564
haftmann
parents: 28447
diff changeset
   228
\isatagquote
28447
haftmann
parents:
diff changeset
   229
%
haftmann
parents:
diff changeset
   230
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   231
\isaverbatim%
haftmann
parents:
diff changeset
   232
\noindent%
haftmann
parents:
diff changeset
   233
\verb|module Example where {|\newline%
haftmann
parents:
diff changeset
   234
\newline%
haftmann
parents:
diff changeset
   235
\newline%
haftmann
parents:
diff changeset
   236
\verb|foldla :: forall a b. (a -> b -> a) -> a -> [b] -> a;|\newline%
haftmann
parents:
diff changeset
   237
\verb|foldla f a [] = a;|\newline%
haftmann
parents:
diff changeset
   238
\verb|foldla f a (x : xs) = foldla f (f a x) xs;|\newline%
haftmann
parents:
diff changeset
   239
\newline%
haftmann
parents:
diff changeset
   240
\verb|rev :: forall a. [a] -> [a];|\newline%
haftmann
parents:
diff changeset
   241
\verb|rev xs = foldla (\ xsa x -> x : xsa) [] xs;|\newline%
haftmann
parents:
diff changeset
   242
\newline%
haftmann
parents:
diff changeset
   243
\verb|list_case :: forall t a. t -> (a -> [a] -> t) -> [a] -> t;|\newline%
haftmann
parents:
diff changeset
   244
\verb|list_case f1 f2 (a : list) = f2 a list;|\newline%
haftmann
parents:
diff changeset
   245
\verb|list_case f1 f2 [] = f1;|\newline%
haftmann
parents:
diff changeset
   246
\newline%
haftmann
parents:
diff changeset
   247
\verb|data Queue a = Queue [a] [a];|\newline%
haftmann
parents:
diff changeset
   248
\newline%
haftmann
parents:
diff changeset
   249
\verb|empty :: forall a. Queue a;|\newline%
haftmann
parents:
diff changeset
   250
\verb|empty = Queue [] [];|\newline%
haftmann
parents:
diff changeset
   251
\newline%
haftmann
parents:
diff changeset
   252
\verb|dequeue :: forall a. Queue a -> (Maybe a, Queue a);|\newline%
haftmann
parents:
diff changeset
   253
\verb|dequeue (Queue [] []) = (Nothing, Queue [] []);|\newline%
haftmann
parents:
diff changeset
   254
\verb|dequeue (Queue xs (y : ys)) = (Just y, Queue xs ys);|\newline%
haftmann
parents:
diff changeset
   255
\verb|dequeue (Queue (v : va) []) =|\newline%
haftmann
parents:
diff changeset
   256
\verb|  let {|\newline%
haftmann
parents:
diff changeset
   257
\verb|    (y : ys) = rev (v : va);|\newline%
haftmann
parents:
diff changeset
   258
\verb|  } in (Just y, Queue [] ys);|\newline%
haftmann
parents:
diff changeset
   259
\newline%
haftmann
parents:
diff changeset
   260
\verb|enqueue :: forall a. a -> Queue a -> Queue a;|\newline%
haftmann
parents:
diff changeset
   261
\verb|enqueue x (Queue xs ys) = Queue (x : xs) ys;|\newline%
haftmann
parents:
diff changeset
   262
\newline%
haftmann
parents:
diff changeset
   263
\verb|}|%
haftmann
parents:
diff changeset
   264
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   265
\isamarkuptrue%
haftmann
parents:
diff changeset
   266
%
28564
haftmann
parents: 28447
diff changeset
   267
\endisatagquote
haftmann
parents: 28447
diff changeset
   268
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   269
%
28564
haftmann
parents: 28447
diff changeset
   270
\isadelimquote
28447
haftmann
parents:
diff changeset
   271
%
28564
haftmann
parents: 28447
diff changeset
   272
\endisadelimquote
28447
haftmann
parents:
diff changeset
   273
%
haftmann
parents:
diff changeset
   274
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   275
\noindent This demonstrates the basic usage of the \hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}} command;
haftmann
parents:
diff changeset
   276
  for more details see \secref{sec:further}.%
haftmann
parents:
diff changeset
   277
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   278
\isamarkuptrue%
haftmann
parents:
diff changeset
   279
%
haftmann
parents:
diff changeset
   280
\isamarkupsubsection{Code generator architecture \label{sec:concept}%
haftmann
parents:
diff changeset
   281
}
haftmann
parents:
diff changeset
   282
\isamarkuptrue%
haftmann
parents:
diff changeset
   283
%
haftmann
parents:
diff changeset
   284
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   285
What you have seen so far should be already enough in a lot of cases.  If you
haftmann
parents:
diff changeset
   286
  are content with this, you can quit reading here.  Anyway, in order to customise
haftmann
parents:
diff changeset
   287
  and adapt the code generator, it is inevitable to gain some understanding
haftmann
parents:
diff changeset
   288
  how it works.
haftmann
parents:
diff changeset
   289
haftmann
parents:
diff changeset
   290
  \begin{figure}[h]
haftmann
parents:
diff changeset
   291
    \centering
haftmann
parents:
diff changeset
   292
    \includegraphics[width=0.7\textwidth]{codegen_process}
haftmann
parents:
diff changeset
   293
    \caption{Code generator architecture}
haftmann
parents:
diff changeset
   294
    \label{fig:arch}
haftmann
parents:
diff changeset
   295
  \end{figure}
haftmann
parents:
diff changeset
   296
haftmann
parents:
diff changeset
   297
  The code generator employs a notion of executability
haftmann
parents:
diff changeset
   298
  for three foundational executable ingredients known
haftmann
parents:
diff changeset
   299
  from functional programming:
haftmann
parents:
diff changeset
   300
  \emph{defining equations}, \emph{datatypes}, and
haftmann
parents:
diff changeset
   301
  \emph{type classes}.  A defining equation as a first approximation
haftmann
parents:
diff changeset
   302
  is a theorem of the form \isa{f\ t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n\ {\isasymequiv}\ t}
haftmann
parents:
diff changeset
   303
  (an equation headed by a constant \isa{f} with arguments
haftmann
parents:
diff changeset
   304
  \isa{t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n} and right hand side \isa{t}).
haftmann
parents:
diff changeset
   305
  Code generation aims to turn defining equations
haftmann
parents:
diff changeset
   306
  into a functional program.  This is achieved by three major
haftmann
parents:
diff changeset
   307
  components which operate sequentially, i.e. the result of one is
haftmann
parents:
diff changeset
   308
  the input
haftmann
parents:
diff changeset
   309
  of the next in the chain,  see diagram \ref{fig:arch}:
haftmann
parents:
diff changeset
   310
haftmann
parents:
diff changeset
   311
  \begin{itemize}
haftmann
parents:
diff changeset
   312
haftmann
parents:
diff changeset
   313
    \item Out of the vast collection of theorems proven in a
haftmann
parents:
diff changeset
   314
      \qn{theory}, a reasonable subset modelling
haftmann
parents:
diff changeset
   315
      defining equations is \qn{selected}.
haftmann
parents:
diff changeset
   316
haftmann
parents:
diff changeset
   317
    \item On those selected theorems, certain
haftmann
parents:
diff changeset
   318
      transformations are carried out
haftmann
parents:
diff changeset
   319
      (\qn{preprocessing}).  Their purpose is to turn theorems
haftmann
parents:
diff changeset
   320
      representing non- or badly executable
haftmann
parents:
diff changeset
   321
      specifications into equivalent but executable counterparts.
haftmann
parents:
diff changeset
   322
      The result is a structured collection of \qn{code theorems}.
haftmann
parents:
diff changeset
   323
haftmann
parents:
diff changeset
   324
    \item Before the selected defining equations are continued with,
haftmann
parents:
diff changeset
   325
      they can be \qn{preprocessed}, i.e. subjected to theorem
haftmann
parents:
diff changeset
   326
      transformations.  This \qn{preprocessor} is an interface which
haftmann
parents:
diff changeset
   327
      allows to apply
haftmann
parents:
diff changeset
   328
      the full expressiveness of ML-based theorem transformations
haftmann
parents:
diff changeset
   329
      to code generation;  motivating examples are shown below, see
haftmann
parents:
diff changeset
   330
      \secref{sec:preproc}.
haftmann
parents:
diff changeset
   331
      The result of the preprocessing step is a structured collection
haftmann
parents:
diff changeset
   332
      of defining equations.
haftmann
parents:
diff changeset
   333
haftmann
parents:
diff changeset
   334
    \item These defining equations are \qn{translated} to a program
haftmann
parents:
diff changeset
   335
      in an abstract intermediate language.  Think of it as a kind
haftmann
parents:
diff changeset
   336
      of \qt{Mini-Haskell} with four \qn{statements}: \isa{data}
haftmann
parents:
diff changeset
   337
      (for datatypes), \isa{fun} (stemming from defining equations),
haftmann
parents:
diff changeset
   338
      also \isa{class} and \isa{inst} (for type classes).
haftmann
parents:
diff changeset
   339
haftmann
parents:
diff changeset
   340
    \item Finally, the abstract program is \qn{serialised} into concrete
haftmann
parents:
diff changeset
   341
      source code of a target language.
haftmann
parents:
diff changeset
   342
haftmann
parents:
diff changeset
   343
  \end{itemize}
haftmann
parents:
diff changeset
   344
haftmann
parents:
diff changeset
   345
  \noindent From these steps, only the two last are carried out outside the logic;  by
haftmann
parents:
diff changeset
   346
  keeping this layer as thin as possible, the amount of code to trust is
haftmann
parents:
diff changeset
   347
  kept to a minimum.%
haftmann
parents:
diff changeset
   348
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   349
\isamarkuptrue%
haftmann
parents:
diff changeset
   350
%
haftmann
parents:
diff changeset
   351
\isadelimtheory
haftmann
parents:
diff changeset
   352
%
haftmann
parents:
diff changeset
   353
\endisadelimtheory
haftmann
parents:
diff changeset
   354
%
haftmann
parents:
diff changeset
   355
\isatagtheory
haftmann
parents:
diff changeset
   356
\isacommand{end}\isamarkupfalse%
haftmann
parents:
diff changeset
   357
%
haftmann
parents:
diff changeset
   358
\endisatagtheory
haftmann
parents:
diff changeset
   359
{\isafoldtheory}%
haftmann
parents:
diff changeset
   360
%
haftmann
parents:
diff changeset
   361
\isadelimtheory
haftmann
parents:
diff changeset
   362
%
haftmann
parents:
diff changeset
   363
\endisadelimtheory
haftmann
parents:
diff changeset
   364
\isanewline
haftmann
parents:
diff changeset
   365
\end{isabellebody}%
haftmann
parents:
diff changeset
   366
%%% Local Variables:
haftmann
parents:
diff changeset
   367
%%% mode: latex
haftmann
parents:
diff changeset
   368
%%% TeX-master: "root"
haftmann
parents:
diff changeset
   369
%%% End: