\[\begin{split}\newcommand{\alors}{\textsf{then}} \newcommand{\alter}{\textsf{alter}} \newcommand{\as}{\kw{as}} \newcommand{\Assum}[3]{\kw{Assum}(#1)(#2:#3)} \newcommand{\bool}{\textsf{bool}} \newcommand{\case}{\kw{case}} \newcommand{\conc}{\textsf{conc}} \newcommand{\cons}{\textsf{cons}} \newcommand{\consf}{\textsf{consf}} \newcommand{\conshl}{\textsf{cons\_hl}} \newcommand{\Def}[4]{\kw{Def}(#1)(#2:=#3:#4)} \newcommand{\emptyf}{\textsf{emptyf}} \newcommand{\End}{\kw{End}} \newcommand{\kwend}{\kw{end}} \newcommand{\EqSt}{\textsf{EqSt}} \newcommand{\even}{\textsf{even}} \newcommand{\evenO}{\textsf{even}_\textsf{O}} \newcommand{\evenS}{\textsf{even}_\textsf{S}} \newcommand{\false}{\textsf{false}} \newcommand{\filter}{\textsf{filter}} \newcommand{\Fix}{\kw{Fix}} \newcommand{\fix}{\kw{fix}} \newcommand{\for}{\textsf{for}} \newcommand{\forest}{\textsf{forest}} \newcommand{\from}{\textsf{from}} \newcommand{\Functor}{\kw{Functor}} \newcommand{\haslength}{\textsf{has\_length}} \newcommand{\hd}{\textsf{hd}} \newcommand{\ident}{\textsf{ident}} \newcommand{\In}{\kw{in}} \newcommand{\Ind}[4]{\kw{Ind}[#2](#3:=#4)} \newcommand{\ind}[3]{\kw{Ind}~[#1]\left(#2\mathrm{~:=~}#3\right)} \newcommand{\Indp}[5]{\kw{Ind}_{#5}(#1)[#2](#3:=#4)} \newcommand{\Indpstr}[6]{\kw{Ind}_{#5}(#1)[#2](#3:=#4)/{#6}} \newcommand{\injective}{\kw{injective}} \newcommand{\kw}[1]{\textsf{#1}} \newcommand{\lb}{\lambda} \newcommand{\length}{\textsf{length}} \newcommand{\letin}[3]{\kw{let}~#1:=#2~\kw{in}~#3} \newcommand{\List}{\textsf{list}} \newcommand{\lra}{\longrightarrow} \newcommand{\Match}{\kw{match}} \newcommand{\Mod}[3]{{\kw{Mod}}({#1}:{#2}\,\zeroone{:={#3}})} \newcommand{\ModA}[2]{{\kw{ModA}}({#1}=={#2})} \newcommand{\ModS}[2]{{\kw{Mod}}({#1}:{#2})} \newcommand{\ModType}[2]{{\kw{ModType}}({#1}:={#2})} \newcommand{\mto}{.\;} \newcommand{\Nat}{\mathbb{N}} \newcommand{\nat}{\textsf{nat}} \newcommand{\Nil}{\textsf{nil}} \newcommand{\nilhl}{\textsf{nil\_hl}} \newcommand{\nO}{\textsf{O}} \newcommand{\node}{\textsf{node}} \newcommand{\nS}{\textsf{S}} \newcommand{\odd}{\textsf{odd}} \newcommand{\oddS}{\textsf{odd}_\textsf{S}} \newcommand{\ovl}[1]{\overline{#1}} \newcommand{\Pair}{\textsf{pair}} \newcommand{\plus}{\mathsf{plus}} \newcommand{\Prod}{\textsf{prod}} \newcommand{\Prop}{\textsf{Prop}} \newcommand{\return}{\kw{return}} \newcommand{\Set}{\textsf{Set}} \newcommand{\si}{\textsf{if}} \newcommand{\sinon}{\textsf{else}} \newcommand{\Sort}{\mathcal{S}} \newcommand{\Str}{\textsf{Stream}} \newcommand{\Struct}{\kw{Struct}} \newcommand{\subst}[3]{#1\{#2/#3\}} \newcommand{\tl}{\textsf{tl}} \newcommand{\tree}{\textsf{tree}} \newcommand{\trii}{\triangleright_\iota} \newcommand{\true}{\textsf{true}} \newcommand{\Type}{\textsf{Type}} \newcommand{\unfold}{\textsf{unfold}} \newcommand{\WEV}[3]{\mbox{$#1[] \vdash #2 \lra #3$}} \newcommand{\WEVT}[3]{\mbox{$#1[] \vdash #2 \lra$}\\ \mbox{$ #3$}} \newcommand{\WF}[2]{{\mathcal{W\!F}}(#1)[#2]} \newcommand{\WFE}[1]{\WF{E}{#1}} \newcommand{\WFT}[2]{#1[] \vdash {\mathcal{W\!F}}(#2)} \newcommand{\WFTWOLINES}[2]{{\mathcal{W\!F}}\begin{array}{l}(#1)\\\mbox{}[{#2}]\end{array}} \newcommand{\with}{\kw{with}} \newcommand{\WS}[3]{#1[] \vdash #2 <: #3} \newcommand{\WSE}[2]{\WS{E}{#1}{#2}} \newcommand{\WT}[4]{#1[#2] \vdash #3 : #4} \newcommand{\WTE}[3]{\WT{E}{#1}{#2}{#3}} \newcommand{\WTEG}[2]{\WTE{\Gamma}{#1}{#2}} \newcommand{\WTM}[3]{\WT{#1}{}{#2}{#3}} \newcommand{\zeroone}[1]{[{#1}]} \newcommand{\zeros}{\textsf{zeros}} \end{split}\]

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

Classes

A class with \(n\) parameters is any defined name with a type forall (ident1 : type1)..(identn:typen), sort. Thus a class with parameters is considered as a single class and not as a family of classes. An object of a class is any term of type class term1 .. termn. In addition to these user-defined classes, we have two built-in classes:

  • Sortclass, the class of sorts; its objects are the terms whose type is a sort (e.g. Prop or Type).
  • 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 classes is defined as:

class ::=  qualid
           | Sortclass
           | Funclass

Coercions

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

  • D is a user-defined class, then the type of f must have the form forall (x₁:A₁)..(xₙ:Aₙ)(y:C x₁..xₙ), D u₁..uₘ where \(m\) is the number of parameters of D.
  • D is Funclass, then the type of f must have the form forall (x₁:A₁)..(xₙ:Aₙ)(y:C x₁..xₙ)(x:A), B.
  • D is Sortclass, then the type of f must have the form forall (x₁:A₁)..(xₙ:Aₙ)(y:C x₁..xₙ), s with s a sort.

We then write f : C >-> D. The restriction on the type of coercions is called the uniform inheritance condition.

Note

The built-in class Sortclass can be used as a source class, but the built-in 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 qualid not declared.
Error qualid is already a coercion.
Error Funclass cannot be a source class.
Error qualid is not a function.
Error Cannot find the source class of qualid.
Error Cannot recognize class as a source class of qualid.
Error qualid does not respect the uniform inheritance condition.
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 type?

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

Variant Local Coercion ident := term type?

This defines ident just like Let ident := term type?, and then declares ident as a coercion between it source and its target.

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

Command Identity Coercion ident : class >-> class

If C is the source class and D the destination, we check that C is a constant with a body of the form fun (x₁:T₁)..(xₙ:Tₙ) => D t₁..tₘ where m is the number of parameters of D. Then we define an identity function with type forall (x₁:T₁)..(xₙ:Tₙ)(y:C x₁..xₙ),D t₁..tₘ, and we declare it as an identity coercion between C and D.

Error class must be a transparent constant.
Variant Local Identity Coercion ident : ident >-> ident

Same as Identity Coercion but locally to the current section.

Variant 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. Identity Coercion Id_ident_ident' : ident >-> ident'.

Variant Local SubClass ident := type

Same as before but locally to the current section.

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.

Command Print Coercion Paths class class

Print the list of valid coercion paths between the two given classes.

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 and Remove @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 constructor (it will be Build_ident if not given). The other identifiers are the names of the fields, and 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. Note 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).

Variant Structure >? ident binders? : sort := ident? { ident :>? term+; }

This is a synonym of Record.

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.

Warning Coercion used but not in scope: qualid. If you want to use this coercion, please Import the module that contains it.

This warning is emitted when typechecking relies on a coercion contained in a module that has not been explicitely imported. It helps migrating code and stop relying on the option above.

Examples

There are three situations:

Coercion at function application

f a is ill-typed 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 sub-typing. 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 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 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 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.

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