doc-src/Codegen/Thy/Further.thy
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 15:03:34 +0200
branchisac-from-Isabelle2009-2
changeset 37913 20e3616b2d9c
parent 37208 1f1f9cbd23ae
child 37401 04d58897bf90
permissions -rw-r--r--
prepare reactivation of isac-update-Isa09-2
haftmann@28213
     1
theory Further
haftmann@28213
     2
imports Setup
haftmann@28213
     3
begin
haftmann@28213
     4
haftmann@28447
     5
section {* Further issues \label{sec:further} *}
haftmann@28213
     6
haftmann@28419
     7
subsection {* Further reading *}
haftmann@28419
     8
haftmann@28419
     9
text {*
paulson@34155
    10
  To dive deeper into the issue of code generation, you should visit
paulson@34155
    11
  the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}, which
haftmann@28593
    12
  contains exhaustive syntax diagrams.
haftmann@28419
    13
*}
haftmann@28419
    14
haftmann@28419
    15
subsection {* Modules *}
haftmann@28419
    16
haftmann@28419
    17
text {*
haftmann@28419
    18
  When invoking the @{command export_code} command it is possible to leave
haftmann@28419
    19
  out the @{keyword "module_name"} part;  then code is distributed over
haftmann@28419
    20
  different modules, where the module name space roughly is induced
haftmann@28635
    21
  by the @{text Isabelle} theory name space.
haftmann@28419
    22
haftmann@28419
    23
  Then sometimes the awkward situation occurs that dependencies between
haftmann@28419
    24
  definitions introduce cyclic dependencies between modules, which in the
haftmann@28419
    25
  @{text Haskell} world leaves you to the mercy of the @{text Haskell} implementation
haftmann@28419
    26
  you are using,  while for @{text SML}/@{text OCaml} code generation is not possible.
haftmann@28419
    27
haftmann@28419
    28
  A solution is to declare module names explicitly.
haftmann@28419
    29
  Let use assume the three cyclically dependent
haftmann@28419
    30
  modules are named \emph{A}, \emph{B} and \emph{C}.
haftmann@28419
    31
  Then, by stating
haftmann@28419
    32
*}
haftmann@28419
    33
haftmann@28593
    34
code_modulename %quote SML
haftmann@28419
    35
  A ABC
haftmann@28419
    36
  B ABC
haftmann@28419
    37
  C ABC
haftmann@28419
    38
paulson@34155
    39
text {*\noindent
haftmann@28419
    40
  we explicitly map all those modules on \emph{ABC},
haftmann@28419
    41
  resulting in an ad-hoc merge of this three modules
haftmann@28419
    42
  at serialisation time.
haftmann@28419
    43
*}
haftmann@28213
    44
haftmann@28213
    45
subsection {* Evaluation oracle *}
haftmann@28213
    46
haftmann@28593
    47
text {*
haftmann@28593
    48
  Code generation may also be used to \emph{evaluate} expressions
haftmann@28593
    49
  (using @{text SML} as target language of course).
paulson@34155
    50
  For instance, the @{command value} reduces an expression to a
haftmann@28593
    51
  normal form with respect to the underlying code equations:
haftmann@28593
    52
*}
haftmann@28593
    53
haftmann@28593
    54
value %quote "42 / (12 :: rat)"
haftmann@28593
    55
haftmann@28593
    56
text {*
haftmann@28593
    57
  \noindent will display @{term "7 / (2 :: rat)"}.
haftmann@28593
    58
haftmann@28593
    59
  The @{method eval} method tries to reduce a goal by code generation to @{term True}
haftmann@28593
    60
  and solves it in that case, but fails otherwise:
haftmann@28593
    61
*}
haftmann@28593
    62
haftmann@28593
    63
lemma %quote "42 / (12 :: rat) = 7 / 2"
haftmann@28593
    64
  by %quote eval
haftmann@28593
    65
haftmann@28593
    66
text {*
haftmann@28593
    67
  \noindent The soundness of the @{method eval} method depends crucially 
haftmann@28593
    68
  on the correctness of the code generator;  this is one of the reasons
haftmann@31050
    69
  why you should not use adaptation (see \secref{sec:adaptation}) frivolously.
haftmann@28593
    70
*}
haftmann@28593
    71
haftmann@28213
    72
subsection {* Code antiquotation *}
haftmann@28213
    73
haftmann@28593
    74
text {*
haftmann@28593
    75
  In scenarios involving techniques like reflection it is quite common
haftmann@28593
    76
  that code generated from a theory forms the basis for implementing
haftmann@28593
    77
  a proof procedure in @{text SML}.  To facilitate interfacing of generated code
haftmann@28593
    78
  with system code, the code generator provides a @{text code} antiquotation:
haftmann@28593
    79
*}
haftmann@28213
    80
haftmann@30880
    81
datatype %quote form = T | F | And form form | Or form form (*<*)
haftmann@30880
    82
haftmann@30880
    83
(*>*) ML %quotett {*
haftmann@28593
    84
  fun eval_form @{code T} = true
haftmann@28593
    85
    | eval_form @{code F} = false
haftmann@28593
    86
    | eval_form (@{code And} (p, q)) =
haftmann@28593
    87
        eval_form p andalso eval_form q
haftmann@28593
    88
    | eval_form (@{code Or} (p, q)) =
haftmann@28593
    89
        eval_form p orelse eval_form q;
haftmann@28593
    90
*}
haftmann@28593
    91
haftmann@28593
    92
text {*
haftmann@28593
    93
  \noindent @{text code} takes as argument the name of a constant;  after the
haftmann@28593
    94
  whole @{text SML} is read, the necessary code is generated transparently
haftmann@28593
    95
  and the corresponding constant names are inserted.  This technique also
haftmann@28593
    96
  allows to use pattern matching on constructors stemming from compiled
haftmann@37208
    97
  @{text "datatypes"}.
haftmann@28593
    98
haftmann@29733
    99
  For a less simplistic example, theory @{theory Ferrack} is
haftmann@28593
   100
  a good reference.
haftmann@28593
   101
*}
haftmann@28213
   102
haftmann@28419
   103
subsection {* Imperative data structures *}
haftmann@28419
   104
haftmann@28593
   105
text {*
haftmann@28593
   106
  If you consider imperative data structures as inevitable for a specific
haftmann@28593
   107
  application, you should consider
haftmann@28593
   108
  \emph{Imperative Functional Programming with Isabelle/HOL}
paulson@34155
   109
  \cite{bulwahn-et-al:2008:imperative};
haftmann@28593
   110
  the framework described there is available in theory @{theory Imperative_HOL}.
haftmann@28593
   111
*}
haftmann@28593
   112
haftmann@28213
   113
end