doc-src/IsarImplementation/Thy/ML.thy
changeset 39827 d829ce302ca4
parent 39825 f9066b94bf07
child 39829 f5ea50decd9f
equal deleted inserted replaced
39826:855104e1047c 39827:d829ce302ca4
    51   be implemented in ML within the Isar context in the same manner: ML
    51   be implemented in ML within the Isar context in the same manner: ML
    52   is part of the regular user-space repertoire of Isabelle.
    52   is part of the regular user-space repertoire of Isabelle.
    53 *}
    53 *}
    54 
    54 
    55 
    55 
    56 section {* Isar ML commands *}
    56 subsection {* Isar ML commands *}
    57 
    57 
    58 text {* The primary Isar source language provides various facilities
    58 text {* The primary Isar source language provides various facilities
    59   to open a ``window'' to the underlying ML compiler.  Especially see
    59   to open a ``window'' to the underlying ML compiler.  Especially see
    60   @{command_ref "use"} and @{command_ref "ML"}, which work exactly the
    60   @{command_ref "use"} and @{command_ref "ML"}, which work exactly the
    61   same way, only the source text is provided via a file vs.\ inlined,
    61   same way, only the source text is provided via a file vs.\ inlined,
   129   ML_val {* factorial 100 *}  (* FIXME check/fix indentation *)
   129   ML_val {* factorial 100 *}  (* FIXME check/fix indentation *)
   130   ML_command {* writeln (string_of_int (factorial 100)) *}
   130   ML_command {* writeln (string_of_int (factorial 100)) *}
   131 qed
   131 qed
   132 
   132 
   133 
   133 
   134 section {* Compile-time context *}
   134 subsection {* Compile-time context *}
   135 
   135 
   136 text {* Whenever the ML compiler is invoked within Isabelle/Isar, the
   136 text {* Whenever the ML compiler is invoked within Isabelle/Isar, the
   137   formal context is passed as a thread-local reference variable.  Thus
   137   formal context is passed as a thread-local reference variable.  Thus
   138   ML code may access the theory context during compilation, by reading
   138   ML code may access the theory context during compilation, by reading
   139   or writing the (local) theory under construction.  Note that such
   139   or writing the (local) theory under construction.  Note that such
   151 
   151 
   152   \item @{ML "ML_Context.the_generic_context ()"} refers to the theory
   152   \item @{ML "ML_Context.the_generic_context ()"} refers to the theory
   153   context of the ML toplevel --- at compile time.  ML code needs to
   153   context of the ML toplevel --- at compile time.  ML code needs to
   154   take care to refer to @{ML "ML_Context.the_generic_context ()"}
   154   take care to refer to @{ML "ML_Context.the_generic_context ()"}
   155   correctly.  Recall that evaluation of a function body is delayed
   155   correctly.  Recall that evaluation of a function body is delayed
   156   until actual runtime.
   156   until actual run-time.
   157 
   157 
   158   \item @{ML "Context.>>"}~@{text f} applies context transformation
   158   \item @{ML "Context.>>"}~@{text f} applies context transformation
   159   @{text f} to the implicit context of the ML toplevel.
   159   @{text f} to the implicit context of the ML toplevel.
   160 
   160 
   161   \end{description}
   161   \end{description}
   162 
   162 
   163   It is very important to note that the above functions are really
   163   It is very important to note that the above functions are really
   164   restricted to the compile time, even though the ML compiler is
   164   restricted to the compile time, even though the ML compiler is
   165   invoked at runtime.  The majority of ML code either uses static
   165   invoked at run-time.  The majority of ML code either uses static
   166   antiquotations (\secref{sec:ML-antiq}) or refers to the theory or
   166   antiquotations (\secref{sec:ML-antiq}) or refers to the theory or
   167   proof context at run-time, by explicit functional abstraction.
   167   proof context at run-time, by explicit functional abstraction.
   168 *}
   168 *}
   169 
   169 
   170 
   170 
   171 section {* Antiquotations \label{sec:ML-antiq} *}
   171 subsection {* Antiquotations \label{sec:ML-antiq} *}
   172 
   172 
   173 text FIXME
   173 text {* A very important consequence of embedding SML into Isar is the
   174 
   174   concept of \emph{ML antiquotation}: the standard token language of
       
   175   ML is augmented by special syntactic entities of the following form:
       
   176 
       
   177   \begin{rail}
       
   178   antiquote: atsign lbrace nameref args rbrace | lbracesym | rbracesym
       
   179   ;
       
   180   \end{rail}
       
   181 
       
   182   \noindent Here the syntax categories @{syntax nameref} and @{syntax
       
   183   args} are defined in \cite{isabelle-isar-ref}; attributes and proof
       
   184   methods use similar syntax.
       
   185 
       
   186   \medskip A regular antiquotation @{text "@{name args}"} processes
       
   187   its arguments by the usual means of the Isar source language, and
       
   188   produces corresponding ML source text, either as literal
       
   189   \emph{inline} text (e.g. @{text "@{term t}"}) or abstract
       
   190   \emph{value} (e.g. @{text "@{thm th}"}).  This pre-compilation
       
   191   scheme allows to refer to formal entities in a robust manner, with
       
   192   proper static scoping and with some degree of logical checking of
       
   193   small portions of the code.
       
   194 
       
   195   Special antiquotations like @{text "@{let \<dots>}"} or @{text "@{note
       
   196   \<dots>}"} augment the compilation context without generating code.  The
       
   197   non-ASCII braces @{text "\<lbrace>"} and @{text "\<rbrace>"} allow to delimit the
       
   198   effect by introducing local blocks within the pre-compilation
       
   199   environment.
       
   200 *}
   175 
   201 
   176 end
   202 end