28213
|
1 |
theory Adaption
|
|
2 |
imports Setup
|
|
3 |
begin
|
|
4 |
|
28419
|
5 |
section {* Adaption to target languages \label{sec:adaption} *}
|
|
6 |
|
|
7 |
subsection {* Common adaption cases *}
|
|
8 |
|
|
9 |
text {*
|
28428
|
10 |
The @{theory HOL} @{theory Main} theory already provides a code
|
28419
|
11 |
generator setup
|
|
12 |
which should be suitable for most applications. Common extensions
|
|
13 |
and modifications are available by certain theories of the @{text HOL}
|
|
14 |
library; beside being useful in applications, they may serve
|
|
15 |
as a tutorial for customising the code generator setup (see below
|
|
16 |
\secref{sec:adaption_mechanisms}).
|
|
17 |
|
|
18 |
\begin{description}
|
|
19 |
|
|
20 |
\item[@{theory "Code_Integer"}] represents @{text HOL} integers by big
|
|
21 |
integer literals in target languages.
|
|
22 |
\item[@{theory "Code_Char"}] represents @{text HOL} characters by
|
|
23 |
character literals in target languages.
|
|
24 |
\item[@{theory "Code_Char_chr"}] like @{text "Code_Char"},
|
|
25 |
but also offers treatment of character codes; includes
|
|
26 |
@{theory "Code_Char_chr"}.
|
|
27 |
\item[@{theory "Efficient_Nat"}] \label{eff_nat} implements natural numbers by integers,
|
|
28 |
which in general will result in higher efficiency; pattern
|
|
29 |
matching with @{term "0\<Colon>nat"} / @{const "Suc"}
|
|
30 |
is eliminated; includes @{theory "Code_Integer"}.
|
|
31 |
\item[@{theory "Code_Index"}] provides an additional datatype
|
|
32 |
@{typ index} which is mapped to target-language built-in integers.
|
|
33 |
Useful for code setups which involve e.g. indexing of
|
|
34 |
target-language arrays.
|
|
35 |
\item[@{theory "Code_Message"}] provides an additional datatype
|
|
36 |
@{typ message_string} which is isomorphic to strings;
|
|
37 |
@{typ message_string}s are mapped to target-language strings.
|
|
38 |
Useful for code setups which involve e.g. printing (error) messages.
|
|
39 |
|
|
40 |
\end{description}
|
|
41 |
|
|
42 |
\begin{warn}
|
|
43 |
When importing any of these theories, they should form the last
|
|
44 |
items in an import list. Since these theories adapt the
|
|
45 |
code generator setup in a non-conservative fashion,
|
|
46 |
strange effects may occur otherwise.
|
|
47 |
\end{warn}
|
|
48 |
*}
|
|
49 |
|
|
50 |
|
|
51 |
subsection {* Adaption mechanisms \label{sec:adaption_mechanisms} *}
|
|
52 |
|
|
53 |
text {*
|
|
54 |
\begin{warn}
|
|
55 |
The mechanisms shown here are especially for the curious; the user
|
|
56 |
rarely needs to do anything on his own beyond the defaults in @{text HOL}.
|
|
57 |
Adaption is a delicated task which requires a lot of dilligence since
|
|
58 |
it happend \emph{completely} outside the logic.
|
|
59 |
\end{warn}
|
|
60 |
*}
|
|
61 |
|
|
62 |
text {*
|
28456
|
63 |
\noindent Consider the following function and its corresponding
|
28419
|
64 |
SML code:
|
|
65 |
*}
|
|
66 |
|
|
67 |
primrec %quoteme in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
|
|
68 |
"in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
|
28447
|
69 |
(*<*)
|
28419
|
70 |
code_type %invisible bool
|
|
71 |
(SML)
|
|
72 |
code_const %invisible True and False and "op \<and>" and Not
|
|
73 |
(SML and and and)
|
28447
|
74 |
(*>*)
|
28419
|
75 |
text %quoteme {*@{code_stmts in_interval (SML)}*}
|
|
76 |
|
|
77 |
text {*
|
|
78 |
\noindent Though this is correct code, it is a little bit unsatisfactory:
|
|
79 |
boolean values and operators are materialised as distinguished
|
|
80 |
entities with have nothing to do with the SML-built-in notion
|
|
81 |
of \qt{bool}. This results in less readable code;
|
|
82 |
additionally, eager evaluation may cause programs to
|
|
83 |
loop or break which would perfectly terminate when
|
|
84 |
the existing SML @{verbatim "bool"} would be used. To map
|
|
85 |
the HOL @{typ bool} on SML @{verbatim "bool"}, we may use
|
|
86 |
\qn{custom serialisations}:
|
|
87 |
*}
|
|
88 |
|
|
89 |
code_type %tt bool
|
|
90 |
(SML "bool")
|
|
91 |
code_const %tt True and False and "op \<and>"
|
|
92 |
(SML "true" and "false" and "_ andalso _")
|
28213
|
93 |
|
28419
|
94 |
text {*
|
28447
|
95 |
\noindent The @{command code_type} command takes a type constructor
|
28419
|
96 |
as arguments together with a list of custom serialisations.
|
|
97 |
Each custom serialisation starts with a target language
|
|
98 |
identifier followed by an expression, which during
|
|
99 |
code serialisation is inserted whenever the type constructor
|
|
100 |
would occur. For constants, @{command code_const} implements
|
|
101 |
the corresponding mechanism. Each ``@{verbatim "_"}'' in
|
|
102 |
a serialisation expression is treated as a placeholder
|
|
103 |
for the type constructor's (the constant's) arguments.
|
|
104 |
*}
|
|
105 |
|
|
106 |
text %quoteme {*@{code_stmts in_interval (SML)}*}
|
|
107 |
|
|
108 |
text {*
|
|
109 |
\noindent This still is not perfect: the parentheses
|
|
110 |
around the \qt{andalso} expression are superfluous.
|
|
111 |
Though the serializer
|
|
112 |
by no means attempts to imitate the rich Isabelle syntax
|
|
113 |
framework, it provides some common idioms, notably
|
|
114 |
associative infixes with precedences which may be used here:
|
|
115 |
*}
|
|
116 |
|
|
117 |
code_const %tt "op \<and>"
|
|
118 |
(SML infixl 1 "andalso")
|
|
119 |
|
|
120 |
text %quoteme {*@{code_stmts in_interval (SML)}*}
|
|
121 |
|
|
122 |
text {*
|
28447
|
123 |
\noindent Next, we try to map HOL pairs to SML pairs, using the
|
28419
|
124 |
infix ``@{verbatim "*"}'' type constructor and parentheses:
|
|
125 |
*}
|
28447
|
126 |
(*<*)
|
28419
|
127 |
code_type %invisible *
|
|
128 |
(SML)
|
|
129 |
code_const %invisible Pair
|
|
130 |
(SML)
|
28447
|
131 |
(*>*)
|
28419
|
132 |
code_type %tt *
|
|
133 |
(SML infix 2 "*")
|
|
134 |
code_const %tt Pair
|
|
135 |
(SML "!((_),/ (_))")
|
|
136 |
|
|
137 |
text {*
|
28447
|
138 |
\noindent The initial bang ``@{verbatim "!"}'' tells the serializer to never put
|
28419
|
139 |
parentheses around the whole expression (they are already present),
|
|
140 |
while the parentheses around argument place holders
|
|
141 |
tell not to put parentheses around the arguments.
|
|
142 |
The slash ``@{verbatim "/"}'' (followed by arbitrary white space)
|
|
143 |
inserts a space which may be used as a break if necessary
|
|
144 |
during pretty printing.
|
|
145 |
|
|
146 |
These examples give a glimpse what mechanisms
|
|
147 |
custom serialisations provide; however their usage
|
|
148 |
requires careful thinking in order not to introduce
|
|
149 |
inconsistencies -- or, in other words:
|
|
150 |
custom serialisations are completely axiomatic.
|
|
151 |
|
|
152 |
A further noteworthy details is that any special
|
|
153 |
character in a custom serialisation may be quoted
|
|
154 |
using ``@{verbatim "'"}''; thus, in
|
|
155 |
``@{verbatim "fn '_ => _"}'' the first
|
|
156 |
``@{verbatim "_"}'' is a proper underscore while the
|
|
157 |
second ``@{verbatim "_"}'' is a placeholder.
|
|
158 |
|
|
159 |
The HOL theories provide further
|
|
160 |
examples for custom serialisations.
|
|
161 |
*}
|
|
162 |
|
|
163 |
|
|
164 |
subsection {* @{text Haskell} serialisation *}
|
|
165 |
|
|
166 |
text {*
|
|
167 |
For convenience, the default
|
|
168 |
@{text HOL} setup for @{text Haskell} maps the @{class eq} class to
|
|
169 |
its counterpart in @{text Haskell}, giving custom serialisations
|
|
170 |
for the class @{class eq} (by command @{command code_class}) and its operation
|
|
171 |
@{const HOL.eq}
|
|
172 |
*}
|
|
173 |
|
|
174 |
code_class %tt eq
|
|
175 |
(Haskell "Eq" where "HOL.eq" \<equiv> "(==)")
|
|
176 |
|
|
177 |
code_const %tt "op ="
|
|
178 |
(Haskell infixl 4 "==")
|
|
179 |
|
|
180 |
text {*
|
28447
|
181 |
\noindent A problem now occurs whenever a type which
|
28419
|
182 |
is an instance of @{class eq} in @{text HOL} is mapped
|
|
183 |
on a @{text Haskell}-built-in type which is also an instance
|
|
184 |
of @{text Haskell} @{text Eq}:
|
|
185 |
*}
|
|
186 |
|
|
187 |
typedecl %quoteme bar
|
|
188 |
|
|
189 |
instantiation %quoteme bar :: eq
|
|
190 |
begin
|
|
191 |
|
|
192 |
definition %quoteme "eq_class.eq (x\<Colon>bar) y \<longleftrightarrow> x = y"
|
|
193 |
|
|
194 |
instance %quoteme by default (simp add: eq_bar_def)
|
28213
|
195 |
|
28428
|
196 |
end %quoteme
|
28419
|
197 |
|
|
198 |
code_type %tt bar
|
|
199 |
(Haskell "Integer")
|
|
200 |
|
|
201 |
text {*
|
28447
|
202 |
\noindent The code generator would produce
|
28419
|
203 |
an additional instance, which of course is rejectedby the @{text Haskell}
|
|
204 |
compiler.
|
|
205 |
To suppress this additional instance, use
|
|
206 |
@{text "code_instance"}:
|
|
207 |
*}
|
|
208 |
|
|
209 |
code_instance %tt bar :: eq
|
|
210 |
(Haskell -)
|
|
211 |
|
|
212 |
end
|