28213
|
1 |
theory Further
|
|
2 |
imports Setup
|
|
3 |
begin
|
|
4 |
|
28447
|
5 |
section {* Further issues \label{sec:further} *}
|
28419
|
6 |
|
38437
|
7 |
subsection {* Modules namespace *}
|
28419
|
8 |
|
|
9 |
text {*
|
38437
|
10 |
When invoking the @{command export_code} command it is possible to leave
|
|
11 |
out the @{keyword "module_name"} part; then code is distributed over
|
|
12 |
different modules, where the module name space roughly is induced
|
|
13 |
by the Isabelle theory name space.
|
|
14 |
|
|
15 |
Then sometimes the awkward situation occurs that dependencies between
|
|
16 |
definitions introduce cyclic dependencies between modules, which in the
|
|
17 |
@{text Haskell} world leaves you to the mercy of the @{text Haskell} implementation
|
|
18 |
you are using, while for @{text SML}/@{text OCaml} code generation is not possible.
|
|
19 |
|
|
20 |
A solution is to declare module names explicitly.
|
|
21 |
Let use assume the three cyclically dependent
|
|
22 |
modules are named \emph{A}, \emph{B} and \emph{C}.
|
|
23 |
Then, by stating
|
|
24 |
*}
|
|
25 |
|
|
26 |
code_modulename %quote SML
|
|
27 |
A ABC
|
|
28 |
B ABC
|
|
29 |
C ABC
|
|
30 |
|
|
31 |
text {*\noindent
|
|
32 |
we explicitly map all those modules on \emph{ABC},
|
|
33 |
resulting in an ad-hoc merge of this three modules
|
|
34 |
at serialisation time.
|
28419
|
35 |
*}
|
|
36 |
|
37426
|
37 |
subsection {* Locales and interpretation *}
|
|
38 |
|
|
39 |
text {*
|
|
40 |
A technical issue comes to surface when generating code from
|
|
41 |
specifications stemming from locale interpretation.
|
|
42 |
|
|
43 |
Let us assume a locale specifying a power operation
|
|
44 |
on arbitrary types:
|
|
45 |
*}
|
|
46 |
|
|
47 |
locale %quote power =
|
|
48 |
fixes power :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
|
|
49 |
assumes power_commute: "power x \<circ> power y = power y \<circ> power x"
|
|
50 |
begin
|
|
51 |
|
|
52 |
text {*
|
|
53 |
\noindent Inside that locale we can lift @{text power} to exponent lists
|
|
54 |
by means of specification relative to that locale:
|
|
55 |
*}
|
|
56 |
|
|
57 |
primrec %quote powers :: "'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
|
|
58 |
"powers [] = id"
|
|
59 |
| "powers (x # xs) = power x \<circ> powers xs"
|
|
60 |
|
|
61 |
lemma %quote powers_append:
|
|
62 |
"powers (xs @ ys) = powers xs \<circ> powers ys"
|
|
63 |
by (induct xs) simp_all
|
|
64 |
|
|
65 |
lemma %quote powers_power:
|
|
66 |
"powers xs \<circ> power x = power x \<circ> powers xs"
|
|
67 |
by (induct xs)
|
|
68 |
(simp_all del: o_apply id_apply add: o_assoc [symmetric],
|
|
69 |
simp del: o_apply add: o_assoc power_commute)
|
|
70 |
|
|
71 |
lemma %quote powers_rev:
|
|
72 |
"powers (rev xs) = powers xs"
|
|
73 |
by (induct xs) (simp_all add: powers_append powers_power)
|
|
74 |
|
|
75 |
end %quote
|
|
76 |
|
|
77 |
text {*
|
38505
|
78 |
After an interpretation of this locale (say, @{command_def
|
37426
|
79 |
interpretation} @{text "fun_power:"} @{term [source] "power (\<lambda>n (f ::
|
|
80 |
'a \<Rightarrow> 'a). f ^^ n)"}), one would expect to have a constant @{text
|
|
81 |
"fun_power.powers :: nat list \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"} for which code
|
|
82 |
can be generated. But this not the case: internally, the term
|
|
83 |
@{text "fun_power.powers"} is an abbreviation for the foundational
|
|
84 |
term @{term [source] "power.powers (\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"}
|
|
85 |
(see \cite{isabelle-locale} for the details behind).
|
|
86 |
|
37836
|
87 |
Fortunately, with minor effort the desired behaviour can be achieved.
|
37426
|
88 |
First, a dedicated definition of the constant on which the local @{text "powers"}
|
|
89 |
after interpretation is supposed to be mapped on:
|
|
90 |
*}
|
|
91 |
|
|
92 |
definition %quote funpows :: "nat list \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
|
|
93 |
[code del]: "funpows = power.powers (\<lambda>n f. f ^^ n)"
|
|
94 |
|
|
95 |
text {*
|
|
96 |
\noindent In general, the pattern is @{text "c = t"} where @{text c} is
|
|
97 |
the name of the future constant and @{text t} the foundational term
|
|
98 |
corresponding to the local constant after interpretation.
|
|
99 |
|
|
100 |
The interpretation itself is enriched with an equation @{text "t = c"}:
|
|
101 |
*}
|
|
102 |
|
|
103 |
interpretation %quote fun_power: power "\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n" where
|
|
104 |
"power.powers (\<lambda>n f. f ^^ n) = funpows"
|
|
105 |
by unfold_locales
|
37432
|
106 |
(simp_all add: expand_fun_eq funpow_mult mult_commute funpows_def)
|
37426
|
107 |
|
|
108 |
text {*
|
|
109 |
\noindent This additional equation is trivially proved by the definition
|
|
110 |
itself.
|
|
111 |
|
|
112 |
After this setup procedure, code generation can continue as usual:
|
|
113 |
*}
|
|
114 |
|
|
115 |
text %quote {*@{code_stmts funpows (consts) Nat.funpow funpows (Haskell)}*}
|
|
116 |
|
|
117 |
|
38437
|
118 |
subsection {* Imperative data structures *}
|
28419
|
119 |
|
|
120 |
text {*
|
38437
|
121 |
If you consider imperative data structures as inevitable for a
|
|
122 |
specific application, you should consider \emph{Imperative
|
|
123 |
Functional Programming with Isabelle/HOL}
|
|
124 |
\cite{bulwahn-et-al:2008:imperative}; the framework described there
|
39064
|
125 |
is available in session @{text Imperative_HOL}.
|
28419
|
126 |
*}
|
|
127 |
|
28213
|
128 |
|
38405
|
129 |
subsection {* ML system interfaces \label{sec:ml} *}
|
|
130 |
|
|
131 |
text {*
|
38510
|
132 |
Since the code generator framework not only aims to provide a nice
|
|
133 |
Isar interface but also to form a base for code-generation-based
|
|
134 |
applications, here a short description of the most fundamental ML
|
|
135 |
interfaces.
|
38405
|
136 |
*}
|
|
137 |
|
|
138 |
subsubsection {* Managing executable content *}
|
|
139 |
|
|
140 |
text %mlref {*
|
|
141 |
\begin{mldecls}
|
38510
|
142 |
@{index_ML Code.read_const: "theory -> string -> string"} \\
|
38405
|
143 |
@{index_ML Code.add_eqn: "thm -> theory -> theory"} \\
|
|
144 |
@{index_ML Code.del_eqn: "thm -> theory -> theory"} \\
|
|
145 |
@{index_ML Code_Preproc.map_pre: "(simpset -> simpset) -> theory -> theory"} \\
|
|
146 |
@{index_ML Code_Preproc.map_post: "(simpset -> simpset) -> theory -> theory"} \\
|
38507
|
147 |
@{index_ML Code_Preproc.add_functrans: "
|
|
148 |
string * (theory -> (thm * bool) list -> (thm * bool) list option)
|
|
149 |
-> theory -> theory"} \\
|
38405
|
150 |
@{index_ML Code_Preproc.del_functrans: "string -> theory -> theory"} \\
|
|
151 |
@{index_ML Code.add_datatype: "(string * typ) list -> theory -> theory"} \\
|
|
152 |
@{index_ML Code.get_type: "theory -> string
|
|
153 |
-> (string * sort) list * ((string * string list) * typ list) list"} \\
|
|
154 |
@{index_ML Code.get_type_of_constr_or_abstr: "theory -> string -> (string * bool) option"}
|
|
155 |
\end{mldecls}
|
|
156 |
|
|
157 |
\begin{description}
|
|
158 |
|
38510
|
159 |
\item @{ML Code.read_const}~@{text thy}~@{text s}
|
|
160 |
reads a constant as a concrete term expression @{text s}.
|
|
161 |
|
38405
|
162 |
\item @{ML Code.add_eqn}~@{text "thm"}~@{text "thy"} adds function
|
|
163 |
theorem @{text "thm"} to executable content.
|
|
164 |
|
|
165 |
\item @{ML Code.del_eqn}~@{text "thm"}~@{text "thy"} removes function
|
|
166 |
theorem @{text "thm"} from executable content, if present.
|
|
167 |
|
|
168 |
\item @{ML Code_Preproc.map_pre}~@{text "f"}~@{text "thy"} changes
|
|
169 |
the preprocessor simpset.
|
|
170 |
|
|
171 |
\item @{ML Code_Preproc.add_functrans}~@{text "(name, f)"}~@{text "thy"} adds
|
|
172 |
function transformer @{text f} (named @{text name}) to executable content;
|
|
173 |
@{text f} is a transformer of the code equations belonging
|
|
174 |
to a certain function definition, depending on the
|
|
175 |
current theory context. Returning @{text NONE} indicates that no
|
|
176 |
transformation took place; otherwise, the whole process will be iterated
|
|
177 |
with the new code equations.
|
|
178 |
|
|
179 |
\item @{ML Code_Preproc.del_functrans}~@{text "name"}~@{text "thy"} removes
|
|
180 |
function transformer named @{text name} from executable content.
|
|
181 |
|
|
182 |
\item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds
|
|
183 |
a datatype to executable content, with generation
|
|
184 |
set @{text cs}.
|
|
185 |
|
|
186 |
\item @{ML Code.get_type_of_constr_or_abstr}~@{text "thy"}~@{text "const"}
|
|
187 |
returns type constructor corresponding to
|
|
188 |
constructor @{text const}; returns @{text NONE}
|
|
189 |
if @{text const} is no constructor.
|
|
190 |
|
|
191 |
\end{description}
|
|
192 |
*}
|
|
193 |
|
|
194 |
|
|
195 |
subsubsection {* Data depending on the theory's executable content *}
|
|
196 |
|
|
197 |
text {*
|
|
198 |
Implementing code generator applications on top
|
|
199 |
of the framework set out so far usually not only
|
|
200 |
involves using those primitive interfaces
|
|
201 |
but also storing code-dependent data and various
|
|
202 |
other things.
|
|
203 |
|
|
204 |
Due to incrementality of code generation, changes in the
|
|
205 |
theory's executable content have to be propagated in a
|
|
206 |
certain fashion. Additionally, such changes may occur
|
|
207 |
not only during theory extension but also during theory
|
|
208 |
merge, which is a little bit nasty from an implementation
|
|
209 |
point of view. The framework provides a solution
|
|
210 |
to this technical challenge by providing a functorial
|
|
211 |
data slot @{ML_functor Code_Data}; on instantiation
|
|
212 |
of this functor, the following types and operations
|
|
213 |
are required:
|
|
214 |
|
|
215 |
\medskip
|
|
216 |
\begin{tabular}{l}
|
|
217 |
@{text "type T"} \\
|
|
218 |
@{text "val empty: T"} \\
|
|
219 |
\end{tabular}
|
|
220 |
|
|
221 |
\begin{description}
|
|
222 |
|
|
223 |
\item @{text T} the type of data to store.
|
|
224 |
|
|
225 |
\item @{text empty} initial (empty) data.
|
|
226 |
|
|
227 |
\end{description}
|
|
228 |
|
|
229 |
\noindent An instance of @{ML_functor Code_Data} provides the following
|
|
230 |
interface:
|
|
231 |
|
|
232 |
\medskip
|
|
233 |
\begin{tabular}{l}
|
|
234 |
@{text "change: theory \<rightarrow> (T \<rightarrow> T) \<rightarrow> T"} \\
|
|
235 |
@{text "change_yield: theory \<rightarrow> (T \<rightarrow> 'a * T) \<rightarrow> 'a * T"}
|
|
236 |
\end{tabular}
|
|
237 |
|
|
238 |
\begin{description}
|
|
239 |
|
|
240 |
\item @{text change} update of current data (cached!)
|
|
241 |
by giving a continuation.
|
|
242 |
|
|
243 |
\item @{text change_yield} update with side result.
|
|
244 |
|
|
245 |
\end{description}
|
|
246 |
*}
|
|
247 |
|
28213
|
248 |
end
|