doc-src/IsarAdvanced/Codegen/Thy/Codegen.thy
author haftmann
Fri, 30 Mar 2007 16:18:59 +0200
changeset 22550 c5039bee2602
parent 22479 de15ea8fb348
child 22751 1bfd75c1f232
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
     1
(* $Id$ *)
9b9910b82645 initial draft
haftmann
parents:
diff changeset
     2
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
     3
(*<*)
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
     4
theory Codegen
9b9910b82645 initial draft
haftmann
parents:
diff changeset
     5
imports Main
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
     6
uses "../../../antiquote_setup.ML"
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
     7
begin
9b9910b82645 initial draft
haftmann
parents:
diff changeset
     8
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
     9
ML {*
22015
12b94d7f7e1f adaptions
haftmann
parents: 21993
diff changeset
    10
CodegenSerializer.code_width := 74;
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
    11
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
    12
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
    13
(*>*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
    14
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
    15
chapter {* Code generation from Isabelle theories *}
9b9910b82645 initial draft
haftmann
parents:
diff changeset
    16
9b9910b82645 initial draft
haftmann
parents:
diff changeset
    17
section {* Introduction *}
9b9910b82645 initial draft
haftmann
parents:
diff changeset
    18
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    19
subsection {* Motivation *}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    20
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
    21
text {*
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    22
  Executing formal specifications as programs is a well-established
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    23
  topic in the theorem proving community.  With increasing
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    24
  application of theorem proving systems in the area of
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    25
  software development and verification, its relevance manifests
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    26
  for running test cases and rapid prototyping.  In logical
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    27
  calculi like constructive type theory,
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    28
  a notion of executability is implicit due to the nature
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    29
  of the calculus.  In contrast, specifications in Isabelle
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    30
  can be highly non-executable.  In order to bridge
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    31
  the gap between logic and executable specifications,
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    32
  an explicit non-trivial transformation has to be applied:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    33
  code generation.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    34
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    35
  This tutorial introduces a generic code generator for the
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    36
  Isabelle system \cite{isa-tutorial}.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    37
  Generic in the sense that the
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    38
  \qn{target language} for which code shall ultimately be
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    39
  generated is not fixed but may be an arbitrary state-of-the-art
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    40
  functional programming language (currently, the implementation
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    41
  supports SML \cite{SML}, OCaml \cite{OCaml} and Haskell
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    42
  \cite{haskell-revised-report}).
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    43
  We aim to provide a
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    44
  versatile environment
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    45
  suitable for software development and verification,
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    46
  structuring the process
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    47
  of code generation into a small set of orthogonal principles
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    48
  while achieving a big coverage of application areas
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    49
  with maximum flexibility.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
    50
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    51
  Conceptually the code generator framework is part
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    52
  of Isabelle's @{text Pure} meta logic; the object logic
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    53
  @{text HOL} which is an extension of @{text Pure}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    54
  already comes with a reasonable framework setup and thus provides
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    55
  a good working horse for raising code-generation-driven
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    56
  applications.  So, we assume some familiarity and experience
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    57
  with the ingredients of the @{text HOL} \emph{Main} theory
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    58
  (see also \cite{isa-tutorial}).
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    59
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    60
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    61
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    62
subsection {* Overview *}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    63
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    64
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    65
  The code generator aims to be usable with no further ado
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    66
  in most cases while allowing for detailed customization.
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    67
  This manifests in the structure of this tutorial:
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    68
  we start with a generic example \secref{sec:example}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    69
  and introduce code generation concepts \secref{sec:concept}.
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    70
  Section
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
    71
  \secref{sec:basics} explains how to use the framework naively,
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    72
  presuming a reasonable default setup.  Then, section
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    73
  \secref{sec:advanced} deals with advanced topics,
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    74
  introducing further aspects of the code generator framework
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    75
  in a motivation-driven manner.  Last, section \secref{sec:ml}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    76
  introduces the framework's internal programming interfaces.
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
    77
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    78
  \begin{warn}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    79
    Ultimately, the code generator which this tutorial deals with
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    80
    is supposed to replace the already established code generator
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    81
    by Stefan Berghofer \cite{Berghofer-Nipkow:2002}.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
    82
    So, for the moment, there are two distinct code generators
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    83
    in Isabelle.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    84
    Also note that while the framework itself is largely
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    85
    object-logic independent, only @{text HOL} provides a reasonable
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    86
    framework setup.    
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    87
  \end{warn}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    88
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    89
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
    90
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    91
section {* An example: a simple theory of search trees \label{sec:example} *}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    92
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    93
text {*
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    94
  When writing executable specifications, it is convenient to use
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    95
  three existing packages: the datatype package for defining
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    96
  datatypes, the function package for (recursive) functions,
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    97
  and the class package for overloaded definitions.
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    98
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
    99
  We develope a small theory of search trees; trees are represented
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   100
  as a datatype with key type @{typ "'a"} and value type @{typ "'b"}:
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   101
*}
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   102
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   103
datatype ('a, 'b) searchtree = Leaf "'a\<Colon>linorder" 'b
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   104
  | Branch "('a, 'b) searchtree" "'a" "('a, 'b) searchtree"
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   105
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   106
text {*
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   107
  \noindent Note that we have constrained the type of keys
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   108
  to the class of total orders, @{text linorder}.
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   109
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   110
  We define @{text find} and @{text update} functions:
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   111
*}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   112
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   113
fun
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   114
  find :: "('a\<Colon>linorder, 'b) searchtree \<Rightarrow> 'a \<Rightarrow> 'b option" where
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   115
  "find (Leaf key val) it = (if it = key then Some val else None)"
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   116
  | "find (Branch t1 key t2) it = (if it \<le> key then find t1 it else find t2 it)"
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   117
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   118
fun
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   119
  update :: "'a\<Colon>linorder \<times> 'b \<Rightarrow> ('a, 'b) searchtree \<Rightarrow> ('a, 'b) searchtree" where
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   120
  "update (it, entry) (Leaf key val) = (
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   121
    if it = key then Leaf key entry
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   122
      else if it \<le> key
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   123
      then Branch (Leaf it entry) it (Leaf key val)
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   124
      else Branch (Leaf key val) it (Leaf it entry)
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   125
   )"
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   126
  | "update (it, entry) (Branch t1 key t2) = (
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   127
    if it \<le> key
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   128
      then (Branch (update (it, entry) t1) key t2)
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   129
      else (Branch t1 key (update (it, entry) t2))
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   130
   )"
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   131
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   132
text {*
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   133
  \noindent For testing purpose, we define a small example
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   134
  using natural numbers @{typ nat} (which are a @{text linorder})
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   135
  as keys and strings values:
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   136
*}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   137
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   138
fun
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   139
  example :: "nat \<Rightarrow> (nat, string) searchtree" where
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   140
  "example n = update (n, ''bar'') (Leaf 0 ''foo'')"
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   141
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   142
text {*
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   143
  \noindent Then we generate code
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   144
*}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   145
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   146
code_gen example (*<*)(SML #)(*>*)(SML "examples/tree.ML")
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   147
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   148
text {*
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   149
  \noindent which looks like:
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   150
  \lstsml{Thy/examples/tree.ML}
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   151
*}
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   152
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   153
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   154
section {* Code generation concepts and process \label{sec:concept} *}
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   155
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   156
text {*
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   157
  \begin{figure}[h]
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   158
  \centering
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   159
  \includegraphics[width=0.7\textwidth]{codegen_process}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   160
  \caption{code generator -- processing overview}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   161
  \label{fig:process}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   162
  \end{figure}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   163
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   164
  The code generator employs a notion of executability
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   165
  for three foundational executable ingredients known
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   166
  from functional programming:
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   167
  \emph{defining equations}, \emph{datatypes}, and
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   168
  \emph{type classes}. A defining equation as a first approximation
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   169
  is a theorem of the form @{text "f t\<^isub>1 t\<^isub>2 \<dots> t\<^isub>n \<equiv> t"}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   170
  (an equation headed by a constant @{text f} with arguments
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   171
  @{text "t\<^isub>1 t\<^isub>2 \<dots> t\<^isub>n"} and right hand side @{text t}.
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   172
  Code generation aims to turn defining equations
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   173
  into a functional program by running through
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   174
  a process (see figure \ref{fig:process}):
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   175
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   176
  \begin{itemize}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   177
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   178
    \item Out of the vast collection of theorems proven in a
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   179
      \qn{theory}, a reasonable subset modeling
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   180
      defining equations is \qn{selected}.
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   181
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   182
    \item On those selected theorems, certain
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   183
      transformations are carried out
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   184
      (\qn{preprocessing}).  Their purpose is to turn theorems
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   185
      representing non- or badly executable
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   186
      specifications into equivalent but executable counterparts.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   187
      The result is a structured collection of \qn{code theorems}.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   188
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
   189
    \item These \qn{code theorems} then are \qn{translated}
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   190
      into an Haskell-like intermediate
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   191
      language.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   192
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   193
    \item Finally, out of the intermediate language the final
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   194
      code in the desired \qn{target language} is \qn{serialized}.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   195
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   196
  \end{itemize}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   197
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   198
  From these steps, only the two last are carried out
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   199
  outside the logic; by keeping this layer as
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   200
  thin as possible, the amount of code to trust is
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   201
  kept to a minimum.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   202
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   203
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   204
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   205
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   206
section {* Basics \label{sec:basics} *}
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   207
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   208
subsection {* Invoking the code generator *}
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   209
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   210
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   211
  Thanks to a reasonable setup of the HOL theories, in
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   212
  most cases code generation proceeds without further ado:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   213
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   214
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   215
fun
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   216
  fac :: "nat \<Rightarrow> nat" where
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   217
    "fac 0 = 1"
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   218
  | "fac (Suc n) = Suc n * fac n"
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   219
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   220
text {*
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   221
  \noindent This executable specification is now turned to SML code:
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   222
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   223
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   224
code_gen fac (*<*)(SML #)(*>*)(SML "examples/fac.ML")
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   225
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   226
text {*
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   227
  \noindent  The @{text "\<CODEGEN>"} command takes a space-separated list of
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   228
  constants together with \qn{serialization directives}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   229
  in parentheses. These start with a \qn{target language}
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   230
  identifier, followed by arguments, their semantics
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   231
  depending on the target. In the SML case, a filename
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   232
  is given where to write the generated code to.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   233
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   234
  Internally, the defining equations for all selected
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   235
  constants are taken, including any transitively required
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   236
  constants, datatypes and classes, resulting in the following
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   237
  code:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   238
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   239
  \lstsml{Thy/examples/fac.ML}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   240
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   241
  The code generator will complain when a required
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   242
  ingredient does not provide a executable counterpart,
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   243
  e.g.~generating code
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   244
  for constants not yielding
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   245
  a defining equation (e.g.~the Hilbert choice
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   246
  operation @{text "SOME"}):
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   247
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   248
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   249
(*<*)
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   250
setup {* Sign.add_path "foo" *}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   251
(*>*)
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   252
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   253
definition
21993
4b802a9e0738 updated manual
haftmann
parents: 21545
diff changeset
   254
  pick_some :: "'a list \<Rightarrow> 'a" where
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   255
  "pick_some xs = (SOME x. x \<in> set xs)"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   256
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   257
(*<*)
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   258
hide const pick_some
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   259
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   260
setup {* Sign.parent_path *}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   261
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   262
definition
21993
4b802a9e0738 updated manual
haftmann
parents: 21545
diff changeset
   263
  pick_some :: "'a list \<Rightarrow> 'a" where
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   264
  "pick_some = hd"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   265
(*>*)
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   266
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   267
code_gen pick_some (SML "examples/fail_const.ML")
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   268
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   269
text {* \noindent will fail. *}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   270
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   271
subsection {* Theorem selection *}
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   272
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   273
text {*
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   274
  The list of all defining equations in a theory may be inspected
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   275
  using the @{text "\<PRINTCODESETUP>"} command:
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   276
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   277
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   278
print_codesetup
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   279
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   280
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   281
  \noindent which displays a table of constant with corresponding
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   282
  defining equations (the additional stuff displayed
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   283
  shall not bother us for the moment). If this table does
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   284
  not provide at least one defining
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   285
  equation for a particular constant, the table of primitive
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   286
  definitions is searched
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   287
  whether it provides one.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   288
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   289
  The typical HOL tools are already set up in a way that
21323
haftmann
parents: 21223
diff changeset
   290
  function definitions introduced by @{text "\<FUN>"},
haftmann
parents: 21223
diff changeset
   291
  @{text "\<FUNCTION>"}, @{text "\<PRIMREC>"}
haftmann
parents: 21223
diff changeset
   292
  @{text "\<RECDEF>"} are implicitly propagated
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   293
  to this defining equation table. Specific theorems may be
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   294
  selected using an attribute: \emph{code func}. As example,
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   295
  a weight selector function:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   296
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   297
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   298
consts
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   299
  pick :: "(nat \<times> 'a) list \<Rightarrow> nat \<Rightarrow> 'a"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   300
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   301
primrec
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   302
  "pick (x#xs) n = (let (k, v) = x in
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   303
    if n < k then v else pick xs (n - k))"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   304
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   305
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   306
  We want to eliminate the explicit destruction
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   307
  of @{term x} to @{term "(k, v)"}:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   308
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   309
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   310
lemma [code func]:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   311
  "pick ((k, v)#xs) n = (if n < k then v else pick xs (n - k))"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   312
  by simp
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   313
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   314
code_gen pick (*<*)(SML #)(*>*)(SML "examples/pick1.ML")
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   315
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   316
text {*
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   317
  This theorem is now added to the defining equation table:
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   318
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   319
  \lstsml{Thy/examples/pick1.ML}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   320
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   321
  It might be convenient to remove the pointless original
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   322
  equation, using the \emph{nofunc} attribute:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   323
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   324
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   325
lemmas [code nofunc] = pick.simps 
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   326
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   327
code_gen pick (*<*)(SML #)(*>*)(SML "examples/pick2.ML")
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   328
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   329
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   330
  \lstsml{Thy/examples/pick2.ML}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   331
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   332
  Syntactic redundancies are implicitly dropped. For example,
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   333
  using a modified version of the @{const fac} function
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   334
  as defining equation, the then redundant (since
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   335
  syntactically subsumed) original defining equations
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   336
  are dropped, resulting in a warning:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   337
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   338
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   339
lemma [code func]:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   340
  "fac n = (case n of 0 \<Rightarrow> 1 | Suc m \<Rightarrow> n * fac m)"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   341
  by (cases n) simp_all
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   342
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   343
code_gen fac (*<*)(SML #)(*>*)(SML "examples/fac_case.ML")
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   344
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   345
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   346
  \lstsml{Thy/examples/fac_case.ML}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   347
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   348
  \begin{warn}
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   349
    The attributes \emph{code} and \emph{code del}
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   350
    associated with the existing code generator also apply to
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   351
    the new one: \emph{code} implies \emph{code func},
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   352
    and \emph{code del} implies \emph{code nofunc}.
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   353
  \end{warn}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   354
*}
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   355
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   356
subsection {* Type classes *}
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   357
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   358
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   359
  Type classes enter the game via the Isar class package.
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   360
  For a short introduction how to use it, see \cite{isabelle-classes};
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   361
  here we just illustrate its impact on code generation.
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   362
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   363
  In a target language, type classes may be represented
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   364
  natively (as in the case of Haskell). For languages
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   365
  like SML, they are implemented using \emph{dictionaries}.
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   366
  Our following example specifies a class \qt{null},
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   367
  assigning to each of its inhabitants a \qt{null} value:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   368
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   369
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   370
class null = type +
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   371
  fixes null :: 'a
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   372
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   373
consts
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   374
  head :: "'a\<Colon>null list \<Rightarrow> 'a"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   375
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   376
primrec
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   377
  "head [] = null"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   378
  "head (x#xs) = x"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   379
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   380
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   381
  We provide some instances for our @{text null}:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   382
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   383
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   384
instance option :: (type) null
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   385
  "null \<equiv> None" ..
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   386
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   387
instance list :: (type) null
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   388
  "null \<equiv> []" ..
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   389
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   390
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   391
  Constructing a dummy example:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   392
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   393
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   394
definition
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   395
  "dummy = head [Some (Suc 0), None]"
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   396
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   397
text {*
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   398
  Type classes offer a suitable occasion to introduce
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   399
  the Haskell serializer.  Its usage is almost the same
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   400
  as SML, but, in accordance with conventions
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   401
  some Haskell systems enforce, each module ends
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   402
  up in a single file. The module hierarchy is reflected in
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   403
  the file system, with root given by the user.
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   404
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   405
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   406
code_gen dummy (Haskell "examples/")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   407
  (* NOTE: you may use Haskell only once in this document, otherwise
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   408
  you have to work in distinct subdirectories *)
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   409
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   410
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   411
  \lsthaskell{Thy/examples/Codegen.hs}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   412
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   413
  (we have left out all other modules).
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   414
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   415
  The whole code in SML with explicit dictionary passing:
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   416
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   417
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   418
code_gen dummy (*<*)(SML #)(*>*)(SML "examples/class.ML")
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   419
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   420
text {*
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   421
  \lstsml{Thy/examples/class.ML}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   422
*}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   423
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   424
text {*
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   425
  or in OCaml:
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   426
*}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   427
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   428
code_gen dummy (OCaml "examples/class.ocaml")
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   429
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   430
text {*
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   431
  \lstsml{Thy/examples/class.ocaml}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   432
*}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   433
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   434
subsection {* Incremental code generation *}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   435
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   436
text {*
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   437
  Code generation is \emph{incremental}: theorems
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   438
  and abstract intermediate code are cached and extended on demand.
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   439
  The cache may be partially or fully dropped if the underlying
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   440
  executable content of the theory changes.
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   441
  Implementation of caching is supposed to transparently
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   442
  hid away the details from the user.  Anyway, caching
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   443
  reaches the surface by using a slightly more general form
21323
haftmann
parents: 21223
diff changeset
   444
  of the @{text "\<CODEGEN>"}: either the list of constants or the
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   445
  list of serialization expressions may be dropped.  If no
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   446
  serialization expressions are given, only abstract code
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   447
  is generated and cached; if no constants are given, the
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   448
  current cache is serialized.
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   449
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   450
  For explorative purpose, the
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   451
  @{text "\<CODETHMS>"} command may prove useful:
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   452
*}
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   453
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   454
code_thms
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   455
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   456
text {*
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   457
  \noindent print all cached defining equations (i.e.~\emph{after}
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   458
  any applied transformation).  A
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   459
  list of constants may be given; their function
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   460
  equations are added to the cache if not already present.
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   461
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   462
  Similarly, the @{text "\<CODEDEPS>"} command shows a graph
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   463
  visualizing dependencies between defining equations.
21075
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   464
*}
d6742ff3b522 continued
haftmann
parents: 21058
diff changeset
   465
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   466
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
   467
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   468
section {* Recipes and advanced topics \label{sec:advanced} *}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   469
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   470
text {*
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   471
  In this tutorial, we do not attempt to give an exhaustive
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   472
  description of the code generator framework; instead,
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   473
  we cast a light on advanced topics by introducing
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   474
  them together with practically motivated examples.  Concerning
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   475
  further reading, see
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   476
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   477
  \begin{itemize}
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   478
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   479
  \item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   480
    for exhaustive syntax diagrams.
21222
6dfdb69e83ef adjusted title
haftmann
parents: 21217
diff changeset
   481
  \item or \fixme[ref] which deals with foundational issues
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   482
    of the code generator framework.
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   483
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   484
  \end{itemize}
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   485
*}
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   486
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   487
subsection {* Library theories *}
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   488
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   489
text {*
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   490
  The HOL \emph{Main} theory already provides a code generator setup
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   491
  which should be suitable for most applications. Common extensions
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   492
  and modifications are available by certain theories of the HOL
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   493
  library; beside being useful in applications, they may serve
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   494
  as a tutorial for customizing the code generator setup.
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   495
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   496
  \begin{description}
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   497
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   498
    \item[@{text "ExecutableSet"}] allows to generate code
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   499
       for finite sets using lists.
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   500
    \item[@{text "ExecutableRat"}] \label{exec_rat} implements rational
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   501
       numbers as triples @{text "(sign, enumerator, denominator)"}.
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   502
    \item[@{text "EfficientNat"}] \label{eff_nat} implements natural numbers by integers,
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   503
       which in general will result in higher efficency; pattern
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   504
       matching with @{const "0\<Colon>nat"} / @{const "Suc"}
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   505
       is eliminated.
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   506
    \item[@{text "MLString"}] provides an additional datatype @{text "mlstring"};
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   507
       in the HOL default setup, strings in HOL are mapped to list
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   508
       of chars in SML; values of type @{text "mlstring"} are
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   509
       mapped to strings in SML.
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   510
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   511
  \end{description}
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   512
*}
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   513
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   514
subsection {* Preprocessing *}
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   515
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   516
text {*
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   517
  Before selected function theorems are turned into abstract
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   518
  code, a chain of definitional transformation steps is carried
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   519
  out: \emph{preprocessing}. There are three possibilities
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   520
  to customize preprocessing: \emph{inline theorems},
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   521
  \emph{inline procedures} and \emph{generic preprocessors}.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   522
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   523
  \emph{Inline theorems} are rewriting rules applied to each
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   524
  defining equation.  Due to the interpretation of theorems
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   525
  of defining equations, rewrites are applied to the right
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   526
  hand side and the arguments of the left hand side of an
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   527
  equation, but never to the constant heading the left hand side.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   528
  Inline theorems may be declared an undeclared using the
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   529
  \emph{code inline} or \emph{code noinline} attribute respectively.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   530
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   531
  Some common applications:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   532
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   533
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   534
text_raw {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   535
  \begin{itemize}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   536
     \item replacing non-executable constructs by executable ones: \\
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   537
*}     
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   538
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   539
lemma [code inline]:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   540
  "x \<in> set xs \<longleftrightarrow> x mem xs" by (induct xs) simp_all
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   541
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   542
text_raw {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   543
     \item eliminating superfluous constants: \\
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   544
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   545
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   546
lemma [code inline]:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   547
  "1 = Suc 0" by simp
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   548
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   549
text_raw {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   550
     \item replacing executable but inconvenient constructs: \\
21089
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   551
*}
cf6defa31209 continued
haftmann
parents: 21075
diff changeset
   552
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   553
lemma [code inline]:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   554
  "xs = [] \<longleftrightarrow> List.null xs" by (induct xs) simp_all
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   555
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   556
text_raw {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   557
  \end{itemize}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   558
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   559
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   560
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   561
  The current set of inline theorems may be inspected using
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   562
  the @{text "\<PRINTCODESETUP>"} command.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   563
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   564
  \emph{Inline procedures} are a generalized version of inline
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   565
  theorems written in ML -- rewrite rules are generated dependent
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   566
  on the function theorems for a certain function.  One
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   567
  application is the implicit expanding of @{typ nat} numerals
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   568
  to @{const "0\<Colon>nat"} / @{const Suc} representation.  See further
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   569
  \secref{sec:ml}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   570
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   571
  \emph{Generic preprocessors} provide a most general interface,
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   572
  transforming a list of function theorems to another
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   573
  list of function theorems, provided that neither the heading
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   574
  constant nor its type change.  The @{const "0\<Colon>nat"} / @{const Suc}
21323
haftmann
parents: 21223
diff changeset
   575
  pattern elimination implemented in
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   576
  theory @{text "EfficientNat"} (\secref{eff_nat}) uses this
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   577
  interface.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   578
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   579
  \begin{warn}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   580
    The order in which single preprocessing steps are carried
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   581
    out currently is not specified; in particular, preprocessing
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   582
    is \emph{no} fix point process.  Keep this in mind when
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   583
    setting up the preprocessor.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   584
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   585
    Further, the attribute \emph{code unfold}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   586
    associated with the existing code generator also applies to
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   587
    the new one: \emph{code unfold} implies \emph{code inline}.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   588
  \end{warn}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   589
*}
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   590
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
   591
subsection {* Customizing serialization  *}
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
   592
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   593
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   594
  Consider the following function and its corresponding
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   595
  SML code:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   596
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   597
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   598
fun
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   599
  in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   600
  "in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   601
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   602
(*<*)
21323
haftmann
parents: 21223
diff changeset
   603
code_type %tt bool
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   604
  (SML)
21323
haftmann
parents: 21223
diff changeset
   605
code_const %tt True and False and "op \<and>" and Not
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   606
  (SML and and and)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   607
(*>*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   608
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   609
code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_literal.ML")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   610
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   611
text {*
21323
haftmann
parents: 21223
diff changeset
   612
  \lstsml{Thy/examples/bool_literal.ML}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   613
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   614
  Though this is correct code, it is a little bit unsatisfactory:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   615
  boolean values and operators are materialized as distinguished
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   616
  entities with have nothing to do with the SML-builtin notion
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   617
  of \qt{bool}.  This results in less readable code;
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   618
  additionally, eager evaluation may cause programs to
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   619
  loop or break which would perfectly terminate when
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   620
  the existing SML \qt{bool} would be used.  To map
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   621
  the HOL \qt{bool} on SML \qt{bool}, we may use
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   622
  \qn{custom serializations}:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   623
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   624
21323
haftmann
parents: 21223
diff changeset
   625
code_type %tt bool
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   626
  (SML "bool")
21323
haftmann
parents: 21223
diff changeset
   627
code_const %tt True and False and "op \<and>"
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   628
  (SML "true" and "false" and "_ andalso _")
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   629
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   630
text {*
21323
haftmann
parents: 21223
diff changeset
   631
  The @{text "\<CODETYPE>"} commad takes a type constructor
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   632
  as arguments together with a list of custom serializations.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   633
  Each custom serialization starts with a target language
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   634
  identifier followed by an expression, which during
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   635
  code serialization is inserted whenever the type constructor
21323
haftmann
parents: 21223
diff changeset
   636
  would occur.  For constants, @{text "\<CODECONST>"} implements
haftmann
parents: 21223
diff changeset
   637
  the corresponding mechanism.  Each ``@{verbatim "_"}'' in
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   638
  a serialization expression is treated as a placeholder
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   639
  for the type constructor's (the constant's) arguments.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   640
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   641
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   642
code_reserved SML
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   643
  bool true false
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   644
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   645
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   646
  To assert that the existing \qt{bool}, \qt{true} and \qt{false}
21323
haftmann
parents: 21223
diff changeset
   647
  is not used for generated code, we use @{text "\<CODERESERVED>"}.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   648
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   649
  After this setup, code looks quite more readable:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   650
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   651
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   652
code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_mlbool.ML")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   653
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   654
text {*
21323
haftmann
parents: 21223
diff changeset
   655
  \lstsml{Thy/examples/bool_mlbool.ML}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   656
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   657
  This still is not perfect: the parentheses
21323
haftmann
parents: 21223
diff changeset
   658
  around the \qt{andalso} expression are superfluous.
haftmann
parents: 21223
diff changeset
   659
  Though the serializer
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   660
  by no means attempts to imitate the rich Isabelle syntax
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   661
  framework, it provides some common idioms, notably
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   662
  associative infixes with precedences which may be used here:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   663
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   664
21323
haftmann
parents: 21223
diff changeset
   665
code_const %tt "op \<and>"
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   666
  (SML infixl 1 "andalso")
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   667
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   668
code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_infix.ML")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   669
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   670
text {*
21323
haftmann
parents: 21223
diff changeset
   671
  \lstsml{Thy/examples/bool_infix.ML}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   672
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   673
  Next, we try to map HOL pairs to SML pairs, using the
21323
haftmann
parents: 21223
diff changeset
   674
  infix ``@{verbatim "*"}'' type constructor and parentheses:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   675
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   676
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   677
(*<*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   678
code_type *
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   679
  (SML)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   680
code_const Pair
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   681
  (SML)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   682
(*>*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   683
21323
haftmann
parents: 21223
diff changeset
   684
code_type %tt *
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   685
  (SML infix 2 "*")
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   686
21323
haftmann
parents: 21223
diff changeset
   687
code_const %tt Pair
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   688
  (SML "!((_),/ (_))")
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   689
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   690
text {*
21323
haftmann
parents: 21223
diff changeset
   691
  The initial bang ``@{verbatim "!"}'' tells the serializer to never put
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   692
  parentheses around the whole expression (they are already present),
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   693
  while the parentheses around argument place holders
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   694
  tell not to put parentheses around the arguments.
21323
haftmann
parents: 21223
diff changeset
   695
  The slash ``@{verbatim "/"}'' (followed by arbitrary white space)
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   696
  inserts a space which may be used as a break if necessary
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   697
  during pretty printing.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   698
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   699
  So far, we did only provide more idiomatic serializations for
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   700
  constructs which would be executable on their own.  Target-specific
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   701
  serializations may also be used to \emph{implement} constructs
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   702
  which have no explicit notion of executability.  For example,
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   703
  take the HOL integers:
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   704
*}
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   705
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   706
definition
21993
4b802a9e0738 updated manual
haftmann
parents: 21545
diff changeset
   707
  double_inc :: "int \<Rightarrow> int" where
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   708
  "double_inc k = 2 * k + 1"
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   709
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   710
code_gen double_inc (SML "examples/integers.ML")
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   711
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   712
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   713
  will fail: @{typ int} in HOL is implemented using a quotient
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   714
  type, which does not provide any notion of executability.
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   715
  \footnote{Eventually, we also want to provide executability
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   716
  for quotients.}.  However, we could use the SML builtin
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   717
  integers:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   718
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   719
21323
haftmann
parents: 21223
diff changeset
   720
code_type %tt int
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   721
  (SML "IntInf.int")
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   722
21323
haftmann
parents: 21223
diff changeset
   723
code_const %tt "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   724
    and "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   725
  (SML "IntInf.+ (_, _)" and "IntInf.* (_, _)")
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   726
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   727
code_gen double_inc (*<*)(SML #)(*>*)(SML "examples/integers.ML")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   728
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   729
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   730
  resulting in:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   731
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   732
  \lstsml{Thy/examples/integers.ML}
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   733
*}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   734
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   735
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   736
  These examples give a glimpse what powerful mechanisms
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   737
  custom serializations provide; however their usage
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   738
  requires careful thinking in order not to introduce
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   739
  inconsistencies -- or, in other words:
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   740
  custom serializations are completely axiomatic.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   741
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   742
  A further noteworthy details is that any special
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   743
  character in a custom serialization may be quoted
21323
haftmann
parents: 21223
diff changeset
   744
  using ``@{verbatim "'"}''; thus, in
haftmann
parents: 21223
diff changeset
   745
  ``@{verbatim "fn '_ => _"}'' the first
haftmann
parents: 21223
diff changeset
   746
  ``@{verbatim "_"}'' is a proper underscore while the
haftmann
parents: 21223
diff changeset
   747
  second ``@{verbatim "_"}'' is a placeholder.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   748
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   749
  The HOL theories provide further
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   750
  examples for custom serializations and form
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   751
  a recommended tutorial on how to use them properly.
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   752
*}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   753
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   754
subsection {* Concerning operational equality *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   755
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   756
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   757
  Surely you have already noticed how equality is treated
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   758
  by the code generator:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   759
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   760
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   761
fun
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   762
  collect_duplicates :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   763
    "collect_duplicates xs ys [] = xs"
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   764
  | "collect_duplicates xs ys (z#zs) = (if z \<in> set xs
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   765
      then if z \<in> set ys
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   766
        then collect_duplicates xs ys zs
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   767
        else collect_duplicates xs (z#ys) zs
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   768
      else collect_duplicates (z#xs) (z#ys) zs)"
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   769
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   770
text {*
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
   771
  The membership test during preprocessing is rewritten,
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   772
  resulting in @{const List.memberl}, which itself
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   773
  performs an explicit equality check.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   774
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   775
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   776
code_gen collect_duplicates (*<*)(SML #)(*>*)(SML "examples/collect_duplicates.ML")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   777
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   778
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   779
  \lstsml{Thy/examples/collect_duplicates.ML}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   780
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   781
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   782
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   783
  Obviously, polymorphic equality is implemented the Haskell
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   784
  way using a type class.  How is this achieved?  By an
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   785
  almost trivial definition in the HOL setup:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   786
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   787
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   788
(*<*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   789
setup {* Sign.add_path "foo" *}
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   790
consts "op =" :: "'a"
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   791
(*>*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   792
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   793
class eq (attach "op =") = type
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   794
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   795
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   796
  This merely introduces a class @{text eq} with corresponding
21993
4b802a9e0738 updated manual
haftmann
parents: 21545
diff changeset
   797
  operation @{text "op ="};
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   798
  the preprocessing framework does the rest.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   799
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   800
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   801
(*<*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   802
hide (open) "class" eq
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   803
hide (open) const "op ="
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   804
setup {* Sign.parent_path *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   805
(*>*)
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   806
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   807
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   808
  For datatypes, instances of @{text eq} are implicitly derived
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   809
  when possible.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   810
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   811
  Though this class is designed to get rarely in the way, there
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   812
  are some cases when it suddenly comes to surface:
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   813
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   814
21223
b3bdc1abc7d3 some corrections
haftmann
parents: 21222
diff changeset
   815
subsubsection {* typedecls interpreted by customary serializations *}
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   816
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   817
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   818
  A common idiom is to use unspecified types for formalizations
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   819
  and interpret them for a specific target language:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   820
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   821
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   822
typedecl key
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   823
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   824
fun
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   825
  lookup :: "(key \<times> 'a) list \<Rightarrow> key \<Rightarrow> 'a option" where
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   826
    "lookup [] l = None"
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   827
  | "lookup ((k, v) # xs) l = (if k = l then Some v else lookup xs l)"
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   828
21323
haftmann
parents: 21223
diff changeset
   829
code_type %tt key
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   830
  (SML "string")
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   831
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   832
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   833
  This, though, is not sufficient: @{typ key} is no instance
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   834
  of @{text eq} since @{typ key} is no datatype; the instance
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   835
  has to be declared manually, including a serialization
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   836
  for the particular instance of @{const "op ="}:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   837
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   838
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   839
instance key :: eq ..
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   840
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   841
code_const %tt "op = \<Colon> key \<Rightarrow> key \<Rightarrow> bool"
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
   842
  (SML "!((_ : string) = _)")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   843
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   844
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   845
  Then everything goes fine:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   846
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   847
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   848
code_gen lookup (*<*)(SML #)(*>*)(SML "examples/lookup.ML")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   849
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   850
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   851
  \lstsml{Thy/examples/lookup.ML}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   852
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   853
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   854
subsubsection {* lexicographic orderings *}
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   855
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   856
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   857
  Another subtlety
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   858
  enters the stage when definitions of overloaded constants
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   859
  are dependent on operational equality.  For example, let
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   860
  us define a lexicographic ordering on tuples:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   861
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   862
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   863
instance * :: (ord, ord) ord
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   864
  "p1 < p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   865
    x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   866
  "p1 \<le> p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   867
    x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" ..
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   868
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   869
lemma ord_prod [code func]:
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   870
  "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   871
  "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)"
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   872
  unfolding "less_eq_*_def" "less_*_def" by simp_all
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   873
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   874
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   875
  Then code generation will fail.  Why?  The definition
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   876
  of @{const "op \<le>"} depends on equality on both arguments,
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   877
  which are polymorphic and impose an additional @{text eq}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   878
  class constraint, thus violating the type discipline
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   879
  for class operations.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   880
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   881
  The solution is to add @{text eq} explicitly to the first sort arguments in the
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   882
  code theorems:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   883
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   884
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   885
(*<*)
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   886
declare ord_prod [code del]
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   887
(*>*)
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   888
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   889
lemma ord_prod_code [code func]:
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   890
  "(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   891
  "(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)"
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   892
  unfolding ord_prod by rule+
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   893
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   894
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   895
  Then code generation succeeds:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   896
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   897
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   898
code_gen "op \<le> \<Colon> 'a\<Colon>{eq, ord} \<times> 'b\<Colon>ord \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool"
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
   899
  (*<*)(SML #)(*>*)(SML "examples/lexicographic.ML")
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   900
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   901
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   902
  \lstsml{Thy/examples/lexicographic.ML}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   903
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   904
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   905
text {*
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   906
  In general, code theorems for overloaded constants may have more
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   907
  restrictive sort constraints than the underlying instance relation
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   908
  between class and type constructor as long as the whole system of
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   909
  constraints is coregular; code theorems violating coregularity
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   910
  are rejected immediately.
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   911
*}
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   912
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   913
subsubsection {* Haskell serialization *}
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   914
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   915
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   916
  For convenience, the default
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   917
  HOL setup for Haskell maps the @{text eq} class to
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   918
  its counterpart in Haskell, giving custom serializations
21323
haftmann
parents: 21223
diff changeset
   919
  for the class (@{text "\<CODECLASS>"}) and its operation:
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   920
*}
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   921
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   922
(*<*)
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   923
setup {* Sign.add_path "bar" *}
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
   924
class eq = type + fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   925
(*>*)
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   926
21323
haftmann
parents: 21223
diff changeset
   927
code_class %tt eq
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   928
  (Haskell "Eq" where eq \<equiv> "(==)")
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   929
21323
haftmann
parents: 21223
diff changeset
   930
code_const %tt eq
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   931
  (Haskell infixl 4 "==")
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   932
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   933
(*<*)
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   934
hide "class" eq
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   935
hide const eq
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   936
setup {* Sign.parent_path *}
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   937
(*>*)
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   938
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   939
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   940
  A problem now occurs whenever a type which
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   941
  is an instance of @{text eq} in HOL is mapped
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   942
  on a Haskell-builtin type which is also an instance
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   943
  of Haskell @{text Eq}:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   944
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   945
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   946
typedecl bar
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   947
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   948
instance bar :: eq ..
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   949
21323
haftmann
parents: 21223
diff changeset
   950
code_type %tt bar
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   951
  (Haskell "Integer")
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   952
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   953
text {*
22188
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   954
  The code generator would produce
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   955
  an additional instance, which of course is rejected.
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   956
  To suppress this additional instance, use
a63889770d57 adjusted manual to improved treatment of overloaded constants
haftmann
parents: 22175
diff changeset
   957
  @{text "\<CODEINSTANCE>"}:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   958
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
   959
21323
haftmann
parents: 21223
diff changeset
   960
code_instance %tt bar :: eq
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   961
  (Haskell -)
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   962
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   963
subsection {* Types matter *}
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   964
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   965
text {*
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   966
  Imagine the following quick-and-dirty setup for implementing
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   967
  some kind of sets as lists in SML:
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   968
*}
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   969
21323
haftmann
parents: 21223
diff changeset
   970
code_type %tt set
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   971
  (SML "_ list")
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   972
21323
haftmann
parents: 21223
diff changeset
   973
code_const %tt "{}" and insert
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   974
  (SML "![]" and infixl 7 "::")
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   975
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
   976
definition
21993
4b802a9e0738 updated manual
haftmann
parents: 21545
diff changeset
   977
  dummy_set :: "(nat \<Rightarrow> nat) set" where
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   978
  "dummy_set = {Suc}"
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   979
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   980
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   981
  Then code generation for @{const dummy_set} will fail.
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   982
  Why? A glimpse at the defining equations will offer:
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   983
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   984
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
   985
code_thms insert
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   986
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   987
text {*
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
   988
  This reveals the defining equation @{thm insert_def}
21223
b3bdc1abc7d3 some corrections
haftmann
parents: 21222
diff changeset
   989
  for @{const insert}, which is operationally meaningless
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   990
  but forces an equality constraint on the set members
21223
b3bdc1abc7d3 some corrections
haftmann
parents: 21222
diff changeset
   991
  (which is not satisfiable if the set members are functions).
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   992
  Even when using set of natural numbers (which are an instance
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   993
  of \emph{eq}), we run into a problem:
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   994
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   995
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   996
definition
21993
4b802a9e0738 updated manual
haftmann
parents: 21545
diff changeset
   997
  foobar_set :: "nat set" where
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   998
  "foobar_set = {0, 1, 2}"
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
   999
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1000
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1001
  In this case the serializer would complain that @{const insert}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1002
  expects dictionaries (namely an \emph{eq} dictionary) but
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1003
  has also been given a customary serialization.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1004
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1005
  The solution to this dilemma:
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1006
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1007
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1008
lemma [code func]:
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1009
  "insert = insert" ..
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1010
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
  1011
code_gen dummy_set foobar_set (*<*)(SML #)(*>*)(SML "examples/dirty_set.ML")
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1012
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1013
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1014
  \lstsml{Thy/examples/dirty_set.ML}
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
  1015
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1016
  Reflexive defining equations by convention are dropped.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1017
  But their presence prevents primitive definitions to be
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1018
  used as defining equations:
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1019
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1020
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1021
code_thms insert
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1022
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1023
text {*
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1024
  will show \emph{no} defining equations for insert.
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
  1025
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1026
  Note that the sort constraints of reflexive equations
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1027
  are considered; so
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1028
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1029
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1030
lemma [code func]:
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1031
  "(insert \<Colon> 'a\<Colon>eq \<Rightarrow> 'a set \<Rightarrow> 'a set) = insert" ..
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1032
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1033
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1034
  would mean nothing else than to introduce the evil
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1035
  sort constraint by hand.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1036
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1037
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1038
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1039
subsection {* Constructor sets for datatypes *}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1040
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1041
text {*
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1042
  \fixme
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1043
*}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1044
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1045
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1046
subsection {* Cyclic module dependencies *}
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
  1047
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1048
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1049
  Sometimes the awkward situation occurs that dependencies
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1050
  between definitions introduce cyclic dependencies
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1051
  between modules, which in the Haskell world leaves
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1052
  you to the mercy of the Haskell implementation you are using,
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1053
  while for SML code generation is not possible.
21178
c3618fc6a6f7 added gfx
haftmann
parents: 21147
diff changeset
  1054
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1055
  A solution is to declare module names explicitly.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1056
  Let use assume the three cyclically dependent
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1057
  modules are named \emph{A}, \emph{B} and \emph{C}.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1058
  Then, by stating
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1059
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1060
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1061
code_modulename SML
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1062
  A ABC
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1063
  B ABC
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1064
  C ABC
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1065
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1066
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1067
  we explicitly map all those modules on \emph{ABC},
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1068
  resulting in an ad-hoc merge of this three modules
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1069
  at serialization time.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1070
*}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1071
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1072
subsection {* Axiomatic extensions *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1073
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1074
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1075
  \begin{warn}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1076
    The extensions introduced in this section, though working
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1077
    in practice, are not the cream of the crop, as you
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1078
    will notice during reading.  They will
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1079
    eventually be replaced by more mature approaches.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1080
  \end{warn}
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1081
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1082
  Sometimes equalities are taken for granted which are
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1083
  not derivable inside the HOL logic but are silently assumed
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1084
  to hold for executable code.  For example, we may want
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1085
  to identify the famous HOL constant @{const arbitrary}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1086
  of type @{typ "'a option"} with @{const None}.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1087
  By brute force:
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1088
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1089
21323
haftmann
parents: 21223
diff changeset
  1090
axiomatization where
haftmann
parents: 21223
diff changeset
  1091
  "arbitrary = None"
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1092
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1093
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1094
  However this has to be considered harmful since this axiom,
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1095
  though probably justifiable for generated code, could
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1096
  introduce serious inconsistencies into the logic.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1097
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1098
  So, there is a distinguished construct for stating axiomatic
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1099
  equalities of constants which apply only for code generation.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1100
  Before introducing this, here is a convenient place to describe
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1101
  shortly how to deal with some restrictions the type discipline
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1102
  imposes.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1103
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1104
  By itself, the constant @{const arbitrary} is a non-overloaded
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1105
  polymorphic constant.  So, there is no way to distinguish
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1106
  different versions of @{const arbitrary} for different types
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1107
  inside the code generator framework.  However, inlining
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1108
  theorems together with auxiliary constants provide a solution:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1109
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1110
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1111
definition
21993
4b802a9e0738 updated manual
haftmann
parents: 21545
diff changeset
  1112
  arbitrary_option :: "'a option" where
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1113
  [symmetric, code inline]: "arbitrary_option = arbitrary"
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1114
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1115
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1116
  By that, we replace any @{const arbitrary} with option type
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1117
  by @{const arbitrary_option} in defining equations.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1118
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1119
  For technical reasons, we further have to provide a
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1120
  synonym for @{const None} which in code generator view
22175
d9e3e4c30d6b adjusted names
haftmann
parents: 22060
diff changeset
  1121
  is a function rather than a term constructor:
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1122
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1123
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1124
definition
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1125
  "None' = None"
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1126
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1127
text {*
21323
haftmann
parents: 21223
diff changeset
  1128
  Then finally we are enabled to use @{text "\<CODEAXIOMS>"}:
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1129
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1130
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1131
code_axioms
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1132
  arbitrary_option \<equiv> None'
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1133
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1134
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1135
  A dummy example:
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1136
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1137
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1138
fun
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1139
  dummy_option :: "'a list \<Rightarrow> 'a option" where
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
  1140
    "dummy_option (x#xs) = Some x"
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
  1141
  | "dummy_option [] = arbitrary"
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1142
21545
54cc492d80a9 adjusted syntax for internal code generation
haftmann
parents: 21452
diff changeset
  1143
code_gen dummy_option (*<*)(SML #)(*>*)(SML "examples/arbitrary.ML")
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1144
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1145
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1146
  \lstsml{Thy/examples/arbitrary.ML}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1147
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1148
  Another axiomatic extension is code generation
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1149
  for abstracted types.  For this, the  
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1150
  @{text "ExecutableRat"} (see \secref{exec_rat})
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1151
  forms a good example.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1152
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1153
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
  1154
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
  1155
section {* ML interfaces \label{sec:ml} *}
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
  1156
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1157
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1158
  Since the code generator framework not only aims to provide
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1159
  a nice Isar interface but also to form a base for
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1160
  code-generation-based applications, here a short
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1161
  description of the most important ML interfaces.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1162
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1163
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1164
subsection {* Constants with type discipline: codegen\_consts.ML *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1165
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1166
text {*
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1167
  This Pure module manages identification of (probably overloaded)
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1168
  constants by unique identifiers.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1169
*}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1170
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1171
text %mlref {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1172
  \begin{mldecls}
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1173
  @{index_ML_type CodegenConsts.const: "string * string option"} \\
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1174
  @{index_ML CodegenConsts.const_of_cexpr: "theory -> string * typ -> CodegenConsts.const"} \\
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1175
 \end{mldecls}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1176
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1177
  \begin{description}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1178
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1179
  \item @{ML_type CodegenConsts.const} is the identifier type:
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1180
     the product of a \emph{string} with a list of \emph{typs}.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1181
     The \emph{string} is the constant name as represented inside Isabelle;
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1182
     for overloaded constants, the attached \emph{string option}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1183
     is either @{text SOME} type constructor denoting an instance,
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1184
     or @{text NONE} for the polymorphic constant.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1185
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1186
  \item @{ML CodegenConsts.const_of_cexpr}~@{text thy}~@{text "(constname, typ)"}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1187
     maps a constant expression @{text "(constname, typ)"}
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1188
     to its canonical identifier.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1190
  \end{description}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1191
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1192
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1193
subsection {* Executable theory content: codegen\_data.ML *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1194
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1195
text {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1196
  This Pure module implements the core notions of
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1197
  executable content of a theory.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1198
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1199
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1200
subsubsection {* Suspended theorems *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1201
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1202
text %mlref {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1203
  \begin{mldecls}
21341
3844037a8e2d adjusted to new fun''
haftmann
parents: 21323
diff changeset
  1204
  @{index_ML CodegenData.lazy: "(unit -> thm list) -> thm list Susp.T"}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1205
  \end{mldecls}
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1206
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1207
  \begin{description}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1208
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1209
  \item @{ML CodegenData.lazy}~@{text f} turns an abstract
21323
haftmann
parents: 21223
diff changeset
  1210
     theorem computation @{text f} into a suspension of theorems.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1211
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1212
  \end{description}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1213
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1214
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1215
subsubsection {* Managing executable content *}
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
  1216
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1217
text %mlref {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1218
  \begin{mldecls}
22550
c5039bee2602 updated
haftmann
parents: 22479
diff changeset
  1219
  @{index_ML CodegenData.add_func: "bool -> thm -> theory -> theory"} \\
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1220
  @{index_ML CodegenData.del_func: "thm -> theory -> theory"} \\
21341
3844037a8e2d adjusted to new fun''
haftmann
parents: 21323
diff changeset
  1221
  @{index_ML CodegenData.add_funcl: "CodegenConsts.const * thm list Susp.T -> theory -> theory"} \\
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1222
  @{index_ML CodegenData.add_inline: "thm -> theory -> theory"} \\
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1223
  @{index_ML CodegenData.del_inline: "thm -> theory -> theory"} \\
22046
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1224
  @{index_ML CodegenData.add_inline_proc: "string * (theory -> cterm list -> thm list)
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1225
    -> theory -> theory"} \\
22046
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1226
  @{index_ML CodegenData.del_inline_proc: "string -> theory -> theory"} \\
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1227
  @{index_ML CodegenData.add_preproc: "string * (theory -> thm list -> thm list)
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1228
    -> theory -> theory"} \\
22046
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1229
  @{index_ML CodegenData.del_preproc: "string -> theory -> theory"} \\
22423
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
  1230
  @{index_ML CodegenData.add_datatype: "string * ((string * sort) list * (string * typ list) list)
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
  1231
    -> theory -> theory"} \\
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1232
  @{index_ML CodegenData.get_datatype: "theory -> string
22479
de15ea8fb348 updated code generation sections
haftmann
parents: 22473
diff changeset
  1233
    -> (string * sort) list * (string * typ list) list"} \\
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1234
  @{index_ML CodegenData.get_datatype_of_constr: "theory -> CodegenConsts.const -> string option"}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1235
  \end{mldecls}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1236
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1237
  \begin{description}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1238
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1239
  \item @{ML CodegenData.add_func}~@{text "thm"}~@{text "thy"} adds function
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1240
     theorem @{text "thm"} to executable content.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1241
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1242
  \item @{ML CodegenData.del_func}~@{text "thm"}~@{text "thy"} removes function
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1243
     theorem @{text "thm"} from executable content, if present.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1244
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1245
  \item @{ML CodegenData.add_funcl}~@{text "(const, lthms)"}~@{text "thy"} adds
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1246
     suspended defining equations @{text lthms} for constant
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1247
     @{text const} to executable content.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1248
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1249
  \item @{ML CodegenData.add_inline}~@{text "thm"}~@{text "thy"} adds
21223
b3bdc1abc7d3 some corrections
haftmann
parents: 21222
diff changeset
  1250
     inlining theorem @{text thm} to executable content.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1251
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1252
  \item @{ML CodegenData.del_inline}~@{text "thm"}~@{text "thy"} remove
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1253
     inlining theorem @{text thm} from executable content, if present.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1254
22046
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1255
  \item @{ML CodegenData.add_inline_proc}~@{text "(name, f)"}~@{text "thy"} adds
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1256
     inline procedure @{text f} (named @{text name}) to executable content;
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1257
     @{text f} is a computation of rewrite rules dependent on
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1258
     the current theory context and the list of all arguments
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1259
     and right hand sides of the defining equations belonging
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1260
     to a certain function definition.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1261
22046
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1262
  \item @{ML CodegenData.del_inline_proc}~@{text "name"}~@{text "thy"} removes
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1263
     inline procedure named @{text name} from executable content.
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1264
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1265
  \item @{ML CodegenData.add_preproc}~@{text "(name, f)"}~@{text "thy"} adds
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1266
     generic preprocessor @{text f} (named @{text name}) to executable content;
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1267
     @{text f} is a transformation of the defining equations belonging
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1268
     to a certain function definition, depending on the
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1269
     current theory context.
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1270
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1271
  \item @{ML CodegenData.del_preproc}~@{text "name"}~@{text "thy"} removes
22046
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1272
     generic preprcoessor named @{text name} from executable content.
ce84c9887e2d named preprocessors
haftmann
parents: 22015
diff changeset
  1273
22423
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
  1274
  \item @{ML CodegenData.add_datatype}~@{text "(name, spec)"}~@{text "thy"} adds
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1275
     a datatype to executable content, with type constructor
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1276
     @{text name} and specification @{text spec}; @{text spec} is
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1277
     a pair consisting of a list of type variable with sort
21223
b3bdc1abc7d3 some corrections
haftmann
parents: 21222
diff changeset
  1278
     constraints and a list of constructors with name
22423
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
  1279
     and types of arguments.
21189
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1280
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1281
  \item @{ML CodegenData.get_datatype_of_constr}~@{text "thy"}~@{text "const"}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1282
     returns type constructor corresponding to
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1283
     constructor @{text const}; returns @{text NONE}
5435ccdb4ea1 (continued)
haftmann
parents: 21178
diff changeset
  1284
     if @{text const} is no constructor.
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1285
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1286
  \end{description}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1287
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1288
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1289
subsection {* Auxiliary *}
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1290
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1291
text %mlref {*
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1292
  \begin{mldecls}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1293
  @{index_ML CodegenConsts.const_ord: "CodegenConsts.const * CodegenConsts.const -> order"} \\
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1294
  @{index_ML CodegenConsts.eq_const: "CodegenConsts.const * CodegenConsts.const -> bool"} \\
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1295
  @{index_ML CodegenConsts.read_const: "theory -> string -> CodegenConsts.const"} \\
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1296
  @{index_ML_structure CodegenConsts.Consttab} \\
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1297
  @{index_ML CodegenFunc.typ_func: "thm -> typ"} \\
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1298
  @{index_ML CodegenFunc.rewrite_func: "thm list -> thm -> thm"} \\
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1299
  \end{mldecls}
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1300
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1301
  \begin{description}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1302
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1303
  \item @{ML CodegenConsts.const_ord},~@{ML CodegenConsts.eq_const}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1304
     provide order and equality on constant identifiers.
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1305
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1306
  \item @{ML_struct CodegenConsts.Consttab}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1307
     provides table structures with constant identifiers as keys.
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1308
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1309
  \item @{ML CodegenConsts.read_const}~@{text thy}~@{text s}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1310
     reads a constant as a concrete term expression @{text s}.
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1311
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1312
  \item @{ML CodegenFunc.typ_func}~@{text thm}
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1313
     extracts the type of a constant in a defining equation @{text thm}.
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1314
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1315
  \item @{ML CodegenFunc.rewrite_func}~@{text rews}~@{text thm}
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1316
     rewrites a defining equation @{text thm} with a set of rewrite
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1317
     rules @{text rews}; only arguments and right hand side are rewritten,
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1318
     not the head of the defining equation.
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1319
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1320
  \end{description}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1321
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1322
*}
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
  1323
9b9910b82645 initial draft
haftmann
parents:
diff changeset
  1324
subsection {* Implementing code generator applications *}
9b9910b82645 initial draft
haftmann
parents:
diff changeset
  1325
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1326
text {*
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1327
  Implementing code generator applications on top
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1328
  of the framework set out so far usually not only
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1329
  involves using those primitive interfaces
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1330
  but also storing code-dependent data and various
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1331
  other things.
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1332
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1333
  \begin{warn}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1334
    Some interfaces discussed here have not reached
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1335
    a final state yet.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1336
    Changes likely to occur in future.
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1337
  \end{warn}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1338
*}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1339
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1340
subsubsection {* Data depending on the theory's executable content *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1341
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1342
text {*
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1343
  Due to incrementality of code generation, changes in the
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1344
  theory's executable content have to be propagated in a
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1345
  certain fashion.  Additionally, such changes may occur
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1346
  not only during theory extension but also during theory
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1347
  merge, which is a little bit nasty from an implementation
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1348
  point of view.  The framework provides a solution
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1349
  to this technical challenge by providing a functorial
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1350
  data slot @{ML_functor CodeDataFun}; on instantiation
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1351
  of this functor, the following types and operations
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1352
  are required:
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1353
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1354
  \medskip
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1355
  \begin{tabular}{l}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1356
  @{text "val name: string"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1357
  @{text "type T"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1358
  @{text "val empty: T"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1359
  @{text "val merge: Pretty.pp \<rightarrow> T * T \<rightarrow> T"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1360
  @{text "val purge: theory option \<rightarrow> CodegenConsts.const list option \<rightarrow> T \<rightarrow> T"}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1361
  \end{tabular}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1362
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1363
  \begin{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1364
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1365
  \item @{text name} is a system-wide unique name identifying the data.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1366
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1367
  \item @{text T} the type of data to store.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1368
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1369
  \item @{text empty} initial (empty) data.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1370
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1371
  \item @{text merge} merging two data slots.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1372
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1373
  \item @{text purge}~@{text thy}~@{text cs} propagates changes in executable content;
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1374
    if possible, the current theory context is handed over
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1375
    as argument @{text thy} (if there is no current theory context (e.g.~during
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1376
    theory merge, @{ML NONE}); @{text cs} indicates the kind
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1377
    of change: @{ML NONE} stands for a fundamental change
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1378
    which invalidates any existing code, @{text "SOME cs"}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1379
    hints that executable content for constants @{text cs}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1380
    has changed.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1381
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1382
  \end{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1383
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1384
  An instance of @{ML_functor CodeDataFun} provides the following
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1385
  interface:
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1386
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1387
  \medskip
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1388
  \begin{tabular}{l}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1389
  @{text "init: theory \<rightarrow> theory"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1390
  @{text "get: theory \<rightarrow> T"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1391
  @{text "change: theory \<rightarrow> (T \<rightarrow> T) \<rightarrow> T"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1392
  @{text "change_yield: theory \<rightarrow> (T \<rightarrow> 'a * T) \<rightarrow> 'a * T"}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1393
  \end{tabular}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1394
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1395
  \begin{description}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1396
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1397
  \item @{text init} initialization during theory setup.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1398
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1399
  \item @{text get} retrieval of the current data.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1400
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1401
  \item @{text change} update of current data (cached!)
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1402
    by giving a continuation.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1403
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1404
  \item @{text change_yield} update with side result.
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1405
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1406
  \end{description}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1407
*}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1408
22292
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1409
(* subsubsection {* Building implementable systems fo defining equations *}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1410
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1411
text {*
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1412
  Out of the executable content of a theory, a normalized
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1413
  defining equation systems may be constructed containing
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1414
  function definitions for constants.  The system is cached
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1415
  until its underlying executable content changes.
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1416
*}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1417
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1418
text %mlref {*
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1419
  \begin{mldecls}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1420
  @{index_ML_type CodegenFuncgr.T} \\
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1421
  @{index_ML CodegenFuncgr.make: "theory -> CodegenConsts.const list -> CodegenFuncgr.T"} \\
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1422
  @{index_ML CodegenFuncgr.funcs: "CodegenFuncgr.T -> CodegenConsts.const -> thm list"} \\
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1423
  @{index_ML CodegenFuncgr.typ: "CodegenFuncgr.T -> CodegenConsts.const -> typ"} \\
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1424
  @{index_ML CodegenFuncgr.deps: "CodegenFuncgr.T
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1425
    -> CodegenConsts.const list -> CodegenConsts.const list list"} \\
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1426
  @{index_ML CodegenFuncgr.all: "CodegenFuncgr.T -> CodegenConsts.const list"}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1427
  \end{mldecls}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1428
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1429
  \begin{description}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1430
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1431
  \item @{ML_type CodegenFuncgr.T} represents
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1432
    a normalized defining equation system.
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1433
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1434
  \item @{ML CodegenFuncgr.make}~@{text thy}~@{text cs}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1435
    returns a normalized defining equation system,
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1436
    with the assertion that it contains any function
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1437
    definition for constants @{text cs} (if existing).
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1438
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1439
  \item @{ML CodegenFuncgr.funcs}~@{text funcgr}~@{text c}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1440
    retrieves function definition for constant @{text c}.
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1441
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1442
  \item @{ML CodegenFuncgr.typ}~@{text funcgr}~@{text c}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1443
    retrieves function type for constant @{text c}.
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1444
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1445
  \item @{ML CodegenFuncgr.deps}~@{text funcgr}~@{text cs}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1446
    returns the transitive closure of dependencies for
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1447
    constants @{text cs} as a partitioning where each partition
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1448
    corresponds to a strongly connected component of
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1449
    dependencies and any partition does \emph{not}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1450
    depend on partitions further left.
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1451
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1452
  \item @{ML CodegenFuncgr.all}~@{text funcgr}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1453
    returns all currently represented constants.
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1454
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1455
  \end{description}
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1456
*} *)
3b118010ec08 adjusted to new code generator Isar commands and changes in implementation
haftmann
parents: 22188
diff changeset
  1457
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1458
subsubsection {* Datatype hooks *}
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1459
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1460
text {*
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1461
  Isabelle/HOL's datatype package provides a mechanism to
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1462
  extend theories depending on datatype declarations:
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1463
  \emph{datatype hooks}.  For example, when declaring a new
22060
8a37090726e8 adjusted manual
haftmann
parents: 22046
diff changeset
  1464
  datatype, a hook proves defining equations for equality on
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1465
  that datatype (if possible).
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1466
*}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1467
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1468
text %mlref {*
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1469
  \begin{mldecls}
21323
haftmann
parents: 21223
diff changeset
  1470
  @{index_ML_type DatatypeHooks.hook: "string list -> theory -> theory"} \\
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1471
  @{index_ML DatatypeHooks.add: "DatatypeHooks.hook -> theory -> theory"}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1472
  \end{mldecls}
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1473
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1474
  \begin{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1475
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1476
  \item @{ML_type DatatypeHooks.hook} specifies the interface
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1477
     of \emph{datatype hooks}: a theory update
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1478
     depending on the list of newly introduced
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1479
     datatype names.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1480
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1481
  \item @{ML DatatypeHooks.add} adds a hook to the
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1482
     chain of all hooks.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1483
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1484
  \end{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1485
*}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1486
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1487
subsubsection {* Trivial typedefs -- type copies *}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1488
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1489
text {*
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1490
  Sometimes packages will introduce new types
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1491
  as \emph{marked type copies} similar to Haskell's
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1492
  @{text newtype} declaration (e.g. the HOL record package)
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1493
  \emph{without} tinkering with the overhead of datatypes.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1494
  Technically, these type copies are trivial forms of typedefs.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1495
  Since these type copies in code generation view are nothing
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1496
  else than datatypes, they have been given a own package
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1497
  in order to faciliate code generation:
21147
737a94f047e3 continued tutorial
haftmann
parents: 21089
diff changeset
  1498
*}
21058
a32d357dfd70 started tutorial
haftmann
parents: 20948
diff changeset
  1499
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1500
text %mlref {*
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1501
  \begin{mldecls}
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1502
  @{index_ML_type TypecopyPackage.info} \\
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1503
  @{index_ML TypecopyPackage.add_typecopy: "
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1504
    bstring * string list -> typ -> (bstring * bstring) option
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1505
    -> theory -> (string * TypecopyPackage.info) * theory"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1506
  @{index_ML TypecopyPackage.get_typecopy_info: "theory
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1507
    -> string -> TypecopyPackage.info option"} \\
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1508
  @{index_ML TypecopyPackage.get_spec: "theory -> string
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1509
    -> (string * sort) list * (string * typ list) list"} \\
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1510
  @{index_ML_type TypecopyPackage.hook: "string * TypecopyPackage.info -> theory -> theory"} \\
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1511
  @{index_ML TypecopyPackage.add_hook:
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1512
     "TypecopyPackage.hook -> theory -> theory"} \\
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1513
  \end{mldecls}
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1514
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1515
  \begin{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1516
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1517
  \item @{ML_type TypecopyPackage.info} a record containing
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1518
     the specification and further data of a type copy.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1519
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1520
  \item @{ML TypecopyPackage.add_typecopy} defines a new
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1521
     type copy.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1522
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1523
  \item @{ML TypecopyPackage.get_typecopy_info} retrieves
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1524
     data of an existing type copy.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1525
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1526
  \item @{ML TypecopyPackage.get_spec} retrieves datatype-like
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1527
     specification of a type copy.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1528
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1529
  \item @{ML_type TypecopyPackage.hook},~@{ML TypecopyPackage.add_hook}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1530
     provide a hook mechanism corresponding to the hook mechanism
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1531
     on datatypes.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1532
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1533
  \end{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1534
*}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1535
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1536
subsubsection {* Unifying type copies and datatypes *}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1537
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1538
text {*
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1539
  Since datatypes and type copies are mapped to the same concept (datatypes)
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1540
  by code generation, the view on both is unified \qt{code types}:
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1541
*}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1542
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1543
text %mlref {*
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1544
  \begin{mldecls}
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1545
  @{index_ML_type DatatypeCodegen.hook: "(string * (bool * ((string * sort) list
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1546
    * (string * typ list) list))) list
21323
haftmann
parents: 21223
diff changeset
  1547
    -> theory -> theory"} \\
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1548
  @{index_ML DatatypeCodegen.add_codetypes_hook_bootstrap: "
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1549
      DatatypeCodegen.hook -> theory -> theory"}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1550
  \end{mldecls}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1551
*}
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1552
21222
6dfdb69e83ef adjusted title
haftmann
parents: 21217
diff changeset
  1553
text {*
21452
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1554
  \begin{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1555
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1556
  \item @{ML_type DatatypeCodegen.hook} specifies the code type hook
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1557
     interface: a theory transformation depending on a list of
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1558
     mutual recursive code types; each entry in the list
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1559
     has the structure @{text "(name, (is_data, (vars, cons)))"}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1560
     where @{text name} is the name of the code type, @{text is_data}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1561
     is true iff @{text name} is a datatype rather then a type copy,
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1562
     and @{text "(vars, cons)"} is the specification of the code type.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1563
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1564
  \item @{ML DatatypeCodegen.add_codetypes_hook_bootstrap} adds a code
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1565
     type hook;  the hook is immediately processed for all already
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1566
     existing datatypes, in blocks of mutual recursive datatypes,
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1567
     where all datatypes a block depends on are processed before
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1568
     the block.
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1569
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1570
  \end{description}
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1571
f825e0b4d566 final draft
haftmann
parents: 21341
diff changeset
  1572
  \emph{Happy proving, happy hacking!}
21222
6dfdb69e83ef adjusted title
haftmann
parents: 21217
diff changeset
  1573
*}
21217
0425fc57510f continued
haftmann
parents: 21189
diff changeset
  1574
20948
9b9910b82645 initial draft
haftmann
parents:
diff changeset
  1575
end