3 theory "ML" imports base begin
5 chapter {* Aesthetics of ML programming *}
9 text {* This style guide is loosely based on
10 \url{http://caml.inria.fr/resources/doc/guides/guidelines.en.html}.
11 % FIMXE \url{http://www.cs.cornell.edu/Courses/cs312/2003sp/handouts/style.htm}
13 Like any style guide, it should not be interpreted dogmatically.
14 Instead, it forms a collection of recommendations which,
15 if obeyed, result in code that is not considered to be
16 obfuscated. In certain cases, derivations are encouraged,
17 as far as you know what you are doing.
21 \item[fundamental law of programming]
22 Whenever writing code, keep in mind: A program is
23 written once, modified ten times, and read
24 100 times. So simplify its writing,
25 always keep future modifications in mind,
26 and never jeopardize readability. Every second you hesitate
27 to spend on making your code more clear you will
28 have to spend ten times understanding what you have
31 \item[white space matters]
32 Treat white space in your code as if it determines
37 \item The space bar is the easiest key to find on the keyboard,
38 press it as often as necessary. {\ttfamily 2 + 2} is better
39 than {\ttfamily 2+2}, likewise {\ttfamily f (x, y)}
40 better than {\ttfamily f(x,y)}.
42 \item Restrict your lines to \emph{at most} 80 characters.
43 This will allow you to keep the beginning of a line
44 in view while watching its end.
46 \item Ban tabs; they are a context-sensitive formatting
47 feature and likely to confuse anyone not using your
50 \item Get rid of trailing whitespace. Instead, do not
51 surpess a trailing newline at the end of your files.
53 \item Choose a generally accepted style of indentation,
54 then use it systematically throughout the whole
55 application. An indentation of two spaces is appropriate.
56 Avoid dangling indentation.
60 \item[cut-and-paste succeeds over copy-and-paste]
61 \emph{Never} copy-and-paste code when programming. If you
62 need the same piece of code twice, introduce a
63 reasonable auxiliary function (if there is no
64 such function, very likely you got something wrong).
65 Any copy-and-paste will turn out to be painful
66 when something has to be changed or fixed later on.
69 are a device which requires careful thinking before using
70 it. The best comment for your code should be the code itself.
71 Prefer efforts to write clear, understandable code
72 over efforts to explain nasty code.
74 \item[functional programming is based on functions]
75 Avoid ``constructivisms'', e.g. pass a table lookup function,
76 rather than an actual table with lookup in body. Accustom
77 your way of codeing to the level of expressiveness
78 a functional programming language is giving onto you.
81 are often in the way. When there is no striking argument
82 to tuple function arguments, just write your function curried.
85 Any name should tell its purpose as exactly as possible,
86 while keeping its length to the absolutely neccessary minimum.
87 Always give the same name to function arguments which
88 have the same meaning. Separate words by underscores
89 (``@{verbatim int_of_string}'', not ``@{verbatim intOfString}'')
95 chapter {* Basic library functions *}
98 Beyond the proposal of the SML/NJ basis library, Isabelle comes
99 with its own library, from which selected parts are given here.
100 See further files \emph{Pure/library.ML} and \emph{Pure/General/*.ML}.
103 section {* Linear transformations *}
107 @{index_ML "(op |>)": "'a * ('a -> 'b) -> 'b"} \\
108 @{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
109 @{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
117 @{index_ML "(op |->)": "('c * 'a) * ('c -> 'a -> 'b) -> 'b"} \\
118 @{index_ML "(op |>>)": "('a * 'c) * ('a -> 'b) -> 'b * 'c"} \\
119 @{index_ML "(op ||>)": "('c * 'a) * ('a -> 'b) -> 'c * 'b"} \\
120 @{index_ML "(op ||>>)": "('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b"} \\
121 @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
127 @{index_ML "(op #>)": "('a -> 'b) * ('b -> 'c) -> 'a -> 'c"} \\
128 @{index_ML "(op #->)": "('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd"} \\
129 @{index_ML "(op #>>)": "('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b"} \\
130 @{index_ML "(op ##>)": "('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd"} \\
131 @{index_ML "(op ##>>)": "('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd"} \\
137 @{index_ML "(op `)": "('b -> 'a) -> 'b -> 'a * 'b"} \\
138 @{index_ML tap: "('b -> 'a) -> 'b -> 'b"} \\
142 section {* Options and partiality *}
146 @{index_ML is_some: "'a option -> bool"} \\
147 @{index_ML is_none: "'a option -> bool"} \\
148 @{index_ML the: "'a option -> 'a"} \\
149 @{index_ML these: "'a list option -> 'a list"} \\
150 @{index_ML the_list: "'a option -> 'a list"} \\
151 @{index_ML the_default: "'a -> 'a option -> 'a"} \\
152 @{index_ML try: "('a -> 'b) -> 'a -> 'b option"} \\
153 @{index_ML can: "('a -> 'b) -> 'a -> bool"} \\
159 section {* Common data structures *}
161 text "FIXME chronicle"
163 subsection {* Lists (as set-like data structures) *}
167 @{index_ML member: "('b * 'a -> bool) -> 'a list -> 'b -> bool"} \\
168 @{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
169 @{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\
170 @{index_ML merge: "('a * 'a -> bool) -> 'a list * 'a list -> 'a list"} \\
176 subsection {* Association lists *}
180 @{index_ML_exc AList.DUP} \\
181 @{index_ML AList.lookup: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option"} \\
182 @{index_ML AList.defined: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool"} \\
183 @{index_ML AList.update: "('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list"} \\
184 @{index_ML AList.default: "('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list"} \\
185 @{index_ML AList.delete: "('a * 'b -> bool) -> 'a -> ('b * 'c) list -> ('b * 'c) list"} \\
186 @{index_ML AList.map_entry: "('a * 'b -> bool) -> 'a
187 -> ('c -> 'c) -> ('b * 'c) list -> ('b * 'c) list"} \\
188 @{index_ML AList.map_default: "('a * 'a -> bool) -> 'a * 'b -> ('b -> 'b)
189 -> ('a * 'b) list -> ('a * 'b) list"} \\
190 @{index_ML AList.join: "('a * 'a -> bool) -> ('a -> 'b * 'b -> 'b) (*exception DUP*)
191 -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)"} \\
192 @{index_ML AList.merge: "('a * 'a -> bool) -> ('b * 'b -> bool)
193 -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)"}
199 subsection {* Tables *}
203 @{index_ML_type Symtab.key} \\
204 @{index_ML_type "'a Symtab.table"} \\
205 @{index_ML_exc Symtab.DUP: Symtab.key} \\
206 @{index_ML_exc Symtab.DUPS: "Symtab.key list"} \\
207 @{index_ML_exc Symtab.SAME} \\
208 @{index_ML_exc Symtab.UNDEF: Symtab.key} \\
209 @{index_ML Symtab.empty: "'a Symtab.table"} \\
210 @{index_ML Symtab.dest: "'a Symtab.table -> (Symtab.key * 'a) list"} \\
211 @{index_ML Symtab.keys: "'a Symtab.table -> Symtab.key list"} \\
212 @{index_ML Symtab.lookup: "'a Symtab.table -> Symtab.key -> 'a option"} \\
213 @{index_ML Symtab.defined: "'a Symtab.table -> Symtab.key -> bool"} \\
214 @{index_ML Symtab.update: "(Symtab.key * 'a) -> 'a Symtab.table -> 'a Symtab.table"} \\
215 @{index_ML Symtab.default: "Symtab.key * 'a -> 'a Symtab.table -> 'a Symtab.table"} \\
216 @{index_ML Symtab.delete: "Symtab.key
217 -> 'a Symtab.table -> 'a Symtab.table (*exception Symtab.UNDEF*)"} \\
218 @{index_ML Symtab.map_entry: "Symtab.key -> ('a -> 'a)
219 -> 'a Symtab.table -> 'a Symtab.table"} \\
220 @{index_ML Symtab.map_default: "(Symtab.key * 'a) -> ('a -> 'a)
221 -> 'a Symtab.table -> 'a Symtab.table"} \\
222 @{index_ML Symtab.join: "(Symtab.key -> 'a * 'a -> 'a) (*exception Symtab.DUP/Symtab.SAME*)
223 -> 'a Symtab.table * 'a Symtab.table
224 -> 'a Symtab.table (*exception Symtab.DUPS*)"} \\
225 @{index_ML Symtab.merge: "('a * 'a -> bool)
226 -> 'a Symtab.table * 'a Symtab.table
227 -> 'a Symtab.table (*exception Symtab.DUPS*)"}
233 chapter {* Cookbook *}
235 section {* A method that depends on declarations in the context *}