Typing rules¶
The underlying formal language of Coq is a Calculus of Inductive Constructions (CIC) whose inference rules are presented in this chapter. The history of this formalism as well as pointers to related work are provided in a separate chapter; see Early history of Coq.
The terms¶
The expressions of the CIC are terms and all terms have a type.
There are types for functions (or programs), there are atomic types
(especially datatypes)... but also types for proofs and types for the
types themselves. Especially, any object handled in the formalism must
belong to a type. For instance, universal quantification is relative
to a type and takes the form “for all x of type
Terms are built from sorts, variables, constants, abstractions, applications, local definitions, and products. From a syntactic point of view, types cannot be distinguished from terms, except that they cannot start by an abstraction or a constructor. More precisely the language of the Calculus of Inductive Constructions is built from the following rules.
the sorts
, , , are terms.variables, hereafter ranged over by letters
, , etc., are termsconstants, hereafter ranged over by letters
, , etc., are terms.if
is a variable and , are terms then (forall x:T, U
in Coq concrete syntax) is a term. If occurs in , reads as “for all of type , ”. As depends on , one says that is a dependent product. If does not occur in then reads as “if then ”. A non-dependent product can be written: .if
is a variable and , are terms then (fun x:T => u
in Coq concrete syntax) is a term. This is a notation for the λ-abstraction of λ-calculus [Bar81]. The term is a function which maps elements of to the expression .if
and are terms then is a term (t u
in Coq concrete syntax). The term reads as “ applied to ”.if
is a variable, and , and are terms then is a term which denotes the term where the variable is locally bound to of type . This stands for the common “let-in” construction of functional programs such as ML or Scheme.
Free variables.
The notion of free variables is defined as usual. In the expressions
Substitution.
The notion of substituting a term
The logical vs programming readings. The constructions of the CIC can be used to express both logical and programming notions, according to the Curry-Howard correspondence between proofs and programs, and between propositions and types [CFC58][How80][dB72].
For instance, let us assume that True
is the always true
proposition. Then
Let us assume that mult
is a function of type eqnat
a
predicate of type fun x:nat => mult x x
in Coq notation) but may build also predicates over the natural
numbers. For instance fun x:nat => eqnat x 0
in Coq notation) will represent the predicate of one variable
Furthermore forall x:nat, P x
will represent the type of functions
which associate with each natural number
Typing rules¶
As objects of type theory, terms are subjected to type discipline. The well typing of a term depends on a local context and a global environment.
Local context.
A local context is an ordered list of declarations of variables.
The declaration of a variable
Global environment. A global environment is an ordered list of declarations. Global declarations are either assumptions, definitions or declarations of inductive objects. Inductive objects declare both constructors and inductive or coinductive types (see Section Theory of inductive definitions).
In the global environment,
assumptions are written as
The rules for inductive definitions (see Section
Theory of inductive definitions) have to be considered as assumption
rules in which the following definitions apply: if the name
Typing rules.
In the following, we define simultaneously two judgments. The first
one
A term
- W-Empty
- W-Local-Assum
- W-Local-Def
- W-Global-Assum
- W-Global-Def
- Ax-SProp
- Ax-Prop
- Ax-Set
- Ax-Type
- Var
- Const
- Prod-SProp
- Prod-Prop
- Prod-Set
- Prod-Type
- Lam
- App
- Let
Note
Prod-Prop and Prod-Set typing-rules make sense if we consider the
semantic difference between
All values of a type that has a sort
are extractable.No values of a type that has a sort
are extractable.
Note
We may have
Subtyping rules¶
At the moment, we did not take into account one rule between universes
which says that any term in a universe of index
if
then ,if
then ,for any
, , , hence, by transitivity, , for any (note: is not related by cumulativity to any other term)if
and then .if
is a universe polymorphic and cumulative (see Chapter Polymorphic Universes) inductive type (see below) and and are two different instances of the same inductive type (differing only in universe levels) with constructorsand
respectively then
(notice that
and are both fully applied, i.e., they have a sort as a type) iffor
and we haveand
where
and .
The conversion rule up to subtyping is now exactly:
- Conv
Normal form. A term which cannot be any more reduced is said to be in normal
form. There are several ways (or strategies) to apply the reduction
rules. Among them, we have to mention the head reduction which will
play an important role (see Chapter Tactics). Any term
Iterating the process of head reduction until the head of the reduced
term is no more an abstraction leads to the β-head normal form of
where
The Calculus of Inductive Constructions with impredicative Set¶
Coq can be used as a type checker for the Calculus of Inductive
Constructions with an impredicative sort -impredicative-set
. For example, using the ordinary coqtop
command, the following is rejected,
Example
- Fail Definition id: Set := forall X:Set,X->X.
- The command has indeed failed with message: The term "forall X : Set, X -> X" has type "Type" while it is expected to have type "Set" (universe inconsistency: Cannot enforce Set+1 <= Set).
while it will type check, if one uses instead the coqtop
-impredicative-set
option..
The major change in the theory concerns the rule for product formation
in the sort
- ProdImp
This extension has consequences on the inductive definitions which are
allowed. In the impredicative system, one can build so-called large
inductive definitions like the example of second-order existential
quantifier (exSet
).
There should be restrictions on the eliminations which can be
performed on such definitions. The elimination rules in the
impredicative system for sort
- Set1
- Set2