yellow Multi Trunk body cross MARNI bag leather black blue striped P4n0HxO
In this chapter, we introduce advanced commands to modify the way Coq parses and prints objects, i.e. the translations between the concrete and internal representations of terms and commands.
The main commands to provide custom symbolic notations for terms are
Notation
and
Infix
. They are described in section
Notations
. There is also a variant of
Notation
which does not modify the parser. This provides with a form of abbreviation and it is described in Section
Abbreviations
. It is sometimes expected that the same symbolic notation has different meanings in different contexts. To achieve this form of overloading,
Coq
offers a notion of interpretation scope. This is described in Section
Print navy Key Print LUELLA West Tshirt Key LUELLA West pearl np681qx
.
The main command to provide custom notations for tactics is
Tactic
Notation
. It is described in Section
Tactic Notations
.
 Set Printing Depth 50.
Notations ¶
Basic notations ¶

Command
Notation
¶
A notation is a symbolic expression denoting some term or term pattern.
A typical notation is the use of the infix symbol
/\
to denote the logical conjunction (and). Such a notation is declared by
 Notation "A /\ B" := ( and A B ).
The expression
(
and
A
B
)
is the abbreviated term and the string
"A
/\
B"
(called a
notation
) tells how it is symbolically written.
A notation is always surrounded by double quotes (except when the abbreviation has the form of an ordinary applicative expression; see
Abbreviations
). The notation is composed of
tokens
separated by spaces. Identifiers in the string (such as
A
and
B
) are the
parameters
of the notation. They must occur at least once each in the denoted term. The other elements of the string (such as
/\
) are the
symbols
.
An identifier can be used as a symbol but it must be surrounded by simple quotes to avoid the confusion with a parameter. Similarly, every symbol of at least 3 characters and starting with a simple quote must be quoted (then it starts by two single quotes). Here is an example.
 Notation "'IF' c1 'then' c2 'else' c3" := ( IF_then_else c1 c2 c3 ).
A notation binds a syntactic expression to a term. Unless the parser and prettyprinter of Coq already know how to deal with the syntactic expression (see 12.1.7), explicit precedences and associativity rules have to be given.
Note
The righthand side of a notation is interpreted at the time the notation is given. In particular, disambiguation of constants, implicit arguments (see Section
Implicit arguments
), coercions (see Section
jeans straight Safado 1 regularfit Safado regularfit DIESEL DIESEL xFpZw88Yq
), etc. are resolved at the time of the declaration of the notation.
Precedences and associativity ¶
Mixing different symbolic notations in the same text may cause serious parsing ambiguity. To deal with the ambiguity of notations, Coq uses precedence levels ranging from 0 to 100 (plus one extra level numbered 200) and associativity rules.
Consider for example the new notation
 Notation "A \/ B" := ( or A B ).
Clearly, an expression such as
forall
A
:
Prop
,
True
/\
A
\/
A
\/
False
is ambiguous. To tell the Coq parser how to interpret the expression, a priority between the symbols
/\
and
\/
has to be given. Assume for instance that we want conjunction to bind more than disjunction. This is expressed by assigning a precedence level to each notation, knowing that a lower level binds more than a higher level. Hence the level for disjunction must be higher than the level for conjunction.
Since connectives are not tight articulation points of a text, it is reasonable to choose levels not so far from the highest level which is 100, for example 85 for disjunction and 80 for conjunction [1] .
Similarly, an associativity is needed to decide whether
True
/\
False
/\
False
defaults to
True
/\
(
False
/\
False
)
(right associativity) or to
(
True
/\
False
)
/\
False
(left associativity). We may even consider that the expression is not well formed and that parentheses are mandatory (this is a “no associativity”)
[2]
. We do not know of a special convention of the associativity of disjunction and conjunction, so let us apply for instance a right associativity (which is the choice of Coq).
Precedence levels and associativity rules of notations have to be given between parentheses in a list of modifiers that the
Notation
command understands. Here is how the previous examples refine.
 Notation "A /\ B" := ( and A B ) ( at level 80, right associativity ).
 Notation "A \/ B" := ( or A B ) ( at level 85, right associativity ).
By default, a notation is considered non associative, but the precedence level is mandatory (except for special cases whose level is canonical). The level is either a number or the phrase next level whose meaning is obvious. The list of levels already assigned is on Figure 3.1.
Complex notations ¶
Notations can be made from arbitrarily complex symbols. One can for instance define prefix notations.
 Notation "~ x" := ( not x ) ( at level 75, right associativity ).
One can also define notations for incomplete terms, with the hole expected to be inferred at typing time.
 Notation "x = y" := (@ eq _ x y ) ( at level 70, no associativity ).
One can define closed notations whose both sides are symbols. In this case, the default precedence level for the inner subexpression is 200, and the default level for the notation itself is 0.
 Notation "( x , y )" := (@ pair _ _ x y ).
 Setting notation at level 0.
One can also define notations for binders.
 Notation "{ x : A  P }" := ( sig A ( fun x => P )).
In the last case though, there is a conflict with the notation for type casts. The notation for types casts, as shown by the command
Print
Grammar
constr
is at level 100. To avoid
x
:
A
being parsed as a type cast, it is necessary to put x at a level below 100, typically 99. Hence, a correct definition is the following:
 Notation "{ x : A  P }" := ( sig A ( fun x => P )) ( x at level 99).
 Setting notation at level 0.
More generally, it is required that notations are explicitly factorized on the left. See the next section for more about factorization.
Simple factorization rules ¶
Coq extensible parsing is performed by Camlp5 which is essentially a LL1 parser: it decides which notation to parse by looking tokens from left to right. Hence, some care has to be taken not to hide already existing rules by new rules. Some simple left factorization work has to be done. Here is an example.
 Notation "x < y" := ( lt x y ) ( at level 70).
 Notation "x < y < z" := ( x < y /\ y < z ) ( at level 70).
 Toplevel input, characters 055: > Notation "x < y < z" := (x < y /\ y < z) (at level 70). > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: Notation "_ < _ < _" is already defined at level 70 with arguments constr at next level, constr at next level, constr at next level while it is now required to be at level 70 with arguments constr at next level, constr at level 200, constr at next level.
In order to factorize the left part of the rules, the subexpression referred by y has to be at the same level in both rules. However the default behavior puts y at the next level below 70 in the first rule (no associativity is the default), and at the level 200 in the second rule (level 200 is the default for inner expressions). To fix this, we need to force the parsing level of y, as follows.
 Notation "x < y" := ( lt x y ) ( at level 70).
 Notation "x < y < z" := ( x < y /\ y < z ) ( at level 70, y at next level ).
For the sake of factorization with Coq predefined rules, simple rules have to be observed for notations starting with a symbol: e.g. rules starting with “{” or “(” should be put at level 0. The list of Coq predefined notations can be found in Chapter
Falcon Royal Robbins Hoodie Falcon Royal Royal Highlands Robbins Hoodie Heather Robbins Heather Highlands XwHxxFqPE
.

Command
Print Grammar constr.
¶ 
This command displays the current state of the Coq term parser.

Command
Print Grammar pattern.
¶ 
This displays the state of the subparser of patterns (the parser used in the grammar of the match with constructions).
Displaying symbolic notations ¶
The command
Notation
has an effect both on the Coq parser and on the Coq printer. For example:
 Check ( and True True yellow MARNI Trunk cross bag striped blue leather Multi body black Red Pre Order Eastbay iWQF4BOJW ).
 True /\ True : Prop
However, printing, especially prettyprinting, also requires some care. We may want specific indentations, line breaks, alignment if on several lines, etc. For prettyprinting, Coq relies on OCaml formatting library, which provides indentation and automatic line breaks depending on page width by means of formatting boxes .
The default printing of notations is rudimentary. For printing a notation, a formatting box is opened in such a way that if the notation and its arguments cannot fit on a single line, a line break is inserted before the symbols of the notation and the arguments on the next lines are aligned with the argument on the first line.
A first, simple control that a user can have on the printing of a notation is the insertion of spaces at some places of the notation. This is performed by adding extra spaces between the symbols and parameters: each extra space (other than the single space needed to separate the components) is interpreted as a space to be inserted by the printer. Here is an example showing how to add spaces around the bar of the notation.
 Notation "{{ x : A  P }}" := ( sig ( fun x : A => P )) ( at level 0, x at level 99).
 Check ( sig ( fun x : nat => x = x )).
 {{ x : nat  x = x }} : Set
The second, more powerful control on printing is by using the format modifier. Here is an example
 Notation "'If' c1 'then' c2 'else' c3" := ( IF_then_else c1 c2 c3 ) ( at level 200, right associativity , format "'[v ' 'If' c1 '/' '[' 'then' c2 ']' '/' '[' 'else' c3 ']' ']'").
 Identifier 'If' now a keyword
 Check ( IF_then_else ( IF_then_else True False True ) ( IF_then_else True False True ) ( IF_then_else True False True )).
 If If True then False else True then If True then False else True else If True then False else True : Prop
A format is an extension of the string denoting the notation with the possible following elements delimited by single quotes:
 extra spaces are translated into simple spaces

tokens of the form
'/ '
are translated into breaking point, in case a line break occurs, an indentation of the number of spaces after the “/
” is applied (2 spaces in the given example) 
token of the form
'//'
force writing on a new line 
wellbracketed pairs of tokens of the form
'[ '
and']'
are translated into printing boxes; in case a line break occurs, an extra indentation of the number of spaces given after the “[
” is applied (4 spaces in the example) 
wellbracketed pairs of tokens of the form
'[hv '
and']'
are translated into horizontalorelsevertical printing boxes; if the content of the box does not fit on a single line, then every breaking point forces a newline and an extra indentation of the number of spaces given after the “[
” is applied at the beginning of each newline (3 spaces in the example) 
wellbracketed pairs of tokens of the form
'[v '
and']'
are translated into vertical printing boxes; every breaking point forces a newline, even if the line is large enough to display the whole content of the box, and an extra indentation of the number of spaces given after the “[
” is applied at the beginning of each newline
Notations do not survive the end of sections. No typing of the denoted expression is performed at definition time. Typechecking is done only at the time of use of the notation.
Note
Sometimes, a notation is expected only for the parser. To do so, the option
only
parsing
is allowed in the list of modifiers of
Notation
. Conversely, the
only
printing
modifier can be used to declare that a notation should only be used for printing and should not declare a parsing rule. In particular, such notations do not modify the parser.
The Infix command ¶
The
Infix
command is a shortening for declaring notations of infix symbols.
Reserving notations ¶
A given notation may be used in different contexts. Coq expects all uses of the notation to be defined at the same precedence and with the same associativity. To avoid giving the precedence and associativity every time, it is possible to declare a parsing rule in advance without giving its interpretation. Here is an example from the initial state of Coq.
 Reserved Notation "x = y" ( at level 70, no associativity ).
Reserving a notation is also useful for simultaneously defining an inductive type or a recursive constant and a notation for it.
Note
The notations mentioned on Figure 3.1 are reserved. Hence their precedence and associativity cannot be changed.
Simultaneous definition of terms and notations ¶
Thanks to reserved notations, the inductive, coinductive, record, recursive and corecursive definitions can benefit of customized notations. To do this, insert a
where
notation clause after the definition of the (co)inductive type or (co)recursive term (or after the definition of each of them in case of mutual definitions). The exact syntax is given by
decl_notation
for inductive, coinductive, recursive and corecursive definitions and in
Record types
for records. Here are examples:
 Inductive and ( A B : Prop ) : Prop := conj : A > B > A /\ B where "A /\ B" := ( and A B ).
 Toplevel input, characters 087: > Inductive and (A B:Prop) : Prop := conj : A > B > A /\ B where "A /\ B" := (and A B). > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Warning: Notation _ /\ _ was already used. [notationoverridden,parsing] Toplevel input, characters 087: > Inductive and (A B:Prop) : Prop := conj : A > B > A /\ B where "A /\ B" := (and A B). > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: The type of constructor conj is not valid; its conclusion must be " and " applied to its parameters.
 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 ).
 plus is defined plus is recursively defined (decreasing on 1st argument)
Displaying informations about notations ¶

Option
Printing Notations
¶ 
To deactivate the printing of all notations, use the command
Unset Printing Notations
. To reactivate it, use the commandSet Printing Notations
.The default is to use notations for printing terms wherever possible.
See also

Printing All
 To disable other elements in addition to notations.
Locating notations ¶
To know to which notations a given symbol belongs to, use the
Locate
command. You can call it on any (composite) symbol surrounded by double quotes. To locate a particular notation, use a string where the variables of the notation are replaced by “_” and where possible single quotes inserted around identifiers or tokens starting with a single quote are dropped.
 Locate "exists".
 Notation "'exists' x .. y , p" := ex ( fun x => .. ( ex ( fun y => p )) ..) : type_scope (default interpretation) "'exists' ! x .. y , p" := ex ( unique ( fun x => .. ( ex ( unique ( fun y => p ))) ..)) : type_scope (default interpretation)
 Locate "exists _ .. _ , _".
 Notation "'exists' x .. y , p" := ex ( fun x => .. ( ex ( fun y => p )) ..) : type_scope (default interpretation)
Notations and binders ¶
Notations can include binders. This section lists different ways to deal with binders. For further examples, see also Section Notations with recursive patterns .
Binders bound in the notation and parsed as identifiers ¶
Here is the basic example of a notation using a binder:
 Notation "'sigma' x : A , B" := ( sigT ( fun x : A => B )) ( at level 200, x ident , A at level 200, right associativity ).
 Identifier 'sigma' now a keyword
The binding variables in the righthand side that occur as a parameter of the notation (here
x
) dynamically bind all the occurrences in their respective binding scope after instantiation of the parameters of the notation. This means that the term bound to
B
can refer to the variable name bound to
x
as shown in the following application of the notation:
 Check sigma z : nat , z = 0.
 sigma z : nat , z = 0 : Set
Notice the modifier
x
ident
in the declaration of the notation. It tells to parse
x
as a single identifier.
Binders bound in the notation and parsed as patterns
Mesh Black Heel Calf Emporio Emporio Calf Heel Mesh Armani Armani Black Emporio z8WBRPBAF
In the same way as patterns can be used as binders, as in
fun
'
(
x
,
y
)
=>
x
+
y
or
fun
'
(
existT
_
x
_)
=>
black Trunk bag striped MARNI leather body blue cross yellow Multi Sale Best Place Red Pre Order Eastbay Cheap Sale Fake M2UPCgeox
x
, notations can be defined so that any
pattern
can be used in place of the binder. Here is an example:
 Notation "'subset' ' p , P " := ( sig ( fun p => P )) ( at level 200, p pattern , format "'subset' ' p , P").
 Identifier 'subset' now a keyword
 Check subset '( x , y ), x + y =0.
 subset ' ( x , y ) , x + y = 0 : Set
The modifier
p
pattern
in the declaration of the notation tells to parse
p
as a pattern. Note that a single variable is both an identifier and a pattern, so, e.g., the following also works:
 Check subset ' x , x =0.
 subset ' x , x = 0 : Set
If one wants to prevent such a notation to be used for printing when the pattern is reduced to a single identifier, one has to use instead the modifier
p
strict
pattern
. For parsing, however, a
strict
pattern
will continue to include the case of a variable. Here is an example showing the difference:
 Notation "'subset_bis' ' p , P" := ( sig ( fun p => P )) ( at level 200, p strict pattern ).
 Identifier 'subset_bis' now a keyword
 Notation "'subset_bis' p , P " := ( sig ( fun p => P )) ( at level 200, p ident ).
 Check subset_bis ' x , x =0.
 subset_bis x , x = 0 : Set
The default level for a
pattern
is 0. One can use a different level by using
pattern
at
level
n
where the scale is the same as the one for terms (see
jeans fit fit ALEXA T T Slim ALEXA Porter Freeman Freeman Slim Porter white jeans qpUBwCtxnA
).
Binders bound in the notation and parsed as terms Red Pre Order Eastbay blue cross Trunk bag black yellow MARNI Multi striped body leather Prices Sale Online Wide Range Of Online 4JWWr ¶
Sometimes, for the sake of factorization of rules, a binder has to be parsed as a term. This is typically the case for a notation such as the following:
 Notation "{ x : A  P }" := ( sig ( fun x : A => P )) ( at level 0, x at level 99 as ident ).
 Toplevel input, characters 092: > Notation "{ x : A  P }" := (sig (fun x : A => P)) (at level 0, x at level 99 as ident). > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: Notation "{ _ : _  _ }" is already defined at level 0 with arguments constr at level 99, constr at level 200, constr at level 200 while it is now required to be at level 0 with arguments as ident at level 99, constr at level 200, constr at level 200.
This is so because the grammar also contains rules starting with
{}
and followed by a term, such as the rule for the notation
{
A
}
+
{
B
}
for the constant
sumbool
(see Section
Specification
).
Then, in the rule,
x
ident
is replaced by
x
at
level
99
as
ident
meaning that
x
is parsed as a term at level 99 (as done in the notation for
sumbool
), but that this term has actually to be an identifier.
The notation
{
x

P
}
is already defined in the standard library with the
as
ident
modifier. We cannot redefine it but one can define an alternative notation, say
{
p
such
that
P
}
, using instead
as
pattern
.
 Notation "{ p 'such' 'that' P }" := ( sig ( fun p => P )) ( at level 0, p at level 99 as pattern ).
 Identifier 'such' now a keyword
Then, the following works:
 Check {( x , y ) such that x + y =0}.
 { ( x , y ) such that x + y = 0 } : Set
To enforce that the pattern should not be used for printing when it is just an identifier, one could have said
p
at
level
99
as
strict
pattern
.
Note also that in the absence of a
as
ident
,
as
strict
pattern
or
as
pattern
modifiers, the default is to consider subexpressions occurring in binding position and parsed as terms to be
as
ident
.
Binders not bound in the notation ¶
We can also have binders in the righthand side of a notation which are not themselves bound in the notation. In this case, the binders are considered up to renaming of the internal binder. E.g., for the notation
 Notation "'exists_different' n" := ( exists p blue bag body striped cross Trunk leather yellow Multi black MARNI Cheap Sale Sneakernews Prices Sale Online Sale Best Place Cheap Sale Fake ZssDR : nat , p <> n ) ( at level 200).
 Identifier 'exists_different' now a keyword
the next command fails because p does not bind in the instance of n.
 Fail Check ( Cheap Sale Fake Cheap Sale Sneakernews Trunk MARNI black Multi leather blue striped yellow body bag cross qozemUko exists_different p ).
 The command has indeed failed with message: The reference p was not found in the current environment.
 Notation "[> a , .. , b <]" := ( cons a .. ( cons b nil ) .., cons b .. ( cons a nil ) ..).
 Setting notation at level 0.
Notations with recursive patterns
STS Ranchwear Shoulder Convertible Bag The Backpack Brown Baroness STS Ranchwear gwFSOg
A mechanism is provided for declaring elementary notations with recursive patterns. The basic example is:
 Notation "[ x ; .. ; y ]" := ( cons x .. ( cons y nil ) ..).
 Setting notation at level 0.
On the righthand side, an extra construction of the form
..
t
..
can be used. Notice that
..
is part of the Coq syntax and it must not be confused with the threedots notation “
…
” used in this manual to denote a sequence of arbitrary size.
On the lefthand side, the part “
x
s
..
s
y
” of the notation parses any number of time (but at least one time) a sequence of expressions separated by the sequence of tokens
s
(in the example,
s
is just “
;
”).
The righthand side must contain a subterm of the form either
φ(x,
..
φ(y,t)
..)
or
φ(y,
..
φ(x,t)
..)
where
φ
(
[
]
E
,
[
]
I
)
, called the
iterator
of the recursive notation is an arbitrary expression with distinguished placeholders and where
t
is called the
terminating expression
of the recursive notation. In the example, we choose the names
x
and
y
but in practice they can of course be chosen arbitrarily. Not atht the placeholder
[
]
I
has to occur only once but
[
]
E
can occur several times.
Parsing the notation produces a list of expressions which are used to fill the first placeholder of the iterating pattern which itself is repeatedly nested as many times as the length of the list, the second placeholder being the nesting point. In the innermost occurrence of the nested iterating pattern, the second placeholder is finally filled with the terminating expression.
In the example above, the iterator
φ
(
[
]
E
,
[
]
I
)
is
c
o
n
s
[
]
E
[
]
I
and the terminating expression is
nil
. Here are other examples:
 Notation "( x , y , .. , z )" := ( pair .. ( pair x y ) .. z ) ( at level 0).
 Notation "[ t * ( x , y , .. , z ) ; ( a , b , .. , c ) * u ]" := ( pair ( pair .. ( pair ( pair t x ) ( pair t y )) .. ( pair t z )) ( pair .. ( pair (