src/Doc/Codegen/Further.thy
author wenzelm
Tue, 01 Nov 2016 01:20:33 +0100
changeset 64439 2bafda87b524
parent 63680 6e1e8b5abbfa
child 65041 2525e680f94f
permissions -rw-r--r--
back to post-release mode -- after fork point;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28213
b52f9205a02d New outline for codegen tutorial -- draft
haftmann
parents:
diff changeset
     1
theory Further
59378
065f349852e6 separate image for prerequisites of codegen tutorial
haftmann
parents: 59377
diff changeset
     2
imports Setup
28213
b52f9205a02d New outline for codegen tutorial -- draft
haftmann
parents:
diff changeset
     3
begin
b52f9205a02d New outline for codegen tutorial -- draft
haftmann
parents:
diff changeset
     4
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
     5
section \<open>Further issues \label{sec:further}\<close>
28419
f65e8b318581 re-canibalised manual
haftmann
parents: 28213
diff changeset
     6
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
     7
subsection \<open>Specialities of the @{text Scala} target language \label{sec:scala}\<close>
42096
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
     8
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
     9
text \<open>
42096
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    10
  @{text Scala} deviates from languages of the ML family in a couple
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    11
  of aspects; those which affect code generation mainly have to do with
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    12
  @{text Scala}'s type system:
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    13
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    14
  \begin{itemize}
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    15
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    16
    \item @{text Scala} prefers tupled syntax over curried syntax.
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    17
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    18
    \item @{text Scala} sacrifices Hindely-Milner type inference for a
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    19
      much more rich type system with subtyping etc.  For this reason
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    20
      type arguments sometimes have to be given explicitly in square
46520
a0abc2ea815e corrected spelling
haftmann
parents: 46514
diff changeset
    21
      brackets (mimicking System F syntax).
42096
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    22
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    23
    \item In contrast to @{text Haskell} where most specialities of
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    24
      the type system are implemented using \emph{type classes},
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    25
      @{text Scala} provides a sophisticated system of \emph{implicit
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    26
      arguments}.
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    27
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    28
  \end{itemize}
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    29
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    30
  \noindent Concerning currying, the @{text Scala} serializer counts
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    31
  arguments in code equations to determine how many arguments
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    32
  shall be tupled; remaining arguments and abstractions in terms
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    33
  rather than function definitions are always curried.
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    34
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    35
  The second aspect affects user-defined adaptations with @{command
55372
3662c44d018c dropped legacy finally
haftmann
parents: 52665
diff changeset
    36
  code_printing}.  For regular terms, the @{text Scala} serializer prints
42096
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    37
  all type arguments explicitly.  For user-defined term adaptations
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    38
  this is only possible for adaptations which take no arguments: here
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    39
  the type arguments are just appended.  Otherwise they are ignored;
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    40
  hence user-defined adaptations for polymorphic constants have to be
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    41
  designed very carefully to avoid ambiguity.
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    42
\<close>
42096
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    43
9f6652122963 added subsection on Scala specialities
haftmann
parents: 40752
diff changeset
    44
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    45
subsection \<open>Modules namespace\<close>
28419
f65e8b318581 re-canibalised manual
haftmann
parents: 28213
diff changeset
    46
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    47
text \<open>
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    48
  When invoking the @{command export_code} command it is possible to
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    49
  leave out the @{keyword "module_name"} part; then code is
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    50
  distributed over different modules, where the module name space
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    51
  roughly is induced by the Isabelle theory name space.
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38405
diff changeset
    52
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    53
  Then sometimes the awkward situation occurs that dependencies
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    54
  between definitions introduce cyclic dependencies between modules,
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    55
  which in the @{text Haskell} world leaves you to the mercy of the
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    56
  @{text Haskell} implementation you are using, while for @{text
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    57
  SML}/@{text OCaml} code generation is not possible.
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38405
diff changeset
    58
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    59
  A solution is to declare module names explicitly.  Let use assume
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    60
  the three cyclically dependent modules are named \emph{A}, \emph{B}
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    61
  and \emph{C}.  Then, by stating
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    62
\<close>
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38405
diff changeset
    63
52378
08dbf9ff2140 documentation on code_printing and code_identifier
haftmann
parents: 51717
diff changeset
    64
code_identifier %quote
08dbf9ff2140 documentation on code_printing and code_identifier
haftmann
parents: 51717
diff changeset
    65
  code_module A \<rightharpoonup> (SML) ABC
08dbf9ff2140 documentation on code_printing and code_identifier
haftmann
parents: 51717
diff changeset
    66
| code_module B \<rightharpoonup> (SML) ABC
08dbf9ff2140 documentation on code_printing and code_identifier
haftmann
parents: 51717
diff changeset
    67
| code_module C \<rightharpoonup> (SML) ABC
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38405
diff changeset
    68
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    69
text \<open>
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    70
  \noindent we explicitly map all those modules on \emph{ABC},
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    71
  resulting in an ad-hoc merge of this three modules at serialisation
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    72
  time.
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    73
\<close>
28419
f65e8b318581 re-canibalised manual
haftmann
parents: 28213
diff changeset
    74
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    75
subsection \<open>Locales and interpretation\<close>
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    76
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    77
text \<open>
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    78
  A technical issue comes to surface when generating code from
61891
76189756ff65 documentation on last state of the art concerning interpretation
haftmann
parents: 61779
diff changeset
    79
  specifications stemming from locale interpretation into global
76189756ff65 documentation on last state of the art concerning interpretation
haftmann
parents: 61779
diff changeset
    80
  theories.
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    81
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    82
  Let us assume a locale specifying a power operation on arbitrary
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    83
  types:
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    84
\<close>
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    85
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    86
locale %quote power =
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    87
  fixes power :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    88
  assumes power_commute: "power x \<circ> power y = power y \<circ> power x"
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    89
begin
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    90
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    91
text \<open>
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
    92
  \noindent Inside that locale we can lift @{text power} to exponent
61779
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
    93
  lists by means of a specification relative to that locale:
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
    94
\<close>
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    95
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    96
primrec %quote powers :: "'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    97
  "powers [] = id"
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    98
| "powers (x # xs) = power x \<circ> powers xs"
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
    99
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   100
lemma %quote powers_append:
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   101
  "powers (xs @ ys) = powers xs \<circ> powers ys"
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   102
  by (induct xs) simp_all
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   103
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   104
lemma %quote powers_power:
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   105
  "powers xs \<circ> power x = power x \<circ> powers xs"
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   106
  by (induct xs)
49739
13aa6d8268ec consolidated names of theorems on composition;
haftmann
parents: 48985
diff changeset
   107
    (simp_all del: o_apply id_apply add: comp_assoc,
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   108
      simp del: o_apply add: o_assoc power_commute)
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   109
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   110
lemma %quote powers_rev:
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   111
  "powers (rev xs) = powers xs"
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   112
    by (induct xs) (simp_all add: powers_append powers_power)
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   113
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   114
end %quote
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   115
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   116
text \<open>
38505
2f8699695cf6 use command_def vs. command more consciously
haftmann
parents: 38437
diff changeset
   117
  After an interpretation of this locale (say, @{command_def
61891
76189756ff65 documentation on last state of the art concerning interpretation
haftmann
parents: 61779
diff changeset
   118
  global_interpretation} @{text "fun_power:"} @{term [source] "power (\<lambda>n (f
61779
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   119
  :: 'a \<Rightarrow> 'a). f ^^ n)"}), one could naively expect to have a constant @{text
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   120
  "fun_power.powers :: nat list \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"} for which code
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   121
  can be generated.  But this not the case: internally, the term
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   122
  @{text "fun_power.powers"} is an abbreviation for the foundational
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   123
  term @{term [source] "power.powers (\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"}
58620
7435b6a3f72e more antiquotations;
wenzelm
parents: 57512
diff changeset
   124
  (see @{cite "isabelle-locale"} for the details behind).
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   125
61891
76189756ff65 documentation on last state of the art concerning interpretation
haftmann
parents: 61779
diff changeset
   126
  Fortunately, a succint solution is available: a dedicated
61779
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   127
  rewrite definition:
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   128
\<close>
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   129
61891
76189756ff65 documentation on last state of the art concerning interpretation
haftmann
parents: 61779
diff changeset
   130
global_interpretation %quote fun_power: power "(\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"
61779
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   131
  defines funpows = fun_power.powers
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   132
  by unfold_locales
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   133
    (simp_all add: fun_eq_iff funpow_mult mult.commute)
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   134
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   135
text \<open>
61779
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   136
  \noindent This amends the interpretation morphisms such that
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   137
  occurrences of the foundational term @{term [source] "power.powers (\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"}
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   138
  are folded to a newly defined constant @{const funpows}.
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   139
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   140
  After this setup procedure, code generation can continue as usual:
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   141
\<close>
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   142
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   143
text %quotetypewriter \<open>
39683
f75a01ee6c41 prefer typewrite tag over raw latex environment
haftmann
parents: 39664
diff changeset
   144
  @{code_stmts funpows (consts) Nat.funpow funpows (Haskell)}
61779
9ace5e8310dc consolidated documentation
haftmann
parents: 61671
diff changeset
   145
\<close>
37426
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   146
04d58897bf90 subsection on locale interpretation
haftmann
parents: 37210
diff changeset
   147
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   148
subsection \<open>Parallel computation\<close>
51172
16eb76ca1e4a note on parallel computation
haftmann
parents: 49739
diff changeset
   149
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   150
text \<open>
63680
6e1e8b5abbfa more symbols;
wenzelm
parents: 63669
diff changeset
   151
  Theory @{text Parallel} in \<^dir>\<open>~~/src/HOL/Library\<close> contains
51172
16eb76ca1e4a note on parallel computation
haftmann
parents: 49739
diff changeset
   152
  operations to exploit parallelism inside the Isabelle/ML
16eb76ca1e4a note on parallel computation
haftmann
parents: 49739
diff changeset
   153
  runtime engine.
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   154
\<close>
51172
16eb76ca1e4a note on parallel computation
haftmann
parents: 49739
diff changeset
   155
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   156
subsection \<open>Imperative data structures\<close>
28419
f65e8b318581 re-canibalised manual
haftmann
parents: 28213
diff changeset
   157
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   158
text \<open>
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38405
diff changeset
   159
  If you consider imperative data structures as inevitable for a
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38405
diff changeset
   160
  specific application, you should consider \emph{Imperative
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38405
diff changeset
   161
  Functional Programming with Isabelle/HOL}
58620
7435b6a3f72e more antiquotations;
wenzelm
parents: 57512
diff changeset
   162
  @{cite "bulwahn-et-al:2008:imperative"}; the framework described there
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   163
  is available in session @{text Imperative_HOL}, together with a
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   164
  short primer document.
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   165
\<close>
28419
f65e8b318581 re-canibalised manual
haftmann
parents: 28213
diff changeset
   166
28213
b52f9205a02d New outline for codegen tutorial -- draft
haftmann
parents:
diff changeset
   167
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   168
subsection \<open>ML system interfaces \label{sec:ml}\<close>
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   169
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   170
text \<open>
38510
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   171
  Since the code generator framework not only aims to provide a nice
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   172
  Isar interface but also to form a base for code-generation-based
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   173
  applications, here a short description of the most fundamental ML
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   174
  interfaces.
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   175
\<close>
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   176
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   177
subsubsection \<open>Managing executable content\<close>
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   178
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   179
text %mlref \<open>
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   180
  \begin{mldecls}
38510
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   181
  @{index_ML Code.read_const: "theory -> string -> string"} \\
63239
d562c9948dee explicit tagging of code equations de-baroquifies interface
haftmann
parents: 61891
diff changeset
   182
  @{index_ML Code.add_eqn: "Code.kind * bool -> thm -> theory -> theory"} \\
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   183
  @{index_ML Code.del_eqn: "thm -> theory -> theory"} \\
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51172
diff changeset
   184
  @{index_ML Code_Preproc.map_pre: "(Proof.context -> Proof.context) -> theory -> theory"} \\
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51172
diff changeset
   185
  @{index_ML Code_Preproc.map_post: "(Proof.context -> Proof.context) -> theory -> theory"} \\
38507
06728ef076a7 output whitespace tuning
haftmann
parents: 38505
diff changeset
   186
  @{index_ML Code_Preproc.add_functrans: "
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55372
diff changeset
   187
    string * (Proof.context -> (thm * bool) list -> (thm * bool) list option)
38507
06728ef076a7 output whitespace tuning
haftmann
parents: 38505
diff changeset
   188
      -> theory -> theory"} \\
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   189
  @{index_ML Code_Preproc.del_functrans: "string -> theory -> theory"} \\
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   190
  @{index_ML Code.add_datatype: "(string * typ) list -> theory -> theory"} \\
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   191
  @{index_ML Code.get_type: "theory -> string
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   192
    -> ((string * sort) list * (string * ((string * sort) list * typ list)) list) * bool"} \\
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   193
  @{index_ML Code.get_type_of_constr_or_abstr: "theory -> string -> (string * bool) option"}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   194
  \end{mldecls}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   195
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   196
  \begin{description}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   197
38510
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   198
  \item @{ML Code.read_const}~@{text thy}~@{text s}
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   199
     reads a constant as a concrete term expression @{text s}.
ec0408c7328b stub for evaluation chapter
haftmann
parents: 38507
diff changeset
   200
63239
d562c9948dee explicit tagging of code equations de-baroquifies interface
haftmann
parents: 61891
diff changeset
   201
  \item @{ML Code.add_eqn}~@{text "(kind, default)"}~@{text "thm"}~@{text "thy"} adds code equation
d562c9948dee explicit tagging of code equations de-baroquifies interface
haftmann
parents: 61891
diff changeset
   202
     @{text "thm"} to executable content.
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   203
63239
d562c9948dee explicit tagging of code equations de-baroquifies interface
haftmann
parents: 61891
diff changeset
   204
  \item @{ML Code.del_eqn}~@{text "thm"}~@{text "thy"} removes code equation
d562c9948dee explicit tagging of code equations de-baroquifies interface
haftmann
parents: 61891
diff changeset
   205
     @{text "thm"} from executable content, if present.
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   206
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   207
  \item @{ML Code_Preproc.map_pre}~@{text "f"}~@{text "thy"} changes
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   208
     the preprocessor simpset.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   209
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   210
  \item @{ML Code_Preproc.add_functrans}~@{text "(name, f)"}~@{text "thy"} adds
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   211
     function transformer @{text f} (named @{text name}) to executable content;
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   212
     @{text f} is a transformer of the code equations belonging
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   213
     to a certain function definition, depending on the
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   214
     current theory context.  Returning @{text NONE} indicates that no
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   215
     transformation took place;  otherwise, the whole process will be iterated
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   216
     with the new code equations.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   217
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   218
  \item @{ML Code_Preproc.del_functrans}~@{text "name"}~@{text "thy"} removes
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   219
     function transformer named @{text name} from executable content.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   220
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   221
  \item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   222
     a datatype to executable content, with generation
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   223
     set @{text cs}.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   224
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   225
  \item @{ML Code.get_type_of_constr_or_abstr}~@{text "thy"}~@{text "const"}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   226
     returns type constructor corresponding to
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   227
     constructor @{text const}; returns @{text NONE}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   228
     if @{text const} is no constructor.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   229
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   230
  \end{description}
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   231
\<close>
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   232
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   233
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   234
subsubsection \<open>Data depending on the theory's executable content\<close>
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   235
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   236
text \<open>
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   237
  Implementing code generator applications on top of the framework set
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   238
  out so far usually not only involves using those primitive
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   239
  interfaces but also storing code-dependent data and various other
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   240
  things.
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   241
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   242
  Due to incrementality of code generation, changes in the theory's
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   243
  executable content have to be propagated in a certain fashion.
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   244
  Additionally, such changes may occur not only during theory
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   245
  extension but also during theory merge, which is a little bit nasty
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   246
  from an implementation point of view.  The framework provides a
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   247
  solution to this technical challenge by providing a functorial data
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   248
  slot @{ML_functor Code_Data}; on instantiation of this functor, the
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   249
  following types and operations are required:
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   250
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   251
  \medskip
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   252
  \begin{tabular}{l}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   253
  @{text "type T"} \\
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   254
  @{text "val empty: T"} \\
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   255
  \end{tabular}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   256
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   257
  \begin{description}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   258
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   259
  \item @{text T} the type of data to store.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   260
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   261
  \item @{text empty} initial (empty) data.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   262
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   263
  \end{description}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   264
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   265
  \noindent An instance of @{ML_functor Code_Data} provides the
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   266
  following interface:
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   267
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   268
  \medskip
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   269
  \begin{tabular}{l}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   270
  @{text "change: theory \<rightarrow> (T \<rightarrow> T) \<rightarrow> T"} \\
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   271
  @{text "change_yield: theory \<rightarrow> (T \<rightarrow> 'a * T) \<rightarrow> 'a * T"}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   272
  \end{tabular}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   273
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   274
  \begin{description}
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   275
40752
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   276
  \item @{text change} update of current data (cached!) by giving a
aae9a020fa77 tuned formatting;
haftmann
parents: 40352
diff changeset
   277
    continuation.
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   278
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   279
  \item @{text change_yield} update with side result.
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   280
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   281
  \end{description}
59377
056945909f60 modernized cartouches
haftmann
parents: 59003
diff changeset
   282
\<close>
38405
7935b334893e sketch of new outline
haftmann
parents: 37836
diff changeset
   283
28213
b52f9205a02d New outline for codegen tutorial -- draft
haftmann
parents:
diff changeset
   284
end