File: arguments-command.rst

package info (click to toggle)
coq-doc 8.16.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm
  • size: 42,788 kB
  • sloc: ml: 219,673; sh: 4,035; python: 3,372; ansic: 2,529; makefile: 728; lisp: 279; javascript: 87; xml: 24; sed: 2
file content (454 lines) | stat: -rw-r--r-- 16,876 bytes parent folder | download
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
.. _ArgumentsCommand:

Setting properties of a function's arguments
++++++++++++++++++++++++++++++++++++++++++++

.. cmd:: Arguments @reference {* @arg_specs } {* , {* @implicits_alt } } {? : {+, @args_modifier } }

   .. insertprodn argument_spec args_modifier

   .. prodn::
      argument_spec ::= {? ! } @name {? % @scope_key }
      arg_specs ::= @argument_spec
      | /
      | &
      | ( {+ @argument_spec } ) {? % @scope_key }
      | [ {+ @argument_spec } ] {? % @scope_key }
      | %{ {+ @argument_spec } %} {? % @scope_key }
      implicits_alt ::= @name
      | [ {+ @name } ]
      | %{ {+ @name } %}
      args_modifier ::= simpl nomatch
      | simpl never
      | default implicits
      | clear implicits
      | clear scopes
      | clear bidirectionality hint
      | rename
      | assert
      | extra scopes
      | clear scopes and implicits
      | clear implicits and scopes

   Specifies properties of the arguments of a function after the function has already
   been defined.  It gives fine-grained
   control over the elaboration process (i.e. the translation of Gallina language
   extensions into the core language used by the kernel).  The command's effects include:

   * Making arguments implicit. Afterward, implicit arguments
     must be omitted in any expression that applies :token:`reference`.
   * Declaring that some arguments of a given function should
     be interpreted in a given scope.
   * Affecting when the :tacn:`simpl` and :tacn:`cbn` tactics unfold the function.
     See :ref:`Args_effect_on_unfolding`.
   * Providing bidirectionality hints.  See :ref:`bidirectionality_hints`.

   This command supports the :attr:`local` and :attr:`global` attributes.
   Default behavior is to limit the effect to the current section but also to
   extend their effect outside the current module or library file.
   Applying :attr:`local` limits the effect of the command to the current module if
   it's not in a section.  Applying :attr:`global` within a section extends the
   effect outside the current sections and current module in which the command appears.

      `/`
         the function will be unfolded only if it's applied to at least the
         arguments appearing before the `/`.  See :ref:`Args_effect_on_unfolding`.

         .. exn:: The / modifier may only occur once.
            :undocumented:

      `&`
         tells the type checking algorithm to first type check the arguments
         before the `&` and then to propagate information from that typing context
         to type check the remaining arguments. See :ref:`bidirectionality_hints`.

         .. exn:: The & modifier may only occur once.
            :undocumented:

      :n:`( ... ) {? % @scope }`
         :n:`(@name__1 @name__2 ...)%@scope` is shorthand for :n:`@name__1%@scope @name__2%@scope ...`

      :n:`[ ... ] {? % @scope }`
         declares the enclosed names as implicit, non-maximally inserted.
         :n:`[@name__1 @name__2 ... ]%@scope` is equivalent to :n:`[@name__1]%@scope [@name__2]%@scope ...`

      :n:`%{ ... %} {? % @scope }`
         declares the enclosed names as implicit, maximally inserted.
         :n:`%{@name__1 @name__2 ... %}%@scope` is equivalent to :n:`%{@name__1%}%@scope %{@name__2%}%@scope ...`

      `!`
         the function will be unfolded only if all the arguments marked with `!`
         evaluate to constructors.  See :ref:`Args_effect_on_unfolding`.

      :n:`@name {? % @scope }`
         a *formal parameter* of the function :n:`@reference` (i.e.
         the parameter name used in the function definition).  Unless `rename` is specified,
         the list of :n:`@name`\s must be a prefix of the formal parameters, including all implicit
         arguments.  `_` can be used to skip over a formal parameter.
         The construct :n:`@name {? % @scope }` declares :n:`@name` as non-implicit if `clear implicits` is specified or at least one other name is declared implicit in the same list of :n:`@name`\s.
         :token:`scope` can be either a scope name or its delimiting key.  See :ref:`binding_to_scope`.

         .. exn:: To rename arguments the 'rename' flag must be specified.
            :undocumented:

         .. exn:: Flag 'rename' expected to rename @name into @name.
            :undocumented:

         .. exn:: Arguments of section variables such as @name may not be renamed.
            :undocumented:

      `clear implicits`
         makes all implicit arguments into explicit arguments

         .. exn:: The 'clear implicits' flag must be omitted if implicit annotations are given.
            :undocumented:

      `default implicits`
         automatically determine the implicit arguments of the object.
         See :ref:`auto_decl_implicit_args`.

         .. exn:: The 'default implicits' flag is incompatible with implicit annotations.
            :undocumented:

      `rename`
         rename implicit arguments for the object.  See the example :ref:`here <renaming_implicit_arguments>`.
      `assert`
         assert that the object has the expected number of arguments with the
         expected names.  See the example here: :ref:`renaming_implicit_arguments`.

         .. warn:: This command is just asserting the names of arguments of @qualid. If this is what you want, add ': assert' to silence the warning. If you want to clear implicit arguments, add ': clear implicits'. If you want to clear notation scopes, add ': clear scopes'
            :undocumented:

      `clear scopes`
         clears argument scopes of :n:`@reference`
      `extra scopes`
         defines extra argument scopes, to be used in case of coercion to ``Funclass``
         (see :ref:`coercions`) or with a computed type.
      `simpl nomatch`
         prevents performing a simplification step for :n:`@reference`
         that would expose a match construct in the head position.  See :ref:`Args_effect_on_unfolding`.
      `simpl never`
         prevents performing a simplification step for :n:`@reference`.  See :ref:`Args_effect_on_unfolding`.

      `clear bidirectionality hint`
         removes the bidirectionality hint, the `&`

      :n:`@implicits_alt`
         use to specify alternative implicit argument declarations
         for functions that can only be
         applied to a fixed number of arguments (excluding, for instance,
         functions whose type is polymorphic).
         For parsing, the longest list of implicit arguments matching the function application
         is used to select which implicit arguments are inserted.
         For printing, the alternative with the most implicit arguments is used; the
         implict arguments will be omitted if :flag:`Printing Implicit` is not set.
         See the example :ref:`here<example_more_implicits>`.

         .. todo the above feature seems a bit unnatural and doesn't play well with partial
            application.  See https://github.com/coq/coq/pull/11718#discussion_r408841762

   Use :cmd:`About` to view the current implicit arguments setting for a :token:`reference`.

   Or use the :cmd:`Print Implicit` command to see the implicit arguments
   of an object (see :ref:`displaying-implicit-args`).

Manual declaration of implicit arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. example::

   .. coqtop:: reset all

      Inductive list (A : Type) : Type :=
      | nil : list A
      | cons : A -> list A -> list A.

      Check (cons nat 3 (nil nat)).

      Arguments cons [A] _ _.

      Arguments nil {A}.

      Check (cons 3 nil).

      Fixpoint map (A B : Type) (f : A -> B) (l : list A) : list B :=
        match l with nil => nil | cons a t => cons (f a) (map A B f t) end.

      Fixpoint length (A : Type) (l : list A) : nat :=
        match l with nil => 0 | cons _ m => S (length A m) end.

      Arguments map [A B] f l.

      Arguments length {A} l. (* A has to be maximally inserted *)

      Check (fun l:list (list nat) => map length l).

.. _example_more_implicits:

.. example:: Multiple alternatives with :n:`@implicits_alt`

   .. coqtop:: all

      Arguments map [A B] f l, [A] B f l, A B f l.

      Check (fun l => map length l = map (list nat) nat length l).

.. _auto_decl_implicit_args:

Automatic declaration of implicit arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   The ":n:`default implicits`" :token:`args_modifier` clause tells Coq to automatically determine the
   implicit arguments of the object.

   Auto-detection is governed by flags specifying whether strict,
   contextual, or reversible-pattern implicit arguments must be
   considered or not (see :ref:`controlling-strict-implicit-args`, :ref:`controlling-contextual-implicit-args`,
   :ref:`controlling-rev-pattern-implicit-args` and also :ref:`controlling-insertion-implicit-args`).

.. example:: Default implicits

   .. coqtop:: reset all

      Inductive list (A:Set) : Set :=
      | nil : list A
      | cons : A -> list A -> list A.

      Arguments cons : default implicits.

      Print Implicit cons.

      Arguments nil : default implicits.

      Print Implicit nil.

      Set Contextual Implicit.

      Arguments nil : default implicits.

      Print Implicit nil.

The computation of implicit arguments takes account of the unfolding
of :term:`constants <constant>`. For instance, the variable ``p`` below has type
``(Transitivity R)`` which is reducible to
``forall x,y:U, R x y -> forall z:U, R y z -> R x z``. As the variables ``x``, ``y`` and ``z``
appear strictly in the :term:`body` of the type, they are implicit.

.. coqtop:: all

   Parameter X : Type.

   Definition Relation := X -> X -> Prop.

   Definition Transitivity (R:Relation) := forall x y:X, R x y -> forall z:X, R y z -> R x z.

   Parameters (R : Relation) (p : Transitivity R).

   Arguments p : default implicits.

   Print p.

   Print Implicit p.

   Parameters (a b c : X) (r1 : R a b) (r2 : R b c).

   Check (p r1 r2).


.. _renaming_implicit_arguments:

Renaming implicit arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. example:: (continued)  Renaming implicit arguments

   .. coqtop:: all

      Arguments p [s t] _ [u] _: rename.

      Check (p r1 (u:=c)).

      Check (p (s:=a) (t:=b) r1 (u:=c) r2).

      Fail Arguments p [s t] _ [w] _ : assert.

.. _binding_to_scope:

Binding arguments to a scope
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   The following command declares that the first two arguments of :g:`plus_fct`
   are in the :token:`scope` delimited by the key ``F`` (``Rfun_scope``) and the third
   argument is in the scope delimited by the key ``R`` (``R_scope``).

      .. coqdoc::

         Arguments plus_fct (f1 f2)%F x%R.

   When interpreting a term, if some of the arguments of :token:`reference` are built
   from a notation, then this notation is interpreted in the scope stack
   extended by the scope bound (if any) to this argument. The effect of
   the scope is limited to the argument itself. It does not propagate to
   subterms but the subterms that, after interpretation of the notation,
   turn to be themselves arguments of a reference are interpreted
   accordingly to the argument scopes bound to this reference.

.. note::

   In notations, the subterms matching the identifiers of the
   notations are interpreted in the scope in which the identifiers
   occurred at the time of the declaration of the notation. Here is an
   example:

   .. coqtop:: all

      Parameter g : bool -> bool.
      Declare Scope mybool_scope.

      Notation "@@" := true (only parsing) : bool_scope.
      Notation "@@" := false (only parsing): mybool_scope.

      Bind Scope bool_scope with bool.
      Notation "# x #" := (g x) (at level 40).
      Check # @@ #.
      Arguments g _%mybool_scope.
      Check # @@ #.
      Delimit Scope mybool_scope with mybool.
      Check # @@%mybool #.

.. _Args_effect_on_unfolding:

Effects of :cmd:`Arguments` on unfolding
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ `simpl never` indicates that a :term:`constant` should never be unfolded by :tacn:`cbn` or
  :tacn:`simpl`:

  .. example::

     .. coqtop:: all

        Arguments minus n m : simpl never.

  After that command an expression like :g:`(minus (S x) y)` is left
  untouched by the tactics :tacn:`cbn` and :tacn:`simpl`.

+ A :term:`constant` can be marked to be unfolded only if it's applied to at least
  the arguments appearing before the `/` in a :cmd:`Arguments` command.

  .. example::

     .. coqtop:: all

        Definition fcomp A B C f (g : A -> B) (x : A) : C := f (g x).
        Arguments fcomp {A B C} f g x /.
        Notation "f \o g" := (fcomp f g) (at level 50).

  After that command the expression :g:`(f \o g)` is left untouched by
  :tacn:`simpl` while :g:`((f \o g) t)` is reduced to :g:`(f (g t))`.
  The same mechanism can be used to make a :term:`constant` volatile, i.e.
  always unfolded.

  .. example::

     .. coqtop:: all

        Definition volatile := fun x : nat => x.
        Arguments volatile / x.

+ A :term:`constant` can be marked to be unfolded only if an entire set of
  arguments evaluates to a constructor. The ``!`` symbol can be used to mark
  such arguments.

  .. example::

     .. coqtop:: all

        Arguments minus !n !m.

  After that command, the expression :g:`(minus (S x) y)` is left untouched
  by :tacn:`simpl`, while :g:`(minus (S x) (S y))` is reduced to :g:`(minus x y)`.

+ `simpl nomatch` indicates that a :term:`constant` should not be unfolded if it would expose
  a `match` construct in the head position.  This affects the :tacn:`cbn`,
  :tacn:`simpl` and :tacn:`hnf` tactics.

  .. example::

     .. coqtop:: all

        Arguments minus n m : simpl nomatch.

  In this case, :g:`(minus (S (S x)) (S y))` is simplified to :g:`(minus (S x) y)`
  even if an extra simplification is possible.

  In detail: the tactic :tacn:`simpl` first applies βι-reduction. Then, it
  expands transparent :term:`constants <constant>` and tries to reduce further using βι-reduction.
  But, when no ι rule is applied after unfolding then
  δ-reductions are not applied. For instance trying to use :tacn:`simpl` on
  :g:`(plus n O) = n` changes nothing.


.. _bidirectionality_hints:

Bidirectionality hints
~~~~~~~~~~~~~~~~~~~~~~

When type-checking an application, Coq normally does not use information from
the context to infer the types of the arguments. It only checks after the fact
that the type inferred for the application is coherent with the expected type.
Bidirectionality hints make it possible to specify that after type-checking the
first arguments of an application, typing information should be propagated from
the context to help inferring the types of the remaining arguments.

.. todo the following text is a start on better wording but not quite complete.
   See https://github.com/coq/coq/pull/11718#discussion_r410219992

  ..
  Two common methods to determine the type of a construct are:

  * *type checking*, which is verifying that a construct matches a known type, and
  * *type inference*, with is inferring the type of a construct by analyzing the construct.

  Methods that combine these approaches are known as *bidirectional typing*.
  Coq normally uses only the first approach to infer the types of arguments,
  then later verifies that the inferred type is consistent with the expected type.
  *Bidirectionality hints* specify to use both methods: after type checking the
  first arguments of an application (appearing before the `&` in :cmd:`Arguments`),
  typing information from them is propagated to the remaining arguments to help infer their types.

An :cmd:`Arguments` command containing :n:`@arg_specs__1 & @arg_specs__2`
provides bidirectionality hints.
It tells the typechecking algorithm, when type checking
applications of :n:`@qualid`, to first type check the arguments in
:n:`@arg_specs__1` and then propagate information from the typing context to
type check the remaining arguments (in :n:`@arg_specs__2`).

.. example:: Bidirectionality hints

   In a context where a coercion was declared from ``bool`` to ``nat``:

   .. coqtop:: in reset

      Definition b2n (b : bool) := if b then 1 else 0.
      Coercion b2n : bool >-> nat.

   Coq cannot automatically coerce existential statements over ``bool`` to
   statements over ``nat``, because the need for inserting a coercion is known
   only from the expected type of a subterm:

   .. coqtop:: all

      Fail Check (ex_intro _ true _ : exists n : nat, n > 0).

   However, a suitable bidirectionality hint makes the example work:

   .. coqtop:: all

      Arguments ex_intro _ _ & _ _.
      Check (ex_intro _ true _ : exists n : nat, n > 0).

Coq will attempt to produce a term which uses the arguments you
provided, but in some cases involving Program mode the arguments after
the bidirectionality starts may be replaced by convertible but
syntactically different terms.