6
$\begingroup$

Say I want to define add : nat -> nat -> nat with a notation Notation "a + b" := add a b and I want to write add as something like

Fixpoint add (a b : nat) : nat := match a with
| 0 => b
| S a => S (a + b) (* note the use of notation + *)

Where can I insert the notation definition so I can write the above?

This may look like an overkill for notation, but that's because it's a simplified example. The real use case is a very long definition, and I want to use notation to simplify it.

$\endgroup$

1 Answer 1

11
$\begingroup$

You can add a where clause at the end. See the documentation there:

https://coq.inria.fr/doc/V8.20.0/refman/user-extensions/syntax-extensions.html#grammar-token-decl_notations

Example:

Fixpoint plus (n m : nat) {struct n} : nat :=
match n with
    | O => m
    | S p => S (p + m)
end
where "n + m" := (plus n m).
$\endgroup$
3
  • 4
    $\begingroup$ By the way, this also works for other kinds of self-referencing definitions, in particular inductive types. $\endgroup$ Commented Nov 20 at 8:35
  • $\begingroup$ You need to reserve the notation before that. That means defining the syntax for proper parsing. $\endgroup$ Commented Nov 20 at 20:09
  • $\begingroup$ It doesn't work for records!!! 😢😢😢😢😢😭😭😭😭😭😭😭 $\endgroup$
    – ice1000
    Commented Nov 20 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.