doc-src/Codegen/Thy/Evaluation.thy
author haftmann
Thu, 02 Sep 2010 16:41:42 +0200
changeset 39065 16a2ed09217a
parent 38510 ec0408c7328b
child 39067 2accb6526d11
permissions -rw-r--r--
set depth to 1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38510
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     1
theory Evaluation
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     2
imports Setup
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     3
begin
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     4
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     5
section {* Evaluation *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     6
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     7
text {* Introduction *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     8
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
     9
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    10
subsection {* Evaluation techniques *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    11
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    12
text {* simplifier *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    13
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    14
text {* nbe *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    15
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    16
text {* eval target: SML standalone vs. Isabelle/SML, example, soundness *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    17
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    18
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    19
subsection {* Dynamic evaluation *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    20
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    21
text {* value (three variants) *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    22
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    23
text {* methods (three variants) *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    24
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    25
text {* corresponding ML interfaces *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    26
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    27
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    28
subsection {* Static evaluation *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    29
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    30
text {* code_simp, nbe (tbd), Eval (tbd, in simple fashion) *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    31
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    32
text {* hand-written: code antiquotation *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    33
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    34
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    35
subsection {* Hybrid techniques *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    36
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    37
text {* code reflect runtime *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    38
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    39
text {* code reflect external *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    40
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    41
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    42
text {* FIXME here the old sections follow *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    43
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    44
subsection {* Evaluation oracle *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    45
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    46
text {*
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    47
  Code generation may also be used to \emph{evaluate} expressions
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    48
  (using @{text SML} as target language of course).
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    49
  For instance, the @{command_def value} reduces an expression to a
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    50
  normal form with respect to the underlying code equations:
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    51
*}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    52
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    53
value %quote "42 / (12 :: rat)"
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    54
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    55
text {*
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    56
  \noindent will display @{term "7 / (2 :: rat)"}.
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    57
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    58
  The @{method eval} method tries to reduce a goal by code generation to @{term True}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    59
  and solves it in that case, but fails otherwise:
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    60
*}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    61
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    62
lemma %quote "42 / (12 :: rat) = 7 / 2"
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    63
  by %quote eval
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    64
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    65
text {*
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    66
  \noindent The soundness of the @{method eval} method depends crucially 
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    67
  on the correctness of the code generator;  this is one of the reasons
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    68
  why you should not use adaptation (see \secref{sec:adaptation}) frivolously.
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    69
*}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    70
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    71
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    72
subsubsection {* Code antiquotation *}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    73
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    74
text {*
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    75
  In scenarios involving techniques like reflection it is quite common
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    76
  that code generated from a theory forms the basis for implementing
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    77
  a proof procedure in @{text SML}.  To facilitate interfacing of generated code
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    78
  with system code, the code generator provides a @{text code} antiquotation:
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    79
*}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    80
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    81
datatype %quote form = T | F | And form form | Or form form (*<*)
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    82
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    83
(*>*) ML %quotett {*
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    84
  fun eval_form @{code T} = true
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    85
    | eval_form @{code F} = false
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    86
    | eval_form (@{code And} (p, q)) =
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    87
        eval_form p andalso eval_form q
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    88
    | eval_form (@{code Or} (p, q)) =
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    89
        eval_form p orelse eval_form q;
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    90
*}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    91
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    92
text {*
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    93
  \noindent @{text code} takes as argument the name of a constant;  after the
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    94
  whole @{text SML} is read, the necessary code is generated transparently
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    95
  and the corresponding constant names are inserted.  This technique also
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    96
  allows to use pattern matching on constructors stemming from compiled
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    97
  @{text "datatypes"}.
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    98
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
    99
  For a less simplistic example, theory @{theory Ferrack} is
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
   100
  a good reference.
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
   101
*}
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
   102
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
   103
ec0408c7328b stub for evaluation chapter
haftmann
parents:
diff changeset
   104
end