doc-src/Codegen/Thy/document/Introduction.tex
author haftmann
Wed, 23 Dec 2009 11:32:40 +0100
changeset 34179 5490151d1052
parent 34155 14aaccb399b3
child 37428 b3d94253e7f2
permissions -rw-r--r--
updated generated document sources
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
\isamarkupsection{Introduction and Overview%
haftmann
parents:
diff changeset
    22
}
haftmann
parents:
diff changeset
    23
\isamarkuptrue%
haftmann
parents:
diff changeset
    24
%
haftmann
parents:
diff changeset
    25
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
    26
This tutorial introduces a generic code generator for the
haftmann
parents:
diff changeset
    27
  \isa{Isabelle} system.
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    28
  The
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    29
  \qn{target language} for which code is
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    30
  generated is not fixed, but may be one of several
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    31
  functional programming languages (currently, the implementation
28447
haftmann
parents:
diff changeset
    32
  supports \isa{SML} \cite{SML}, \isa{OCaml} \cite{OCaml} and \isa{Haskell}
haftmann
parents:
diff changeset
    33
  \cite{haskell-revised-report}).
haftmann
parents:
diff changeset
    34
haftmann
parents:
diff changeset
    35
  Conceptually the code generator framework is part
haftmann
parents:
diff changeset
    36
  of Isabelle's \hyperlink{theory.Pure}{\mbox{\isa{Pure}}} meta logic framework; the logic
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    37
  \hyperlink{theory.HOL}{\mbox{\isa{HOL}}} \cite{isa-tutorial}, which is an extension of \hyperlink{theory.Pure}{\mbox{\isa{Pure}}},
28447
haftmann
parents:
diff changeset
    38
  already comes with a reasonable framework setup and thus provides
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    39
  a good basis for creating code-generation-driven
28447
haftmann
parents:
diff changeset
    40
  applications.  So, we assume some familiarity and experience
haftmann
parents:
diff changeset
    41
  with the ingredients of the \hyperlink{theory.HOL}{\mbox{\isa{HOL}}} distribution theories.
haftmann
parents:
diff changeset
    42
haftmann
parents:
diff changeset
    43
  The code generator aims to be usable with no further ado
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    44
  in most cases, while allowing for detailed customisation.
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    45
  This can be seen in the structure of this tutorial: after a short
28447
haftmann
parents:
diff changeset
    46
  conceptual introduction with an example (\secref{sec:intro}),
haftmann
parents:
diff changeset
    47
  we discuss the generic customisation facilities (\secref{sec:program}).
31050
555b56b66fcf adaptation replaces adaption
haftmann
parents: 30882
diff changeset
    48
  A further section (\secref{sec:adaptation}) is dedicated to the matter of
555b56b66fcf adaptation replaces adaption
haftmann
parents: 30882
diff changeset
    49
  \qn{adaptation} to specific target language environments.  After some
28447
haftmann
parents:
diff changeset
    50
  further issues (\secref{sec:further}) we conclude with an overview
haftmann
parents:
diff changeset
    51
  of some ML programming interfaces (\secref{sec:ml}).
haftmann
parents:
diff changeset
    52
haftmann
parents:
diff changeset
    53
  \begin{warn}
haftmann
parents:
diff changeset
    54
    Ultimately, the code generator which this tutorial deals with
haftmann
parents:
diff changeset
    55
    is supposed to replace the existing code generator
haftmann
parents:
diff changeset
    56
    by Stefan Berghofer \cite{Berghofer-Nipkow:2002}.
haftmann
parents:
diff changeset
    57
    So, for the moment, there are two distinct code generators
haftmann
parents:
diff changeset
    58
    in Isabelle.  In case of ambiguity, we will refer to the framework
haftmann
parents:
diff changeset
    59
    described here as \isa{generic\ code\ generator}, to the
haftmann
parents:
diff changeset
    60
    other as \isa{SML\ code\ generator}.
haftmann
parents:
diff changeset
    61
    Also note that while the framework itself is
haftmann
parents:
diff changeset
    62
    object-logic independent, only \hyperlink{theory.HOL}{\mbox{\isa{HOL}}} provides a reasonable
haftmann
parents:
diff changeset
    63
    framework setup.    
haftmann
parents:
diff changeset
    64
  \end{warn}%
haftmann
parents:
diff changeset
    65
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    66
\isamarkuptrue%
haftmann
parents:
diff changeset
    67
%
haftmann
parents:
diff changeset
    68
\isamarkupsubsection{Code generation via shallow embedding \label{sec:intro}%
haftmann
parents:
diff changeset
    69
}
haftmann
parents:
diff changeset
    70
\isamarkuptrue%
haftmann
parents:
diff changeset
    71
%
haftmann
parents:
diff changeset
    72
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
    73
The key concept for understanding \isa{Isabelle}'s code generation is
haftmann
parents:
diff changeset
    74
  \emph{shallow embedding}, i.e.~logical entities like constants, types and
haftmann
parents:
diff changeset
    75
  classes are identified with corresponding concepts in the target language.
haftmann
parents:
diff changeset
    76
haftmann
parents:
diff changeset
    77
  Inside \hyperlink{theory.HOL}{\mbox{\isa{HOL}}}, the \hyperlink{command.datatype}{\mbox{\isa{\isacommand{datatype}}}} and
haftmann
parents:
diff changeset
    78
  \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
    79
  the core of a functional programming language.  The default code generator setup
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
    80
   transforms those into functional programs immediately.
28447
haftmann
parents:
diff changeset
    81
  This means that \qt{naive} code generation can proceed without further ado.
haftmann
parents:
diff changeset
    82
  For example, here a simple \qt{implementation} of amortised queues:%
haftmann
parents:
diff changeset
    83
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    84
\isamarkuptrue%
haftmann
parents:
diff changeset
    85
%
28564
haftmann
parents: 28447
diff changeset
    86
\isadelimquote
28447
haftmann
parents:
diff changeset
    87
%
28564
haftmann
parents: 28447
diff changeset
    88
\endisadelimquote
28447
haftmann
parents:
diff changeset
    89
%
28564
haftmann
parents: 28447
diff changeset
    90
\isatagquote
28447
haftmann
parents:
diff changeset
    91
\isacommand{datatype}\isamarkupfalse%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    92
\ {\isacharprime}a\ queue\ {\isacharequal}\ AQueue\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
28447
haftmann
parents:
diff changeset
    93
\isanewline
haftmann
parents:
diff changeset
    94
\isacommand{definition}\isamarkupfalse%
haftmann
parents:
diff changeset
    95
\ empty\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    96
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
28447
haftmann
parents:
diff changeset
    97
\isanewline
haftmann
parents:
diff changeset
    98
\isacommand{primrec}\isamarkupfalse%
haftmann
parents:
diff changeset
    99
\ enqueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   100
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys{\isachardoublequoteclose}\isanewline
28447
haftmann
parents:
diff changeset
   101
\isanewline
haftmann
parents:
diff changeset
   102
\isacommand{fun}\isamarkupfalse%
haftmann
parents:
diff changeset
   103
\ dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ option\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   104
\ \ \ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   105
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   106
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   107
\ \ \ \ \ \ {\isacharparenleft}case\ rev\ xs\ of\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
28564
haftmann
parents: 28447
diff changeset
   108
\endisatagquote
haftmann
parents: 28447
diff changeset
   109
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   110
%
28564
haftmann
parents: 28447
diff changeset
   111
\isadelimquote
28447
haftmann
parents:
diff changeset
   112
%
28564
haftmann
parents: 28447
diff changeset
   113
\endisadelimquote
28447
haftmann
parents:
diff changeset
   114
%
haftmann
parents:
diff changeset
   115
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   116
\noindent Then we can generate code e.g.~for \isa{SML} as follows:%
haftmann
parents:
diff changeset
   117
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   118
\isamarkuptrue%
haftmann
parents:
diff changeset
   119
%
28564
haftmann
parents: 28447
diff changeset
   120
\isadelimquote
28447
haftmann
parents:
diff changeset
   121
%
28564
haftmann
parents: 28447
diff changeset
   122
\endisadelimquote
28447
haftmann
parents:
diff changeset
   123
%
28564
haftmann
parents: 28447
diff changeset
   124
\isatagquote
28447
haftmann
parents:
diff changeset
   125
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann
parents:
diff changeset
   126
\ empty\ dequeue\ enqueue\ \isakeyword{in}\ SML\isanewline
haftmann
parents:
diff changeset
   127
\ \ \isakeyword{module{\isacharunderscore}name}\ Example\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}example{\isachardot}ML{\isachardoublequoteclose}%
28564
haftmann
parents: 28447
diff changeset
   128
\endisatagquote
haftmann
parents: 28447
diff changeset
   129
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   130
%
28564
haftmann
parents: 28447
diff changeset
   131
\isadelimquote
28447
haftmann
parents:
diff changeset
   132
%
28564
haftmann
parents: 28447
diff changeset
   133
\endisadelimquote
28447
haftmann
parents:
diff changeset
   134
%
haftmann
parents:
diff changeset
   135
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   136
\noindent resulting in the following code:%
haftmann
parents:
diff changeset
   137
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   138
\isamarkuptrue%
haftmann
parents:
diff changeset
   139
%
28564
haftmann
parents: 28447
diff changeset
   140
\isadelimquote
28447
haftmann
parents:
diff changeset
   141
%
28564
haftmann
parents: 28447
diff changeset
   142
\endisadelimquote
28447
haftmann
parents:
diff changeset
   143
%
28564
haftmann
parents: 28447
diff changeset
   144
\isatagquote
28447
haftmann
parents:
diff changeset
   145
%
haftmann
parents:
diff changeset
   146
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   147
\isatypewriter%
28447
haftmann
parents:
diff changeset
   148
\noindent%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   149
\hspace*{0pt}structure Example :~sig\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   150
\hspace*{0pt} ~val foldl :~('a -> 'b -> 'a) -> 'a -> 'b list -> 'a\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   151
\hspace*{0pt} ~val rev :~'a list -> 'a list\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   152
\hspace*{0pt} ~val list{\char95}case :~'a -> ('b -> 'b list -> 'a) -> 'b list -> 'a\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   153
\hspace*{0pt} ~datatype 'a queue = AQueue of 'a list * 'a list\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   154
\hspace*{0pt} ~val empty :~'a queue\\
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   155
\hspace*{0pt} ~val dequeue :~'a queue -> 'a option * 'a queue\\
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   156
\hspace*{0pt} ~val enqueue :~'a -> 'a queue -> 'a queue\\
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   157
\hspace*{0pt}end = struct\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   158
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   159
\hspace*{0pt}fun foldl f a [] = a\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   160
\hspace*{0pt} ~| foldl f a (x ::~xs) = foldl f (f a x) xs;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   161
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   162
\hspace*{0pt}fun rev xs = foldl (fn xsa => fn x => x ::~xsa) [] xs;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   163
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   164
\hspace*{0pt}fun list{\char95}case f1 f2 (a ::~lista) = f2 a lista\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   165
\hspace*{0pt} ~| list{\char95}case f1 f2 [] = f1;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   166
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   167
\hspace*{0pt}datatype 'a queue = AQueue of 'a list * 'a list;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   168
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   169
\hspace*{0pt}val empty :~'a queue = AQueue ([],~[]);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   170
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   171
\hspace*{0pt}fun dequeue (AQueue ([],~[])) = (NONE,~AQueue ([],~[]))\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   172
\hspace*{0pt} ~| dequeue (AQueue (xs,~y ::~ys)) = (SOME y,~AQueue (xs,~ys))\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   173
\hspace*{0pt} ~| dequeue (AQueue (v ::~va,~[])) =\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   174
\hspace*{0pt} ~~~let\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   175
\hspace*{0pt} ~~~~~val y ::~ys = rev (v ::~va);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   176
\hspace*{0pt} ~~~in\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   177
\hspace*{0pt} ~~~~~(SOME y,~AQueue ([],~ys))\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   178
\hspace*{0pt} ~~~end;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   179
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   180
\hspace*{0pt}fun enqueue x (AQueue (xs,~ys)) = AQueue (x ::~xs,~ys);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   181
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   182
\hspace*{0pt}end;~(*struct Example*)%
28447
haftmann
parents:
diff changeset
   183
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   184
\isamarkuptrue%
haftmann
parents:
diff changeset
   185
%
28564
haftmann
parents: 28447
diff changeset
   186
\endisatagquote
haftmann
parents: 28447
diff changeset
   187
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   188
%
28564
haftmann
parents: 28447
diff changeset
   189
\isadelimquote
28447
haftmann
parents:
diff changeset
   190
%
28564
haftmann
parents: 28447
diff changeset
   191
\endisadelimquote
28447
haftmann
parents:
diff changeset
   192
%
haftmann
parents:
diff changeset
   193
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   194
\noindent The \hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}} command takes a space-separated list of
haftmann
parents:
diff changeset
   195
  constants for which code shall be generated;  anything else needed for those
haftmann
parents:
diff changeset
   196
  is added implicitly.  Then follows a target language identifier
haftmann
parents:
diff changeset
   197
  (\isa{SML}, \isa{OCaml} or \isa{Haskell}) and a freely chosen module name.
haftmann
parents:
diff changeset
   198
  A file name denotes the destination to store the generated code.  Note that
haftmann
parents:
diff changeset
   199
  the semantics of the destination depends on the target language:  for
haftmann
parents:
diff changeset
   200
  \isa{SML} and \isa{OCaml} it denotes a \emph{file}, for \isa{Haskell}
haftmann
parents:
diff changeset
   201
  it denotes a \emph{directory} where a file named as the module name
haftmann
parents:
diff changeset
   202
  (with extension \isa{{\isachardot}hs}) is written:%
haftmann
parents:
diff changeset
   203
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   204
\isamarkuptrue%
haftmann
parents:
diff changeset
   205
%
28564
haftmann
parents: 28447
diff changeset
   206
\isadelimquote
28447
haftmann
parents:
diff changeset
   207
%
28564
haftmann
parents: 28447
diff changeset
   208
\endisadelimquote
28447
haftmann
parents:
diff changeset
   209
%
28564
haftmann
parents: 28447
diff changeset
   210
\isatagquote
28447
haftmann
parents:
diff changeset
   211
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann
parents:
diff changeset
   212
\ empty\ dequeue\ enqueue\ \isakeyword{in}\ Haskell\isanewline
haftmann
parents:
diff changeset
   213
\ \ \isakeyword{module{\isacharunderscore}name}\ Example\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}%
28564
haftmann
parents: 28447
diff changeset
   214
\endisatagquote
haftmann
parents: 28447
diff changeset
   215
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   216
%
28564
haftmann
parents: 28447
diff changeset
   217
\isadelimquote
28447
haftmann
parents:
diff changeset
   218
%
28564
haftmann
parents: 28447
diff changeset
   219
\endisadelimquote
28447
haftmann
parents:
diff changeset
   220
%
haftmann
parents:
diff changeset
   221
\begin{isamarkuptext}%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   222
\noindent This is the corresponding code in \isa{Haskell}:%
28447
haftmann
parents:
diff changeset
   223
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   224
\isamarkuptrue%
haftmann
parents:
diff changeset
   225
%
28564
haftmann
parents: 28447
diff changeset
   226
\isadelimquote
28447
haftmann
parents:
diff changeset
   227
%
28564
haftmann
parents: 28447
diff changeset
   228
\endisadelimquote
28447
haftmann
parents:
diff changeset
   229
%
28564
haftmann
parents: 28447
diff changeset
   230
\isatagquote
28447
haftmann
parents:
diff changeset
   231
%
haftmann
parents:
diff changeset
   232
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   233
\isatypewriter%
28447
haftmann
parents:
diff changeset
   234
\noindent%
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   235
\hspace*{0pt}module Example where {\char123}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   236
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   237
\hspace*{0pt}foldla ::~forall a b.~(a -> b -> a) -> a -> [b] -> a;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   238
\hspace*{0pt}foldla f a [] = a;\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   239
\hspace*{0pt}foldla f a (x :~xs) = foldla f (f a x) xs;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   240
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   241
\hspace*{0pt}rev ::~forall a.~[a] -> [a];\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   242
\hspace*{0pt}rev xs = foldla ({\char92}~xsa x -> x :~xsa) [] xs;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   243
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   244
\hspace*{0pt}list{\char95}case ::~forall a b.~a -> (b -> [b] -> a) -> [b] -> a;\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   245
\hspace*{0pt}list{\char95}case f1 f2 (a :~list) = f2 a list;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   246
\hspace*{0pt}list{\char95}case f1 f2 [] = f1;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   247
\hspace*{0pt}\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   248
\hspace*{0pt}data Queue a = AQueue [a] [a];\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   249
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   250
\hspace*{0pt}empty ::~forall a.~Queue a;\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   251
\hspace*{0pt}empty = AQueue [] [];\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   252
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   253
\hspace*{0pt}dequeue ::~forall a.~Queue a -> (Maybe a,~Queue a);\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   254
\hspace*{0pt}dequeue (AQueue [] []) = (Nothing,~AQueue [] []);\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   255
\hspace*{0pt}dequeue (AQueue xs (y :~ys)) = (Just y,~AQueue xs ys);\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   256
\hspace*{0pt}dequeue (AQueue (v :~va) []) =\\
31848
e5ab21d14974 updated generated document
haftmann
parents: 31544
diff changeset
   257
\hspace*{0pt} ~let {\char123}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   258
\hspace*{0pt} ~~~(y :~ys) = rev (v :~va);\\
31848
e5ab21d14974 updated generated document
haftmann
parents: 31544
diff changeset
   259
\hspace*{0pt} ~{\char125}~in (Just y,~AQueue [] ys);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   260
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   261
\hspace*{0pt}enqueue ::~forall a.~a -> Queue a -> Queue a;\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   262
\hspace*{0pt}enqueue x (AQueue xs ys) = AQueue (x :~xs) ys;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   263
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28636
diff changeset
   264
\hspace*{0pt}{\char125}%
28447
haftmann
parents:
diff changeset
   265
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   266
\isamarkuptrue%
haftmann
parents:
diff changeset
   267
%
28564
haftmann
parents: 28447
diff changeset
   268
\endisatagquote
haftmann
parents: 28447
diff changeset
   269
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   270
%
28564
haftmann
parents: 28447
diff changeset
   271
\isadelimquote
28447
haftmann
parents:
diff changeset
   272
%
28564
haftmann
parents: 28447
diff changeset
   273
\endisadelimquote
28447
haftmann
parents:
diff changeset
   274
%
haftmann
parents:
diff changeset
   275
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   276
\noindent This demonstrates the basic usage of the \hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}} command;
haftmann
parents:
diff changeset
   277
  for more details see \secref{sec:further}.%
haftmann
parents:
diff changeset
   278
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   279
\isamarkuptrue%
haftmann
parents:
diff changeset
   280
%
haftmann
parents:
diff changeset
   281
\isamarkupsubsection{Code generator architecture \label{sec:concept}%
haftmann
parents:
diff changeset
   282
}
haftmann
parents:
diff changeset
   283
\isamarkuptrue%
haftmann
parents:
diff changeset
   284
%
haftmann
parents:
diff changeset
   285
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   286
What you have seen so far should be already enough in a lot of cases.  If you
haftmann
parents:
diff changeset
   287
  are content with this, you can quit reading here.  Anyway, in order to customise
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   288
  and adapt the code generator, it is necessary to gain some understanding
28447
haftmann
parents:
diff changeset
   289
  how it works.
haftmann
parents:
diff changeset
   290
haftmann
parents:
diff changeset
   291
  \begin{figure}[h]
30882
d15725e84091 moved generated eps/pdf to main directory, for proper display in dvi;
wenzelm
parents: 30880
diff changeset
   292
    \includegraphics{architecture}
28447
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:
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   300
  \emph{code equations}, \emph{datatypes}, and
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   301
  \emph{type classes}.  A code equation as a first approximation
28447
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}).
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   305
  Code generation aims to turn code equations
28447
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
30880
257cbe43faa8 tuned manual
haftmann
parents: 30836
diff changeset
   309
  of the next in the chain,  see figure \ref{fig:arch}:
28447
haftmann
parents:
diff changeset
   310
haftmann
parents:
diff changeset
   311
  \begin{itemize}
haftmann
parents:
diff changeset
   312
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   313
    \item The starting point is a collection of raw code equations in a
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   314
      theory. It is not relevant where they
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   315
      stem from, but typically they were either produced by specification
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   316
      tools or proved explicitly by the user.
30836
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   317
      
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   318
    \item These raw code equations can be subjected to theorem transformations.  This
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   319
      \qn{preprocessor} can apply the full
30836
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   320
      expressiveness of ML-based theorem transformations to code
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33707
diff changeset
   321
      generation.  The result of preprocessing is a
30836
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   322
      structured collection of code equations.
28447
haftmann
parents:
diff changeset
   323
30836
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   324
    \item These code equations are \qn{translated} to a program in an
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   325
      abstract intermediate language.  Think of it as a kind
28447
haftmann
parents:
diff changeset
   326
      of \qt{Mini-Haskell} with four \qn{statements}: \isa{data}
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   327
      (for datatypes), \isa{fun} (stemming from code equations),
28447
haftmann
parents:
diff changeset
   328
      also \isa{class} and \isa{inst} (for type classes).
haftmann
parents:
diff changeset
   329
haftmann
parents:
diff changeset
   330
    \item Finally, the abstract program is \qn{serialised} into concrete
haftmann
parents:
diff changeset
   331
      source code of a target language.
30836
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   332
      This step only produces concrete syntax but does not change the
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   333
      program in essence; all conceptual transformations occur in the
1344132160bb proper external tikz pictures
haftmann
parents: 30227
diff changeset
   334
      translation step.
28447
haftmann
parents:
diff changeset
   335
haftmann
parents:
diff changeset
   336
  \end{itemize}
haftmann
parents:
diff changeset
   337
haftmann
parents:
diff changeset
   338
  \noindent From these steps, only the two last are carried out outside the logic;  by
haftmann
parents:
diff changeset
   339
  keeping this layer as thin as possible, the amount of code to trust is
haftmann
parents:
diff changeset
   340
  kept to a minimum.%
haftmann
parents:
diff changeset
   341
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   342
\isamarkuptrue%
haftmann
parents:
diff changeset
   343
%
haftmann
parents:
diff changeset
   344
\isadelimtheory
haftmann
parents:
diff changeset
   345
%
haftmann
parents:
diff changeset
   346
\endisadelimtheory
haftmann
parents:
diff changeset
   347
%
haftmann
parents:
diff changeset
   348
\isatagtheory
haftmann
parents:
diff changeset
   349
\isacommand{end}\isamarkupfalse%
haftmann
parents:
diff changeset
   350
%
haftmann
parents:
diff changeset
   351
\endisatagtheory
haftmann
parents:
diff changeset
   352
{\isafoldtheory}%
haftmann
parents:
diff changeset
   353
%
haftmann
parents:
diff changeset
   354
\isadelimtheory
haftmann
parents:
diff changeset
   355
%
haftmann
parents:
diff changeset
   356
\endisadelimtheory
haftmann
parents:
diff changeset
   357
\isanewline
haftmann
parents:
diff changeset
   358
\end{isabellebody}%
haftmann
parents:
diff changeset
   359
%%% Local Variables:
haftmann
parents:
diff changeset
   360
%%% mode: latex
haftmann
parents:
diff changeset
   361
%%% TeX-master: "root"
haftmann
parents:
diff changeset
   362
%%% End: