doc-src/Codegen/Thy/ML.thy
author haftmann
Thu, 14 May 2009 15:09:48 +0200
changeset 31156 90fed3d4430f
parent 31143 2ce5c0c4d697
child 31999 cb1f26c0de5b
permissions -rw-r--r--
merged module code_unit.ML into code.ML
     1 theory "ML"
     2 imports Setup
     3 begin
     4 
     5 section {* ML system interfaces \label{sec:ml} *}
     6 
     7 text {*
     8   Since the code generator framework not only aims to provide
     9   a nice Isar interface but also to form a base for
    10   code-generation-based applications, here a short
    11   description of the most important ML interfaces.
    12 *}
    13 
    14 subsection {* Executable theory content: @{text Code} *}
    15 
    16 text {*
    17   This Pure module implements the core notions of
    18   executable content of a theory.
    19 *}
    20 
    21 subsubsection {* Managing executable content *}
    22 
    23 text %mlref {*
    24   \begin{mldecls}
    25   @{index_ML Code.add_eqn: "thm -> theory -> theory"} \\
    26   @{index_ML Code.del_eqn: "thm -> theory -> theory"} \\
    27   @{index_ML Code.add_eqnl: "string * (thm * bool) list lazy -> theory -> theory"} \\
    28   @{index_ML Code_Preproc.map_pre: "(simpset -> simpset) -> theory -> theory"} \\
    29   @{index_ML Code_Preproc.map_post: "(simpset -> simpset) -> theory -> theory"} \\
    30   @{index_ML Code_Preproc.add_functrans: "string * (theory -> (thm * bool) list -> (thm * bool) list option)
    31     -> theory -> theory"} \\
    32   @{index_ML Code_Preproc.del_functrans: "string -> theory -> theory"} \\
    33   @{index_ML Code.add_datatype: "(string * typ) list -> theory -> theory"} \\
    34   @{index_ML Code.get_datatype: "theory -> string
    35     -> (string * sort) list * (string * typ list) list"} \\
    36   @{index_ML Code.get_datatype_of_constr: "theory -> string -> string option"}
    37   \end{mldecls}
    38 
    39   \begin{description}
    40 
    41   \item @{ML Code.add_eqn}~@{text "thm"}~@{text "thy"} adds function
    42      theorem @{text "thm"} to executable content.
    43 
    44   \item @{ML Code.del_eqn}~@{text "thm"}~@{text "thy"} removes function
    45      theorem @{text "thm"} from executable content, if present.
    46 
    47   \item @{ML Code.add_eqnl}~@{text "(const, lthms)"}~@{text "thy"} adds
    48      suspended code equations @{text lthms} for constant
    49      @{text const} to executable content.
    50 
    51   \item @{ML Code_Preproc.map_pre}~@{text "f"}~@{text "thy"} changes
    52      the preprocessor simpset.
    53 
    54   \item @{ML Code_Preproc.add_functrans}~@{text "(name, f)"}~@{text "thy"} adds
    55      function transformer @{text f} (named @{text name}) to executable content;
    56      @{text f} is a transformer of the code equations belonging
    57      to a certain function definition, depending on the
    58      current theory context.  Returning @{text NONE} indicates that no
    59      transformation took place;  otherwise, the whole process will be iterated
    60      with the new code equations.
    61 
    62   \item @{ML Code_Preproc.del_functrans}~@{text "name"}~@{text "thy"} removes
    63      function transformer named @{text name} from executable content.
    64 
    65   \item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds
    66      a datatype to executable content, with generation
    67      set @{text cs}.
    68 
    69   \item @{ML Code.get_datatype_of_constr}~@{text "thy"}~@{text "const"}
    70      returns type constructor corresponding to
    71      constructor @{text const}; returns @{text NONE}
    72      if @{text const} is no constructor.
    73 
    74   \end{description}
    75 *}
    76 
    77 subsection {* Auxiliary *}
    78 
    79 text %mlref {*
    80   \begin{mldecls}
    81   @{index_ML Code.read_const: "theory -> string -> string"} \\
    82   @{index_ML Code.rewrite_eqn: "simpset -> thm -> thm"} \\
    83   \end{mldecls}
    84 
    85   \begin{description}
    86 
    87   \item @{ML Code.read_const}~@{text thy}~@{text s}
    88      reads a constant as a concrete term expression @{text s}.
    89 
    90   \item @{ML Code.rewrite_eqn}~@{text ss}~@{text thm}
    91      rewrites a code equation @{text thm} with a simpset @{text ss};
    92      only arguments and right hand side are rewritten,
    93      not the head of the code equation.
    94 
    95   \end{description}
    96 
    97 *}
    98 
    99 subsection {* Implementing code generator applications *}
   100 
   101 text {*
   102   Implementing code generator applications on top
   103   of the framework set out so far usually not only
   104   involves using those primitive interfaces
   105   but also storing code-dependent data and various
   106   other things.
   107 *}
   108 
   109 subsubsection {* Data depending on the theory's executable content *}
   110 
   111 text {*
   112   Due to incrementality of code generation, changes in the
   113   theory's executable content have to be propagated in a
   114   certain fashion.  Additionally, such changes may occur
   115   not only during theory extension but also during theory
   116   merge, which is a little bit nasty from an implementation
   117   point of view.  The framework provides a solution
   118   to this technical challenge by providing a functorial
   119   data slot @{ML_functor CodeDataFun}; on instantiation
   120   of this functor, the following types and operations
   121   are required:
   122 
   123   \medskip
   124   \begin{tabular}{l}
   125   @{text "type T"} \\
   126   @{text "val empty: T"} \\
   127   @{text "val purge: theory \<rightarrow> string list option \<rightarrow> T \<rightarrow> T"}
   128   \end{tabular}
   129 
   130   \begin{description}
   131 
   132   \item @{text T} the type of data to store.
   133 
   134   \item @{text empty} initial (empty) data.
   135 
   136   \item @{text purge}~@{text thy}~@{text consts} propagates changes in executable content;
   137     @{text consts} indicates the kind
   138     of change: @{ML NONE} stands for a fundamental change
   139     which invalidates any existing code, @{text "SOME consts"}
   140     hints that executable content for constants @{text consts}
   141     has changed.
   142 
   143   \end{description}
   144 
   145   \noindent An instance of @{ML_functor CodeDataFun} provides the following
   146   interface:
   147 
   148   \medskip
   149   \begin{tabular}{l}
   150   @{text "get: theory \<rightarrow> T"} \\
   151   @{text "change: theory \<rightarrow> (T \<rightarrow> T) \<rightarrow> T"} \\
   152   @{text "change_yield: theory \<rightarrow> (T \<rightarrow> 'a * T) \<rightarrow> 'a * T"}
   153   \end{tabular}
   154 
   155   \begin{description}
   156 
   157   \item @{text get} retrieval of the current data.
   158 
   159   \item @{text change} update of current data (cached!)
   160     by giving a continuation.
   161 
   162   \item @{text change_yield} update with side result.
   163 
   164   \end{description}
   165 *}
   166 
   167 text {*
   168   \bigskip
   169 
   170   \emph{Happy proving, happy hacking!}
   171 *}
   172 
   173 end