# Coq Reference Manual

# Chapter 18 Implicit Coercions

- 18.1 General Presentation
- 18.2 Classes
- 18.3 Coercions
- 18.4 Identity Coercions
- 18.5 Inheritance Graph
- 18.6 Declaration of Coercions
- 18.7 Displaying Available Coercions
- 18.8 Activating the Printing of Coercions
- 18.9 Classes as Records
- 18.10 Coercions and Sections
- 18.11 Coercions and Modules
- 18.12 Examples

Amokrane Saïbi

## 18.1 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 where f:forall x:A, B and a:A′ when A′ can be seen in some sense as a subtype of A.
- x:A when A is not a type, but can be seen in a certain sense as a type: set, group, category etc.
- f a when f is not a function, but can be seen in a certain sense as a function: bijection, functor, any structure morphism etc.

## 18.2 Classes

A class with n parameters is any defined name with a type forall (x1:A1)..(xn:An), 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 t1 .. tn. In addition to these user-classes, we have two abstract classes:

- Sortclass, the class of sorts; its objects are the terms whose type is a sort.
- Funclass, the class of functions; its objects are all the terms with a functional type, i.e. of form forall x:A, B.

Formally, the syntax of a classes is defined on Figure 18.1.

> class | ::= | qualid
> | | | Sortclass
> | | | Funclass

> Figure 18.1: Syntax of classes

## 18.3 Coercions

A name f can be declared as a coercion between a source user-class C with n parameters and a target class D if one of these conditions holds:

- D is a user-class, then the type of f must have the form forall (x1 : A1)..(xn : An)(y: C x1..xn), D u1..um where m is the number of parameters of D.
- D is Funclass, then the type of f must have the form forall (x1: A1)..(xn: An)(y: C x1..xn)(x:A), B.
- D is Sortclass, then the type of f must have the form forall (x1: A1)..(xn: An)(y: C x1..xn), s with s a sort.

We then write f:C >-> D. The restriction on the type of coercions is called the uniform inheritance condition. Remark that the abstract classes Funclass and Sortclass cannot be source classes.

To coerce an object t:C t1..tn of C towards D, we have to apply the coercion f to it; the obtained term f t1..tn t is then an object of D.

## 18.4 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 (x1:T1)..(xk:Tk)(y:C u1..un), D v1..vm 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 (x1:T1)..(xk:Tk) => C u1..un

We then define an identity coercion between C′ and C:

Id_C′_C | := | fun (x1:T1)..(xk:Tk)(y:C′ x1..xk) => (y:C u1..un)

We can now declare f as coercion from C′ to D, since we can “cast” its type as forall (x1:T1)..(xk:Tk)(y:C′ x1..xk),D v1..vm.
The identity coercions have a special status: to coerce an object t:C′ t1..tk of C′ towards C, we does not have to insert explicitly Id_C′_C since Id_C′_C t1..tk t is convertible with t. However we “rewrite” the type of t to become an object of C; in this case, it becomes C u1*..uk* where each ui* is the result of the substitution in ui of the variables xj by tj.

## 18.5 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 [f1;..;fk]:C >-> D is the coercion path composed by the coercions f1..fk. The application of a coercion path to a term consists of the successive application of its coercions.

## 18.6 Declaration of Coercions

### 18.6.1 Coercion qualid : class1 >-> class2.

Declares the construction denoted by qualid as a coercion between class1 and class2.

Error messages:

1. qualid not declared
2. qualid is already a coercion
3. Funclass cannot be a source class
4. Sortclass cannot be a source class
5. qualid is not a function
6. Cannot find the source class of qualid
7. Cannot recognize class1 as a source class of qualid
8. qualid does not respect the uniform inheritance condition
9. Found target class class instead of class2

When the coercion qualid is added to the inheritance graph, non valid coercion paths are ignored; they are signaled by a warning.
Warning :

1. Ambiguous paths: | [f11;..;fn11] : C1>->D1
   | ...
   | [f1m;..;fnmm] : Cm>->Dm

Variants:

1. Local Coercion qualid : class1 >-> class2.
   Declares the construction denoted by qualid as a coercion local to the current section.
2. Coercion ident := term
   This defines ident just like Definition ident := term, and then declares ident as a coercion between it source and its target.
3. Coercion ident := term : type
   This defines ident just like Definition ident : type := term, and then declares ident as a coercion between it source and its target.
4. Local Coercion ident := term
   This defines ident just like Let ident := term, and then declares ident as a coercion between it source and its target.
5. Assumptions can be declared as coercions at declaration time. This extends the grammar of assumptions from Figure 1.3 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.

6. 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 1.3 as follows:

   inductive | ::= | Inductive ind_body with … with ind_body .
   | | | CoInductive ind_body with … 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.

### 18.6.2 Identity Coercion ident:class1 >-> class2.

We check that class1 is a constant with a value of the form fun (x1:T1)..(xn:Tn) => (class2 t1..tm) where m is the number of parameters of class2. Then we define an identity function with the type forall (x1:T1)..(xn:Tn)(y:class1 x1..xn), class2 t1..tm, and we declare it as an identity coercion between class1 and class2.

Error messages:

1. class1 must be a transparent constant

Variants:

1. Local Identity Coercion ident:ident1 >-> ident2.
   Idem but locally to the current section.
2. SubClass ident := type.
   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.

   followed by

   Identity Coercion Id_ident_ident’:ident >-> ident’.

3. Local SubClass ident := type.
   Same as before but locally to the current section.

## 18.7 Displaying Available Coercions

### 18.7.1 Print Classes.

Print the list of declared classes in the current context.

### 18.7.2 Print Coercions.

Print the list of declared coercions in the current context.

### 18.7.3 Print Graph.

Print the list of valid coercion paths in the current context.

### 18.7.4 Print Coercion Paths class1 class2.

Print the list of valid coercion paths from class1 to class2.

## 18.8 Activating the Printing of Coercions

### 18.8.1 Set Printing Coercions.

This command forces all the coercions to be printed. Conversely, to skip the printing of coercions, use Unset Printing Coercions. By default, coercions are not printed.

### 18.8.2 Set Printing Coercion qualid.

This command forces coercion denoted by qualid to be printed. To skip the printing of coercion qualid, use Unset Printing Coercion qualid. By default, a coercion is never printed.

## 18.9 Classes as Records

We allow the definition of Structures with Inheritance (or classes as records) by extending the existing Record macro (see Section 2.1). Its new syntax is:

Record [>] ident [binders] : sort := [ident0] `{`
ident1 [:|:>] term1 ;
...
identn [:|:>] termn `}`.

The identifier ident is the name of the defined record and sort is its type. The identifier ident0 is the name of its constructor. The identifiers ident1, .., identn are the names of its fields and term1, .., termn their respective types. The alternative [:|:>] is “:” or “:>”. If identi:>termi, then identi is automatically declared as coercion from ident to the class of termi. Remark that identi always verifies the uniform inheritance condition. If the optional “>” before ident is present, then ident0 (or the default name Build_ident if ident0 is omitted) is automatically declared as a coercion from the class of termn to ident (this may fail if the uniform inheritance condition is not satisfied).

Remark: The keyword Structure is a synonym of Record.

## 18.10 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.

## 18.11 Coercions and Modules

From 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, whatever it was imported or not.

To recover the behavior of the versions of Coq prior to 8.3, use the following command:

    Set Automatic Coercions Import.

To cancel the effect of the option, use instead:

    Unset Automatic Coercions Import.

## 18.12 Examples

There are three situations:

- f a is ill-typed where f:forall x:A,B and a:A′. If there is a coercion path between A′ and A, 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

  Coq < Definition bool_in_nat (b:bool) := if b then 0 else 1.
  bool_in_nat is defined
  Coq < Coercion bool_in_nat : bool >-> nat.
  bool_in_nat is now a coercion
  Coq < Check (0 = true).
  0 = true
  : Prop
  Coq < Set Printing Coercions.
  Coq < Check (0 = true).
  0 = bool_in_nat true
  : Prop

  Warning: “`Check true=O.`” fails. This is “normal” behaviour 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.

  Coq < Parameters
  Coq < (C : nat -> Set) (D : nat -> bool -> Set) (E : bool -> Set).
  C is assumed
  D is assumed
  E is assumed
  Coq < Parameter f : forall n:nat, C n -> D (S n) true.
  f is assumed
  Coq < Coercion f : C >-> D.
  f is now a coercion
  Coq < Parameter g : forall (n:nat) (b:bool), D n b -> E b.
  g is assumed
  Coq < Coercion g : D >-> E.
  g is now a coercion
  Coq < Parameter c : C 0.
  c is assumed
  Coq < Parameter T : E true -> nat.
  T is assumed
  Coq < Check (T c).
  T c
  : nat
  Coq < Set Printing Coercions.
  Coq < Check (T c).
  T (g 1 true (f 0 c))
  : nat

  We give now an example using identity coercions.

  Coq < Definition D’ (b:bool) := D 1 b.
  D’ is defined
  Coq < Identity Coercion IdD’D : D’ >-> D.
  Coq < 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 _]
  Coq < Parameter d’ : D’ true.
  d’ is assumed
  Coq < Check (T d’).
  T d’
  : nat
  Coq < Set Printing Coercions.
  Coq < Check (T d’).
  T (g 1 true d’)
  : nat

  In the case of functional arguments, we use the monotonic rule of sub-typing. Approximatively, to coerce t:forall x:A, B towards forall x:A′,B′, one have to coerce A′ towards A and B towards B′. An example is given below:

  Coq < Parameters (A B : Set) (h : A -> B).
  A is assumed
  B is assumed
  h is assumed
  Coq < Coercion h : A >-> B.
  h is now a coercion
  Coq < Parameter U : (A -> E true) -> nat.
  U is assumed
  Coq < Parameter t : B -> C 0.
  t is assumed
  Coq < Check (U t).
  U (fun x : A => t x)
  : nat
  Coq < Set Printing Coercions.
  Coq < Check (U t).
  U (fun x : A => g 1 true (f 0 (t (h x))))
  : nat

  Remark the changes in the result following the modification of the previous example.

  Coq < Parameter U’ : (C 0 -> B) -> nat.
  U’ is assumed
  Coq < Parameter t’ : E true -> A.
  t’ is assumed
  Coq < Check (U’ t’).
  U’ (fun x : C 0 => t’ x)
  : nat
  Coq < Set Printing Coercions.
  Coq < Check (U’ t’).
  U’ (fun x : C 0 => h (t’ (g 1 true (f 0 x))))
  : nat
- An assumption x:A when A is not a type, is ill-typed. 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 be applied to B also if necessary.
  Coq < Parameter Graph : Type.
  Graph is assumed
  Coq < Parameter Node : Graph -> Type.
  Node is assumed
  Coq < Coercion Node : Graph >-> Sortclass.
  Node is now a coercion
  Coq < Parameter G : Graph.
  G is assumed
  Coq < Parameter Arrows : G -> G -> Type.
  Arrows is assumed
  Coq < Check Arrows.
  Arrows
  : G -> G -> Type
  Coq < Parameter fg : G -> G.
  fg is assumed
  Coq < Check fg.
  fg
  : G -> G
  Coq < Set Printing Coercions.
  Coq < Check fg.
  fg
  : Node G -> Node G
- f a is ill-typed 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.
  Coq < Parameter bij : Set -> Set -> Set.
  bij is assumed
  Coq < Parameter ap : forall A B:Set, bij A B -> A -> B.
  ap is assumed
  Coq < Coercion ap : bij >-> Funclass.
  ap is now a coercion
  Coq < Parameter b : bij nat nat.
  b is assumed
  Coq < Check (b 0).
  b 0
  : nat
  Coq < Set Printing Coercions.
  Coq < Check (b 0).
  ap nat nat b 0
  : nat

  Let us see the resulting graph of this session.

  Coq < Print Graph.
  [sigT_of_sig; sig_of_sigT] : sig >-> sig
  [sigT_of_sig] : sig >-> sigT
  [sig_of_sigT] : sigT >-> sig
  [sig_of_sigT; sigT_of_sig] : sigT >-> sigT
  [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