Debian Codegraph @

doc/guide/src/SyntacticConventions.adoc

raw
1 = SyntacticConventions
2 :toc:
3
4 Here are a number of syntactic conventions useful for programming in
5 SML.
6
7
8 == General
9
10 * A line of code never exceeds 80 columns.
11
12 * Only split a syntactic entity across multiple lines if it doesn't fit on one line within 80 columns.
13
14 * Use alphabetical order wherever possible.
15
16 * Avoid redundant parentheses.
17
18 * When using `:`, there is no space before the colon, and a single space after it.
19
20
21 == Identifiers
22
23 * Variables, record labels and type constructors begin with and use
24 small letters, using capital letters to separate words.
25 +
26 [source,sml]
27 ----
28 cost
29 maxValue
30 ----
31
32 * Variables that represent collections of objects (lists, arrays,
33 vectors, ...) are often suffixed with an `s`.
34 +
35 [source,sml]
36 ----
37 xs
38 employees
39 ----
40
41 * Constructors, structure identifiers, and functor identifiers begin
42 with a capital letter.
43 +
44 [source,sml]
45 ----
46 Queue
47 LinkedList
48 ----
49
50 * Signature identifiers are in all capitals, using `_` to separate
51 words.
52 +
53 [source,sml]
54 ----
55 LIST
56 BINARY_HEAP
57 ----
58
59
60 == Types
61
62 * Alphabetize record labels. In a record type, there are spaces after
63 colons and commas, but not before colons or commas, or at the
64 delimiters `{` and `}`.
65 +
66 [source,sml]
67 ----
68 {bar: int, foo: int}
69 ----
70
71 * Only split a record type across multiple lines if it doesn't fit on
72 one line. If a record type must be split over multiple lines, put one
73 field per line.
74 +
75 [source,sml]
76 ----
77 {bar: int,
78 foo: real * real,
79 zoo: bool}
80 ----
81
82
83 * In a tuple type, there are spaces before and after each `*`.
84 +
85 [source,sml]
86 ----
87 int * bool * real
88 ----
89
90 * Only split a tuple type across multiple lines if it doesn't fit on
91 one line. In a tuple type split over multiple lines, there is one
92 type per line, and the `*`-s go at the beginning of the lines.
93 +
94 [source,sml]
95 ----
96 int
97 * bool
98 * real
99 ----
100 +
101 It may also be useful to parenthesize to make the grouping more
102 apparent.
103 +
104 [source,sml]
105 ----
106 (int
107 * bool
108 * real)
109 ----
110
111 * In an arrow type split over multiple lines, put the arrow at the
112 beginning of its line.
113 +
114 [source,sml]
115 ----
116 int * real
117 -> bool
118 ----
119 +
120 It may also be useful to parenthesize to make the grouping more
121 apparent.
122 +
123 [source,sml]
124 ----
125 (int * real
126 -> bool)
127 ----
128
129 * Avoid redundant parentheses.
130
131 * Arrow types associate to the right, so write
132 +
133 [source,sml]
134 ----
135 a -> b -> c
136 ----
137 +
138 not
139 +
140 [source,sml]
141 ----
142 a -> (b -> c)
143 ----
144
145 * Type constructor application associates to the left, so write
146 +
147 [source,sml]
148 ----
149 int ref list
150 ----
151 +
152 not
153 +
154 [source,sml]
155 ----
156 (int ref) list
157 ----
158
159 * Type constructor application binds more tightly than a tuple type,
160 so write
161 +
162 [source,sml]
163 ----
164 int list * bool list
165 ----
166 +
167 not
168 +
169 [source,sml]
170 ----
171 (int list) * (bool list)
172 ----
173
174 * Tuple types bind more tightly than arrow types, so write
175 +
176 [source,sml]
177 ----
178 int * bool -> real
179 ----
180 +
181 not
182 +
183 [source,sml]
184 ----
185 (int * bool) -> real
186 ----
187
188
189 == Core
190
191 * A core expression or declaration split over multiple lines does not
192 contain any blank lines.
193
194 * A record field selector has no space between the `#` and the record
195 label. So, write
196 +
197 [source,sml]
198 ----
199 #foo
200 ----
201 +
202 not
203 +
204 [source,sml]
205 ----
206 # foo
207 ----
208 +
209
210 * A tuple has a space after each comma, but not before, and not at the
211 delimiters `(` and `)`.
212 +
213 [source,sml]
214 ----
215 (e1, e2, e3)
216 ----
217
218 * A tuple split over multiple lines has one element per line, and the
219 commas go at the end of the lines.
220 +
221 [source,sml]
222 ----
223 (e1,
224 e2,
225 e3)
226 ----
227
228 * A list has a space after each comma, but not before, and not at the
229 delimiters `[` and `]`.
230 +
231 [source,sml]
232 ----
233 [e1, e2, e3]
234 ----
235
236 * A list split over multiple lines has one element per line, and the
237 commas at the end of the lines.
238 +
239 [source,sml]
240 ----
241 [e1,
242 e2,
243 e3]
244 ----
245
246 * A record has spaces before and after `=`, a space after each comma,
247 but not before, and not at the delimiters `{` and `}`. Field names
248 appear in alphabetical order.
249 +
250 [source,sml]
251 ----
252 {bar = 13, foo = true}
253 ----
254
255 * A sequence expression has a space after each semicolon, but not before.
256 +
257 [source,sml]
258 ----
259 (e1; e2; e3)
260 ----
261
262 * A sequence expression split over multiple lines has one expression
263 per line, and the semicolons at the beginning of lines. Lisp and
264 Scheme programmers may find this hard to read at first.
265 +
266 [source,sml]
267 ----
268 (e1
269 ; e2
270 ; e3)
271 ----
272 +
273 _Rationale_: this makes it easy to visually spot the beginning of each
274 expression, which becomes more valuable as the expressions themselves
275 are split across multiple lines.
276
277 * An application expression has a space between the function and the
278 argument. There are no parens unless the argument is a tuple (in
279 which case the parens are really part of the tuple, not the
280 application).
281 +
282 [source,sml]
283 ----
284 f a
285 f (a1, a2, a3)
286 ----
287
288 * Avoid redundant parentheses. Application associates to left, so
289 write
290 +
291 [source,sml]
292 ----
293 f a1 a2 a3
294 ----
295 +
296 not
297 +
298 [source,sml]
299 ----
300 ((f a1) a2) a3
301 ----
302
303 * Infix operators have a space before and after the operator.
304 +
305 [source,sml]
306 ----
307 x + y
308 x * y - z
309 ----
310
311 * Avoid redundant parentheses. Use <<OperatorPrecedence#>>. So, write
312 +
313 [source,sml]
314 ----
315 x + y * z
316 ----
317 +
318 not
319 +
320 [source,sml]
321 ----
322 x + (y * z)
323 ----
324
325 * An `andalso` expression split over multiple lines has the `andalso`
326 at the beginning of subsequent lines.
327 +
328 [source,sml]
329 ----
330 e1
331 andalso e2
332 andalso e3
333 ----
334
335 * A `case` expression is indented as follows
336 +
337 [source,sml]
338 ----
339 case e1 of
340 p1 => e1
341 | p2 => e2
342 | p3 => e3
343 ----
344
345 * A `datatype`&rsquo;s constructors are alphabetized.
346 +
347 [source,sml]
348 ----
349 datatype t = A | B | C
350 ----
351
352 * A `datatype` declaration has a space before and after each `|`.
353 +
354 [source,sml]
355 ----
356 datatype t = A | B of int | C
357 ----
358
359 * A `datatype` split over multiple lines has one constructor per line,
360 with the `|` at the beginning of lines and the constructors beginning
361 3 columns to the right of the `datatype`.
362 +
363 [source,sml]
364 ----
365 datatype t =
366 A
367 | B
368 | C
369 ----
370
371 * A `fun` declaration may start its body on the subsequent line,
372 indented 3 spaces.
373 +
374 [source,sml]
375 ----
376 fun f x y =
377 let
378 val z = x + y + z
379 in
380 z
381 end
382 ----
383
384 * An `if` expression is indented as follows.
385 +
386 [source,sml]
387 ----
388 if e1
389 then e2
390 else e3
391 ----
392
393 * A sequence of `if`-`then`-`else`-s is indented as follows.
394 +
395 [source,sml]
396 ----
397 if e1
398 then e2
399 else if e3
400 then e4
401 else if e5
402 then e6
403 else e7
404 ----
405
406 * A `let` expression has the `let`, `in`, and `end` on their own
407 lines, starting in the same column. Declarations and the body are
408 indented 3 spaces.
409 +
410 [source,sml]
411 ----
412 let
413 val x = 13
414 val y = 14
415 in
416 x + y
417 end
418 ----
419
420 * A `local` declaration has the `local`, `in`, and `end` on their own
421 lines, starting in the same column. Declarations are indented 3
422 spaces.
423 +
424 [source,sml]
425 ----
426 local
427 val x = 13
428 in
429 val y = x
430 end
431 ----
432
433 * An `orelse` expression split over multiple lines has the `orelse` at
434 the beginning of subsequent lines.
435 +
436 [source,sml]
437 ----
438 e1
439 orelse e2
440 orelse e3
441 ----
442
443 * A `val` declaration has a space before and after the `=`.
444 +
445 [source,sml]
446 ----
447 val p = e
448 ----
449
450 * A `val` declaration can start the expression on the subsequent line,
451 indented 3 spaces.
452 +
453 [source,sml]
454 ----
455 val p =
456 if e1 then e2 else e3
457 ----
458
459
460 == Signatures
461
462 * A `signature` declaration is indented as follows.
463 +
464 [source,sml]
465 ----
466 signature FOO =
467 sig
468 val x: int
469 end
470 ----
471 +
472 _Exception_: a signature declaration in a file to itself can omit the
473 indentation to save horizontal space.
474 +
475 [source,sml]
476 ----
477 signature FOO =
478 sig
479
480 val x: int
481
482 end
483 ----
484 +
485 In this case, there should be a blank line after the `sig` and before
486 the `end`.
487
488 * A `val` specification has a space after the colon, but not before.
489 +
490 [source,sml]
491 ----
492 val x: int
493 ----
494 +
495 _Exception_: in the case of operators (like `+`), there is a space
496 before the colon to avoid lexing the colon as part of the operator.
497 +
498 [source,sml]
499 ----
500 val + : t * t -> t
501 ----
502
503 * Alphabetize specifications in signatures.
504 +
505 [source,sml]
506 ----
507 sig
508 val x: int
509 val y: bool
510 end
511 ----
512
513
514 == Structures
515
516 * A `structure` declaration has a space on both sides of the `=`.
517 +
518 [source,sml]
519 ----
520 structure Foo = Bar
521 ----
522
523 * A `structure` declaration split over multiple lines is indented as
524 follows.
525 +
526 [source,sml]
527 ----
528 structure S =
529 struct
530 val x = 13
531 end
532 ----
533 +
534 _Exception_: a structure declaration in a file to itself can omit the
535 indentation to save horizontal space.
536 +
537 [source,sml]
538 ----
539 structure S =
540 struct
541
542 val x = 13
543
544 end
545 ----
546 +
547 In this case, there should be a blank line after the `struct` and
548 before the `end`.
549
550 * Declarations in a `struct` are separated by blank lines.
551 +
552 [source,sml]
553 ----
554 struct
555 val x =
556 let
557 y = 13
558 in
559 y + 1
560 end
561
562 val z = 14
563 end
564 ----
565
566
567 == Functors
568
569 * A `functor` declaration has spaces after each `:` (or `:>`) but not
570 before, and a space before and after the `=`. It is indented as
571 follows.
572 +
573 [source,sml]
574 ----
575 functor Foo (S: FOO_ARG): FOO =
576 struct
577 val x = S.x
578 end
579 ----
580 +
581 _Exception_: a functor declaration in a file to itself can omit the
582 indentation to save horizontal space.
583 +
584 [source,sml]
585 ----
586 functor Foo (S: FOO_ARG): FOO =
587 struct
588
589 val x = S.x
590
591 end
592 ----
593 +
594 In this case, there should be a blank line after the `struct`
595 and before the `end`.