5 section {* ML system interfaces \label{sec:ml} *}
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.
14 subsection {* Executable theory content: @{text Code} *}
17 This Pure module implements the core notions of
18 executable content of a theory.
21 subsubsection {* Managing executable content *}
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"}
41 \item @{ML Code.add_eqn}~@{text "thm"}~@{text "thy"} adds function
42 theorem @{text "thm"} to executable content.
44 \item @{ML Code.del_eqn}~@{text "thm"}~@{text "thy"} removes function
45 theorem @{text "thm"} from executable content, if present.
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.
51 \item @{ML Code_Preproc.map_pre}~@{text "f"}~@{text "thy"} changes
52 the preprocessor simpset.
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.
62 \item @{ML Code_Preproc.del_functrans}~@{text "name"}~@{text "thy"} removes
63 function transformer named @{text name} from executable content.
65 \item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds
66 a datatype to executable content, with generation
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.
77 subsection {* Auxiliary *}
81 @{index_ML Code.read_const: "theory -> string -> string"} \\
82 @{index_ML Code.rewrite_eqn: "simpset -> thm -> thm"} \\
87 \item @{ML Code.read_const}~@{text thy}~@{text s}
88 reads a constant as a concrete term expression @{text s}.
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.
99 subsection {* Implementing code generator applications *}
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
109 subsubsection {* Data depending on the theory's executable content *}
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
126 @{text "val empty: T"} \\
127 @{text "val purge: theory \<rightarrow> string list option \<rightarrow> T \<rightarrow> T"}
132 \item @{text T} the type of data to store.
134 \item @{text empty} initial (empty) data.
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}
145 \noindent An instance of @{ML_functor CodeDataFun} provides the following
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"}
157 \item @{text get} retrieval of the current data.
159 \item @{text change} update of current data (cached!)
160 by giving a continuation.
162 \item @{text change_yield} update with side result.
170 \emph{Happy proving, happy hacking!}