author | haftmann |
Wed, 13 Sep 2023 17:08:55 +0000 | |
changeset 78665 | b0ddfa5b9ddc |
parent 76987 | 4c275405faae |
permissions | -rw-r--r-- |
28213 | 1 |
theory Further |
69422
472af2d7835d
clarified session dependencies: faster build_doc/build_release;
wenzelm
parents:
68484
diff
changeset
|
2 |
imports Setup |
28213 | 3 |
begin |
4 |
||
59377 | 5 |
section \<open>Further issues \label{sec:further}\<close> |
28419 | 6 |
|
78665 | 7 |
subsection \<open>Runtime environments for \<^text>\<open>Haskell\<close> and \<^text>\<open>OCaml\<close>\<close> |
8 |
||
9 |
text \<open> |
|
10 |
The Isabelle System Manual \<^cite>\<open>"isabelle-system"\<close> provides some hints |
|
11 |
how runtime environments for \<^text>\<open>Haskell\<close> and \<^text>\<open>OCaml\<close> can be |
|
12 |
set up and maintained conveniently using managed installations within |
|
13 |
the Isabelle environments. |
|
14 |
\<close> |
|
15 |
||
16 |
||
69505 | 17 |
subsection \<open>Incorporating generated code directly into the system runtime -- \<open>code_reflect\<close>\<close> |
65041 | 18 |
|
19 |
subsubsection \<open>Static embedding of generated code into the system runtime\<close> |
|
20 |
||
21 |
text \<open> |
|
22 |
The @{ML_antiquotation code} antiquotation is lightweight, but the generated code |
|
23 |
is only accessible while the ML section is processed. Sometimes this |
|
24 |
is not appropriate, especially if the generated code contains datatype |
|
25 |
declarations which are shared with other parts of the system. In these |
|
26 |
cases, @{command_def code_reflect} can be used: |
|
27 |
\<close> |
|
28 |
||
29 |
code_reflect %quote Sum_Type |
|
30 |
datatypes sum = Inl | Inr |
|
31 |
functions "Sum_Type.sum.projl" "Sum_Type.sum.projr" |
|
32 |
||
33 |
text \<open> |
|
34 |
\noindent @{command code_reflect} takes a structure name and |
|
35 |
references to datatypes and functions; for these code is compiled |
|
36 |
into the named ML structure and the \emph{Eval} target is modified |
|
37 |
in a way that future code generation will reference these |
|
38 |
precompiled versions of the given datatypes and functions. This |
|
39 |
also allows to refer to the referenced datatypes and functions from |
|
40 |
arbitrary ML code as well. |
|
41 |
||
42 |
A typical example for @{command code_reflect} can be found in the |
|
69597 | 43 |
\<^theory>\<open>HOL.Predicate\<close> theory. |
65041 | 44 |
\<close> |
45 |
||
46 |
||
47 |
subsubsection \<open>Separate compilation\<close> |
|
48 |
||
49 |
text \<open> |
|
50 |
For technical reasons it is sometimes necessary to separate |
|
51 |
generation and compilation of code which is supposed to be used in |
|
52 |
the system runtime. For this @{command code_reflect} with an |
|
75074
78c2a92a8be4
updated documentation to current matter of affairs
haftmann
parents:
70022
diff
changeset
|
53 |
optional \<^theory_text>\<open>file_prefix\<close> argument can be used: |
65041 | 54 |
\<close> |
55 |
||
56 |
code_reflect %quote Rat |
|
57 |
datatypes rat |
|
58 |
functions Fract |
|
59 |
"(plus :: rat \<Rightarrow> rat \<Rightarrow> rat)" "(minus :: rat \<Rightarrow> rat \<Rightarrow> rat)" |
|
60 |
"(times :: rat \<Rightarrow> rat \<Rightarrow> rat)" "(divide :: rat \<Rightarrow> rat \<Rightarrow> rat)" |
|
70022
49e178cbf923
'code_reflect' only supports new-style 'file_prefix';
wenzelm
parents:
69697
diff
changeset
|
61 |
file_prefix rat |
65041 | 62 |
|
63 |
text \<open> |
|
75074
78c2a92a8be4
updated documentation to current matter of affairs
haftmann
parents:
70022
diff
changeset
|
64 |
\noindent This generates the referenced code as logical files within the theory context, |
78c2a92a8be4
updated documentation to current matter of affairs
haftmann
parents:
70022
diff
changeset
|
65 |
similar to @{command export_code}. |
65041 | 66 |
\<close> |
67 |
||
69505 | 68 |
subsection \<open>Specialities of the \<open>Scala\<close> target language \label{sec:scala}\<close> |
42096 | 69 |
|
59377 | 70 |
text \<open> |
69505 | 71 |
\<open>Scala\<close> deviates from languages of the ML family in a couple |
42096 | 72 |
of aspects; those which affect code generation mainly have to do with |
69505 | 73 |
\<open>Scala\<close>'s type system: |
42096 | 74 |
|
75 |
\begin{itemize} |
|
76 |
||
69505 | 77 |
\item \<open>Scala\<close> prefers tupled syntax over curried syntax. |
42096 | 78 |
|
69505 | 79 |
\item \<open>Scala\<close> sacrifices Hindely-Milner type inference for a |
42096 | 80 |
much more rich type system with subtyping etc. For this reason |
81 |
type arguments sometimes have to be given explicitly in square |
|
46520 | 82 |
brackets (mimicking System F syntax). |
42096 | 83 |
|
69505 | 84 |
\item In contrast to \<open>Haskell\<close> where most specialities of |
42096 | 85 |
the type system are implemented using \emph{type classes}, |
69505 | 86 |
\<open>Scala\<close> provides a sophisticated system of \emph{implicit |
42096 | 87 |
arguments}. |
88 |
||
89 |
\end{itemize} |
|
90 |
||
69505 | 91 |
\noindent Concerning currying, the \<open>Scala\<close> serializer counts |
42096 | 92 |
arguments in code equations to determine how many arguments |
93 |
shall be tupled; remaining arguments and abstractions in terms |
|
94 |
rather than function definitions are always curried. |
|
95 |
||
96 |
The second aspect affects user-defined adaptations with @{command |
|
69505 | 97 |
code_printing}. For regular terms, the \<open>Scala\<close> serializer prints |
42096 | 98 |
all type arguments explicitly. For user-defined term adaptations |
99 |
this is only possible for adaptations which take no arguments: here |
|
100 |
the type arguments are just appended. Otherwise they are ignored; |
|
101 |
hence user-defined adaptations for polymorphic constants have to be |
|
102 |
designed very carefully to avoid ambiguity. |
|
67207
ad538f6c5d2f
dedicated case option for code generation to Scala
haftmann
parents:
66453
diff
changeset
|
103 |
|
ad538f6c5d2f
dedicated case option for code generation to Scala
haftmann
parents:
66453
diff
changeset
|
104 |
Note also that name clashes can occur when generating Scala code |
ad538f6c5d2f
dedicated case option for code generation to Scala
haftmann
parents:
66453
diff
changeset
|
105 |
to case-insensitive file systems; these can be avoid using the |
ad538f6c5d2f
dedicated case option for code generation to Scala
haftmann
parents:
66453
diff
changeset
|
106 |
``\<open>(case_insensitive)\<close>'' option for @{command export_code}. |
59377 | 107 |
\<close> |
42096 | 108 |
|
109 |
||
59377 | 110 |
subsection \<open>Modules namespace\<close> |
28419 | 111 |
|
59377 | 112 |
text \<open> |
40752 | 113 |
When invoking the @{command export_code} command it is possible to |
114 |
leave out the @{keyword "module_name"} part; then code is |
|
115 |
distributed over different modules, where the module name space |
|
116 |
roughly is induced by the Isabelle theory name space. |
|
38437 | 117 |
|
40752 | 118 |
Then sometimes the awkward situation occurs that dependencies |
119 |
between definitions introduce cyclic dependencies between modules, |
|
69505 | 120 |
which in the \<open>Haskell\<close> world leaves you to the mercy of the |
121 |
\<open>Haskell\<close> implementation you are using, while for \<open>SML\<close>/\<open>OCaml\<close> code generation is not possible. |
|
38437 | 122 |
|
40752 | 123 |
A solution is to declare module names explicitly. Let use assume |
124 |
the three cyclically dependent modules are named \emph{A}, \emph{B} |
|
125 |
and \emph{C}. Then, by stating |
|
59377 | 126 |
\<close> |
38437 | 127 |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51717
diff
changeset
|
128 |
code_identifier %quote |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51717
diff
changeset
|
129 |
code_module A \<rightharpoonup> (SML) ABC |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51717
diff
changeset
|
130 |
| code_module B \<rightharpoonup> (SML) ABC |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51717
diff
changeset
|
131 |
| code_module C \<rightharpoonup> (SML) ABC |
38437 | 132 |
|
59377 | 133 |
text \<open> |
40752 | 134 |
\noindent we explicitly map all those modules on \emph{ABC}, |
135 |
resulting in an ad-hoc merge of this three modules at serialisation |
|
136 |
time. |
|
59377 | 137 |
\<close> |
28419 | 138 |
|
59377 | 139 |
subsection \<open>Locales and interpretation\<close> |
37426 | 140 |
|
59377 | 141 |
text \<open> |
37426 | 142 |
A technical issue comes to surface when generating code from |
61891
76189756ff65
documentation on last state of the art concerning interpretation
haftmann
parents:
61779
diff
changeset
|
143 |
specifications stemming from locale interpretation into global |
76189756ff65
documentation on last state of the art concerning interpretation
haftmann
parents:
61779
diff
changeset
|
144 |
theories. |
37426 | 145 |
|
40752 | 146 |
Let us assume a locale specifying a power operation on arbitrary |
147 |
types: |
|
59377 | 148 |
\<close> |
37426 | 149 |
|
150 |
locale %quote power = |
|
151 |
fixes power :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" |
|
152 |
assumes power_commute: "power x \<circ> power y = power y \<circ> power x" |
|
153 |
begin |
|
154 |
||
59377 | 155 |
text \<open> |
69505 | 156 |
\noindent Inside that locale we can lift \<open>power\<close> to exponent |
61779 | 157 |
lists by means of a specification relative to that locale: |
59377 | 158 |
\<close> |
37426 | 159 |
|
160 |
primrec %quote powers :: "'a list \<Rightarrow> 'b \<Rightarrow> 'b" where |
|
161 |
"powers [] = id" |
|
162 |
| "powers (x # xs) = power x \<circ> powers xs" |
|
163 |
||
164 |
lemma %quote powers_append: |
|
165 |
"powers (xs @ ys) = powers xs \<circ> powers ys" |
|
166 |
by (induct xs) simp_all |
|
167 |
||
168 |
lemma %quote powers_power: |
|
169 |
"powers xs \<circ> power x = power x \<circ> powers xs" |
|
170 |
by (induct xs) |
|
49739 | 171 |
(simp_all del: o_apply id_apply add: comp_assoc, |
37426 | 172 |
simp del: o_apply add: o_assoc power_commute) |
173 |
||
174 |
lemma %quote powers_rev: |
|
175 |
"powers (rev xs) = powers xs" |
|
176 |
by (induct xs) (simp_all add: powers_append powers_power) |
|
177 |
||
178 |
end %quote |
|
179 |
||
59377 | 180 |
text \<open> |
38505 | 181 |
After an interpretation of this locale (say, @{command_def |
69505 | 182 |
global_interpretation} \<open>fun_power:\<close> @{term [source] "power (\<lambda>n (f |
183 |
:: 'a \<Rightarrow> 'a). f ^^ n)"}), one could naively expect to have a constant \<open>fun_power.powers :: nat list \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a\<close> for which code |
|
37426 | 184 |
can be generated. But this not the case: internally, the term |
69505 | 185 |
\<open>fun_power.powers\<close> is an abbreviation for the foundational |
37426 | 186 |
term @{term [source] "power.powers (\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"} |
76987 | 187 |
(see \<^cite>\<open>"isabelle-locale"\<close> for the details behind). |
37426 | 188 |
|
61891
76189756ff65
documentation on last state of the art concerning interpretation
haftmann
parents:
61779
diff
changeset
|
189 |
Fortunately, a succint solution is available: a dedicated |
61779 | 190 |
rewrite definition: |
59377 | 191 |
\<close> |
37426 | 192 |
|
61891
76189756ff65
documentation on last state of the art concerning interpretation
haftmann
parents:
61779
diff
changeset
|
193 |
global_interpretation %quote fun_power: power "(\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)" |
61779 | 194 |
defines funpows = fun_power.powers |
195 |
by unfold_locales |
|
196 |
(simp_all add: fun_eq_iff funpow_mult mult.commute) |
|
37426 | 197 |
|
59377 | 198 |
text \<open> |
61779 | 199 |
\noindent This amends the interpretation morphisms such that |
200 |
occurrences of the foundational term @{term [source] "power.powers (\<lambda>n (f :: 'a \<Rightarrow> 'a). f ^^ n)"} |
|
69597 | 201 |
are folded to a newly defined constant \<^const>\<open>funpows\<close>. |
37426 | 202 |
|
203 |
After this setup procedure, code generation can continue as usual: |
|
59377 | 204 |
\<close> |
37426 | 205 |
|
69660
2bc2a8599369
canonical operation to typeset generated code makes dedicated environment obsolete
haftmann
parents:
69597
diff
changeset
|
206 |
text %quote \<open> |
69697
4d95261fab5a
more conventional syntax for code_stmts antiquotation
haftmann
parents:
69660
diff
changeset
|
207 |
@{code_stmts funpows constant: Nat.funpow funpows (Haskell)} |
61779 | 208 |
\<close> |
37426 | 209 |
|
210 |
||
59377 | 211 |
subsection \<open>Parallel computation\<close> |
51172 | 212 |
|
59377 | 213 |
text \<open> |
69505 | 214 |
Theory \<open>Parallel\<close> in \<^dir>\<open>~~/src/HOL/Library\<close> contains |
51172 | 215 |
operations to exploit parallelism inside the Isabelle/ML |
216 |
runtime engine. |
|
59377 | 217 |
\<close> |
51172 | 218 |
|
59377 | 219 |
subsection \<open>Imperative data structures\<close> |
28419 | 220 |
|
59377 | 221 |
text \<open> |
38437 | 222 |
If you consider imperative data structures as inevitable for a |
223 |
specific application, you should consider \emph{Imperative |
|
224 |
Functional Programming with Isabelle/HOL} |
|
76987 | 225 |
\<^cite>\<open>"bulwahn-et-al:2008:imperative"\<close>; the framework described there |
69505 | 226 |
is available in session \<open>Imperative_HOL\<close>, together with a |
40752 | 227 |
short primer document. |
59377 | 228 |
\<close> |
28419 | 229 |
|
28213 | 230 |
end |