The Coq library¶
The Coq library is structured into two parts:
 The initial library: it contains elementary logical notions and datatypes. It constitutes the basic state of the system directly available when running Coq;
 The standard library: generalpurpose libraries containing various developments of Coq axiomatizations about sets, lists, sorting, arithmetic, etc. This library comes with the system and its modules are directly accessible through the
Require
command (see Section Compiled files);
In addition, userprovided libraries or developments are provided by Coq users' community. These libraries and developments are available for download at http://coq.inria.fr (see Section Users’ contributions).
This chapter briefly reviews the Coq libraries whose contents can also be browsed at http://coq.inria.fr/stdlib.
The basic library¶
This section lists the basic notions and results which are directly
available in the standard Coq system. Most of these constructions
are defined in the Prelude
module in directory theories/Init
at the Coq root directory; this includes the modules
Notations
,
Logic
,
Datatypes
,
Specif
,
Peano
,
Wf
and
Tactics
.
Module Logic_Type
also makes it in the initial state.
Notations¶
This module defines the parsing and prettyprinting of many symbols (infixes, prefixes, etc.). However, it does not assign a meaning to these notations. The purpose of this is to define and fix once for all the precedence and associativity of very common notations. The main notations fixed in the initial state are :
Notation  Precedence  Associativity 

_ > _ 
99  right 
_ <> _ 
95  no 
_ \/ _ 
85  right 
_ /\ _ 
80  right 
~ _ 
75  right 
_ = _ 
70  no 
_ = _ = _ 
70  no 
_ = _ :> _ 
70  no 
_ <> _ 
70  no 
_ <> _ :> _ 
70  no 
_ < _ 
70  no 
_ > _ 
70  no 
_ <= _ 
70  no 
_ >= _ 
70  no 
_ < _ < _ 
70  no 
_ < _ <= _ 
70  no 
_ <= _ < _ 
70  no 
_ <= _ <= _ 
70  no 
_ + _ 
50  left 
_  _ 
50  left 
_  _ 
50  left 
_ * _ 
40  left 
_ _ 
40  left 
_ / _ 
40  left 
 _ 
35  right 
/ _ 
35  right 
_ ^ _ 
30  right 
Logic¶
The basic library of Coq comes with the definitions of standard
(intuitionistic) logical connectives (they are defined as inductive
constructions). They are equipped with an appealing syntax enriching the
subclass form
of the syntactic class term
. The syntax of
form
is shown below:
form ::= True (True) False (False) ~form
(not)form
/\form
(and)form
\/form
(or)form
>form
(primitive implication)form
<>form
(iff) forallident
:type
,form
(primitive for all) existsident
[:specif
],form
(ex) exists2ident
[:specif
],form
&form
(ex2)term
=term
(eq)term
=term
:>specif
(eq)
Note
Implication is not defined but primitive (it is a nondependent product of a proposition over another proposition). There is also a primitive universal quantification (it is a dependent product over a proposition). The primitive universal quantification allows both firstorder and higherorder quantification.
Propositional Connectives¶
First, we find propositional calculus connectives:
Quantifiers¶
Then we find firstorder quantifiers:
 Definition all (A:Set) (P:A > Prop) := forall x:A, P x.
 all is defined
 Inductive ex (A: Set) (P:A > Prop) : Prop := ex_intro (x:A) (_:P x).
 ex is defined ex_ind is defined
 Inductive ex2 (A:Set) (P Q:A > Prop) : Prop := ex_intro2 (x:A) (_:P x) (_:Q x).
 ex2 is defined ex2_ind is defined
The following abbreviations are allowed:
exists x:A, P 
ex A (fun x:A => P) 
exists x, P 
ex _ (fun x => P) 
exists2 x:A, P & Q 
ex2 A (fun x:A => P) (fun x:A => Q) 
exists2 x, P & Q 
ex2 _ (fun x => P) (fun x => Q) 
The type annotation :A
can be omitted when A
can be
synthesized by the system.
Equality¶
Then, we find equality, defined as an inductive relation. That is,
given a type A
and an x
of type A
, the
predicate (eq A x)
is the smallest one which contains x
.
This definition, due to Christine PaulinMohring, is equivalent to
define eq
as the smallest reflexive relation, and it is also
equivalent to Leibniz' equality.
 Inductive eq (A:Type) (x:A) : A > Prop := eq_refl : eq A x x.
 eq is defined eq_rect is defined eq_ind is defined eq_rec is defined
Lemmas¶
Finally, a few easy lemmas are provided.
The theorem f_equal
is extended to functions with two to five
arguments. The theorem are names f_equal2
, f_equal3
,
f_equal4
and f_equal5
.
For instance f_equal3
is defined the following way.
 Theorem f_equal3 : forall (A1 A2 A3 B:Type) (f:A1 > A2 > A3 > B) (x1 y1:A1) (x2 y2:A2) (x3 y3:A3), x1 = y1 > x2 = y2 > x3 = y3 > f x1 x2 x3 = f y1 y2 y3.
 1 subgoal ============================ forall (A1 A2 A3 B : Type) (f : A1 > A2 > A3 > B) (x1 y1 : A1) (x2 y2 : A2) (x3 y3 : A3), x1 = y1 > x2 = y2 > x3 = y3 > f x1 x2 x3 = f y1 y2 y3
Datatypes¶
In the basic library, we find in Datatypes.v
the definition
of the basic datatypes of programming,
defined as inductive constructions over the sort Set
. Some of
them come with a special syntax shown below (this syntax table is common with
the next section Specification):
specif ::=specif
*specif
(prod)specif
+specif
(sum)specif
+ {specif
} (sumor) {specif
} + {specif
} (sumbool) {ident
:specif
form
} (sig) {ident
:specif
form
&form
} (sig2) {ident
:specif
&specif
} (sigT) {ident
:specif
&specif
&specif
} (sigT2) term ::= (term
,term
) (pair)
Programming¶
 Inductive unit : Set := tt.
 unit is defined unit_rect is defined unit_ind is defined unit_rec is defined
 Inductive bool : Set := true  false.
 bool is defined bool_rect is defined bool_ind is defined bool_rec is defined
 Inductive nat : Set := O  S (n:nat).
 nat is defined nat_rect is defined nat_ind is defined nat_rec is defined
 Inductive option (A:Set) : Set := Some (_:A)  None.
 option is defined option_rect is defined option_ind is defined option_rec is defined
 Inductive identity (A:Type) (a:A) : A > Type := refl_identity : identity A a a.
 identity is defined identity_rect is defined identity_ind is defined identity_rec is defined
Note that zero is the letter O
, and not the numeral 0
.
The predicate identity
is logically
equivalent to equality but it lives in sort Type
.
It is mainly maintained for compatibility.
We then define the disjoint sum of A+B
of two sets A
and
B
, and their product A*B
.
 Inductive sum (A B:Set) : Set := inl (_:A)  inr (_:B).
 sum is defined sum_rect is defined sum_ind is defined sum_rec is defined
 Inductive prod (A B:Set) : Set := pair (_:A) (_:B).
 prod is defined prod_rect is defined prod_ind is defined prod_rec is defined
 Section projections.
 Variables A B : Set.
 A is declared B is declared
 Definition fst (H: prod A B) := match H with  pair _ _ x y => x end.
 fst is defined
 Definition snd (H: prod A B) := match H with  pair _ _ x y => y end.
 snd is defined
 End projections.
Some operations on bool
are also provided: andb
(with
infix notation &&
), orb
(with
infix notation 
), xorb
, implb
and negb
.
Specification¶
The following notions defined in module Specif.v
allow to build new datatypes and specifications.
They are available with the syntax shown in the previous section Datatypes.
For instance, given A:Type
and P:A>Prop
, the construct
{x:A  P x}
(in abstract syntax (sig A P)
) is a
Type
. We may build elements of this set as (exist x p)
whenever we have a witness x:A
with its justification
p:P x
.
From such a (exist x p)
we may in turn extract its witness
x:A
(using an elimination construct such as match
) but
not its justification, which stays hidden, like in an abstract
datatype. In technical terms, one says that sig
is a weak
(dependent) sum. A variant sig2
with two predicates is also
provided.
 Inductive sig (A:Set) (P:A > Prop) : Set := exist (x:A) (_:P x).
 sig is defined sig_rect is defined sig_ind is defined sig_rec is defined
 Inductive sig2 (A:Set) (P Q:A > Prop) : Set := exist2 (x:A) (_:P x) (_:Q x).
 sig2 is defined sig2_rect is defined sig2_ind is defined sig2_rec is defined
A strong (dependent) sum {x:A & P x}
may be also defined,
when the predicate P
is now defined as a
constructor of types in Type
.
 Inductive sigT (A:Type) (P:A > Type) : Type := existT (x:A) (_:P x).
 sigT is defined sigT_rect is defined sigT_ind is defined sigT_rec is defined
 Section Projections2.
 Variable A : Type.
 A is declared
 Variable P : A > Type.
 P is declared
 Definition projT1 (H:sigT A P) := let (x, h) := H in x.
 projT1 is defined
 Definition projT2 (H:sigT A P) := match H return P (projT1 H) with existT _ _ x h => h end.
 projT2 is defined
 End Projections2.
 Inductive sigT2 (A: Type) (P Q:A > Type) : Type := existT2 (x:A) (_:P x) (_:Q x).
 sigT2 is defined sigT2_rect is defined sigT2_ind is defined sigT2_rec is defined
A related nondependent construct is the constructive sum
{A}+{B}
of two propositions A
and B
.
 Inductive sumbool (A B:Prop) : Set := left (_:A)  right (_:B).
 sumbool is defined sumbool_rect is defined sumbool_ind is defined sumbool_rec is defined
This sumbool
construct may be used as a kind of indexed boolean
datatype. An intermediate between sumbool
and sum
is
the mixed sumor
which combines A:Set
and B:Prop
in the construction A+{B}
in Set
.
 Inductive sumor (A:Set) (B:Prop) : Set :=  inleft (_:A)  inright (_:B).
 sumor is defined sumor_rect is defined sumor_ind is defined sumor_rec is defined
We may define variants of the axiom of choice, like in MartinLöf's Intuitionistic Type Theory.
The next construct builds a sum between a datatype A:Type
and
an exceptional value encoding errors:
 Definition Exc := option.
 Exc is defined
 Definition value := Some.
 value is defined
 Definition error := None.
 error is defined
This module ends with theorems, relating the sorts Set
or
Type
and Prop
in a way which is consistent with the
realizability interpretation.
Basic Arithmetics¶
The basic library includes a few elementary properties of natural
numbers, together with the definitions of predecessor, addition and
multiplication, in module Peano.v
. It also
provides a scope nat_scope
gathering standard notations for
common operations (+
, *
) and a decimal notation for
numbers, allowing for instance to write 3
for S (S (S O)))
. This also works on
the left hand side of a match
expression (see for example
section refine
). This scope is opened by default.
Example
The following example is not part of the standard library, but it shows the usage of the notations:
 Fixpoint even (n:nat) : bool := match n with  0 => true  1 => false  S (S n) => even n end.
 even is defined even is recursively defined (decreasing on 1st argument)
Now comes the content of module Peano
:
Finally, it gives the definition of the usual orderings le
,
lt
, ge
and gt
.
 Inductive le (n:nat) : nat > Prop :=  le_n : le n n  le_S : forall m:nat, n <= m > n <= (S m) where "n <= m" := (le n m) : nat_scope.
 Toplevel input, characters 0137: > Inductive le (n:nat) : nat > Prop :=  le_n : le n n  le_S : forall m:nat, n <= m > n <= (S m) where "n <= m" := (le n m) : nat_scope. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Warning: Notation "_ <= _" was already used in scope nat_scope. [notationoverridden,parsing] le is defined le_ind is defined Toplevel input, characters 0137: > Inductive le (n:nat) : nat > Prop :=  le_n : le n n  le_S : forall m:nat, n <= m > n <= (S m) where "n <= m" := (le n m) : nat_scope. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Warning: Notation "_ <= _" was already used in scope nat_scope.
 Definition lt (n m:nat) := S n <= m.
 lt is defined
 Definition ge (n m:nat) := m <= n.
 ge is defined
 Definition gt (n m:nat) := m < n.
 gt is defined
Properties of these relations are not initially known, but may be
required by the user from modules Le
and Lt
. Finally,
Peano
gives some lemmas allowing pattern matching, and a double
induction principle.
Wellfounded recursion¶
The basic library contains the basics of wellfounded recursion and
wellfounded induction, in module Wf.v
.
The automatically generated scheme Acc_rect
can be used to define functions by fixpoints using
wellfounded relations to justify termination. Assuming
extensionality of the functional used for the recursive call, the
fixpoint equation can be proved.
Accessing the Type level¶
The standard library includes Type
level definitions of counterparts of some
logic concepts and basic lemmas about them.
The module Datatypes
defines identity
, which is the Type
level counterpart
of equality:
 Inductive identity (A:Type) (a:A) : A > Type := identity_refl : identity A a a.
 identity is defined identity_rect is defined identity_ind is defined identity_rec is defined
Some properties of identity
are proved in the module Logic_Type
, which also
provides the definition of Type
level negation:
 Definition notT (A:Type) := A > False.
 notT is defined
Tactics¶
A few tactics defined at the user level are provided in the initial
state, in module Tactics.v
. They are listed at
http://coq.inria.fr/stdlib, in paragraph Init
, link Tactics
.
The standard library¶
Survey¶
The rest of the standard library is structured into the following subdirectories:
 Logic : Classical logic and dependent equality
 Arith : Basic Peano arithmetic
 PArith : Basic positive integer arithmetic
 NArith : Basic binary natural number arithmetic
 ZArith : Basic relative integer arithmetic
 Numbers : Various approaches to natural, integer and cyclic numbers (currently axiomatically and on top of 2^31 binary words)
 Bool : Booleans (basic functions and results)
 Lists : Monomorphic and polymorphic lists (basic functions and results), Streams (infinite sequences defined with coinductive types)
 Sets : Sets (classical, constructive, finite, infinite, power set, etc.)
 FSets : Specification and implementations of finite sets and finite maps (by lists and by AVL trees)
 Reals : Axiomatization of real numbers (classical, basic functions, integer part, fractional part, limit, derivative, Cauchy series, power series and results,...)
 Relations : Relations (definitions and basic results)
 Sorting : Sorted list (basic definitions and heapsort correctness)
 Strings : 8bits characters and strings
 Wellfounded : Wellfounded relations (basic results)
These directories belong to the initial load path of the system, and
the modules they provide are compiled at installation time. So they
are directly accessible with the command Require
(see
Section Compiled files).
The different modules of the Coq standard library are documented online at http://coq.inria.fr/stdlib.
Peano’s arithmetic (nat)¶
While in the initial state, many operations and predicates of Peano's
arithmetic are defined, further operations and results belong to other
modules. For instance, the decidability of the basic predicates are
defined here. This is provided by requiring the module Arith
.
The following table describes the notations available in scope
nat_scope
:
Notation  Interpretation 

_ < _ 
lt 
_ <= _ 
le 
_ > _ 
gt 
_ >= _ 
ge 
x < y < z 
x < y /\ y < z 
x < y <= z 
x < y /\ y <= z 
x <= y < z 
x <= y /\ y < z 
x <= y <= z 
x <= y /\ y <= z 
_ + _ 
plus 
_  _ 
minus 
_ * _ 
mult 
Notations for integer arithmetics¶
The following table describes the syntax of expressions
for integer arithmetics. It is provided by requiring and opening the module ZArith
and opening scope Z_scope
.
It specifies how notations are interpreted and, when not
already reserved, the precedence and associativity.
Notation  Interpretation  Precedence  Associativity 

_ < _ 
Z.lt 

_ <= _ 
Z.le 

_ > _ 
Z.gt 

_ >= _ 
Z.ge 

x < y < z 
x < y /\ y < z 

x < y <= z 
x < y /\ y <= z 

x <= y < z 
x <= y /\ y < z 

x <= y <= z 
x <= y /\ y <= z 

_ ?= _ 
Z.compare 
70  no 
_ + _ 
Z.add 

_  _ 
Z.sub 

_ * _ 
Z.mul 

_ / _ 
Z.div 

_ mod _ 
Z.modulo 
40  no 
 _ 
Z.opp 

_ ^ _ 
Z.pow 
Example
 Require Import ZArith.
 [Loading ML file newring_plugin.cmxs ... done] [Loading ML file omega_plugin.cmxs ... done]
 Check (2 + 3)%Z.
 (2 + 3)%Z : Z
 Open Scope Z_scope.
 Check 2 + 3.
 2 + 3 : Z
Real numbers library¶
Notations for real numbers¶
This is provided by requiring and opening the module Reals
and
opening scope R_scope
. This set of notations is very similar to
the notation for integer arithmetics. The inverse function was added.
Notation  Interpretation 

_ < _ 
Rlt 
_ <= _ 
Rle 
_ > _ 
Rgt 
_ >= _ 
Rge 
x < y < z 
x < y /\ y < z 
x < y <= z 
x < y /\ y <= z 
x <= y < z 
x <= y /\ y < z 
x <= y <= z 
x <= y /\ y <= z 
_ + _ 
Rplus 
_  _ 
Rminus 
_ * _ 
Rmult 
_ / _ 
Rdiv 
 _ 
Ropp 
/ _ 
Rinv 
_ ^ _ 
pow 
Example
 Require Import Reals.
 [Loading ML file r_syntax_plugin.cmxs ... done] [Loading ML file micromega_plugin.cmxs ... done]
 Check (2 + 3)%R.
 (2 + 3)%R : R
 Open Scope R_scope.
 Check 2 + 3.
 2 + 3 : R
Some tactics for real numbers¶
In addition to the powerful ring
, field
and lra
tactics (see Chapter Tactics), there are also:

discrR
¶ Proves that two real integer constants are different.
Example
 Require Import DiscrR.
 Open Scope R_scope.
 Goal 5 <> 0.
 1 subgoal ============================ 5 <> 0
 discrR.

split_Rabs
¶ Allows unfolding the
Rabs
constant and splits corresponding conjunctions.
Example
 Require Import Reals.
 Open Scope R_scope.
 Goal forall x:R, x <= Rabs x.
 1 subgoal ============================ forall x : R, x <= Rabs x
 intro; split_Rabs.
 2 subgoals x : R Hlt : x < 0 ============================ x <=  x subgoal 2 is: x <= x

split_Rmult
¶ Splits a condition that a product is non null into subgoals corresponding to the condition on each operand of the product.
Example
 Require Import Reals.
 Open Scope R_scope.
 Goal forall x y z:R, x * y * z <> 0.
 1 subgoal ============================ forall x y z : R, x * y * z <> 0
 intros; split_Rmult.
 3 subgoals x, y, z : R ============================ x <> 0 subgoal 2 is: y <> 0 subgoal 3 is: z <> 0
These tactics has been written with the tactic language L
_{tac}
described in Chapter The tactic language.
List library¶
Some elementary operations on polymorphic lists are defined here.
They can be accessed by requiring module List
.
It defines the following notions:
length
head
: first element (with default)tail
: all but first elementapp
: concatenationrev
: reversenth
: accessing nth element (with default)map
: applying a functionflat_map
: applying a function returning listsfold_left
: iterator (from head to tail)fold_right
: iterator (from tail to head)
The following table shows notations available when opening scope list_scope
.
Notation  Interpretation  Precedence  Associativity 

_ ++ _ 
app 
60  right 
_ :: _ 
cons 
60  right 
Users’ contributions¶
Numerous users' contributions have been collected and are available at URL http://coq.inria.fr/opam/www/. On this web page, you have a list of all contributions with informations (author, institution, quick description, etc.) and the possibility to download them one by one. You will also find informations on how to submit a new contribution.