Implicit Coercions¶
Author:  Amokrane Saïbi 

General Presentation¶
This section describes the inheritance mechanism of Coq. In Coq with inheritance, we are not interested in adding any expressive power to our theory, but only convenience. Given a term, possibly not typable, we are interested in the problem of determining if it can be well typed modulo insertion of appropriate coercions. We allow to write:
f a
wheref:(forall x:A,B)
anda:A'
whenA'
can be seen in some sense as a subtype ofA
.x:A
whenA
is not a type, but can be seen in a certain sense as a type: set, group, category etc.f a
whenf
is not a function, but can be seen in a certain sense as a function: bijection, functor, any structure morphism etc.
Classes¶
A class with n parameters is any defined name with a type
forall (x₁:A₁)..(xₙ:Aₙ),s
where s
is a sort. Thus a class with
parameters is considered as a single class and not as a family of
classes. An object of a class C
is any term of type C t₁ .. tₙ
.
In addition to these userdefined classes, we have two builtin classes:
Sortclass
, the class of sorts; its objects are the terms whose type is a sort (e.g.Prop
orType
).Funclass
, the class of functions; its objects are all the terms with a functional type, i.e. of formforall x:A,B
.
Formally, the syntax of a classes is defined as:
class ::= qualid Sortclass
Funclass
Coercions¶
A name f
can be declared as a coercion between a source userdefined class
C
with n parameters and a target class D
if one of these
conditions holds:
D
is a userdefined class, then the type off
must have the formforall (x₁:A₁)..(xₙ:Aₙ)(y:C x₁..xₙ), D u₁..uₘ
where m is the number of parameters ofD
.D
isFunclass
, then the type off
must have the formforall (x₁:A₁)..(xₙ:Aₙ)(y:C x₁..xₙ)(x:A), B
.D
isSortclass
, then the type off
must have the formforall (x₁:A₁)..(xₙ:Aₙ)(y:C x₁..xₙ), s
withs
a sort.
We then write f : C >> D
. The restriction on the type
of coercions is called the uniform inheritance condition.
Note
The builtin class Sortclass
can be used as a source class, but
the builtin class Funclass
cannot.
To coerce an object t:C t₁..tₙ
of C
towards D
, we have to
apply the coercion f
to it; the obtained term f t₁..tₙ t
is
then an object of D
.
Identity Coercions¶
Identity coercions are special cases of coercions used to go around
the uniform inheritance condition. Let C
and D
be two classes
with respectively n and m parameters and
f:forall (x₁:T₁)..(xₖ:Tₖ)(y:C u₁..uₙ), D v₁..vₘ
a function which
does not verify the uniform inheritance condition. To declare f
as
coercion, one has first to declare a subclass C'
of C
:
C' := fun (x₁:T₁)..(xₖ:Tₖ) => C u₁..uₙ
We then define an identity coercion between C'
and C
:
Id_C'_C := fun (x₁:T₁)..(xₖ:Tₖ)(y:C' x₁..xₖ) => (y:C u₁..uₙ)
We can now declare f
as coercion from C'
to D
, since we can
"cast" its type as
forall (x₁:T₁)..(xₖ:Tₖ)(y:C' x₁..xₖ),D v₁..vₘ
.
The identity coercions have a special status: to coerce an object
t:C' t₁..tₖ
of C'
towards C
, we do not have to insert explicitly Id_C'_C
since Id_C'_C t₁..tₖ t
is convertible with t
. However we
"rewrite" the type of t
to become an object of C
; in this case,
it becomes C uₙ'..uₖ'
where each uᵢ'
is the result of the
substitution in uᵢ
of the variables xⱼ
by tⱼ
.
Inheritance Graph¶
Coercions form an inheritance graph with classes as nodes. We call
coercion path an ordered list of coercions between two nodes of
the graph. A class C
is said to be a subclass of D
if there is a
coercion path in the graph from C
to D
; we also say that C
inherits from D
. Our mechanism supports multiple inheritance since a
class may inherit from several classes, contrary to simple inheritance
where a class inherits from at most one class. However there must be
at most one path between two classes. If this is not the case, only
the oldest one is valid and the others are ignored. So the order
of declaration of coercions is important.
We extend notations for coercions to coercion paths. For instance
[f₁;..;fₖ] : C >> D
is the coercion path composed
by the coercions f₁..fₖ
. The application of a coercion path to a
term consists of the successive application of its coercions.
Declaring Coercions¶

Command
Coercion qualid : class >> class
¶ Declares the construction denoted by qualid as a coercion between the two given classes.

Error
Funclass cannot be a source class.
¶

Error
Found target class ... instead of ...
¶

Warning
Ambiguous path.
¶ When the coercion
qualid
is added to the inheritance graph, invalid coercion paths are ignored; they are signaled by a warning displaying these paths of the form[f₁;..;fₙ] : C >> D
.

Variant
Local Coercion qualid : class >> class
Declares the construction denoted by qualid as a coercion local to the current section.

Variant
Coercion ident := term
This defines ident just like
Definition
ident:=
term, and then declares ident as a coercion between it source and its target.

Error
Assumptions can be declared as coercions at declaration time. This extends the grammar of assumptions from Figure The Vernacular as follows:
assumption ::= assumption_keyword assums . assums ::= simple_assums  (simple_assums) ... (simple_assums) simple_assums ::= ident ... ident :[>] term
If the extra >
is present before the type of some assumptions, these
assumptions are declared as coercions.
Similarly, constructors of inductive types can be declared as coercions at definition time of the inductive type. This extends and modifies the grammar of inductive types from Figure The Vernacular as follows:
inductive ::=Inductive
ind_bodywith
...with
ind_body CoInductive
ind_bodywith
...with
ind_body ind_body ::= ident [binders] : term := [[] constructor  ...  constructor] constructor ::= ident [binders] [:[>] term]
Especially, if the extra >
is present in a constructor
declaration, this constructor is declared as a coercion.

Command
Identity Coercion ident : class >> class
¶ If
C
is the source class andD
the destination, we check thatC
is a constant with a body of the formfun (x₁:T₁)..(xₙ:Tₙ) => D t₁..tₘ
where m is the number of parameters ofD
. Then we define an identity function with typeforall (x₁:T₁)..(xₙ:Tₙ)(y:C x₁..xₙ),D t₁..tₘ
, and we declare it as an identity coercion betweenC
andD
.
Variant
Local Identity Coercion ident : ident >> ident
Same as
Identity Coercion
but locally to the current section.

Variant
SubClass ident := type

Variant
:name: SubClass
If type is a class ident' applied to some arguments then ident is defined and an identity coercion of name Id_ident_ident' is declared. Otherwise said, this is an abbreviation for
Definition
ident:=
type.Identity Coercion
Id_ident_ident' : ident>>
ident'.

Variant
Displaying Available Coercions¶

Command
Print Classes
¶ Print the list of declared classes in the current context.

Command
Print Coercions
¶ Print the list of declared coercions in the current context.

Command
Print Graph
¶ Print the list of valid coercion paths in the current context.
Activating the Printing of Coercions¶

Flag
Printing Coercions
¶ When on, this option forces all the coercions to be printed. By default, coercions are not printed.

Table
Printing Coercion qualid
¶ Specifies a set of qualids for which coercions are always displayed. Use the
Add @table
andRemove @table
commands to update the set of qualids.
Classes as Records¶
We allow the definition of Structures with Inheritance (or classes as records)
by extending the existing Record
macro. Its new syntax is:

Variant
Record >? ident binders? : sort := ident? { ident :>? term+; }
The first identifier ident is the name of the defined record and sort is its type. The optional identifier after
:=
is the name of the constuctor (it will beBuild_
ident if not given). The other identifiers are the names of the fields, and the term are their respective types. If:>
is used instead of:
in the declaration of a field, then the name of this field is automatically declared as a coercion from the record name to the class of this field type. Remark that the fields always verify the uniform inheritance condition. If the optional>
is given before the record name, then the constructor name is automatically declared as a coercion from the class of the last field type to the record name (this may fail if the uniform inheritance condition is not satisfied).
Coercions and Sections¶
The inheritance mechanism is compatible with the section mechanism. The global classes and coercions defined inside a section are redefined after its closing, using their new value and new type. The classes and coercions which are local to the section are simply forgotten. Coercions with a local source class or a local target class, and coercions which do not verify the uniform inheritance condition any longer are also forgotten.
Coercions and Modules¶

Flag
Automatic Coercions Import
¶ Since Coq version 8.3, the coercions present in a module are activated only when the module is explicitly imported. Formerly, the coercions were activated as soon as the module was required, whether it was imported or not.
This option makes it possible to recover the behavior of the versions of Coq prior to 8.3.
Examples¶
There are three situations:
Coercion at function application¶
f a
is illtyped where f:forall x:A,B
and a:A'
. If there is a
coercion path between A'
and A
, then f a
is transformed into
f a'
where a'
is the result of the application of this
coercion path to a
.
We first give an example of coercion between atomic inductive types
 Definition bool_in_nat (b:bool) := if b then 0 else 1.
 bool_in_nat is defined
 Coercion bool_in_nat : bool >> nat.
 bool_in_nat is now a coercion
 Check (0 = true).
 0 = true : Prop
 Set Printing Coercions.
 Check (0 = true).
 0 = bool_in_nat true : Prop
 Unset Printing Coercions.
Warning
Note that Check true=O
would fail. This is "normal" behavior of
coercions. To validate true=O
, the coercion is searched from
nat
to bool
. There is none.
We give an example of coercion between classes with parameters.
 Parameters (C : nat > Set) (D : nat > bool > Set) (E : bool > Set).
 C is declared D is declared E is declared
 Parameter f : forall n:nat, C n > D (S n) true.
 f is declared
 Coercion f : C >> D.
 f is now a coercion
 Parameter g : forall (n:nat) (b:bool), D n b > E b.
 g is declared
 Coercion g : D >> E.
 g is now a coercion
 Parameter c : C 0.
 c is declared
 Parameter T : E true > nat.
 T is declared
 Check (T c).
 T c : nat
 Set Printing Coercions.
 Check (T c).
 T (g 1 true (f 0 c)) : nat
 Unset Printing Coercions.
We give now an example using identity coercions.
 Definition D' (b:bool) := D 1 b.
 D' is defined
 Identity Coercion IdD'D : D' >> D.
 Print IdD'D.
 IdD'D = (fun (b : bool) (x : D' b) => x) : forall b : bool, D' b > D 1 b : forall b : bool, D' b > D 1 b Argument scopes are [bool_scope _] IdD'D is a coercion
 Parameter d' : D' true.
 d' is declared
 Check (T d').
 T d' : nat
 Set Printing Coercions.
 Check (T d').
 T (g 1 true d') : nat
 Unset Printing Coercions.
In the case of functional arguments, we use the monotonic rule of
subtyping. To coerce t : forall x : A, B
towards
forall x : A', B'
, we have to coerce A'
towards A
and B
towards B'
. An example is given below:
 Parameters (A B : Set) (h : A > B).
 A is declared B is declared h is declared
 Coercion h : A >> B.
 h is now a coercion
 Parameter U : (A > E true) > nat.
 U is declared
 Parameter t : B > C 0.
 t is declared
 Check (U t).
 U (fun x : A => t x) : nat
 Set Printing Coercions.
 Check (U t).
 U (fun x : A => g 1 true (f 0 (t (h x)))) : nat
 Unset Printing Coercions.
Remark the changes in the result following the modification of the previous example.
 Parameter U' : (C 0 > B) > nat.
 U' is declared
 Parameter t' : E true > A.
 t' is declared
 Check (U' t').
 U' (fun x : C 0 => t' x) : nat
 Set Printing Coercions.
 Check (U' t').
 U' (fun x : C 0 => h (t' (g 1 true (f 0 x)))) : nat
 Unset Printing Coercions.
Coercion to a type¶
An assumption x:A
when A
is not a type, is illtyped. It is
replaced by x:A'
where A'
is the result of the application to
A
of the coercion path between the class of A
and
Sortclass
if it exists. This case occurs in the abstraction
fun x:A => t
, universal quantification forall x:A,B
, global
variables and parameters of (co)inductive definitions and
functions. In forall x:A,B
, such a coercion path may also be applied
to B
if necessary.
 Parameter Graph : Type.
 Graph is declared
 Parameter Node : Graph > Type.
 Node is declared
 Coercion Node : Graph >> Sortclass.
 Node is now a coercion
 Parameter G : Graph.
 G is declared
 Parameter Arrows : G > G > Type.
 Arrows is declared
 Check Arrows.
 Arrows : G > G > Type
 Parameter fg : G > G.
 fg is declared
 Check fg.
 fg : G > G
 Set Printing Coercions.
 Check fg.
 fg : Node G > Node G
 Unset Printing Coercions.
Coercion to a function¶
f a
is illtyped because f:A
is not a function. The term
f
is replaced by the term obtained by applying to f
the
coercion path between A
and Funclass
if it exists.
 Parameter bij : Set > Set > Set.
 bij is declared
 Parameter ap : forall A B:Set, bij A B > A > B.
 ap is declared
 Coercion ap : bij >> Funclass.
 ap is now a coercion
 Parameter b : bij nat nat.
 b is declared
 Check (b 0).
 b 0 : nat
 Set Printing Coercions.
 Check (b 0).
 ap nat nat b 0 : nat
 Unset Printing Coercions.
Let us see the resulting graph after all these examples.
 Print Graph.
 [bool_in_nat] : bool >> nat [f] : C >> D [f; g] : C >> E [g] : D >> E [IdD'D] : D' >> D [IdD'D; g] : D' >> E [h] : A >> B [Node] : Graph >> Sortclass [ap] : bij >> Funclass