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