Chapter 17  Implicit Coercions

Amokrane Saïbi

17.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:

17.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:

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


class::=qualid
 |Sortclass
 |Funclass
Figure 17.1: Syntax of classes

17.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:

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.

17.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.

17.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.

17.6  Declaration of Coercions

17.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.

17.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.

17.7  Displaying Available Coercions

17.7.1  Print Classes.

Print the list of declared classes in the current context.

17.7.2  Print Coercions.

Print the list of declared coercions in the current context.

17.7.3  Print Graph.

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

17.7.4  Print Coercion Paths class1 class2.

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

17.8  Activating the Printing of Coercions

17.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.

17.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.

17.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.

17.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.

17.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.

17.12  Examples

There are three situations: