src/Doc/Codegen/Further.thy
author haftmann
Mon Jun 06 21:28:46 2016 +0200 (2016-06-06)
changeset 63239 d562c9948dee
parent 61891 76189756ff65
child 63241 f59fd6cc935e
permissions -rw-r--r--
explicit tagging of code equations de-baroquifies interface
haftmann@28213
     1
theory Further
haftmann@59378
     2
imports Setup
haftmann@28213
     3
begin
haftmann@28213
     4
haftmann@59377
     5
section \<open>Further issues \label{sec:further}\<close>
haftmann@28419
     6
haftmann@59377
     7
subsection \<open>Specialities of the @{text Scala} target language \label{sec:scala}\<close>
haftmann@42096
     8
haftmann@59377
     9
text \<open>
haftmann@42096
    10
  @{text Scala} deviates from languages of the ML family in a couple
haftmann@42096
    11
  of aspects; those which affect code generation mainly have to do with
haftmann@42096
    12
  @{text Scala}'s type system:
haftmann@42096
    13
haftmann@42096
    14
  \begin{itemize}
haftmann@42096
    15
haftmann@42096
    16
    \item @{text Scala} prefers tupled syntax over curried syntax.
haftmann@42096
    17
haftmann@42096
    18
    \item @{text Scala} sacrifices Hindely-Milner type inference for a
haftmann@42096
    19
      much more rich type system with subtyping etc.  For this reason
haftmann@42096
    20
      type arguments sometimes have to be given explicitly in square
haftmann@46520
    21
      brackets (mimicking System F syntax).
haftmann@42096
    22
haftmann@42096
    23
    \item In contrast to @{text Haskell} where most specialities of
haftmann@42096
    24
      the type system are implemented using \emph{type classes},
haftmann@42096
    25
      @{text Scala} provides a sophisticated system of \emph{implicit
haftmann@42096
    26
      arguments}.
haftmann@42096
    27
haftmann@42096
    28
  \end{itemize}
haftmann@42096
    29
haftmann@42096
    30
  \noindent Concerning currying, the @{text Scala} serializer counts
haftmann@42096
    31
  arguments in code equations to determine how many arguments
haftmann@42096
    32
  shall be tupled; remaining arguments and abstractions in terms
haftmann@42096
    33
  rather than function definitions are always curried.
haftmann@42096
    34
haftmann@42096
    35
  The second aspect affects user-defined adaptations with @{command
haftmann@55372
    36
  code_printing}.  For regular terms, the @{text Scala} serializer prints
haftmann@42096
    37
  all type arguments explicitly.  For user-defined term adaptations
haftmann@42096
    38
  this is only possible for adaptations which take no arguments: here
haftmann@42096
    39
  the type arguments are just appended.  Otherwise they are ignored;
haftmann@42096
    40
  hence user-defined adaptations for polymorphic constants have to be
haftmann@42096
    41
  designed very carefully to avoid ambiguity.
haftmann@42096
    42
haftmann@42096
    43
  Isabelle's type classes are mapped onto @{text Scala} implicits; in
haftmann@42096
    44
  cases with diamonds in the subclass hierarchy this can lead to
haftmann@42096
    45
  ambiguities in the generated code:
haftmann@59377
    46
\<close>
haftmann@42096
    47
haftmann@42096
    48
class %quote class1 =
haftmann@42096
    49
  fixes foo :: "'a \<Rightarrow> 'a"
haftmann@42096
    50
haftmann@42096
    51
class %quote class2 = class1
haftmann@42096
    52
haftmann@42096
    53
class %quote class3 = class1
haftmann@42096
    54
haftmann@59377
    55
text \<open>
haftmann@42096
    56
  \noindent Here both @{class class2} and @{class class3} inherit from @{class class1},
haftmann@42096
    57
  forming the upper part of a diamond.
haftmann@59377
    58
\<close>
haftmann@42096
    59
haftmann@42096
    60
definition %quote bar :: "'a :: {class2, class3} \<Rightarrow> 'a" where
haftmann@42096
    61
  "bar = foo"
haftmann@42096
    62
haftmann@59377
    63
text \<open>
haftmann@42096
    64
  \noindent This yields the following code:
haftmann@59377
    65
\<close>
haftmann@42096
    66
haftmann@59377
    67
text %quotetypewriter \<open>
haftmann@42096
    68
  @{code_stmts bar (Scala)}
haftmann@59377
    69
\<close>
haftmann@42096
    70
haftmann@59377
    71
text \<open>
haftmann@42096
    72
  \noindent This code is rejected by the @{text Scala} compiler: in
haftmann@42096
    73
  the definition of @{text bar}, it is not clear from where to derive
haftmann@42096
    74
  the implicit argument for @{text foo}.
haftmann@42096
    75
haftmann@42096
    76
  The solution to the problem is to close the diamond by a further
haftmann@42096
    77
  class with inherits from both @{class class2} and @{class class3}:
haftmann@59377
    78
\<close>
haftmann@42096
    79
haftmann@42096
    80
class %quote class4 = class2 + class3
haftmann@42096
    81
haftmann@59377
    82
text \<open>
haftmann@42096
    83
  \noindent Then the offending code equation can be restricted to
haftmann@42096
    84
  @{class class4}:
haftmann@59377
    85
\<close>
haftmann@42096
    86
haftmann@42096
    87
lemma %quote [code]:
haftmann@42096
    88
  "(bar :: 'a::class4 \<Rightarrow> 'a) = foo"
haftmann@42096
    89
  by (simp only: bar_def)
haftmann@42096
    90
haftmann@59377
    91
text \<open>
haftmann@42096
    92
  \noindent with the following code:
haftmann@59377
    93
\<close>
haftmann@42096
    94
haftmann@59377
    95
text %quotetypewriter \<open>
haftmann@42096
    96
  @{code_stmts bar (Scala)}
haftmann@59377
    97
\<close>
haftmann@42096
    98
haftmann@59377
    99
text \<open>
haftmann@42096
   100
  \noindent which exposes no ambiguity.
haftmann@42096
   101
haftmann@42096
   102
  Since the preprocessor (cf.~\secref{sec:preproc}) propagates sort
haftmann@42096
   103
  constraints through a system of code equations, it is usually not
haftmann@42096
   104
  very difficult to identify the set of code equations which actually
haftmann@42096
   105
  needs more restricted sort constraints.
haftmann@59377
   106
\<close>
haftmann@42096
   107
haftmann@59377
   108
subsection \<open>Modules namespace\<close>
haftmann@28419
   109
haftmann@59377
   110
text \<open>
haftmann@40752
   111
  When invoking the @{command export_code} command it is possible to
haftmann@40752
   112
  leave out the @{keyword "module_name"} part; then code is
haftmann@40752
   113
  distributed over different modules, where the module name space
haftmann@40752
   114
  roughly is induced by the Isabelle theory name space.
haftmann@38437
   115
haftmann@40752
   116
  Then sometimes the awkward situation occurs that dependencies
haftmann@40752
   117
  between definitions introduce cyclic dependencies between modules,
haftmann@40752
   118
  which in the @{text Haskell} world leaves you to the mercy of the
haftmann@40752
   119
  @{text Haskell} implementation you are using, while for @{text
haftmann@40752
   120
  SML}/@{text OCaml} code generation is not possible.
haftmann@38437
   121
haftmann@40752
   122
  A solution is to declare module names explicitly.  Let use assume
haftmann@40752
   123
  the three cyclically dependent modules are named \emph{A}, \emph{B}
haftmann@40752
   124
  and \emph{C}.  Then, by stating
haftmann@59377
   125
\<close>
haftmann@38437
   126
haftmann@52378
   127
code_identifier %quote
haftmann@52378
   128
  code_module A \<rightharpoonup> (SML) ABC
haftmann@52378
   129
| code_module B \<rightharpoonup> (SML) ABC
haftmann@52378
   130
| code_module C \<rightharpoonup> (SML) ABC
haftmann@38437
   131
haftmann@59377
   132
text \<open>
haftmann@40752
   133
  \noindent we explicitly map all those modules on \emph{ABC},
haftmann@40752
   134
  resulting in an ad-hoc merge of this three modules at serialisation
haftmann@40752
   135
  time.
haftmann@59377
   136
\<close>
haftmann@28419
   137
haftmann@59377
   138
subsection \<open>Locales and interpretation\<close>
haftmann@37426
   139
haftmann@59377
   140
text \<open>
haftmann@37426
   141
  A technical issue comes to surface when generating code from
haftmann@61891
   142
  specifications stemming from locale interpretation into global
haftmann@61891
   143
  theories.
haftmann@37426
   144
haftmann@40752
   145
  Let us assume a locale specifying a power operation on arbitrary
haftmann@40752
   146
  types:
haftmann@59377
   147
\<close>
haftmann@37426
   148
haftmann@37426
   149
locale %quote power =
haftmann@37426
   150
  fixes power :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@37426
   151
  assumes power_commute: "power x \<circ> power y = power y \<circ> power x"
haftmann@37426
   152
begin
haftmann@37426
   153
haftmann@59377
   154
text \<open>
haftmann@40752
   155
  \noindent Inside that locale we can lift @{text power} to exponent
haftmann@61779
   156
  lists by means of a specification relative to that locale:
haftmann@59377
   157
\<close>
haftmann@37426
   158
haftmann@37426
   159
primrec %quote powers :: "'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@37426
   160
  "powers [] = id"
haftmann@37426
   161
| "powers (x # xs) = power x \<circ> powers xs"
haftmann@37426
   162
haftmann@37426
   163
lemma %quote powers_append:
haftmann@37426
   164
  "powers (xs @ ys) = powers xs \<circ> powers ys"
haftmann@37426
   165
  by (induct xs) simp_all
haftmann@37426
   166
haftmann@37426
   167
lemma %quote powers_power:
haftmann@37426
   168
  "powers xs \<circ> power x = power x \<circ> powers xs"
haftmann@37426
   169
  by (induct xs)
haftmann@49739
   170
    (simp_all del: o_apply id_apply add: comp_assoc,
haftmann@37426
   171
      simp del: o_apply add: o_assoc power_commute)
haftmann@37426
   172
haftmann@37426
   173
lemma %quote powers_rev:
haftmann@37426
   174
  "powers (rev xs) = powers xs"
haftmann@37426
   175
    by (induct xs) (simp_all add: powers_append powers_power)
haftmann@37426
   176
haftmann@37426
   177
end %quote
haftmann@37426
   178
haftmann@59377
   179
text \<open>
haftmann@38505
   180
  After an interpretation of this locale (say, @{command_def
haftmann@61891
   181
  global_interpretation} @{text "fun_power:"} @{term [source] "power (\<lambda>n (f
haftmann@61779
   182
  :: 'a \<Rightarrow> 'a). f ^^ n)"}), one could naively expect to have a constant @{text
haftmann@37426
   183
  "fun_power.powers :: nat list \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"} for which code
haftmann@37426
   184
  can be generated.  But this not the case: internally, the term
haftmann@37426
   185
  @{text "fun_power.powers"} is an abbreviation for the foundational
haftmann@37426
   186
  term @{term [source] "power.powers (\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"}
wenzelm@58620
   187
  (see @{cite "isabelle-locale"} for the details behind).
haftmann@37426
   188
haftmann@61891
   189
  Fortunately, a succint solution is available: a dedicated
haftmann@61779
   190
  rewrite definition:
haftmann@59377
   191
\<close>
haftmann@37426
   192
haftmann@61891
   193
global_interpretation %quote fun_power: power "(\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"
haftmann@61779
   194
  defines funpows = fun_power.powers
haftmann@61779
   195
  by unfold_locales
haftmann@61779
   196
    (simp_all add: fun_eq_iff funpow_mult mult.commute)
haftmann@37426
   197
haftmann@59377
   198
text \<open>
haftmann@61779
   199
  \noindent This amends the interpretation morphisms such that
haftmann@61779
   200
  occurrences of the foundational term @{term [source] "power.powers (\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"}
haftmann@61779
   201
  are folded to a newly defined constant @{const funpows}.
haftmann@37426
   202
haftmann@37426
   203
  After this setup procedure, code generation can continue as usual:
haftmann@59377
   204
\<close>
haftmann@37426
   205
haftmann@59377
   206
text %quotetypewriter \<open>
haftmann@39683
   207
  @{code_stmts funpows (consts) Nat.funpow funpows (Haskell)}
haftmann@61779
   208
\<close>
haftmann@37426
   209
haftmann@37426
   210
haftmann@59377
   211
subsection \<open>Parallel computation\<close>
haftmann@51172
   212
haftmann@59377
   213
text \<open>
wenzelm@52665
   214
  Theory @{text Parallel} in @{file "~~/src/HOL/Library"} contains
haftmann@51172
   215
  operations to exploit parallelism inside the Isabelle/ML
haftmann@51172
   216
  runtime engine.
haftmann@59377
   217
\<close>
haftmann@51172
   218
haftmann@59377
   219
subsection \<open>Imperative data structures\<close>
haftmann@28419
   220
haftmann@59377
   221
text \<open>
haftmann@38437
   222
  If you consider imperative data structures as inevitable for a
haftmann@38437
   223
  specific application, you should consider \emph{Imperative
haftmann@38437
   224
  Functional Programming with Isabelle/HOL}
wenzelm@58620
   225
  @{cite "bulwahn-et-al:2008:imperative"}; the framework described there
haftmann@40752
   226
  is available in session @{text Imperative_HOL}, together with a
haftmann@40752
   227
  short primer document.
haftmann@59377
   228
\<close>
haftmann@28419
   229
haftmann@28213
   230
haftmann@59377
   231
subsection \<open>ML system interfaces \label{sec:ml}\<close>
haftmann@38405
   232
haftmann@59377
   233
text \<open>
haftmann@38510
   234
  Since the code generator framework not only aims to provide a nice
haftmann@38510
   235
  Isar interface but also to form a base for code-generation-based
haftmann@38510
   236
  applications, here a short description of the most fundamental ML
haftmann@38510
   237
  interfaces.
haftmann@59377
   238
\<close>
haftmann@38405
   239
haftmann@59377
   240
subsubsection \<open>Managing executable content\<close>
haftmann@38405
   241
haftmann@59377
   242
text %mlref \<open>
haftmann@38405
   243
  \begin{mldecls}
haftmann@38510
   244
  @{index_ML Code.read_const: "theory -> string -> string"} \\
haftmann@63239
   245
  @{index_ML Code.add_eqn: "Code.kind * bool -> thm -> theory -> theory"} \\
haftmann@38405
   246
  @{index_ML Code.del_eqn: "thm -> theory -> theory"} \\
wenzelm@51717
   247
  @{index_ML Code_Preproc.map_pre: "(Proof.context -> Proof.context) -> theory -> theory"} \\
wenzelm@51717
   248
  @{index_ML Code_Preproc.map_post: "(Proof.context -> Proof.context) -> theory -> theory"} \\
haftmann@38507
   249
  @{index_ML Code_Preproc.add_functrans: "
haftmann@55757
   250
    string * (Proof.context -> (thm * bool) list -> (thm * bool) list option)
haftmann@38507
   251
      -> theory -> theory"} \\
haftmann@38405
   252
  @{index_ML Code_Preproc.del_functrans: "string -> theory -> theory"} \\
haftmann@38405
   253
  @{index_ML Code.add_datatype: "(string * typ) list -> theory -> theory"} \\
haftmann@38405
   254
  @{index_ML Code.get_type: "theory -> string
haftmann@40752
   255
    -> ((string * sort) list * (string * ((string * sort) list * typ list)) list) * bool"} \\
haftmann@38405
   256
  @{index_ML Code.get_type_of_constr_or_abstr: "theory -> string -> (string * bool) option"}
haftmann@38405
   257
  \end{mldecls}
haftmann@38405
   258
haftmann@38405
   259
  \begin{description}
haftmann@38405
   260
haftmann@38510
   261
  \item @{ML Code.read_const}~@{text thy}~@{text s}
haftmann@38510
   262
     reads a constant as a concrete term expression @{text s}.
haftmann@38510
   263
haftmann@63239
   264
  \item @{ML Code.add_eqn}~@{text "(kind, default)"}~@{text "thm"}~@{text "thy"} adds code equation
haftmann@63239
   265
     @{text "thm"} to executable content.
haftmann@38405
   266
haftmann@63239
   267
  \item @{ML Code.del_eqn}~@{text "thm"}~@{text "thy"} removes code equation
haftmann@63239
   268
     @{text "thm"} from executable content, if present.
haftmann@38405
   269
haftmann@38405
   270
  \item @{ML Code_Preproc.map_pre}~@{text "f"}~@{text "thy"} changes
haftmann@38405
   271
     the preprocessor simpset.
haftmann@38405
   272
haftmann@38405
   273
  \item @{ML Code_Preproc.add_functrans}~@{text "(name, f)"}~@{text "thy"} adds
haftmann@38405
   274
     function transformer @{text f} (named @{text name}) to executable content;
haftmann@38405
   275
     @{text f} is a transformer of the code equations belonging
haftmann@38405
   276
     to a certain function definition, depending on the
haftmann@38405
   277
     current theory context.  Returning @{text NONE} indicates that no
haftmann@38405
   278
     transformation took place;  otherwise, the whole process will be iterated
haftmann@38405
   279
     with the new code equations.
haftmann@38405
   280
haftmann@38405
   281
  \item @{ML Code_Preproc.del_functrans}~@{text "name"}~@{text "thy"} removes
haftmann@38405
   282
     function transformer named @{text name} from executable content.
haftmann@38405
   283
haftmann@38405
   284
  \item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds
haftmann@38405
   285
     a datatype to executable content, with generation
haftmann@38405
   286
     set @{text cs}.
haftmann@38405
   287
haftmann@38405
   288
  \item @{ML Code.get_type_of_constr_or_abstr}~@{text "thy"}~@{text "const"}
haftmann@38405
   289
     returns type constructor corresponding to
haftmann@38405
   290
     constructor @{text const}; returns @{text NONE}
haftmann@38405
   291
     if @{text const} is no constructor.
haftmann@38405
   292
haftmann@38405
   293
  \end{description}
haftmann@59377
   294
\<close>
haftmann@38405
   295
haftmann@38405
   296
haftmann@59377
   297
subsubsection \<open>Data depending on the theory's executable content\<close>
haftmann@38405
   298
haftmann@59377
   299
text \<open>
haftmann@40752
   300
  Implementing code generator applications on top of the framework set
haftmann@40752
   301
  out so far usually not only involves using those primitive
haftmann@40752
   302
  interfaces but also storing code-dependent data and various other
haftmann@40752
   303
  things.
haftmann@38405
   304
haftmann@40752
   305
  Due to incrementality of code generation, changes in the theory's
haftmann@40752
   306
  executable content have to be propagated in a certain fashion.
haftmann@40752
   307
  Additionally, such changes may occur not only during theory
haftmann@40752
   308
  extension but also during theory merge, which is a little bit nasty
haftmann@40752
   309
  from an implementation point of view.  The framework provides a
haftmann@40752
   310
  solution to this technical challenge by providing a functorial data
haftmann@40752
   311
  slot @{ML_functor Code_Data}; on instantiation of this functor, the
haftmann@40752
   312
  following types and operations are required:
haftmann@38405
   313
haftmann@38405
   314
  \medskip
haftmann@38405
   315
  \begin{tabular}{l}
haftmann@38405
   316
  @{text "type T"} \\
haftmann@38405
   317
  @{text "val empty: T"} \\
haftmann@38405
   318
  \end{tabular}
haftmann@38405
   319
haftmann@38405
   320
  \begin{description}
haftmann@38405
   321
haftmann@38405
   322
  \item @{text T} the type of data to store.
haftmann@38405
   323
haftmann@38405
   324
  \item @{text empty} initial (empty) data.
haftmann@38405
   325
haftmann@38405
   326
  \end{description}
haftmann@38405
   327
haftmann@40752
   328
  \noindent An instance of @{ML_functor Code_Data} provides the
haftmann@40752
   329
  following interface:
haftmann@38405
   330
haftmann@38405
   331
  \medskip
haftmann@38405
   332
  \begin{tabular}{l}
haftmann@38405
   333
  @{text "change: theory \<rightarrow> (T \<rightarrow> T) \<rightarrow> T"} \\
haftmann@38405
   334
  @{text "change_yield: theory \<rightarrow> (T \<rightarrow> 'a * T) \<rightarrow> 'a * T"}
haftmann@38405
   335
  \end{tabular}
haftmann@38405
   336
haftmann@38405
   337
  \begin{description}
haftmann@38405
   338
haftmann@40752
   339
  \item @{text change} update of current data (cached!) by giving a
haftmann@40752
   340
    continuation.
haftmann@38405
   341
haftmann@38405
   342
  \item @{text change_yield} update with side result.
haftmann@38405
   343
haftmann@38405
   344
  \end{description}
haftmann@59377
   345
\<close>
haftmann@38405
   346
haftmann@28213
   347
end
haftmann@46514
   348