doc-src/Codegen/Thy/Evaluation.thy
changeset 39609 0af12dea761f
parent 39599 d9c247f7afa3
child 39643 29cc021398fc
equal deleted inserted replaced
39608:76bc7e4999f8 39609:0af12dea761f
    68   be trusted, including code generator configurations by the user.
    68   be trusted, including code generator configurations by the user.
    69 
    69 
    70   Evaluation is carried out in a target language \emph{Eval} which
    70   Evaluation is carried out in a target language \emph{Eval} which
    71   inherits from \emph{SML} but for convenience uses parts of the
    71   inherits from \emph{SML} but for convenience uses parts of the
    72   Isabelle runtime environment.  The soundness of computation carried
    72   Isabelle runtime environment.  The soundness of computation carried
    73   out there crucially on the correctness of the code generator; this
    73   out there depends crucially on the correctness of the code
    74   is one of the reasons why you should not use adaptation (see
    74   generator; this is one of the reasons why you should not use
    75   \secref{sec:adaptation}) frivolously.
    75   adaptation (see \secref{sec:adaptation}) frivolously.
    76 *}
    76 *}
    77 
    77 
    78 
    78 
    79 subsection {* Aspects of evaluation *}
    79 subsection {* Aspects of evaluation *}
    80 
    80 
   185 *}
   185 *}
   186 
   186 
   187 
   187 
   188 subsection {* Intimate connection between logic and system runtime *}
   188 subsection {* Intimate connection between logic and system runtime *}
   189 
   189 
   190 text {* FIXME *}
   190 text {*
   191 
   191   The toolbox of static evaluation conversions forms a reasonable base
   192 
   192   to interweave generated code and system tools.  However in some
   193 subsubsection {* Static embedding of generated code into system runtime -- the code antiquotation *}
   193   situations more direct interaction is desirable.
   194 
   194 *}
   195 text {*
   195 
   196   FIXME
   196 
   197 
   197 subsubsection {* Static embedding of generated code into system runtime -- the @{text code} antiquotation *}
   198   In scenarios involving techniques like reflection it is quite common
   198 
   199   that code generated from a theory forms the basis for implementing a
   199 text {*
   200   proof procedure in @{text SML}.  To facilitate interfacing of
   200   The @{text code} antiquotation allows to include constants from
   201   generated code with system code, the code generator provides a
   201   generated code directly into ML system code, as in the following toy
   202   @{text code} antiquotation:
   202   example:
   203 *}
   203 *}
   204 
   204 
   205 datatype %quote form = T | F | And form form | Or form form (*<*)
   205 datatype %quote form = T | F | And form form | Or form form (*<*)
   206 
   206 
   207 (*>*) ML %quotett {*
   207 (*>*) ML %quotett {*
   212     | eval_form (@{code Or} (p, q)) =
   212     | eval_form (@{code Or} (p, q)) =
   213         eval_form p orelse eval_form q;
   213         eval_form p orelse eval_form q;
   214 *}
   214 *}
   215 
   215 
   216 text {*
   216 text {*
   217   \noindent @{text code} takes as argument the name of a constant;  after the
   217   \noindent @{text code} takes as argument the name of a constant;
   218   whole @{text SML} is read, the necessary code is generated transparently
   218   after the whole ML is read, the necessary code is generated
   219   and the corresponding constant names are inserted.  This technique also
   219   transparently and the corresponding constant names are inserted.
   220   allows to use pattern matching on constructors stemming from compiled
   220   This technique also allows to use pattern matching on constructors
   221   @{text "datatypes"}.
   221   stemming from compiled @{text "datatypes"}.  Note that it is not
   222 
   222   possible to refer to constants which carry adaptations by means of
   223   For a less simplistic example, theory @{text Ferrack} is
   223   @{text code}; here you have to refer to the adapted code directly.
   224   a good reference.
   224 
       
   225   For a less simplistic example, theory @{text Approximation} in
       
   226   the @{text Decision_Procs} session is a good reference.
   225 *}
   227 *}
   226 
   228 
   227 
   229 
   228 subsubsection {* Static embedding of generated code into system runtime -- @{text code_reflect} *}
   230 subsubsection {* Static embedding of generated code into system runtime -- @{text code_reflect} *}
   229 
   231 
   230 text {* FIXME @{command_def code_reflect} *}
   232 text {*
       
   233   The @{text code} antiquoation is lightweight, but the generated code
       
   234   is only accessible while the ML section is processed.  Sometimes this
       
   235   is not appropriate, especially if the generated code contains datatype
       
   236   declarations which are shared with other parts of the system.  In these
       
   237   cases, @{command_def code_reflect} can be used:
       
   238 *}
       
   239 
       
   240 code_reflect %quote Sum_Type
       
   241   datatypes sum = Inl | Inr
       
   242   functions "Sum_Type.Projl" "Sum_Type.Projr"
       
   243 
       
   244 text {*
       
   245   \noindent @{command_def code_reflect} takes a structure name and
       
   246   references to datatypes and functions; for these code is compiled
       
   247   into the named ML structure and the \emph{Eval} target is modified
       
   248   in a way that future code generation will reference these
       
   249   precompiled versions of the given datatypes and functions.  This
       
   250   also allows to refer to the referenced datatypes and functions from
       
   251   arbitrary ML code as well.
       
   252 
       
   253   A typical example for @{command code_reflect} can be found in the
       
   254   @{theory Predicate} theory.
       
   255 *}
       
   256 
   231 
   257 
   232 subsubsection {* Separate compilation -- @{text code_reflect} *}
   258 subsubsection {* Separate compilation -- @{text code_reflect} *}
   233 
   259 
   234 text {* FIXME *}
   260 text {*
       
   261   For technical reasons it is sometimes necessary to separate
       
   262   generation and compilation of code which is supposed to be used in
       
   263   the system runtime.  For this @{command code_reflect} with an
       
   264   optional @{text "file"} argument can be used:
       
   265 *}
       
   266 
       
   267 code_reflect %quote Rat
       
   268   datatypes rat = Frct
       
   269   functions Fract
       
   270     "(plus :: rat \<Rightarrow> rat \<Rightarrow> rat)" "(minus :: rat \<Rightarrow> rat \<Rightarrow> rat)"
       
   271     "(times :: rat \<Rightarrow> rat \<Rightarrow> rat)" "(divide :: rat \<Rightarrow> rat \<Rightarrow> rat)"
       
   272   file "examples/rat.ML"
       
   273 
       
   274 text {*
       
   275   \noindent This just generates the references code to the specified
       
   276   file which can be included into the system runtime later on.
       
   277 *}
   235 
   278 
   236 end
   279 end