Debian Codegraph @

dev/doc/style.md

raw symbols SCIP
1 # OCaml Style for Rocq
2
3 > Style uniformity is more important than style itself
4 > -- <cite>Kernigan & Pike, The Practice of Programming</cite>
5
6 ## Spacing and indentation
7
8 - indent your code (using tuareg default)
9 - no strong constraints in formatting `let in`; possible styles are:
10 ```ocaml
11 let x = ... in
12 ```
13 ```ocaml
14 let x =
15 ... in
16 ```
17 ```ocaml
18 let x =
19 ...
20 in
21 ```
22 - but: no extra indentation before a `in` coming on next line,
23 otherwise, it first shifts further and further on the right,
24 reducing the amount of space available; second, it is not robust to
25 insertion of a new `let`
26 - it is established usage to have space around `|` as in
27 ```ocaml
28 match c with
29 | [] | [a] -> ...
30 | a::b::l -> ...
31 ```
32 - the tendency is to use the following format which
33 limit excessive indentation while providing an interesting "block" aspect
34 ```ocaml
35 type t =
36 | A
37 | B of machin
38
39 let f expr = match expr with
40 | A -> ...
41 | B x -> ...
42
43 let f expr = function
44 | A -> ...
45 | B x -> ...
46 ```
47 - add spaces around `=` and `==` (make the code "breathe")
48 (note that use of ocaml stdlib `=` is discouraged)
49 - the common usage is to write `let x,y = ... in ...` rather than
50 `let (x,y) = ... in ...`
51 - parenthesizing with either `(` and `)` or with `begin` and `end` is
52 common practice
53 - preferred layout for conditionals:
54 ```ocaml
55 if condition then
56 first-case
57 else
58 second-case
59 ```
60 - in case of effects in branches, use `begin ... end` rather than
61 parentheses
62 ```ocaml
63 if condition then begin
64 instr1;
65 instr2
66 end else begin
67 instr3;
68 instr4
69 end
70 ```
71 - avoid semicolon after single branch `if`, ie instead of
72 ~~~ocaml
73 if foo then bar;
74 baz
75 ~~~
76 do
77 ~~~ocaml
78 let () = if foo then bar in
79 baz
80 ~~~
81 - if the first branch raises an exception, avoid the `else`, i.e.
82 use
83 ```ocaml
84 let () = if condition then error "foo" in
85 bar
86 ```
87 instead of
88 ```ocaml
89 if condition then
90 error "foo"
91 else
92 bar
93 ```
94 - it is the usage not to use `;;` to end OCaml sentences (however,
95 inserting `;;` can be useful for debugging syntax errors crossing
96 the boundary of functions)
97 - relevant options in tuareg:
98 ```
99 (setq tuareg-in-indent 2)
100 (setq tuareg-with-indent 0)
101 (setq tuareg-function-indent 0)
102 (setq tuareg-let-always-indent nil)
103 ```
104 - when a match fails to compile due to unbound constructors (eg
105 `match x with VarRef x -> bla | ConstRef x -> bli | _ -> blo` when
106 `GlobRef` is not open) it can be resolved in several ways:
107 + locally or globally open `GlobRef`
108 + type annotate `x : GlobRef.t` (where it is introduced, or in the `match` expression, whichever is nicer)
109 + annotate the first branch `GlobRef.VarRef x -> bla`
110 this last solution is not robust to branch reordering so should not be prefered
111
112 ## Coding methodology
113
114 - no catchall `try ... with _ -> ...` which catches even `Sys.Break` (Ctrl-C),
115 `Out_of_memory`, `Stack_overflow`, etc.
116 at least, use `try with e when CErrors.noncritical e -> ...`
117 - do not abuse fancy combinators: sometimes what a `let rec` loop
118 does is more readable and simpler to grasp than what a `fold` does
119 - do not break abstractions: if an internal property is hidden
120 behind an interface, do no rely on it in code which uses this
121 interface (e.g. do not use `List.map` thinking it is left-to-right,
122 use `map_left`)
123 - in particular, do not use `=` on abstract types: there is no
124 reason a priori that it is the intended equality on this type; use the
125 `equal` function normally provided with the abstract type
126 - avoid polymorphically typed `=` whose implementation is not
127 optimized in OCaml and which has moreover no reason to be the
128 intended implementation of the equality when it comes to be
129 instantiated on a particular type (e.g. use `List.mem_f`,
130 `List.assoc_f`, rather than `List.mem`, `List.assoc`, etc, unless it is
131 absolutely clear that `=` will implement the intended equality, and
132 with the right complexity)
133 - any new general-purpose enough combinator on list should be put in
134 `cList.ml`, on type option in `option.ml`, etc.
135 - unless for a good reason not to do so, follow the style of the
136 surrounding code in the same file as much as possible,
137 the general guidelines are otherwise "let spacing breathe" (we
138 have large screen nowadays), "make your code easy to read and
139 to understand"
140 - document what is tricky, but do not overdocument, sometimes the
141 choice of names and the structure of the code are better
142 documentation than a long discourse; use of unicode in comments is
143 welcome if it can make comments more readable (then
144 `toggle-enable-multibyte-characters` can help when using the
145 debugger in emacs)
146 - all of initial `open Module`, or of small-scope `let open Module in` or `Module.(...)`, or
147 per-ident `Module.foo` are common practices.
148 `open Module` in the middle of a file should probably be avoided (keep global opens at the top)
149
150 ## Choice of variable names
151
152 - be consistent when naming from one function to another
153 - be consistent with the naming adopted in the functions from the
154 same file, or with the naming used elsewhere by similar functions
155 - use variable names which express meaning
156 - keep `cst` or `con` for constants and avoid it for constructors which is
157 otherwise a source of confusion
158 - for constructors, use `ctor` in type constructor (resp. `ctoru` in
159 constructor puniverse); avoid `constr` for `constructor` which
160 could be think as the name of an arbitrary Constr.t
161 - for inductive types, use `ind` in the type inductive (resp `indu`
162 in inductive puniverse)
163 - for `env`, use `env`
164 - for `evar_map`, use `sigma`, with tolerance for `evm` and `evd`
165 - for `named_context` or `rel_context`, use `ctxt` or `ctx` (or `sign`)
166 - for formal/actual indices of inductive types: `realdecls`, `realargs`
167 - for formal/actual parameters of inductive types: `paramdecls`, `paramargs`
168 - for terms, use e.g. `c`, `b`, `a`, ...
169 - if a term is known to be a function: `f`, ...
170 - if a term is known to be a type: `t`, `u`, `typ`, ...
171 - for a declaration, use `d` or `decl`
172 - for errors, exceptions, use `e`
173
174 ## Common OCaml pitfalls
175
176 - in
177 ```ocaml
178 match ... with Case1 -> try ... with ... -> ... | Case2 -> ...
179 ```
180 or in
181 ```ocaml
182 match ... with Case1 -> match ... with SubCase -> ... | Case2 -> ...
183 ```
184 parentheses (or `begin`/`end` which looks nicer) are needed around the `try` and the inner `match`
185 - in
186 ```ocaml
187 if ... then ... else ... ++ ...
188 ```
189 the default parenthesizing
190 is somehow counter-intuitive; use
191 ```ocaml
192 (if ... then ... else ...) ++ ...
193 ```
194 - in `let myspecialfun = mygenericfun args`, be sure that it does not
195 do side-effect; prefer otherwise
196 ```ocaml
197 let myspecialfun arg = mygenericfun args arg
198 ```
199 to ensure that the function is evaluated at
200 runtime.