18537
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{ML}%
|
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
\isanewline
|
18543
|
7 |
\isanewline
|
18537
|
8 |
%
|
|
9 |
\endisadelimtheory
|
|
10 |
%
|
|
11 |
\isatagtheory
|
|
12 |
\isacommand{theory}\isamarkupfalse%
|
18543
|
13 |
\ {\isachardoublequoteopen}ML{\isachardoublequoteclose}\ \isakeyword{imports}\ base\ \isakeyword{begin}%
|
|
14 |
\endisatagtheory
|
|
15 |
{\isafoldtheory}%
|
|
16 |
%
|
|
17 |
\isadelimtheory
|
|
18 |
%
|
|
19 |
\endisadelimtheory
|
|
20 |
%
|
|
21 |
\isamarkupchapter{Aesthetics of ML programming%
|
|
22 |
}
|
|
23 |
\isamarkuptrue%
|
|
24 |
%
|
|
25 |
\begin{isamarkuptext}%
|
21501
|
26 |
FIXME%
|
|
27 |
\end{isamarkuptext}%
|
|
28 |
\isamarkuptrue%
|
|
29 |
%
|
|
30 |
\begin{isamarkuptext}%
|
21148
|
31 |
This style guide is loosely based on
|
|
32 |
\url{http://caml.inria.fr/resources/doc/guides/guidelines.en.html}.
|
|
33 |
% FIMXE \url{http://www.cs.cornell.edu/Courses/cs312/2003sp/handouts/style.htm}
|
|
34 |
|
|
35 |
Like any style guide, it should not be interpreted dogmatically.
|
|
36 |
Instead, it forms a collection of recommendations which,
|
|
37 |
if obeyed, result in code that is not considered to be
|
|
38 |
obfuscated. In certain cases, derivations are encouraged,
|
|
39 |
as far as you know what you are doing.
|
|
40 |
|
|
41 |
\begin{description}
|
|
42 |
|
|
43 |
\item[fundamental law of programming]
|
|
44 |
Whenever writing code, keep in mind: A program is
|
|
45 |
written once, modified ten times, and read
|
|
46 |
100 times. So simplify its writing,
|
|
47 |
always keep future modifications in mind,
|
21172
|
48 |
and never jeopardize readability. Every second you hesitate
|
21148
|
49 |
to spend on making your code more clear you will
|
|
50 |
have to spend ten times understanding what you have
|
|
51 |
written later on.
|
|
52 |
|
|
53 |
\item[white space matters]
|
|
54 |
Treat white space in your code as if it determines
|
|
55 |
the meaning of code.
|
|
56 |
|
|
57 |
\begin{itemize}
|
|
58 |
|
|
59 |
\item The space bar is the easiest key to find on the keyboard,
|
|
60 |
press it as often as necessary. {\ttfamily 2 + 2} is better
|
|
61 |
than {\ttfamily 2+2}, likewise {\ttfamily f (x, y)}
|
|
62 |
better than {\ttfamily f(x,y)}.
|
|
63 |
|
|
64 |
\item Restrict your lines to \emph{at most} 80 characters.
|
|
65 |
This will allow you to keep the beginning of a line
|
|
66 |
in view while watching its end.
|
|
67 |
|
|
68 |
\item Ban tabs; they are a context-sensitive formatting
|
|
69 |
feature and likely to confuse anyone not using your
|
|
70 |
favourite editor.
|
|
71 |
|
|
72 |
\item Get rid of trailing whitespace. Instead, do not
|
|
73 |
surpess a trailing newline at the end of your files.
|
|
74 |
|
|
75 |
\item Choose a generally accepted style of indentation,
|
|
76 |
then use it systematically throughout the whole
|
|
77 |
application. An indentation of two spaces is appropriate.
|
|
78 |
Avoid dangling indentation.
|
|
79 |
|
|
80 |
\end{itemize}
|
|
81 |
|
|
82 |
\item[cut-and-paste succeeds over copy-and-paste]
|
|
83 |
\emph{Never} copy-and-paste code when programming. If you
|
|
84 |
need the same piece of code twice, introduce a
|
|
85 |
reasonable auxiliary function (if there is no
|
|
86 |
such function, very likely you got something wrong).
|
|
87 |
Any copy-and-paste will turn out to be painful
|
|
88 |
when something has to be changed or fixed later on.
|
|
89 |
|
|
90 |
\item[comments]
|
|
91 |
are a device which requires careful thinking before using
|
|
92 |
it. The best comment for your code should be the code itself.
|
|
93 |
Prefer efforts to write clear, understandable code
|
|
94 |
over efforts to explain nasty code.
|
|
95 |
|
|
96 |
\item[functional programming is based on functions]
|
|
97 |
Avoid ``constructivisms'', e.g. pass a table lookup function,
|
|
98 |
rather than an actual table with lookup in body. Accustom
|
|
99 |
your way of codeing to the level of expressiveness
|
|
100 |
a functional programming language is giving onto you.
|
|
101 |
|
|
102 |
\item[tuples]
|
|
103 |
are often in the way. When there is no striking argument
|
|
104 |
to tuple function arguments, just write your function curried.
|
|
105 |
|
|
106 |
\item[telling names]
|
|
107 |
Any name should tell its purpose as exactly as possible,
|
|
108 |
while keeping its length to the absolutely neccessary minimum.
|
|
109 |
Always give the same name to function arguments which
|
|
110 |
have the same meaning. Separate words by underscores
|
21501
|
111 |
(``\verb|int_of_string|'', not ``\verb|intOfString|'')
|
21148
|
112 |
|
|
113 |
\end{description}%
|
18543
|
114 |
\end{isamarkuptext}%
|
|
115 |
\isamarkuptrue%
|
|
116 |
%
|
|
117 |
\isamarkupchapter{Basic library functions%
|
|
118 |
}
|
|
119 |
\isamarkuptrue%
|
|
120 |
%
|
|
121 |
\begin{isamarkuptext}%
|
22293
|
122 |
Beyond the proposal of the SML/NJ basis library, Isabelle comes
|
|
123 |
with its own library, from which selected parts are given here.
|
|
124 |
See further files \emph{Pure/library.ML} and \emph{Pure/General/*.ML}.%
|
|
125 |
\end{isamarkuptext}%
|
|
126 |
\isamarkuptrue%
|
|
127 |
%
|
|
128 |
\isamarkupsection{Linear transformations%
|
|
129 |
}
|
|
130 |
\isamarkuptrue%
|
|
131 |
%
|
|
132 |
\isadelimmlref
|
|
133 |
%
|
|
134 |
\endisadelimmlref
|
|
135 |
%
|
|
136 |
\isatagmlref
|
|
137 |
%
|
|
138 |
\begin{isamarkuptext}%
|
|
139 |
\begin{mldecls}
|
|
140 |
\indexml{(op |$>$)}\verb|(op |\verb,|,\verb|>): 'a * ('a -> 'b) -> 'b| \\
|
|
141 |
\indexml{fold}\verb|fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b| \\
|
|
142 |
\indexml{fold-rev}\verb|fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b| \\
|
|
143 |
\end{mldecls}%
|
|
144 |
\end{isamarkuptext}%
|
|
145 |
\isamarkuptrue%
|
|
146 |
%
|
|
147 |
\endisatagmlref
|
|
148 |
{\isafoldmlref}%
|
|
149 |
%
|
|
150 |
\isadelimmlref
|
|
151 |
%
|
|
152 |
\endisadelimmlref
|
|
153 |
%
|
22322
|
154 |
\isadelimML
|
|
155 |
%
|
|
156 |
\endisadelimML
|
|
157 |
%
|
|
158 |
\isatagML
|
|
159 |
%
|
|
160 |
\endisatagML
|
|
161 |
{\isafoldML}%
|
|
162 |
%
|
|
163 |
\isadelimML
|
|
164 |
%
|
|
165 |
\endisadelimML
|
|
166 |
%
|
22293
|
167 |
\begin{isamarkuptext}%
|
22322
|
168 |
Many problems in functional programming can be thought of
|
|
169 |
as linear transformations, i.e.~a caluclation starts with a
|
|
170 |
particular value \isa{x\ {\isasymColon}\ foo} which is then transformed
|
|
171 |
by application of a function \isa{f\ {\isasymColon}\ foo\ {\isasymRightarrow}\ foo},
|
|
172 |
continued by an application of a function \isa{g\ {\isasymColon}\ foo\ {\isasymRightarrow}\ bar},
|
|
173 |
and so on. As a canoncial example, take primitive functions enriching
|
|
174 |
theories by constants and definitions:
|
22550
|
175 |
\verb|Sign.add_consts_i: (string * typ * mixfix) list -> theory|\isasep\isanewline%
|
|
176 |
\verb|-> theory|
|
|
177 |
and \verb|Theory.add_defs_i: bool -> bool|\isasep\isanewline%
|
|
178 |
\verb|-> (bstring * term) list -> theory -> theory|.
|
22322
|
179 |
Written with naive application, an addition of a constant with
|
|
180 |
a corresponding definition would look like:
|
22550
|
181 |
\verb|Theory.add_defs_i false false [dummy_def]|\isasep\isanewline%
|
|
182 |
\verb| (Sign.add_consts_i [dummy_const] thy)|.
|
22322
|
183 |
With increasing numbers of applications, this code gets quite unreadable.
|
|
184 |
Using composition, at least the nesting of brackets may be reduced:
|
22550
|
185 |
\verb|(Theory.add_defs_i false false [dummy_def] o Sign.add_consts_i|\isasep\isanewline%
|
|
186 |
\verb| [dummy_const]) thy|.
|
22322
|
187 |
What remains unsatisfactory is that things are written down in the opposite order
|
|
188 |
as they actually ``happen''.%
|
|
189 |
\end{isamarkuptext}%
|
|
190 |
\isamarkuptrue%
|
|
191 |
%
|
|
192 |
\isadelimML
|
|
193 |
%
|
|
194 |
\endisadelimML
|
|
195 |
%
|
|
196 |
\isatagML
|
|
197 |
%
|
|
198 |
\endisatagML
|
|
199 |
{\isafoldML}%
|
|
200 |
%
|
|
201 |
\isadelimML
|
|
202 |
%
|
|
203 |
\endisadelimML
|
|
204 |
%
|
|
205 |
\begin{isamarkuptext}%
|
|
206 |
At this stage, Isabelle offers some combinators which allow for more convenient
|
|
207 |
notation, most notably reverse application:
|
|
208 |
\isasep\isanewline%
|
|
209 |
\verb|thy|\isasep\isanewline%
|
|
210 |
\verb||\verb,|,\verb|> Sign.add_consts_i [dummy_const]|\isasep\isanewline%
|
|
211 |
\verb||\verb,|,\verb|> Theory.add_defs_i false false [dummy_def]|%
|
|
212 |
\end{isamarkuptext}%
|
|
213 |
\isamarkuptrue%
|
|
214 |
%
|
|
215 |
\begin{isamarkuptext}%
|
22550
|
216 |
\noindent When iterating over a list of parameters \isa{{\isacharbrackleft}x\isactrlisub {\isadigit{1}}{\isacharcomma}\ x\isactrlisub {\isadigit{2}}{\isacharcomma}\ {\isasymdots}\ x\isactrlisub n{\isacharbrackright}\ {\isasymColon}\ {\isacharprime}a\ list},
|
22322
|
217 |
the \verb|fold| combinator lifts a single function \isa{f\ {\isasymColon}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}b\ {\isacharminus}{\isachargreater}\ {\isacharprime}b}:
|
|
218 |
\isa{y\ {\isacharbar}{\isachargreater}\ fold\ f\ {\isacharbrackleft}x\isactrlisub {\isadigit{1}}{\isacharcomma}\ x\isactrlisub {\isadigit{2}}{\isacharcomma}\ {\isasymdots}\ x\isactrlisub n{\isacharbrackright}\ {\isasymequiv}\ y\ {\isacharbar}{\isachargreater}\ f\ x\isactrlisub {\isadigit{1}}\ {\isacharbar}{\isachargreater}\ f\ x\isactrlisub {\isadigit{2}}\ {\isacharbar}{\isachargreater}\ {\isasymdots}\ {\isacharbar}{\isachargreater}\ f\ x\isactrlisub n}%
|
22293
|
219 |
\end{isamarkuptext}%
|
|
220 |
\isamarkuptrue%
|
|
221 |
%
|
|
222 |
\isadelimmlref
|
|
223 |
%
|
|
224 |
\endisadelimmlref
|
|
225 |
%
|
|
226 |
\isatagmlref
|
|
227 |
%
|
|
228 |
\begin{isamarkuptext}%
|
|
229 |
\begin{mldecls}
|
|
230 |
\indexml{(op |-$>$)}\verb|(op |\verb,|,\verb|->): ('c * 'a) * ('c -> 'a -> 'b) -> 'b| \\
|
|
231 |
\indexml{(op |$>$$>$)}\verb|(op |\verb,|,\verb|>>): ('a * 'c) * ('a -> 'b) -> 'b * 'c| \\
|
|
232 |
\indexml{(op ||$>$)}\verb|(op |\verb,|,\verb||\verb,|,\verb|>): ('c * 'a) * ('a -> 'b) -> 'c * 'b| \\
|
|
233 |
\indexml{(op ||$>$$>$)}\verb|(op |\verb,|,\verb||\verb,|,\verb|>>): ('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b| \\
|
|
234 |
\indexml{fold-map}\verb|fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b| \\
|
|
235 |
\end{mldecls}%
|
|
236 |
\end{isamarkuptext}%
|
|
237 |
\isamarkuptrue%
|
|
238 |
%
|
22322
|
239 |
\endisatagmlref
|
|
240 |
{\isafoldmlref}%
|
|
241 |
%
|
|
242 |
\isadelimmlref
|
|
243 |
%
|
|
244 |
\endisadelimmlref
|
|
245 |
%
|
|
246 |
\begin{isamarkuptext}%
|
22550
|
247 |
\noindent FIXME transformations involving side results%
|
22322
|
248 |
\end{isamarkuptext}%
|
|
249 |
\isamarkuptrue%
|
|
250 |
%
|
|
251 |
\isadelimmlref
|
|
252 |
%
|
|
253 |
\endisadelimmlref
|
|
254 |
%
|
|
255 |
\isatagmlref
|
|
256 |
%
|
22293
|
257 |
\begin{isamarkuptext}%
|
|
258 |
\begin{mldecls}
|
|
259 |
\indexml{(op \#$>$)}\verb|(op #>): ('a -> 'b) * ('b -> 'c) -> 'a -> 'c| \\
|
|
260 |
\indexml{(op \#-$>$)}\verb|(op #->): ('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd| \\
|
|
261 |
\indexml{(op \#$>$$>$)}\verb|(op #>>): ('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b| \\
|
|
262 |
\indexml{(op \#\#$>$)}\verb|(op ##>): ('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd| \\
|
|
263 |
\indexml{(op \#\#$>$$>$)}\verb|(op ##>>): ('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd| \\
|
|
264 |
\end{mldecls}%
|
|
265 |
\end{isamarkuptext}%
|
|
266 |
\isamarkuptrue%
|
|
267 |
%
|
22322
|
268 |
\endisatagmlref
|
|
269 |
{\isafoldmlref}%
|
|
270 |
%
|
|
271 |
\isadelimmlref
|
|
272 |
%
|
|
273 |
\endisadelimmlref
|
|
274 |
%
|
|
275 |
\begin{isamarkuptext}%
|
22550
|
276 |
\noindent All those linear combinators also exist in higher-order
|
|
277 |
variants which do not expect a value on the left hand side
|
|
278 |
but a function.%
|
22322
|
279 |
\end{isamarkuptext}%
|
|
280 |
\isamarkuptrue%
|
|
281 |
%
|
|
282 |
\isadelimmlref
|
|
283 |
%
|
|
284 |
\endisadelimmlref
|
|
285 |
%
|
|
286 |
\isatagmlref
|
|
287 |
%
|
22293
|
288 |
\begin{isamarkuptext}%
|
|
289 |
\begin{mldecls}
|
|
290 |
\indexml{(op `)}\verb|(op `): ('b -> 'a) -> 'b -> 'a * 'b| \\
|
|
291 |
\indexml{tap}\verb|tap: ('b -> 'a) -> 'b -> 'b| \\
|
|
292 |
\end{mldecls}%
|
|
293 |
\end{isamarkuptext}%
|
|
294 |
\isamarkuptrue%
|
|
295 |
%
|
|
296 |
\endisatagmlref
|
|
297 |
{\isafoldmlref}%
|
|
298 |
%
|
|
299 |
\isadelimmlref
|
|
300 |
%
|
|
301 |
\endisadelimmlref
|
|
302 |
%
|
22550
|
303 |
\begin{isamarkuptext}%
|
|
304 |
\noindent FIXME%
|
|
305 |
\end{isamarkuptext}%
|
|
306 |
\isamarkuptrue%
|
|
307 |
%
|
22293
|
308 |
\isamarkupsection{Options and partiality%
|
|
309 |
}
|
|
310 |
\isamarkuptrue%
|
|
311 |
%
|
|
312 |
\isadelimmlref
|
|
313 |
%
|
|
314 |
\endisadelimmlref
|
|
315 |
%
|
|
316 |
\isatagmlref
|
|
317 |
%
|
|
318 |
\begin{isamarkuptext}%
|
|
319 |
\begin{mldecls}
|
|
320 |
\indexml{is-some}\verb|is_some: 'a option -> bool| \\
|
|
321 |
\indexml{is-none}\verb|is_none: 'a option -> bool| \\
|
|
322 |
\indexml{the}\verb|the: 'a option -> 'a| \\
|
|
323 |
\indexml{these}\verb|these: 'a list option -> 'a list| \\
|
|
324 |
\indexml{the-list}\verb|the_list: 'a option -> 'a list| \\
|
|
325 |
\indexml{the-default}\verb|the_default: 'a -> 'a option -> 'a| \\
|
|
326 |
\indexml{try}\verb|try: ('a -> 'b) -> 'a -> 'b option| \\
|
|
327 |
\indexml{can}\verb|can: ('a -> 'b) -> 'a -> bool| \\
|
|
328 |
\end{mldecls}%
|
|
329 |
\end{isamarkuptext}%
|
|
330 |
\isamarkuptrue%
|
|
331 |
%
|
|
332 |
\endisatagmlref
|
|
333 |
{\isafoldmlref}%
|
|
334 |
%
|
|
335 |
\isadelimmlref
|
|
336 |
%
|
|
337 |
\endisadelimmlref
|
|
338 |
%
|
|
339 |
\begin{isamarkuptext}%
|
22550
|
340 |
Standard selector functions on \isa{option}s are provided.
|
|
341 |
The \verb|try| and \verb|can| functions provide a convenient
|
|
342 |
interface for handling exceptions -- both take as arguments
|
|
343 |
a function \isa{f} together with a parameter \isa{x}
|
|
344 |
and catch any exception during the evaluation of the application
|
|
345 |
of \isa{f} to \isa{x}, either return a lifted result
|
|
346 |
(\verb|NONE| on failure) or a boolean value (\verb|false| on failure).%
|
22293
|
347 |
\end{isamarkuptext}%
|
|
348 |
\isamarkuptrue%
|
|
349 |
%
|
|
350 |
\isamarkupsection{Common data structures%
|
|
351 |
}
|
|
352 |
\isamarkuptrue%
|
|
353 |
%
|
|
354 |
\isamarkupsubsection{Lists (as set-like data structures)%
|
|
355 |
}
|
|
356 |
\isamarkuptrue%
|
|
357 |
%
|
|
358 |
\begin{isamarkuptext}%
|
|
359 |
\begin{mldecls}
|
|
360 |
\indexml{member}\verb|member: ('b * 'a -> bool) -> 'a list -> 'b -> bool| \\
|
|
361 |
\indexml{insert}\verb|insert: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list| \\
|
|
362 |
\indexml{remove}\verb|remove: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list| \\
|
|
363 |
\indexml{merge}\verb|merge: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list| \\
|
|
364 |
\end{mldecls}%
|
|
365 |
\end{isamarkuptext}%
|
|
366 |
\isamarkuptrue%
|
|
367 |
%
|
|
368 |
\begin{isamarkuptext}%
|
22503
|
369 |
Lists are often used as set-like data structures -- set-like in
|
|
370 |
then sense that they support notion of \verb|member|-ship,
|
|
371 |
\verb|insert|-ing and \verb|remove|-ing, but are order-sensitive.
|
|
372 |
This is convenient when implementing a history-like mechanism:
|
|
373 |
\verb|insert| adds an element \emph{to the front} of a list,
|
|
374 |
if not yet present; \verb|remove| removes \emph{all} occurences
|
|
375 |
of a particular element. Correspondingly \verb|merge| implements a
|
|
376 |
a merge on two lists suitable for merges of context data
|
|
377 |
(\secref{sec:context-theory}).
|
|
378 |
|
|
379 |
Functions are parametrized by an explicit equality function
|
|
380 |
to accomplish overloaded equality; in most cases of monomorphic
|
|
381 |
equality, writing \verb|(op =)| should suffice.%
|
22293
|
382 |
\end{isamarkuptext}%
|
|
383 |
\isamarkuptrue%
|
|
384 |
%
|
|
385 |
\isamarkupsubsection{Association lists%
|
|
386 |
}
|
|
387 |
\isamarkuptrue%
|
|
388 |
%
|
|
389 |
\begin{isamarkuptext}%
|
|
390 |
\begin{mldecls}
|
|
391 |
\indexmlexception{AList.DUP}\verb|exception AList.DUP| \\
|
|
392 |
\indexml{AList.lookup}\verb|AList.lookup: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option| \\
|
|
393 |
\indexml{AList.defined}\verb|AList.defined: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool| \\
|
|
394 |
\indexml{AList.update}\verb|AList.update: ('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list| \\
|
|
395 |
\indexml{AList.default}\verb|AList.default: ('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list| \\
|
|
396 |
\indexml{AList.delete}\verb|AList.delete: ('a * 'b -> bool) -> 'a -> ('b * 'c) list -> ('b * 'c) list| \\
|
|
397 |
\indexml{AList.map-entry}\verb|AList.map_entry: ('a * 'b -> bool) -> 'a|\isasep\isanewline%
|
|
398 |
\verb| -> ('c -> 'c) -> ('b * 'c) list -> ('b * 'c) list| \\
|
|
399 |
\indexml{AList.map-default}\verb|AList.map_default: ('a * 'a -> bool) -> 'a * 'b -> ('b -> 'b)|\isasep\isanewline%
|
|
400 |
\verb| -> ('a * 'b) list -> ('a * 'b) list| \\
|
|
401 |
\indexml{AList.join}\verb|AList.join: ('a * 'a -> bool) -> ('a -> 'b * 'b -> 'b) (*exception DUP*)|\isasep\isanewline%
|
|
402 |
\verb| -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)| \\
|
|
403 |
\indexml{AList.merge}\verb|AList.merge: ('a * 'a -> bool) -> ('b * 'b -> bool)|\isasep\isanewline%
|
|
404 |
\verb| -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)|
|
|
405 |
\end{mldecls}%
|
|
406 |
\end{isamarkuptext}%
|
|
407 |
\isamarkuptrue%
|
|
408 |
%
|
|
409 |
\begin{isamarkuptext}%
|
22503
|
410 |
Association lists can be seens as an extension of set-like lists:
|
|
411 |
on the one hand, they may be used to implement finite mappings,
|
|
412 |
on the other hand, they remain order-sensitive and allow for
|
|
413 |
multiple key-value-pair with the same key: \verb|AList.lookup|
|
|
414 |
returns the \emph{first} value corresponding to a particular
|
|
415 |
key, if present. \verb|AList.update| updates
|
|
416 |
the \emph{first} occurence of a particular key; if no such
|
|
417 |
key exists yet, the key-value-pair is added \emph{to the front}.
|
|
418 |
\verb|AList.delete| only deletes the \emph{first} occurence of a key.
|
|
419 |
\verb|AList.merge| provides an operation suitable for merges of context data
|
|
420 |
(\secref{sec:context-theory}), where an equality parameter on
|
|
421 |
values determines whether a merge should be considered a conflict.
|
|
422 |
A slightly generalized operation if implementend by the \verb|AList.join|
|
|
423 |
function which allows for explicit conflict resolution.%
|
22293
|
424 |
\end{isamarkuptext}%
|
|
425 |
\isamarkuptrue%
|
|
426 |
%
|
|
427 |
\isamarkupsubsection{Tables%
|
|
428 |
}
|
|
429 |
\isamarkuptrue%
|
|
430 |
%
|
|
431 |
\begin{isamarkuptext}%
|
|
432 |
\begin{mldecls}
|
|
433 |
\indexmltype{Symtab.key}\verb|type Symtab.key| \\
|
|
434 |
\indexmltype{'a Symtab.table}\verb|type 'a Symtab.table| \\
|
|
435 |
\indexmlexception{Symtab.DUP}\verb|exception Symtab.DUP of Symtab.key| \\
|
|
436 |
\indexmlexception{Symtab.DUPS}\verb|exception Symtab.DUPS of Symtab.key list| \\
|
|
437 |
\indexmlexception{Symtab.SAME}\verb|exception Symtab.SAME| \\
|
|
438 |
\indexmlexception{Symtab.UNDEF}\verb|exception Symtab.UNDEF of Symtab.key| \\
|
|
439 |
\indexml{Symtab.empty}\verb|Symtab.empty: 'a Symtab.table| \\
|
|
440 |
\indexml{Symtab.dest}\verb|Symtab.dest: 'a Symtab.table -> (Symtab.key * 'a) list| \\
|
|
441 |
\indexml{Symtab.keys}\verb|Symtab.keys: 'a Symtab.table -> Symtab.key list| \\
|
|
442 |
\indexml{Symtab.lookup}\verb|Symtab.lookup: 'a Symtab.table -> Symtab.key -> 'a option| \\
|
|
443 |
\indexml{Symtab.defined}\verb|Symtab.defined: 'a Symtab.table -> Symtab.key -> bool| \\
|
|
444 |
\indexml{Symtab.update}\verb|Symtab.update: (Symtab.key * 'a) -> 'a Symtab.table -> 'a Symtab.table| \\
|
|
445 |
\indexml{Symtab.default}\verb|Symtab.default: Symtab.key * 'a -> 'a Symtab.table -> 'a Symtab.table| \\
|
|
446 |
\indexml{Symtab.delete}\verb|Symtab.delete: Symtab.key|\isasep\isanewline%
|
|
447 |
\verb| -> 'a Symtab.table -> 'a Symtab.table (*exception Symtab.UNDEF*)| \\
|
|
448 |
\indexml{Symtab.map-entry}\verb|Symtab.map_entry: Symtab.key -> ('a -> 'a)|\isasep\isanewline%
|
|
449 |
\verb| -> 'a Symtab.table -> 'a Symtab.table| \\
|
|
450 |
\indexml{Symtab.map-default}\verb|Symtab.map_default: (Symtab.key * 'a) -> ('a -> 'a)|\isasep\isanewline%
|
|
451 |
\verb| -> 'a Symtab.table -> 'a Symtab.table| \\
|
|
452 |
\indexml{Symtab.join}\verb|Symtab.join: (Symtab.key -> 'a * 'a -> 'a) (*exception Symtab.DUP/Symtab.SAME*)|\isasep\isanewline%
|
|
453 |
\verb| -> 'a Symtab.table * 'a Symtab.table|\isasep\isanewline%
|
|
454 |
\verb| -> 'a Symtab.table (*exception Symtab.DUPS*)| \\
|
|
455 |
\indexml{Symtab.merge}\verb|Symtab.merge: ('a * 'a -> bool)|\isasep\isanewline%
|
|
456 |
\verb| -> 'a Symtab.table * 'a Symtab.table|\isasep\isanewline%
|
|
457 |
\verb| -> 'a Symtab.table (*exception Symtab.DUPS*)|
|
|
458 |
\end{mldecls}%
|
|
459 |
\end{isamarkuptext}%
|
|
460 |
\isamarkuptrue%
|
|
461 |
%
|
|
462 |
\begin{isamarkuptext}%
|
22503
|
463 |
Tables are an efficient representation of finite mappings without
|
|
464 |
any notion of order; due to their efficiency they should be used
|
|
465 |
whenever such pure finite mappings are neccessary.
|
|
466 |
|
|
467 |
The key type of tables must be given explicitly by instantiating
|
|
468 |
the \verb|TableFun| functor which takes the key type
|
|
469 |
together with its \verb|order|; for convience, we restrict
|
|
470 |
here to the \verb|Symtab| instance with \verb|string|
|
|
471 |
as key type.
|
|
472 |
|
|
473 |
Most table functions correspond to those of association lists.%
|
18543
|
474 |
\end{isamarkuptext}%
|
|
475 |
\isamarkuptrue%
|
|
476 |
%
|
20490
|
477 |
\isamarkupchapter{Cookbook%
|
|
478 |
}
|
|
479 |
\isamarkuptrue%
|
|
480 |
%
|
20491
|
481 |
\isamarkupsection{A method that depends on declarations in the context%
|
20490
|
482 |
}
|
|
483 |
\isamarkuptrue%
|
|
484 |
%
|
|
485 |
\begin{isamarkuptext}%
|
|
486 |
FIXME%
|
|
487 |
\end{isamarkuptext}%
|
|
488 |
\isamarkuptrue%
|
|
489 |
%
|
18543
|
490 |
\isadelimtheory
|
|
491 |
%
|
|
492 |
\endisadelimtheory
|
|
493 |
%
|
|
494 |
\isatagtheory
|
|
495 |
\isacommand{end}\isamarkupfalse%
|
18537
|
496 |
%
|
|
497 |
\endisatagtheory
|
|
498 |
{\isafoldtheory}%
|
|
499 |
%
|
|
500 |
\isadelimtheory
|
|
501 |
%
|
|
502 |
\endisadelimtheory
|
18543
|
503 |
\isanewline
|
18537
|
504 |
\end{isabellebody}%
|
|
505 |
%%% Local Variables:
|
|
506 |
%%% mode: latex
|
|
507 |
%%% TeX-master: "root"
|
|
508 |
%%% End:
|