1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
=============================
Documenting Coq with Sphinx
=============================
..
README.rst is auto-generated from README.template.rst and the coqrst/*.py files
(in particular coqdomain.py). Use ``doc/tools/coqrst/regen_readme.py`` to rebuild it.
Coq's reference manual is written in `reStructuredText <http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html>`_ (“reST”), and compiled with `Sphinx <http://www.sphinx-doc.org/en/master/>`_.
In addition to standard reST directives (a directive is similar to a LaTeX environment) and roles (a role is similar to a LaTeX command), the ``coqrst`` plugin loaded by the documentation uses a custom *Coq domain* — a set of Coq-specific directives that define *objects* like tactics, commands (vernacs), warnings, etc. —, some custom *directives*, and a few custom *roles*. Finally, this manual uses a small DSL to describe tactic invocations and commands.
Coq objects
===========
Our Coq domain define multiple `objects`_. Each object has a *signature* (think *type signature*), followed by an optional body (a description of that object). The following example defines two objects: a variant of the ``simpl`` tactic, and an error that it may raise::
.. tacv:: simpl @pattern at {+ @natural}
:name: simpl_at
This applies ``simpl`` only to the :n:`{+ @natural}` occurrences of the subterms
matching :n:`@pattern` in the current goal.
.. exn:: Too few occurrences
:undocumented:
Objects are automatically collected into indices, and can be linked to using the role version of the object's directive. For example, you could link to the tactic variant above using ``:tacv:`simpl_at```, and to its exception using ``:exn:`Too few occurrences```.
Names (link targets) are auto-generated for most simple objects, though they can always be overwritten using a ``:name:`` option, as shown above.
- Options, errors, warnings have their name set to their signature, with ``...`` replacing all notation bits. For example, the auto-generated name of ``.. exn:: @qualid is not a module`` is ``... is not a module``, and a link to it would take the form ``:exn:`... is not a module```.
- Vernacs (commands) have their name set to the first word of their signature. For example, the auto-generated name of ``Axiom @ident : @term`` is ``Axiom``, and a link to it would take the form ``:cmd:`Axiom```.
- Vernac variants, tactic notations, and tactic variants do not have a default name.
Most objects should have a body (i.e. a block of indented text following the signature, called “contents” in Sphinx terms). Undocumented objects should have the ``:undocumented:`` flag instead, as shown above. When multiple objects have a single description, they can be grouped into a single object, like this (semicolons can be used to separate the names of the objects; names starting with ``_`` will be omitted from the indexes)::
.. cmdv:: Lemma @ident {* @binder } : @type
Remark @ident {* @binder } : @type
Fact @ident {* @binder } : @type
Corollary @ident {* @binder } : @type
Proposition @ident {* @binder } : @type
:name: Lemma; Remark; Fact; Corollary; Proposition
These commands are all synonyms of :n:`Theorem @ident {* @binder } : type`.
Notations
---------
The signatures of most objects can be written using a succinct DSL for Coq notations (think regular expressions written with a Lispy syntax). A typical signature might look like ``Hint Extern @natural {? @pattern} => @tactic``, which means that the ``Hint Extern`` command takes a number (``natural``), followed by an optional pattern, and a mandatory tactic. The language has the following constructs (the full grammar is in `TacticNotations.g </doc/tools/coqrst/notations/TacticNotations.g>`_):
``@…``
A placeholder (``@ident``, ``@natural``, ``@tactic``\ …)
``{? …}``
an optional block
``{* …}``, ``{+ …}``
an optional (``*``) or mandatory (``+``) block that can be repeated, with repetitions separated by spaces
``{*, …}``, ``{+, …}``
an optional or mandatory repeatable block, with repetitions separated by commas
``{| … | … | … }``
an alternative, indicating than one of multiple constructs can be used
``%{``, ``%}``, ``%|``
an escaped character (rendered without the leading ``%``). In most cases,
escaping is not necessary. In particular, the following expressions are
all parsed as plain text, and do not need escaping: ``{ xyz }``, ``x |- y``.
But the following escapes *are* needed: ``{| a b %| c | d }``, ``all: %{``.
(We use ``%`` instead of the usual ``\`` because you'd have to type ``\``
twice in your reStructuredText file.)
For more details and corner cases, see `Advanced uses of notations`_ below.
..
FIXME document the new subscript support
As an exercise, what do the following patterns mean?
.. code::
pattern {+, @term {? at {+ @natural}}}
generalize {+, @term at {+ @natural} as @ident}
fix @ident @natural with {+ (@ident {+ @binder} {? {struct @ident'}} : @type)}
Objects
-------
Here is the list of all objects of the Coq domain (The symbol :black_nib: indicates an object whose signature can be written using the notations DSL):
``.. attr::`` :black_nib: An attribute.
Example::
.. attr:: local
``.. cmd::`` :black_nib: A Coq command.
Example::
.. cmd:: Infix @string := @one_term {? ( {+, @syntax_modifier } ) } {? : @ident }
This command is equivalent to :n:`…`.
``.. cmdv::`` :black_nib: A variant of a Coq command.
Example::
.. cmd:: Axiom @ident : @term.
This command links :token:`term` to the name :token:`term` as its specification in
the global environment. The fact asserted by :token:`term` is thus assumed as a
postulate.
.. cmdv:: Parameter @ident : @term.
This is equivalent to :n:`Axiom @ident : @term`.
``.. exn::`` :black_nib: An error raised by a Coq command or tactic.
This commonly appears nested in the ``.. tacn::`` that raises the
exception.
Example::
.. tacv:: assert @form by @tactic
This tactic applies :n:`@tactic` to solve the subgoals generated by
``assert``.
.. exn:: Proof is not complete
Raised if :n:`@tactic` does not fully solve the goal.
``.. flag::`` :black_nib: A Coq flag (i.e. a boolean setting).
Example::
.. flag:: Nonrecursive Elimination Schemes
Controls whether types declared with the keywords
:cmd:`Variant` and :cmd:`Record` get an automatic declaration of
induction principles.
``.. opt::`` :black_nib: A Coq option (a setting with non-boolean value, e.g. a string or numeric value).
Example::
.. opt:: Hyps Limit @natural
:name Hyps Limit
Controls the maximum number of hypotheses displayed in goals after
application of a tactic.
``.. prodn::`` A grammar production.
Use ``.. prodn`` to document grammar productions instead of Sphinx
`production lists
<http://www.sphinx-doc.org/en/stable/markup/para.html#directive-productionlist>`_.
prodn displays multiple productions together with alignment similar to ``.. productionlist``,
however unlike ``.. productionlist``\ s, this directive accepts notation syntax.
Example::
.. prodn:: occ_switch ::= { {? {| + | - } } {* @natural } }
term += let: @pattern := @term in @term
| second_production
The first line defines "occ_switch", which must be unique in the document. The second
references and expands the definition of "term", whose main definition is elsewhere
in the document. The third form is for continuing the
definition of a nonterminal when it has multiple productions. It leaves the first
column in the output blank.
``.. table::`` :black_nib: A Coq table, i.e. a setting that is a set of values.
Example::
.. table:: Search Blacklist @string
:name: Search Blacklist
Controls ...
``.. tacn::`` :black_nib: A tactic, or a tactic notation.
Example::
.. tacn:: do @natural @expr
:token:`expr` is evaluated to ``v`` which must be a tactic value. …
``.. tacv::`` :black_nib: A variant of a tactic.
Example::
.. tacn:: fail
This is the always-failing tactic: it does not solve any goal. It is
useful for defining other tacticals since it can be caught by
:tacn:`try`, :tacn:`repeat`, :tacn:`match goal`, or the branching
tacticals. …
.. tacv:: fail @natural
The number is the failure level. If no level is specified, it
defaults to 0. …
``.. thm::`` A theorem.
Example::
.. thm:: Bound on the ceiling function
Let :math:`p` be an integer and :math:`c` a rational constant. Then
:math:`p \ge c \rightarrow p \ge \lceil{c}\rceil`.
``.. warn::`` :black_nib: An warning raised by a Coq command or tactic..
Do not mistake this for ``.. warning::``; this directive is for warning
messages produced by Coq.
Example::
.. warn:: Ambiguous path
When the coercion :token:`qualid` is added to the inheritance graph, non
valid coercion paths are ignored.
Coq directives
==============
In addition to the objects above, the ``coqrst`` Sphinx plugin defines the following directives:
``.. coqtop::`` A reST directive to describe interactions with Coqtop.
Usage::
.. coqtop:: options…
Coq code to send to coqtop
Example::
.. coqtop:: in reset
Print nat.
Definition a := 1.
The blank line after the directive is required. If you begin a proof,
use the ``abort`` option to reset coqtop for the next example.
Here is a list of permissible options:
- Display options (choose exactly one)
- ``all``: Display input and output
- ``in``: Display only input
- ``out``: Display only output
- ``none``: Display neither (useful for setup commands)
- Behavior options
- ``reset``: Send a ``Reset Initial`` command before running this block
- ``fail``: Don't die if a command fails, implies ``warn`` (so no need to put both)
- ``warn``: Don't die if a command emits a warning
- ``restart``: Send a ``Restart`` command before running this block (only works in proof mode)
- ``abort``: Send an ``Abort All`` command after running this block (leaves all pending proofs if any)
``coqtop``\ 's state is preserved across consecutive ``.. coqtop::`` blocks
of the same document (``coqrst`` creates a single ``coqtop`` process per
reST source file). Use the ``reset`` option to reset Coq's state.
``.. coqdoc::`` A reST directive to display Coqtop-formatted source code.
Usage::
.. coqdoc::
Coq code to highlight
Example::
.. coqdoc::
Definition test := 1.
``.. example::`` A reST directive for examples.
This behaves like a generic admonition; see
http://docutils.sourceforge.net/docs/ref/rst/directives.html#generic-admonition
for more details.
Optionally, any text immediately following the ``.. example::`` header is
used as the example's title.
Example::
.. example:: Adding a hint to a database
The following adds ``plus_comm`` to the ``plu`` database:
.. coqdoc::
Hint Resolve plus_comm : plu.
``.. inference::`` A reST directive to format inference rules.
This also serves as a small illustration of the way to create new Sphinx
directives.
Usage::
.. inference:: name
newline-separated premises
--------------------------
conclusion
Example::
.. inference:: Prod-Pro
\WTEG{T}{s}
s \in \Sort
\WTE{\Gamma::(x:T)}{U}{\Prop}
-----------------------------
\WTEG{\forall~x:T,U}{\Prop}
``.. preamble::`` A reST directive to include a TeX file.
Mostly useful to let MathJax know about `\def`\s and `\newcommand`\s. The
contents of the TeX file are wrapped in a math environment, as MathJax
doesn't process LaTeX definitions otherwise.
Usage::
.. preamble:: preamble.tex
Coq roles
=========
In addition to the objects and directives above, the ``coqrst`` Sphinx plugin defines the following roles:
``:g:`` Coq code.
Use this for Gallina and Ltac snippets::
:g:`apply plus_comm; reflexivity`
:g:`Set Printing All.`
:g:`forall (x: t), P(x)`
``:n:`` Any text using the notation syntax (``@id``, ``{+, …}``, etc.).
Use this to explain tactic equivalences. For example, you might write
this::
:n:`generalize @term as @ident` is just like :n:`generalize @term`, but
it names the introduced hypothesis :token:`ident`.
Note that this example also uses ``:token:``. That's because ``ident`` is
defined in the Coq manual as a grammar production, and ``:token:``
creates a link to that. When referring to a placeholder that happens to be
a grammar production, ``:token:`…``` is typically preferable to ``:n:`@…```.
``:production:`` A grammar production not included in a ``prodn`` directive.
Useful to informally introduce a production, as part of running text.
Example::
:production:`string` indicates a quoted string.
You're not likely to use this role very commonly; instead, use a ``prodn``
directive and reference its tokens using ``:token:`…```.
``:gdef:`` Marks the definition of a glossary term inline in the text. Matching :term:`XXX`
constructs will link to it. Use the form :gdef:`text <term>` to display "text"
for the definition of "term", such as when "term" must be capitalized or plural
for grammatical reasons. The term will also appear in the Glossary Index.
Examples::
A :gdef:`prime` number is divisible only by itself and 1.
:gdef:`Composite <composite>` numbers are the non-prime numbers.
Common mistakes
===============
Improper nesting
----------------
DO
.. code::
.. cmd:: Foo @bar
Foo the first instance of :token:`bar`\ s.
.. cmdv:: Foo All
Foo all the :token:`bar`\ s in
the current context
DON'T
.. code::
.. cmd:: Foo @bar
Foo the first instance of :token:`bar`\ s.
.. cmdv:: Foo All
Foo all the :token:`bar`\ s in
the current context
You can set the ``report_undocumented_coq_objects`` setting in ``conf.py`` to ``"info"`` or ``"warning"`` to get a list of all Coq objects without a description.
Overusing ``:token:``
---------------------
DO
.. code::
This is equivalent to :n:`Axiom @ident : @term`.
DON'T
.. code::
This is equivalent to ``Axiom`` :token:`ident` : :token:`term`.
..
DO
.. code::
:n:`power_tac @term [@ltac]`
allows :tacn:`ring` and :tacn:`ring_simplify` to recognize …
DON'T
.. code::
power_tac :n:`@term` [:n:`@ltac`]
allows :tacn:`ring` and :tacn:`ring_simplify` to recognize …
..
DO
.. code::
:n:`name={*; attr}`
DON'T
.. code::
``name=``:n:`{*; attr}`
Omitting annotations
--------------------
DO
.. code::
.. tacv:: assert @form as @simple_intropattern
DON'T
.. code::
.. tacv:: assert form as simple_intropattern
Using the ``.. coqtop::`` directive for syntax highlighting
-----------------------------------------------------------
DO
.. code::
A tactic of the form:
.. coqdoc::
do [ t1 | … | tn ].
is equivalent to the standard Ltac expression:
.. coqdoc::
first [ t1 | … | tn ].
DON'T
.. code::
A tactic of the form:
.. coqtop:: in
do [ t1 | … | tn ].
is equivalent to the standard Ltac expression:
.. coqtop:: in
first [ t1 | … | tn ].
Overusing plain quotes
----------------------
DO
.. code::
The :tacn:`refine` tactic can raise the :exn:`Invalid argument` exception.
The term :g:`let a = 1 in a a` is ill-typed.
DON'T
.. code::
The ``refine`` tactic can raise the ``Invalid argument`` exception.
The term ``let a = 1 in a a`` is ill-typed.
Plain quotes produce plain text, without highlighting or cross-references.
Overusing the ``example`` directive
-----------------------------------
DO
.. code::
Here is a useful axiom:
.. coqdoc::
Axiom proof_irrelevance : forall (P : Prop) (x y : P), x=y.
DO
.. code::
.. example:: Using proof-irrelevance
If you assume the axiom above, …
DON'T
.. code::
Here is a useful axiom:
.. example::
.. coqdoc::
Axiom proof_irrelevance : forall (P : Prop) (x y : P), x=y.
Tips and tricks
===============
Nested lemmas
-------------
The ``.. coqtop::`` directive does *not* reset Coq after running its contents. That is, the following will create two nested lemmas (which by default results in a failure)::
.. coqtop:: all
Lemma l1: 1 + 1 = 2.
.. coqtop:: all
Lemma l2: 2 + 2 <> 1.
Add either ``abort`` to the first block or ``reset`` to the second block to avoid nesting lemmas.
Abbreviations and macros
------------------------
Substitutions for specially-formatted names (like ``|Cic|``, ``|Ltac|`` and ``|Latex|``), along with some useful LaTeX macros, are defined in a `separate file </doc/sphinx/refman-preamble.rst>`_. This file is automatically included in all manual pages.
Emacs
-----
The ``dev/tools/coqdev.el`` folder contains a convenient Emacs function to quickly insert Sphinx roles and quotes. It takes a single character (one of ``gntm:```), and inserts one of ``:g:``, ``:n:``, ``:t:``, or an arbitrary role, or double quotes. You can also select a region of text, and wrap it in single or double backticks using that function.
Use the following snippet to bind it to `F12` in ``rst-mode``::
(with-eval-after-load 'rst
(define-key rst-mode-map (kbd "<f12>") #'coqdev-sphinx-rst-coq-action))
Advanced uses of notations
--------------------------
- Use `%` to escape grammar literal strings that are the same as metasyntax,
such as ``{``, ``|``, ``}`` and ``{|``. (While this is optional for
``|`` and ``{ ... }`` outside of ``{| ... }``, always using the escape
requires less thought.)
- Literals such as ``|-`` and ``||`` don't need to be escaped.
- The literal ``%`` shouldn't be escaped.
- Don't use the escape for a ``|`` separator in ``{*`` and ``{+``. These
should appear as ``{*|`` and ``{+|``.
|