102 subsection {* Naming conventions *} |
102 subsection {* Naming conventions *} |
103 |
103 |
104 text {* Since ML is the primary medium to express the meaning of the |
104 text {* Since ML is the primary medium to express the meaning of the |
105 source text, naming of ML entities requires special care. |
105 source text, naming of ML entities requires special care. |
106 |
106 |
107 \paragraph{Notation.} A name consists of 1--3 \emph{words} (not |
107 \paragraph{Notation.} A name consists of 1--3 \emph{words} (rarely |
108 more) that are separated by underscore. There are three variants |
108 4, but not more) that are separated by underscore. There are three |
109 concerning upper or lower case letters, which are just for certain |
109 variants concerning upper or lower case letters, which are just for |
110 ML categories as follows: |
110 certain ML categories as follows: |
111 |
111 |
112 \medskip |
112 \medskip |
113 \begin{tabular}{lll} |
113 \begin{tabular}{lll} |
114 variant & example & ML categories \\\hline |
114 variant & example & ML categories \\\hline |
115 lower-case & @{verbatim foo_bar} & values, types, record fields \\ |
115 lower-case & @{verbatim foo_bar} & values, types, record fields \\ |
126 words is essential for readability.\footnote{Camel-case was invented |
126 words is essential for readability.\footnote{Camel-case was invented |
127 to workaround the lack of underscore in some early non-ASCII |
127 to workaround the lack of underscore in some early non-ASCII |
128 character sets and keywords. Later it became a habitual in some |
128 character sets and keywords. Later it became a habitual in some |
129 language communities that are now strong in numbers.} |
129 language communities that are now strong in numbers.} |
130 |
130 |
131 A single character does not count as ``word'' in this respect: some |
131 A single (capital) character does not count as ``word'' in this |
132 Isabelle/ML are suffixed by extra markers like this: @{verbatim |
132 respect: some Isabelle/ML are suffixed by extra markers like this: |
133 foo_barT}. |
133 @{verbatim foo_barT}. |
|
134 |
|
135 Name variants are produced by adding 1--3 primes, e.g.\ @{verbatim |
|
136 foo'}, @{verbatim foo''}, or @{verbatim foo'''}, but not @{verbatim |
|
137 foo''''} or more. Decimal digits scale better to larger numbers, |
|
138 e.g.\ @{verbatim foo0}, @{verbatim foo1}, @{verbatim foo42}. |
134 |
139 |
135 \paragraph{Scopes.} Apart from very basic library modules, ML |
140 \paragraph{Scopes.} Apart from very basic library modules, ML |
136 structures are not ``opened'', but names are referenced with |
141 structures are not ``opened'', but names are referenced with |
137 explicit qualification: as in @{ML Syntax.string_of_term} for |
142 explicit qualification, as in @{ML Syntax.string_of_term} for |
138 example. When devising names for structures and their components it |
143 example. When devising names for structures and their components it |
139 is important aim at eye-catching compositions of both parts, because |
144 is important aim at eye-catching compositions of both parts, because |
140 this is how they are always seen in the sources and documentation. |
145 this is how they are always seen in the sources and documentation. |
141 For the same reasons, aliases of well-known library functions should |
146 For the same reasons, aliases of well-known library functions should |
142 be avoided. |
147 be avoided. |
169 in ... end; |
182 in ... end; |
170 |
183 |
171 \end{verbatim} |
184 \end{verbatim} |
172 |
185 |
173 |
186 |
174 \paragraph{Specific conventions.} FIXME |
187 \paragraph{Specific conventions.} Here are some important specific |
|
188 name forms that occur frequently in the sources. |
|
189 |
|
190 \begin{itemize} |
|
191 |
|
192 \item A function that maps @{verbatim foo} to @{verbatim bar} is |
|
193 called @{verbatim foo_to_bar} or @{verbatim bar_of_foo} (never |
|
194 @{verbatim foo2bar}, @{verbatim bar_from_foo}, @{verbatim |
|
195 bar_for_foo}, or @{verbatim bar4foo}). |
|
196 |
|
197 \item The name component @{verbatim legacy} means that the operation |
|
198 is about to be discontinued soon. |
|
199 |
|
200 \item The name component @{verbatim old} means that this is historic |
|
201 material that might disappear at some later stage. |
|
202 |
|
203 \item The name component @{verbatim global} means that this works |
|
204 with the background theory instead of the regular local context |
|
205 (\secref{sec:context}), sometimes for historical reasons, sometimes |
|
206 due a genuine lack of locality of the concept involved, sometimes as |
|
207 a fall-back for the lack of a proper context in the application |
|
208 code. Whenever there is a non-global variant available, the |
|
209 application should be migrated to use it with a proper local |
|
210 context. |
|
211 |
|
212 \item Variables of the main context types of the Isabelle/Isar |
|
213 framework (\secref{sec:context} and \chref{ch:local-theory}) have |
|
214 firm naming conventions to allow to visualize the their data flow |
|
215 via plain regular expressions in the editor. In particular: |
|
216 |
|
217 \begin{itemize} |
|
218 |
|
219 \item theories are called @{verbatim thy}, rarely @{verbatim theory} |
|
220 (never @{verbatim thry}) |
|
221 |
|
222 \item proof contexts are called @{verbatim ctxt}, rarely @{verbatim |
|
223 context} (never @{verbatim ctx}) |
|
224 |
|
225 \item generic contexts are called @{verbatim context}, rarely |
|
226 @{verbatim ctxt} |
|
227 |
|
228 \item local theories are called @{verbatim lthy}, unless there is an |
|
229 implicit conversion to a general proof context (semantic super-type) |
|
230 |
|
231 \end{itemize} |
|
232 |
|
233 Variations with primed or decimal numbers are always possible, as |
|
234 well as ``sematic prefixes'' like @{verbatim foo_thy} or @{verbatim |
|
235 bar_ctxt}, but the base conventions above need to be preserved. |
|
236 |
|
237 \item The main logical entities (\secref{ch:logic}) also have |
|
238 established naming convention: |
|
239 |
|
240 \begin{itemize} |
|
241 |
|
242 \item sorts are called @{verbatim S} |
|
243 |
|
244 \item types are called @{verbatim T}, @{verbatim U}, or @{verbatim |
|
245 ty} (never @{verbatim t}) |
|
246 |
|
247 \item terms are called @{verbatim t}, @{verbatim u}, or @{verbatim |
|
248 tm} (never @{verbatim trm}) |
|
249 |
|
250 \item certified types are called @{verbatim cT}, rarely @{verbatim |
|
251 T}, with variants as for types |
|
252 |
|
253 \item certified terms are called @{verbatim ct}, rarely @{verbatim |
|
254 t}, with variants as for terms |
|
255 |
|
256 \item theorems are called @{verbatim th}, or @{verbatim thm} |
|
257 |
|
258 \end{itemize} |
|
259 |
|
260 Proper semantic names override these conventions completely. For |
|
261 example, the left-hand side of an equation (as a term) can be called |
|
262 @{verbatim lhs} (not @{verbatim lhs_tm}). Or a term that is treated |
|
263 specifically as a variable can be called @{verbatim v} or @{verbatim |
|
264 x}. |
|
265 |
|
266 \end{itemize} |
175 *} |
267 *} |
176 |
268 |
177 |
269 |
178 subsection {* General source layout *} |
270 subsection {* General source layout *} |
179 |
271 |
183 programming. |
275 programming. |
184 |
276 |
185 \paragraph{Line length} is 80 characters according to ancient |
277 \paragraph{Line length} is 80 characters according to ancient |
186 standards, but we allow as much as 100 characters, not |
278 standards, but we allow as much as 100 characters, not |
187 more.\footnote{Readability requires to keep the beginning of a line |
279 more.\footnote{Readability requires to keep the beginning of a line |
188 in view while watching its end. Modern wide-screen displays did not |
280 in view while watching its end. Modern wide-screen displays do not |
189 change the way how the human brain works. As a rule of thumb, |
281 change the way how the human brain works. As a rule of thumb, |
190 sources need to be printable on plain paper.} The extra 20 |
282 sources need to be printable on plain paper.} The extra 20 |
191 characters acknowledge the space requirements due to qualified |
283 characters acknowledge the space requirements due to qualified |
192 library references in Isabelle/ML. |
284 library references in Isabelle/ML. |
193 |
285 |
270 \emph{maintainability}) etc. |
362 \emph{maintainability}) etc. |
271 |
363 |
272 \medskip For similar reasons, any kind of two-dimensional or tabular |
364 \medskip For similar reasons, any kind of two-dimensional or tabular |
273 layouts, ASCII-art with lines or boxes of stars etc. should be |
365 layouts, ASCII-art with lines or boxes of stars etc. should be |
274 avoided. |
366 avoided. |
|
367 |
|
368 \paragraph{Complex expressions} consisting of multi-clausal function |
|
369 definitions, @{verbatim case}, @{verbatim handle}, @{verbatim let}, |
|
370 or combinations require special attention. The syntax of Standard |
|
371 ML is a bit too ambitious in admitting too much variance that can |
|
372 distort the meaning of the text. |
|
373 |
|
374 Clauses of @{verbatim fun}, @{verbatim fn}, @{verbatim case}, |
|
375 @{verbatim handle} get extra indentation to indicate the nesting |
|
376 clearly. For example: |
|
377 |
|
378 \begin{verbatim} |
|
379 (* RIGHT *) |
|
380 |
|
381 fun foo p1 = |
|
382 expr1 |
|
383 | foo p2 = |
|
384 expr2 |
|
385 |
|
386 |
|
387 (* WRONG *) |
|
388 |
|
389 fun foo p1 = |
|
390 expr1 |
|
391 | foo p2 = |
|
392 expr2 |
|
393 \end{verbatim} |
|
394 |
|
395 Body expressions consisting of @{verbatim case} or @{verbatim let} |
|
396 require care to maintain compositionality, to prevent loss of |
|
397 logical indentation where it is particularly imporant to see the |
|
398 structure of the text later on. Example: |
|
399 |
|
400 \begin{verbatim} |
|
401 (* RIGHT *) |
|
402 |
|
403 fun foo p1 = |
|
404 (case e of |
|
405 q1 => ... |
|
406 | q2 => ...) |
|
407 | foo p2 = |
|
408 let |
|
409 ... |
|
410 in |
|
411 ... |
|
412 end |
|
413 |
|
414 |
|
415 (* WRONG *) |
|
416 |
|
417 fun foo p1 = case e of |
|
418 q1 => ... |
|
419 | q2 => ... |
|
420 | foo p2 = |
|
421 let |
|
422 ... |
|
423 in |
|
424 ... |
|
425 end |
|
426 \end{verbatim} |
|
427 |
|
428 Extra parentheses around @{verbatim case} expressions are optional, |
|
429 but help to analyse the nesting easily based on simple string |
|
430 matching in the editor. |
|
431 |
|
432 \medskip There are two main exceptions to the overall principle of |
|
433 compositionality in the layout of complex expressions. |
|
434 |
|
435 \begin{enumerate} |
|
436 |
|
437 \item @{verbatim "if"} expressions are iterated as if there would be |
|
438 a multi-branch conditional in SML, e.g. |
|
439 |
|
440 \begin{verbatim} |
|
441 (* RIGHT *) |
|
442 |
|
443 if b1 then e1 |
|
444 else if b2 then e2 |
|
445 else e3 |
|
446 \end{verbatim} |
|
447 |
|
448 \item @{verbatim fn} abstractions are often layed-out as if they |
|
449 would lack any structure by themselves. This traditional form is |
|
450 motivated by the possibility to shift function arguments back and |
|
451 forth wrt.\ additional combinators. For example: |
|
452 |
|
453 \begin{verbatim} |
|
454 (* RIGHT *) |
|
455 |
|
456 fun foo x y = fold (fn z => |
|
457 expr) |
|
458 \end{verbatim} |
|
459 |
|
460 Here the visual appearance is that of three arguments @{verbatim x}, |
|
461 @{verbatim y}, @{verbatim z}. |
|
462 |
|
463 \end{enumerate} |
|
464 |
|
465 Such weakly structured layout should be use with great care. Here |
|
466 is a counter-example involving @{verbatim let} expressions: |
|
467 |
|
468 \begin{verbatim} |
|
469 (* WRONG *) |
|
470 |
|
471 fun foo x = let |
|
472 val y = ... |
|
473 in ... end |
|
474 |
|
475 fun foo x = |
|
476 let |
|
477 val y = ... |
|
478 in ... end |
|
479 \end{verbatim} |
|
480 |
|
481 \medskip In general the source layout is meant to emphasize the |
|
482 structure of complex language expressions, not to pretend that SML |
|
483 had a completely different syntax (say that of Haskell or Java). |
275 *} |
484 *} |
276 |
485 |
277 |
486 |
278 section {* SML embedded into Isabelle/Isar *} |
487 section {* SML embedded into Isabelle/Isar *} |
279 |
488 |