| 1 |
Style guide for the standard library |
| 2 |
==================================== |
| 3 |
|
| 4 |
This is very much a work-in-progress and is not exhaustive. Furthermore, many of |
| 5 |
these are aspirations, and may be violated in certain parts of the library. |
| 6 |
It is hoped that at some point a linter will be developed for Agda which will |
| 7 |
automate most of this. |
| 8 |
|
| 9 |
## File structure |
| 10 |
|
| 11 |
* The standard library uses a standard line length of 72 characters. Please |
| 12 |
try to stay within this limit. Having said that this is the most violated |
| 13 |
rule in the style-guide and it is recognised that it is not always possible |
| 14 |
to achieve whilst using meaningful names. |
| 15 |
|
| 16 |
#### Indentation |
| 17 |
|
| 18 |
* The contents of a top-level module should have zero indentation. |
| 19 |
|
| 20 |
* Every subsequent nested scope should then be indented by an additional |
| 21 |
two spaces. |
| 22 |
|
| 23 |
* `where` blocks should be indented by two spaces and their contents |
| 24 |
should be aligned with the `where`. |
| 25 |
|
| 26 |
* If the type of a term does not fit on one line then the subsequent |
| 27 |
lines of the type should all be aligned with the first character |
| 28 |
of the first line of type, e.g. |
| 29 |
```agda |
| 30 |
map-cong₂ : ∀ {a b} {A : Set a} {B : Set b} → |
| 31 |
∀ {f g : A → B} {xs} → |
| 32 |
All (λ x → f x ≡ g x) xs → map f xs ≡ map g xs |
| 33 |
``` |
| 34 |
|
| 35 |
* As can be seen in the example above, function arrows at line breaks |
| 36 |
should always go at the end of the line rather than the beginning of the |
| 37 |
next line. |
| 38 |
|
| 39 |
#### Empty lines |
| 40 |
|
| 41 |
* All module headers and standard term definitions should have a single |
| 42 |
empty line after them. |
| 43 |
|
| 44 |
* There should be _two_ empty lines between adjacent record or module definitions |
| 45 |
in order to better distinguish the end of the record or module, as they will |
| 46 |
already be using single empty lines between internal definitions. |
| 47 |
|
| 48 |
* For example: |
| 49 |
```agda |
| 50 |
module Test1 where |
| 51 |
|
| 52 |
def1 : ... |
| 53 |
def1 = ... |
| 54 |
|
| 55 |
def2 : ... |
| 56 |
def2 = ... |
| 57 |
|
| 58 |
|
| 59 |
module Test2 where |
| 60 |
|
| 61 |
record Record1 : Set where |
| 62 |
field |
| 63 |
field1 : ... |
| 64 |
|
| 65 |
aux1 : ... |
| 66 |
aux1 = ... |
| 67 |
|
| 68 |
aux2 : ... |
| 69 |
aux2 = ... |
| 70 |
|
| 71 |
|
| 72 |
record Record2 : Set where |
| 73 |
field |
| 74 |
field2 : ... |
| 75 |
|
| 76 |
|
| 77 |
record1 : Record1 |
| 78 |
record1 = { field1 = ... } |
| 79 |
|
| 80 |
record2 : Record2 |
| 81 |
record2 = { field2 = ... } |
| 82 |
``` |
| 83 |
|
| 84 |
#### Modules |
| 85 |
|
| 86 |
* As a rule of thumb, there should only be one named module per file. Anonymous |
| 87 |
modules are fine, but named internal modules should either be opened publicly |
| 88 |
immediately or split out into a separate file. |
| 89 |
|
| 90 |
* Module parameters should be put on a single line if they fit. |
| 91 |
|
| 92 |
* Otherwise, they should be spread out over multiple lines, each indented by two |
| 93 |
spaces. If they can be grouped logically by line, then it is fine to do so. |
| 94 |
Otherwise, a line each is probably clearest. The `where` keyword should be placed |
| 95 |
on an additional line of code at the end. For example: |
| 96 |
```agda |
| 97 |
module Relation.Binary.Reasoning.Base.Single |
| 98 |
{a ℓ} {A : Set a} (_∼_ : Rel A ℓ) |
| 99 |
(refl : Reflexive _∼_) (trans : Transitive _∼_) |
| 100 |
where |
| 101 |
``` |
| 102 |
|
| 103 |
* There should always be a single blank line after a module declaration. |
| 104 |
|
| 105 |
#### Imports |
| 106 |
|
| 107 |
* All imports should be placed in a list at the top of the file |
| 108 |
immediately after the module declaration. |
| 109 |
|
| 110 |
* The list of imports should be declared in alphabetical order. |
| 111 |
|
| 112 |
* If the module takes parameters that require imports from other files, |
| 113 |
then those imports only may be placed above the module declaration, e.g. |
| 114 |
```agda |
| 115 |
open import Algebra using (Ring) |
| 116 |
|
| 117 |
module Algebra.Properties.Ring {a l} (ring : Ring a l) where |
| 118 |
|
| 119 |
... other imports |
| 120 |
``` |
| 121 |
|
| 122 |
* If it is important that certain names only come into scope later in |
| 123 |
the file then the module should still be imported at the top of the |
| 124 |
file but it can be imported *qualified*, i.e. given a shorter name |
| 125 |
using the keyword `as` and then opened later on in the file when needed, |
| 126 |
e.g. |
| 127 |
```agda |
| 128 |
import Data.List.Relation.Binary.Equality.Setoid as SetoidEquality |
| 129 |
... |
| 130 |
... |
| 131 |
open SetoidEquality S |
| 132 |
``` |
| 133 |
|
| 134 |
* If importing a parameterised module, qualified or otherwise, with its |
| 135 |
parameters instantiated, then such 'instantiated imports' should be placed |
| 136 |
*after* the main block of `import`s, and *before* any `variable` declarations. |
| 137 |
|
| 138 |
* Naming conventions for qualified `import`s: if importing a module under |
| 139 |
a root of the form `Data.X` (e.g. the `Base` module for basic operations, |
| 140 |
or `Properties` for lemmas about them etc.) then conventionally, the |
| 141 |
qualified name(s) for the import(s) should (all) share as qualified name |
| 142 |
that of the name of the `X` datatype defined: i.e. `Data.Nat.Base` |
| 143 |
should be imported as `ℕ`, `Data.List.Properties` as `List`, etc. |
| 144 |
In this spirit, the convention applies also to (the datatype defined by) |
| 145 |
`Relation.Binary.PropositionalEquality.*` which should be imported qualified |
| 146 |
with the name `≡`. |
| 147 |
Other modules should be given a 'suitable' qualified name based on its 'long' |
| 148 |
path-derived name (such as `SetoidEquality` in the example above); commonly |
| 149 |
occurring examples such as `Algebra.Structures` should be imported qualified |
| 150 |
as `Structures` etc. |
| 151 |
NB. Historical legacy means that these conventions have not always been observed! |
| 152 |
|
| 153 |
* Special case of the above for `*-Reasoning` (sub-)modules: by analogy with |
| 154 |
`Relation.Binary.PropositionalEquality.≡-Reasoning`, when importing qualified |
| 155 |
the `-Reasoning` (sub-)module associated with a given (canonical) choice of |
| 156 |
symbol (eg. `≲` for `Preorder` reasoning), use the qualified name |
| 157 |
`<symbol>-Reasoning`, ie. `≲-Reasoning` for the example given. |
| 158 |
|
| 159 |
* Qualified `open import`s should, in general, avoid `renaming` |
| 160 |
identifiers, in favour of using the long(er) qualified name, |
| 161 |
although similar remarks about legacy failure to observe this |
| 162 |
recommendation apply! |
| 163 |
NB. `renaming` directives are, of course, permitted when a module is |
| 164 |
imported qualified, in order to be *subsequently* `open`ed for |
| 165 |
`public` export (see below). |
| 166 |
|
| 167 |
* When using only a few items (i.e. < 5) from a module, it is a good practice to |
| 168 |
enumerate the items that will be used by declaring the import statement |
| 169 |
with the directive `using`. This makes the dependencies clearer, e.g. |
| 170 |
```agda |
| 171 |
open import Data.Nat.Properties using (+-assoc) |
| 172 |
``` |
| 173 |
|
| 174 |
* Re-exporting terms from a module using the `public` modifier |
| 175 |
should *not* be done in the list of imports as it is very hard to spot. |
| 176 |
Instead, the best approach is often to rename the import and then open it |
| 177 |
publicly later in the file in a more obvious fashion, e.g. |
| 178 |
```agda |
| 179 |
-- Import list |
| 180 |
... |
| 181 |
import Data.Nat.Properties as NatProperties |
| 182 |
... |
| 183 |
|
| 184 |
-- Re-export ring |
| 185 |
open NatProperties public |
| 186 |
using (+-*-ring) |
| 187 |
``` |
| 188 |
|
| 189 |
* If multiple import modifiers are used, then they should occur in the |
| 190 |
following order: `public`, `using` `renaming`, and if `public` is used |
| 191 |
then the `using` and `renaming` modifiers should occur on a separate line. |
| 192 |
For example: |
| 193 |
```agda |
| 194 |
open Monoid monoid public |
| 195 |
using (ε) renaming (_∙_ to _+_) |
| 196 |
``` |
| 197 |
|
| 198 |
#### Layout of data declarations |
| 199 |
|
| 200 |
* The `:` for each constructor should be aligned. |
| 201 |
|
| 202 |
#### Layout of record declarations |
| 203 |
|
| 204 |
* The `:` for each field should be aligned. |
| 205 |
|
| 206 |
* If defining multiple records back-to-back then there should be a double |
| 207 |
empty line between each record. |
| 208 |
|
| 209 |
#### Layout of record instances |
| 210 |
|
| 211 |
* The `record` keyword should go on the same line as the rest of the proof. |
| 212 |
|
| 213 |
* The next line with the first record item should start with a single `{`. |
| 214 |
|
| 215 |
* Every subsequent item of the record should go on its own line starting with |
| 216 |
a `;`. |
| 217 |
|
| 218 |
* The final line should end with `}` on its own. |
| 219 |
|
| 220 |
* The `=` signs for each field should be aligned. |
| 221 |
|
| 222 |
* For example: |
| 223 |
```agda |
| 224 |
≤-isPreorder : IsPreorder _≡_ _≤_ |
| 225 |
≤-isPreorder = record |
| 226 |
{ isEquivalence = isEquivalence |
| 227 |
; reflexive = ≤-reflexive |
| 228 |
; trans = ≤-trans |
| 229 |
} |
| 230 |
``` |
| 231 |
|
| 232 |
#### Layout of initial `private` block |
| 233 |
|
| 234 |
* Since the introduction of generalizable `variable`s (see below), |
| 235 |
this block provides a very useful way to 'fix'/standardise notation |
| 236 |
for the rest of the module, as well as introducing local |
| 237 |
instantiations of parameterised `module` definitions, again for the |
| 238 |
sake of fixing notation via qualified names. |
| 239 |
|
| 240 |
* It should typically follow the `import` and `open` declarations, as |
| 241 |
above, separated by one blankline, and be followed by *two* blank |
| 242 |
lines ahead of the main module body. |
| 243 |
|
| 244 |
* The current preferred layout is to use successive indentation by two spaces, eg. |
| 245 |
```agda |
| 246 |
private |
| 247 |
variable |
| 248 |
a : Level |
| 249 |
A : Set a |
| 250 |
``` |
| 251 |
rather than to use the more permissive 'stacked' style, available |
| 252 |
since [agda/agda#5319](https://github.com/agda/agda/pull/5319). |
| 253 |
|
| 254 |
* A possible exception to the above rule is when a *single* declaration |
| 255 |
is made, such as eg. |
| 256 |
```agda |
| 257 |
private open module M = ... |
| 258 |
``` |
| 259 |
|
| 260 |
#### Layout of `where` blocks |
| 261 |
|
| 262 |
* `where` blocks are preferred rather than the `let` construction. |
| 263 |
|
| 264 |
* The `where` keyword should be placed on the line below the main proof, |
| 265 |
indented by two spaces. |
| 266 |
|
| 267 |
* If the content of the block is non-trivial then types should be |
| 268 |
provided alongside the terms, and all terms should be on lines after |
| 269 |
the `where`, e.g. |
| 270 |
```agda |
| 271 |
statement : Statement |
| 272 |
statement = proof |
| 273 |
where |
| 274 |
proof : Proof |
| 275 |
proof = some-very-long-proof |
| 276 |
``` |
| 277 |
|
| 278 |
* If the content of the block is trivial or is an `open` statement then |
| 279 |
it can be provided on the same line as the `where` and a type can be |
| 280 |
omitted, e.g. |
| 281 |
```agda |
| 282 |
statement : Statement |
| 283 |
statement = proof |
| 284 |
where proof = x |
| 285 |
``` |
| 286 |
|
| 287 |
#### Layout of equational reasoning |
| 288 |
|
| 289 |
* The `begin` clause should go on the same line as the rest of the proof. |
| 290 |
|
| 291 |
* Every subsequent combinator `_≡⟨_⟩_` should be placed on an additional |
| 292 |
line of code, indented by two spaces. |
| 293 |
|
| 294 |
* The relation sign (e.g. `≡`) for each line should be aligned if possible. |
| 295 |
|
| 296 |
* For example: |
| 297 |
```agda |
| 298 |
+-comm : Commutative _+_ |
| 299 |
+-comm zero n = sym (+-identityʳ n) |
| 300 |
+-comm (suc m) n = begin |
| 301 |
suc m + n ≡⟨⟩ |
| 302 |
suc (m + n) ≡⟨ cong suc (+-comm m n) ⟩ |
| 303 |
suc (n + m) ≡⟨ sym (+-suc n m) ⟩ |
| 304 |
n + suc m ∎ |
| 305 |
``` |
| 306 |
|
| 307 |
* When multiple reasoning frameworks need to be used in the same file, the |
| 308 |
`open` statement should always come in a where clause local to the |
| 309 |
definition. This way users can easily see which reasoning toolkit is |
| 310 |
being used. For instance: |
| 311 |
```agda |
| 312 |
foo m n p = begin |
| 313 |
(...) ∎ |
| 314 |
where open ≤-Reasoning |
| 315 |
``` |
| 316 |
|
| 317 |
#### Layout of deprecation blocks |
| 318 |
|
| 319 |
* There is standard boilerplate text which should go at the end of a |
| 320 |
file, separated by two blank lines from the main module body: |
| 321 |
```agda |
| 322 |
------------------------------------------------------------------------ |
| 323 |
-- DEPRECATED NAMES |
| 324 |
------------------------------------------------------------------------ |
| 325 |
-- Please use the new names as continuing support for the old names is |
| 326 |
-- not guaranteed. |
| 327 |
``` |
| 328 |
|
| 329 |
* For each (new) version with deprecations, there should be a single |
| 330 |
line comment, separated by one blank line from the preceding |
| 331 |
deprecation block: |
| 332 |
```agda |
| 333 |
-- Version X.Y |
| 334 |
``` |
| 335 |
|
| 336 |
* There is standard boilerplate text for deprecating `old-name` in |
| 337 |
favour of a definition in terms of `new-name`: the actual definition |
| 338 |
(which need not have a type signature if this is a simple matter of |
| 339 |
aliasing, or simple function composition), followed by: |
| 340 |
```agda |
| 341 |
{-# WARNING_ON_USAGE <old-name> |
| 342 |
"Warning: <old-name> was deprecated in vX.Y. |
| 343 |
Please use <newname> instead." |
| 344 |
#-} |
| 345 |
``` |
| 346 |
where the advice on what to use instead may also contain additional |
| 347 |
information regarding eg. location, or usage patterns. |
| 348 |
|
| 349 |
* Each entry in the block should be correlated with a suitable |
| 350 |
`CHANGELOG` entry for the associated release version. |
| 351 |
|
| 352 |
#### Mutual and private blocks |
| 353 |
|
| 354 |
* Non-trivial proofs in `private` blocks are generally discouraged. If it is |
| 355 |
non-trivial, then chances are that someone will want to reuse it at some |
| 356 |
point! |
| 357 |
|
| 358 |
* Instead, private blocks should only be used to prevent temporary terms and |
| 359 |
records that are defined for convenience from being exported by the module. |
| 360 |
|
| 361 |
* The mutual block is considered obsolete. Please use the standard approach |
| 362 |
of placing the type signatures of the mutually recursive functions before |
| 363 |
their definitions. |
| 364 |
|
| 365 |
#### Function arguments |
| 366 |
|
| 367 |
* Function arguments should be aligned between cases where possible, e.g. |
| 368 |
```agda |
| 369 |
+-comm : Commutative _+_ |
| 370 |
+-comm zero n = ... |
| 371 |
+-comm (suc m) n = ... |
| 372 |
``` |
| 373 |
|
| 374 |
* If an argument is unused in a case, it may at the author's |
| 375 |
discretion be replaced by an underscore, e.g. |
| 376 |
```agda |
| 377 |
+-assoc : Associative _+_ |
| 378 |
+-assoc zero _ _ = refl |
| 379 |
+-assoc (suc m) n o = cong suc (+-assoc m n o) |
| 380 |
``` |
| 381 |
|
| 382 |
* If it is necessary to refer to an implicit argument in one case then |
| 383 |
the implicit argument brackets must be included in every other case as |
| 384 |
well, e.g. |
| 385 |
```agda |
| 386 |
m≤n⇒m∸n≡0 : ∀ {m n} → m ≤ n → m ∸ n ≡ 0 |
| 387 |
m≤n⇒m∸n≡0 {n = n} z≤n = 0∸n≡0 n |
| 388 |
m≤n⇒m∸n≡0 {n = _} (s≤s m≤n) = m≤n⇒m∸n≡0 m≤n |
| 389 |
``` |
| 390 |
|
| 391 |
* As of Agda 2.6.0 dot patterns are no longer necessary when unifying |
| 392 |
function arguments and therefore should not be prepended to function |
| 393 |
arguments. |
| 394 |
|
| 395 |
#### Comments |
| 396 |
|
| 397 |
* Comments should be placed above a term rather than on the same line, e.g. |
| 398 |
```agda |
| 399 |
-- Multiplication of two elements |
| 400 |
_*_ : A → A → A |
| 401 |
_*_ = ... |
| 402 |
``` |
| 403 |
rather than: |
| 404 |
```agda |
| 405 |
_*_ : A → A → A -- Multiplication of two elements |
| 406 |
_*_ = ... |
| 407 |
``` |
| 408 |
|
| 409 |
* Files can be separated into different logical parts using comments of |
| 410 |
the following style, where the header is 72 characters wide: |
| 411 |
```agda |
| 412 |
------------------------------------------------------------------------ |
| 413 |
-- <Title> |
| 414 |
``` |
| 415 |
Use sentence case in the title: `Rounding functions`, not `Rounding Functions` or `ROUNDING FUNCTIONS`. |
| 416 |
|
| 417 |
#### Other |
| 418 |
|
| 419 |
* The `with` syntax is preferred over the use of `case` from the `Function` |
| 420 |
module. The `|` should not be aligned with the `with` statement, i.e. |
| 421 |
```agda |
| 422 |
filter p (x ∷ xs) with p x |
| 423 |
... | true = x ∷ filter p xs |
| 424 |
... | false = filter p xs |
| 425 |
``` |
| 426 |
instead of |
| 427 |
```agda |
| 428 |
filter p (x ∷ xs) with p x |
| 429 |
... | true = x ∷ filter p xs |
| 430 |
... | false = filter p xs |
| 431 |
``` |
| 432 |
|
| 433 |
* Instance arguments, and their types, should use the vanilla ASCII/UTF-8 `{{_}}` |
| 434 |
syntax in preference to the Unicode `⦃_⦄` syntax (written using `\{{`/`\}}`), |
| 435 |
which moreover requires additional whitespace to parse correctly. |
| 436 |
NB. Even for irrelevant instances, such as typically for `NonZero` arguments, |
| 437 |
nevertheless it is necessary to supply an underscore binding `{{_ : NonZero n}}` |
| 438 |
if subsequent terms occurring in the type rely on that argument to be well-formed: |
| 439 |
eg in `Data.Nat.DivMod`, in the use of `_/ n` and `_% n` |
| 440 |
```agda |
| 441 |
m≡m%n+[m/n]*n : ∀ m n .{{_ : NonZero n}} → m ≡ m % n + (m / n) * n |
| 442 |
``` |
| 443 |
|
| 444 |
## Types |
| 445 |
|
| 446 |
#### Implicit and explicit arguments |
| 447 |
|
| 448 |
* Function arguments should be implicit if they can "almost always" |
| 449 |
be inferred. If there are common cases where they cannot be inferred |
| 450 |
then they should be left explicit. |
| 451 |
|
| 452 |
* If there are lots of implicit arguments that are common to a collection |
| 453 |
of proofs they should be extracted by using an anonymous module. |
| 454 |
|
| 455 |
#### Variables |
| 456 |
|
| 457 |
* `Level` and `Set`s can always be generalised using the keyword `variable`. |
| 458 |
|
| 459 |
* A file may only declare variables of other types if those types are used |
| 460 |
in the definition of the main type that the file concerns itself with. |
| 461 |
At the moment the policy is *not* to generalise over any other types to |
| 462 |
minimise the amount of information that users have to keep in their head |
| 463 |
concurrently. |
| 464 |
|
| 465 |
* Example 1: the main type in `Data.List.Properties` is `List A` where `A : Set a`. |
| 466 |
Therefore it may declare variables over `Level`, `Set a`, `A`, `List A`. It may |
| 467 |
not declare variables, for example, over predicates (e.g. `P : Pred A p`) as |
| 468 |
predicates are not used in the definition of `List`, even though they are used |
| 469 |
in many list functions such as `filter`. |
| 470 |
|
| 471 |
* Example 2: the main type in `Data.List.Relation.Unary.All` is `All P xs` where |
| 472 |
`A : Set a`, `P : Pred A p`, `xs : List A`. It therefore may declare variables |
| 473 |
over `Level`, `Set a`, `A`, `List A`, `Pred A p`. It may not declare, for example, |
| 474 |
variables of type `Rel` or `Vec`. |
| 475 |
|
| 476 |
## Naming conventions |
| 477 |
|
| 478 |
* Names should be descriptive - i.e. given the name of a proof and the |
| 479 |
module it lives in, then users should be able to make a reasonable |
| 480 |
guess at its meaning. |
| 481 |
|
| 482 |
* Terms from other modules should only be renamed to avoid name clashes, |
| 483 |
otherwise, all names should be used as defined. |
| 484 |
|
| 485 |
* Datatype names should be capitalized, being its first letter in uppercase |
| 486 |
and the remaining letters in lowercase. |
| 487 |
|
| 488 |
* Function names should follow the camelCase naming convention, in which each |
| 489 |
word within a compound word is capitalized except for the first word. |
| 490 |
|
| 491 |
#### Variables |
| 492 |
|
| 493 |
* Sets are named `A`, `B`, `C` etc. |
| 494 |
|
| 495 |
* Predicates are named `P`, `Q`, `R` etc. |
| 496 |
|
| 497 |
* Relations are named either `R`, `S`, `T` in the general case |
| 498 |
or `_≈_`/`_∼_`/`_≤_`/`_<_` if they are known to be an |
| 499 |
equivalence/preorder/partial order/strict partial order. |
| 500 |
|
| 501 |
* Level variables are typically chosen to match the name of the |
| 502 |
relation, e.g. `a` for the level of a set `A`, `p` for a predicate |
| 503 |
`P`. By convention the name `0ℓ` is preferred over `zero` for the |
| 504 |
zeroth level. |
| 505 |
|
| 506 |
* Natural variables are named `m`, `n`, `o`, ... (default `n`) |
| 507 |
|
| 508 |
* Integer variables are named `i`, `j`, `k`, ... (default `i`) |
| 509 |
|
| 510 |
* Rational variables are named `p`, `q`, `r`, ... (default `p`) |
| 511 |
|
| 512 |
* All other variables should be named `x`, `y`, `z`. |
| 513 |
|
| 514 |
* Collections of elements are usually indicated by appending an `s` |
| 515 |
(e.g. if you are naming your variables `x` and `y` then lists |
| 516 |
should be named `xs` and `ys`). |
| 517 |
|
| 518 |
#### Preconditions and postconditions |
| 519 |
|
| 520 |
* Preconditions should only be included in names of results if |
| 521 |
"important" (mostly a judgment call). |
| 522 |
|
| 523 |
* Preconditions of results should be prepended to a description |
| 524 |
of the result by using the symbol `⇒` in names (e.g. `asym⇒antisym`) |
| 525 |
|
| 526 |
* Preconditions and postconditions should be combined using the symbols |
| 527 |
`∨` and `∧` (e.g. `m*n≡0⇒m≡0∨n≡0`) |
| 528 |
|
| 529 |
* Try to avoid the need for bracketing, but if necessary, use square |
| 530 |
brackets (e.g. `[m∸n]⊓[n∸m]≡0`) |
| 531 |
|
| 532 |
* When naming proofs, the variables should occur in alphabetical order, |
| 533 |
e.g. `m≤n+m` rather than `n≤m+n`. |
| 534 |
|
| 535 |
#### Operators and relations |
| 536 |
|
| 537 |
* Concrete operators and relations should be defined using |
| 538 |
[mixfix](https://agda.readthedocs.io/en/latest/language/mixfix-operators.html) |
| 539 |
notation where applicable (e.g. `_+_`, `_<_`) |
| 540 |
|
| 541 |
* Common properties such as those in rings/orders/equivalences etc. |
| 542 |
have defined abbreviations (e.g. commutativity is shortened to `comm`). |
| 543 |
`Data.Nat.Properties` is a good place to look for examples. |
| 544 |
|
| 545 |
* Properties should be prefixed by the relevant operator/relation and |
| 546 |
separated from its name by a hyphen `-` (e.g. commutativity of sum |
| 547 |
results in a compositional name `+-comm` where `-` acts as a separator). |
| 548 |
|
| 549 |
* If the relevant Unicode characters are available, negated forms of |
| 550 |
relations should be used over the `¬` symbol (e.g. `m+n≮n` should be |
| 551 |
used instead of `¬m+n<n`). |
| 552 |
|
| 553 |
#### Symbols for operators and relations |
| 554 |
|
| 555 |
* The stdlib aims to use a consistent set of notations, governed by a |
| 556 |
consistent set of conventions, but sometimes, different |
| 557 |
Unicode/emacs-input-method symbols nevertheless can be rendered by |
| 558 |
identical-*seeming* symbols, so this is an attempt to document these. |
| 559 |
|
| 560 |
* The typical binary operator in the `Algebra` hierarchy, inheriting |
| 561 |
from the root `Structure`/`Bundle` `isMagma`/`Magma`, is written as |
| 562 |
infix `∙`, obtained as `\.`, NOT as `\bu2`. Nevertheless, there is |
| 563 |
also a 'generic' operator, written as infix `·`, obtained as |
| 564 |
`\cdot`. Do NOT attempt to use related, but typographically |
| 565 |
indistinguishable, symbols. |
| 566 |
|
| 567 |
* Similarly, 'primed' names and symbols, used to standardise names |
| 568 |
apart, or to provide (more) simply-typed versions of |
| 569 |
dependently-typed operations, should be written using `\'`, NOT the |
| 570 |
unmarked `'` character. |
| 571 |
|
| 572 |
* Likewise, standard infix symbols for eg, divisibility on numeric |
| 573 |
datatypes/algebraic structure, should be written `\|`, NOT the |
| 574 |
unmarked `|` character. Ditto. mutual divisibility `\||` etc. An |
| 575 |
exception to this is the *strict* ordering relation, written using |
| 576 |
`<`, NOT `\<` as might be expected. |
| 577 |
|
| 578 |
* Since v2.0, the `Algebra` hierarchy systematically introduces |
| 579 |
consistent symbolic notation for the negated versions of the usual |
| 580 |
binary predicates for equality, ordering etc. These are obtained |
| 581 |
from the corresponding input sequence by adding `n` to the symbol |
| 582 |
name, so that `≤`, obtained as `\le`, becomes `≰` obtained as |
| 583 |
`\len`, etc. |
| 584 |
|
| 585 |
* Correspondingly, the flipped symbols (and their negations) for the |
| 586 |
converse relations are systematically introduced, eg `≥` as `\ge` |
| 587 |
and `≱` as `\gen`. |
| 588 |
|
| 589 |
* Any exceptions to these conventions should be flagged on the GitHub |
| 590 |
`agda-stdlib` issue tracker in the usual way. |
| 591 |
|
| 592 |
#### Fixity |
| 593 |
|
| 594 |
All functions and operators that are not purely prefix (typically |
| 595 |
anything that has a `_` in its name) should have an explicit fixity |
| 596 |
declared for it. The guidelines for these are as follows: |
| 597 |
|
| 598 |
General operations and relations: |
| 599 |
|
| 600 |
* binary relations of all kinds are `infix 4` |
| 601 |
|
| 602 |
* unary prefix relations `infix 4 ε∣_` |
| 603 |
|
| 604 |
* unary postfix relations `infixr 8 _∣0` |
| 605 |
|
| 606 |
* multiplication-like: `infixl 7 _*_` |
| 607 |
|
| 608 |
* addition-like `infixl 6 _+_` |
| 609 |
|
| 610 |
* arithmetic prefix minus-like `infix 8 -_` |
| 611 |
|
| 612 |
* arithmetic infix binary minus-like `infixl 6 _-_` |
| 613 |
|
| 614 |
* and-like `infixr 7 _∧_` |
| 615 |
|
| 616 |
* or-like `infixr 6 _∨_` |
| 617 |
|
| 618 |
* negation-like `infix 3 ¬_` |
| 619 |
|
| 620 |
* post-fix inverse `infix 8 _⁻¹` |
| 621 |
|
| 622 |
* bind `infixl 1 _>>=_` |
| 623 |
|
| 624 |
* list concat-like `infixr 5 _∷_` |
| 625 |
|
| 626 |
* ternary reasoning `infix 1 _⊢_≈_` |
| 627 |
|
| 628 |
* composition `infixr 9 _∘_` |
| 629 |
|
| 630 |
* application `infixr -1 _$_ _$!_` |
| 631 |
|
| 632 |
* combinatorics `infixl 6.5 _P_ _P′_ _C_ _C′_` |
| 633 |
|
| 634 |
* pair `infixr 4 _,_` |
| 635 |
|
| 636 |
Reasoning: |
| 637 |
|
| 638 |
* QED `infix 3 _∎` |
| 639 |
|
| 640 |
* stepping `infixr 2 _≡⟨⟩_ step-≡ step-≡˘` |
| 641 |
|
| 642 |
* begin `infix 1 begin_` |
| 643 |
|
| 644 |
Type formers: |
| 645 |
|
| 646 |
* product-like `infixr 2 _×_ _-×-_ _-,-_` |
| 647 |
|
| 648 |
* sum-like `infixr 1 _⊎_` |
| 649 |
|
| 650 |
* binary properties `infix 4 _Absorbs_` |
| 651 |
|
| 652 |
#### Functions and relations over specific datatypes |
| 653 |
|
| 654 |
* When defining a new relation `P` over a datatype `X` in a `Data.X.Relation` module, |
| 655 |
it is often common to define how to introduce and eliminate that relation |
| 656 |
with respect to various functions. Suppose you have a function `f`, then |
| 657 |
- `f⁺` is a lemma of the form `Precondition -> P(f)` |
| 658 |
- `f⁻` is a lemma of the form `P(f) -> Postcondition` |
| 659 |
The logic behind the name is that `⁺` makes f appear in the conclusion while |
| 660 |
`⁻` makes it disappear from the hypothesis. |
| 661 |
|
| 662 |
For example, in `Data.List.Relation.Binary.Pointwise` we have `map⁺` to show |
| 663 |
how the `map` function may be introduced and `map⁻` to show how it may be |
| 664 |
eliminated: |
| 665 |
```agda |
| 666 |
map⁺ : Pointwise (λ a b → R (f a) (g b)) as bs → Pointwise R (map f as) (map g bs) |
| 667 |
map⁻ : Pointwise R (map f as) (map g bs) → Pointwise (λ a b → R (f a) (g b)) as bs |
| 668 |
``` |
| 669 |
|
| 670 |
* When specifying a property over a container, there are usually two choices. Either |
| 671 |
assume the property holds for generally (e.g. `map id xs ≡ xs`) or a assume that |
| 672 |
it only holds for the elements within the container (e.g. `All (λ x → f x ≡ x) xs → map f xs ≡ xs`). |
| 673 |
The naming convention is to add a `-local` suffix on to the name of the latter variety. |
| 674 |
e.g. |
| 675 |
```agda |
| 676 |
map-id : map id xs ≡ xs |
| 677 |
map-id-local : All (λ x → f x ≡ x) xs → map f xs ≡ xs |
| 678 |
``` |
| 679 |
|
| 680 |
#### Keywords |
| 681 |
|
| 682 |
* If the name of something clashes with a keyword in Agda, then convention |
| 683 |
is to place angular brackets around the name, e.g. `⟨set⟩` and `⟨module⟩`. |
| 684 |
|
| 685 |
#### Reflected syntax |
| 686 |
|
| 687 |
* When using reflection, the name of anything of type `Term` should be preceded |
| 688 |
by a backtick. For example ```List : Term → Term`` would be the function |
| 689 |
constructing the reflection of the `List` type. |
| 690 |
|
| 691 |
* The names of patterns for reflected syntax are also *appended* with an |
| 692 |
additional backtick. |
| 693 |
|
| 694 |
#### Specific pragmatics/idiomatic patterns |
| 695 |
|
| 696 |
## Use of `pattern` synonyms |
| 697 |
|
| 698 |
In general, these are intended to be used to provide specialised |
| 699 |
constructors for `Data` types (and sometimes, inductive |
| 700 |
families/binary relations such as `Data.Nat.Divisibility._∣_`), and as |
| 701 |
such, their use should be restricted to `Base` or `Core` modules, and |
| 702 |
not pollute the namespaces of `Properties` or other modules. |
| 703 |
|
| 704 |
## Use of `with` notation |
| 705 |
|
| 706 |
Thinking on this has changed since the early days of the library, with |
| 707 |
a desire to avoid 'unnecessary' uses of `with`: see Issues |
| 708 |
[#1937](https://github.com/agda/agda-stdlib/issues/1937) and |
| 709 |
[#2123](https://github.com/agda/agda-stdlib/issues/2123). |
| 710 |
|
| 711 |
## Proving instances of `Decidable` for sets, predicates, relations, ... |
| 712 |
|
| 713 |
Issue [#803](https://github.com/agda/agda-stdlib/issues/803) |
| 714 |
articulates a programming pattern for writing proofs of decidability, |
| 715 |
used successfully in PR |
| 716 |
[#799](https://github.com/agda/agda-stdlib/pull/799) and made |
| 717 |
systematic for `Nary` relations in PR |
| 718 |
[#811](https://github.com/agda/agda-stdlib/pull/811) |
| 719 |
|