author | haftmann |
Fri, 24 Sep 2010 15:11:38 +0200 | |
changeset 39683 | f75a01ee6c41 |
parent 39664 | 0afaf89ab591 |
child 39745 | 3aa2bc9c5478 |
permissions | -rw-r--r-- |
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 |
|
39559 | 106 |
(simp_all add: fun_eq_iff 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 |
||
39683 | 115 |
text %quote %typewriter {* |
116 |
@{code_stmts funpows (consts) Nat.funpow funpows (Haskell)} |
|
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39559
diff
changeset
|
117 |
*} |
37426 | 118 |
|
119 |
||
38437 | 120 |
subsection {* Imperative data structures *} |
28419 | 121 |
|
122 |
text {* |
|
38437 | 123 |
If you consider imperative data structures as inevitable for a |
124 |
specific application, you should consider \emph{Imperative |
|
125 |
Functional Programming with Isabelle/HOL} |
|
126 |
\cite{bulwahn-et-al:2008:imperative}; the framework described there |
|
39064 | 127 |
is available in session @{text Imperative_HOL}. |
28419 | 128 |
*} |
129 |
||
28213 | 130 |
|
38405 | 131 |
subsection {* ML system interfaces \label{sec:ml} *} |
132 |
||
133 |
text {* |
|
38510 | 134 |
Since the code generator framework not only aims to provide a nice |
135 |
Isar interface but also to form a base for code-generation-based |
|
136 |
applications, here a short description of the most fundamental ML |
|
137 |
interfaces. |
|
38405 | 138 |
*} |
139 |
||
140 |
subsubsection {* Managing executable content *} |
|
141 |
||
142 |
text %mlref {* |
|
143 |
\begin{mldecls} |
|
38510 | 144 |
@{index_ML Code.read_const: "theory -> string -> string"} \\ |
38405 | 145 |
@{index_ML Code.add_eqn: "thm -> theory -> theory"} \\ |
146 |
@{index_ML Code.del_eqn: "thm -> theory -> theory"} \\ |
|
147 |
@{index_ML Code_Preproc.map_pre: "(simpset -> simpset) -> theory -> theory"} \\ |
|
148 |
@{index_ML Code_Preproc.map_post: "(simpset -> simpset) -> theory -> theory"} \\ |
|
38507 | 149 |
@{index_ML Code_Preproc.add_functrans: " |
150 |
string * (theory -> (thm * bool) list -> (thm * bool) list option) |
|
151 |
-> theory -> theory"} \\ |
|
38405 | 152 |
@{index_ML Code_Preproc.del_functrans: "string -> theory -> theory"} \\ |
153 |
@{index_ML Code.add_datatype: "(string * typ) list -> theory -> theory"} \\ |
|
154 |
@{index_ML Code.get_type: "theory -> string |
|
155 |
-> (string * sort) list * ((string * string list) * typ list) list"} \\ |
|
156 |
@{index_ML Code.get_type_of_constr_or_abstr: "theory -> string -> (string * bool) option"} |
|
157 |
\end{mldecls} |
|
158 |
||
159 |
\begin{description} |
|
160 |
||
38510 | 161 |
\item @{ML Code.read_const}~@{text thy}~@{text s} |
162 |
reads a constant as a concrete term expression @{text s}. |
|
163 |
||
38405 | 164 |
\item @{ML Code.add_eqn}~@{text "thm"}~@{text "thy"} adds function |
165 |
theorem @{text "thm"} to executable content. |
|
166 |
||
167 |
\item @{ML Code.del_eqn}~@{text "thm"}~@{text "thy"} removes function |
|
168 |
theorem @{text "thm"} from executable content, if present. |
|
169 |
||
170 |
\item @{ML Code_Preproc.map_pre}~@{text "f"}~@{text "thy"} changes |
|
171 |
the preprocessor simpset. |
|
172 |
||
173 |
\item @{ML Code_Preproc.add_functrans}~@{text "(name, f)"}~@{text "thy"} adds |
|
174 |
function transformer @{text f} (named @{text name}) to executable content; |
|
175 |
@{text f} is a transformer of the code equations belonging |
|
176 |
to a certain function definition, depending on the |
|
177 |
current theory context. Returning @{text NONE} indicates that no |
|
178 |
transformation took place; otherwise, the whole process will be iterated |
|
179 |
with the new code equations. |
|
180 |
||
181 |
\item @{ML Code_Preproc.del_functrans}~@{text "name"}~@{text "thy"} removes |
|
182 |
function transformer named @{text name} from executable content. |
|
183 |
||
184 |
\item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds |
|
185 |
a datatype to executable content, with generation |
|
186 |
set @{text cs}. |
|
187 |
||
188 |
\item @{ML Code.get_type_of_constr_or_abstr}~@{text "thy"}~@{text "const"} |
|
189 |
returns type constructor corresponding to |
|
190 |
constructor @{text const}; returns @{text NONE} |
|
191 |
if @{text const} is no constructor. |
|
192 |
||
193 |
\end{description} |
|
194 |
*} |
|
195 |
||
196 |
||
197 |
subsubsection {* Data depending on the theory's executable content *} |
|
198 |
||
199 |
text {* |
|
200 |
Implementing code generator applications on top |
|
201 |
of the framework set out so far usually not only |
|
202 |
involves using those primitive interfaces |
|
203 |
but also storing code-dependent data and various |
|
204 |
other things. |
|
205 |
||
206 |
Due to incrementality of code generation, changes in the |
|
207 |
theory's executable content have to be propagated in a |
|
208 |
certain fashion. Additionally, such changes may occur |
|
209 |
not only during theory extension but also during theory |
|
210 |
merge, which is a little bit nasty from an implementation |
|
211 |
point of view. The framework provides a solution |
|
212 |
to this technical challenge by providing a functorial |
|
213 |
data slot @{ML_functor Code_Data}; on instantiation |
|
214 |
of this functor, the following types and operations |
|
215 |
are required: |
|
216 |
||
217 |
\medskip |
|
218 |
\begin{tabular}{l} |
|
219 |
@{text "type T"} \\ |
|
220 |
@{text "val empty: T"} \\ |
|
221 |
\end{tabular} |
|
222 |
||
223 |
\begin{description} |
|
224 |
||
225 |
\item @{text T} the type of data to store. |
|
226 |
||
227 |
\item @{text empty} initial (empty) data. |
|
228 |
||
229 |
\end{description} |
|
230 |
||
231 |
\noindent An instance of @{ML_functor Code_Data} provides the following |
|
232 |
interface: |
|
233 |
||
234 |
\medskip |
|
235 |
\begin{tabular}{l} |
|
236 |
@{text "change: theory \<rightarrow> (T \<rightarrow> T) \<rightarrow> T"} \\ |
|
237 |
@{text "change_yield: theory \<rightarrow> (T \<rightarrow> 'a * T) \<rightarrow> 'a * T"} |
|
238 |
\end{tabular} |
|
239 |
||
240 |
\begin{description} |
|
241 |
||
242 |
\item @{text change} update of current data (cached!) |
|
243 |
by giving a continuation. |
|
244 |
||
245 |
\item @{text change_yield} update with side result. |
|
246 |
||
247 |
\end{description} |
|
248 |
*} |
|
249 |
||
28213 | 250 |
end |