We have seen that the version of the Calculus of Constructions that
has been implemented in Lean includes dependent function types,
inductive types, and a hierarchy of universes that starts with an
impredicative, proof-irrelevantProp at the bottom. In this
chapter, we consider ways of extending the CIC with additional axioms
and rules. Extending a foundational system in such a way is often
convenient; it can make it possible to prove more theorems, as well as
make it easier to prove theorems that could have been proved
otherwise. But there can be negative consequences of adding additional
axioms, consequences which may go beyond concerns about their
correctness. In particular, the use of axioms bears on the
computational content of definitions and theorems, in ways we will
explore here.
Lean is designed to support both computational and classical
reasoning. Users that are so inclined can stick to a “computationally
pure” fragment, which guarantees that closed expressions in the system
evaluate to canonical normal forms. In particular, any closed
computationally pure expression of type Nat, for example, will
reduce to a numeral.
Lean's standard library defines an additional axiom, propositional
extensionality, and a quotient construction which in turn implies the
principle of function extensionality. These extensions are used, for
example, to develop theories of sets and finite sets. We will see
below that using these theorems can block evaluation in Lean's kernel,
so that closed terms of type Nat no longer evaluate to numerals. But
Lean erases types and propositional information when compiling
definitions to executable code, and since
these axioms only add new propositions, they are compatible with that
computational interpretation. Even computationally inclined users may
wish to use the classical law of the excluded middle to reason about
computation. This also blocks evaluation in the kernel, but it is
compatible with compiled code.
The standard library also defines a choice principle that is entirely
antithetical to a computational interpretation, since it magically
produces “data” from a proposition asserting its existence. Its use is
essential to some classical constructions, and users can import it
when needed. But expressions that use this construction to produce
data do not have computational content, and in Lean we are required to
mark such definitions as noncomputable to flag that fact.
Using a clever trick (known as Diaconescu's theorem), one can use
propositional extensionality, function extensionality, and choice to
derive the law of the excluded middle. As noted above, however, use of
the law of the excluded middle is still compatible with
compilation, as are other classical principles, as
long as they are not used to manufacture data.
To summarize, then, on top of the underlying framework of universes,
dependent function types, and inductive types, the standard library
adds three additional components:
the axiom of propositional extensionality
a quotient construction, which implies function extensionality
a choice principle, which produces data from an existential proposition.
The first two of these block normalization within Lean, but are
compatible with code generation, whereas the third is not amenable
to computational interpretation. We will spell out the details more
precisely below.
For most of its history, mathematics was essentially computational:
geometry dealt with constructions of geometric objects, algebra was
concerned with algorithmic solutions to systems of equations, and
analysis provided means to compute the future behavior of systems
evolving over time. From the proof of a theorem to the effect that
“for every x, there is a y such that ...”, it was generally
straightforward to extract an algorithm to compute such a y given
x.
In the nineteenth century, however, increases in the complexity of
mathematical arguments pushed mathematicians to develop new styles of
reasoning that suppress algorithmic information and invoke
descriptions of mathematical objects that abstract away the details of
how those objects are represented. The goal was to obtain a powerful
“conceptual” understanding without getting bogged down in
computational details, but this had the effect of admitting
mathematical theorems that are simply false on a direct
computational reading.
There is still fairly uniform agreement today that computation is
important to mathematics. But there are different views as to how best
to address computational concerns. From a constructive point of
view, it is a mistake to separate mathematics from its computational
roots; every meaningful mathematical theorem should have a direct
computational interpretation. From a classical point of view, it is
more fruitful to maintain a separation of concerns: we can use one
language and body of methods to write computer programs, while
maintaining the freedom to use nonconstructive theories and methods
to reason about them. Lean is designed to support both of these
approaches. Core parts of the library are developed constructively,
but the system also provides support for carrying out classical
mathematical reasoning.
Computationally, the purest part of dependent type theory avoids the
use of Prop entirely. Inductive types and dependent function types
can be viewed as data types, and terms of these types can be
“evaluated” by applying reduction rules until no more rules can be
applied. In principle, any closed term (that is, term with no free
variables) of type Nat should evaluate to a numeral, succ(…(succzero)…).
Introducing a proof-irrelevant Prop and marking theorems
irreducible represents a first step towards separation of
concerns. The intention is that elements of a type p : Prop should
play no role in computation, and so the particular construction of a
term prf : p is “irrelevant” in that sense. One can still define
computational objects that incorporate elements of type Prop; the
point is that these elements can help us reason about the effects of
the computation, but can be ignored when we extract “code” from the
term. Elements of type Prop are not entirely innocuous,
however. They include equations s=t:α for any type α, and
such equations can be used as casts, to type check terms. Below, we
will see examples of how such casts can block computation in the
system. However, computation is still possible under an evaluation
scheme that erases propositional content, ignores intermediate typing
constraints, and reduces terms until they reach a normal form. This is
precisely what Lean's virtual machine does.
Having adopted a proof-irrelevant Prop, one might consider it
legitimate to use, for example, the law of the excluded middle,
p∨¬p, where p is any proposition. Of course, this, too, can block
computation according to the rules of CIC, but it does not prevent the generation
of executable code, as described above. It is only the choice
principles discussed in the section on choice that completely erase the
distinction between the proof-irrelevant and data-relevant parts of
the theory.
It asserts that when two propositions imply one another, they are
actually equal. This is consistent with set-theoretic interpretations
in which any element a : Prop is either empty or the singleton set
\{\ast\}, for some distinguished element \ast. The axiom has the
effect that equivalent propositions can be substituted for one another
in any context:
Similar to propositional extensionality, function extensionality
asserts that any two functions of type (x:α)→βx that agree on
all their inputs are equal:
From a classical, set-theoretic perspective, this is exactly what it
means for two functions to be equal. This is known as an “extensional”
view of functions. From a constructive perspective, however, it is
sometimes more natural to think of functions as algorithms, or
computer programs, that are presented in some explicit way. It is
certainly the case that two computer programs can compute the same
answer for every input despite the fact that they are syntactically
quite different. In much the same way, you might want to maintain a
view of functions that does not force you to identify two functions
that have the same input / output behavior. This is known as an
“intensional” view of functions.
In fact, function extensionality follows from the existence of
quotients, which we describe in the next section. In the Lean standard
library, therefore, funext is thus
proved from the quotient construction.
Suppose that for α:Typeu we define the Setα:=α→Prop to
denote the type of subsets of α, essentially identifying subsets
with predicates. By combining funext and propext, we obtain an
extensional theory of such sets:
defempty:Setα:=fun_=>Falsenotation(priority:=high)"∅"=>emptydefinter(ab:Setα):Setα:=funx=>x∈a∧x∈binfix:70" ∩ "=>intertheoreminter_self(a:Setα):a∩a=a:=setextfunVariable name `x` is not explicitly referenced.Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:[apply]_xNote: This linter can be disabled with `set_option linter.unusedVariables false`x=>Iff.intro(fun⟨h,_⟩=>h)(funh=>⟨h,h⟩)theoreminter_empty(a:Setα):a∩∅=∅:=setextfun_=>Iff.intro(fun⟨_,h⟩=>h)(funh=>False.elimh)theoremempty_inter(a:Setα):∅∩a=∅:=setextfun_=>Iff.intro(fun⟨h,_⟩=>h)(funh=>False.elimh)theoreminter.comm(ab:Setα):a∩b=b∩a:=setextfun_=>Iff.intro(fun⟨h₁,h₂⟩=>⟨h₂,h₁⟩)(fun⟨h₁,h₂⟩=>⟨h₂,h₁⟩)
endSet
The following is an example of how function extensionality blocks
computation inside the Lean kernel:
deff(x:Nat):=xdefg(x:Nat):=0+xtheoremf_eq_g:f=g:=funextfunx=>(Nat.zero_addx).symmdefval:Nat:=Eq.recOn(motive:=fun__=>Nat)f_eq_g0-- does not reduce to 0f_eq_g▸0#reduceval
First, we show that the two functions f and g are equal using
function extensionality, and then we cast 0 of type Nat by
replacing f by g in the type. Of course, the cast is
vacuous, because Nat does not depend on f. But that is enough
to do the damage: under the computational rules of the system, we now
have a closed term of Nat that does not reduce to a numeral. In this
case, we may be tempted to reduce the expression to 0. But in
nontrivial examples, eliminating cast changes the type of the term,
which might make an ambient expression type incorrect. The virtual
machine, however, has no trouble evaluating the expression to
0. Here is a similarly contrived example that shows how
propext can get in the way:
Current research programs, including work on observational type
theory and cubical type theory, aim to extend type theory in ways
that permit reductions for casts involving function extensionality,
quotients, and more. But the solutions are not so clear-cut, and the
rules of Lean's underlying calculus do not sanction such reductions.
In a sense, however, a cast does not change the meaning of an
expression. Rather, it is a mechanism to reason about the expression's
type. Given an appropriate semantics, it then makes sense to reduce
terms in ways that preserve their meaning, ignoring the intermediate
bookkeeping needed to make the reductions type-correct. In that case,
adding new axioms in Prop does not matter; by proof irrelevance,
an expression in Prop carries no information, and can be safely
ignored by the reduction procedures.
Let α be any type, and let r be an equivalence relation on
α. It is mathematically common to form the “quotient” α/r,
that is, the type of elements of α “modulo” r. Set
theoretically, one can view α/r as the set of equivalence
classes of α modulo r. If f : α→β is any function that
respects the equivalence relation in the sense that for every
xy : α, rxy implies fx=fy, then f “lifts” to a function
f':α/r→β defined on each equivalence class ⟦x⟧ by
f'⟦x⟧=fx. Lean's standard library extends the Calculus of
Constructions with additional constants that perform exactly these
constructions, and installs this last equation as a definitional
reduction rule.
In its most basic form, the quotient construction does not even
require r to be an equivalence relation. The following constants
are built into Lean:
The first one forms a type Quotr given a type α by any binary
relation r on α. The second maps α to Quot α, so that
if r : α→α→Prop and a : α, then Quot.mkra is an
element of Quotr. The third principle, Quot.ind, says that
every element of Quot.mkra is of this form. As for
Quot.lift, given a function f : α→β, if h is a proof
that f respects the relation r, then Quot.liftfh is the
corresponding function on Quotr. The idea is that for each
element a in α, the function Quot.liftfh maps
Quot.mkra (the r-class containing a) to fa, wherein h
shows that this function is well defined. In fact, the computation
principle is declared as a reduction rule, as the proof below makes
clear.
The four constants, Quot, Quot.mk, Quot.ind, and
Quot.lift in and of themselves are not very strong. You can check
that the Quot.ind is satisfied if we take Quotr to be simply
α, and take Quot.lift to be the identity function (ignoring
h). For that reason, these four constants are not viewed as
additional axioms.
They are, like inductively defined types and the associated
constructors and recursors, viewed as part of the logical framework.
What makes the Quot construction into a bona fide quotient is the
following additional axiom:
This is the axiom that asserts that any two elements of α that are
related by r become identified in the quotient. If a theorem or
definition makes use of Quot.sound, it will show up in the
#print axioms command.
Of course, the quotient construction is most commonly used in
situations when r is an equivalence relation. Given r as
above, if we define r' according to the rule r'ab iff
Quot.mkra=Quot.mkrb, then it's clear that r' is an
equivalence relation. Indeed, r' is the kernel of the function
funa=>Quot.mkra. The axiom Quot.sound says that rab
implies r'ab. Using Quot.lift and Quot.ind, we can show
that r' is the smallest equivalence relation containing r, in
the sense that if r'' is any equivalence relation containing
r, then r'ab implies r''ab. In particular, if r
was an equivalence relation to start with, then for all a and
b we have rab iff r'ab.
To support this common use case, the standard library defines the
notion of a setoid, which is simply a type with an associated
equivalence relation:
The constants Quotient.mk, Quotient.ind, Quotient.lift,
and Quotient.sound are nothing more than the specializations of
the corresponding elements of Quot. The fact that type class
inference can find the setoid associated to a type α brings a
number of benefits. First, we can use the notation a≈b (entered
with \approx) for Setoid.rab, where the instance of
Setoid is implicit in the notation Setoid.r. We can use the
generic theorems Setoid.refl, Setoid.symm, Setoid.trans to
reason about the relation. Specifically with quotients we can use the
theorem Quotient.exact:
Together with Quotient.sound, this implies that the elements of
the quotient correspond exactly to the equivalence classes of elements
in α.
Recall that in the standard library, α×β represents the
Cartesian product of the types α and β. To illustrate the use
of quotients, let us define the type of unordered pairs of elements
of a type α as a quotient of the type α×α. First, we define
the relevant equivalence relation:
The next step is to prove that eqv is in fact an equivalence
relation, which is to say, it is reflexive, symmetric and
transitive. We can prove these three facts in a convenient and
readable way by using dependent pattern matching to perform
case-analysis and break the hypotheses into pieces that are then
reassembled to produce the conclusion.
Notice that we locally define the notation {a₁,a₂} for unordered
pairs as Quotient.mk'(a₁,a₂). This is useful for illustrative
purposes, but it is not a good idea in general, since the notation
will shadow other uses of curly brackets, such as for records and
sets.
We can easily prove that {a₁,a₂}={a₂,a₁} using Quot.sound,
since we have (a₁,a₂)~(a₂,a₁).
To complete the example, given a:α and u:UProdα, we
define the proposition a ∈ u which should hold if a is one of
the elements of the unordered pair u. First, we define a similar
proposition mem_fnau on (ordered) pairs; then we show that
mem_fn respects the equivalence relation eqv with the lemma
mem_respects. This is an idiom that is used extensively in the
Lean standard library.
For convenience, the standard library also defines Quotient.lift₂
for lifting binary functions, and Quotient.ind₂ for induction on
two variables.
We close this section with some hints as to why the quotient
construction implies function extensionality. It is not hard to show
that extensional equality on the (x:α)→βx is an equivalence
relation, and so we can consider the type extfunαβ of functions
“up to equivalence.” Of course, application respects that equivalence
in the sense that if f₁ is equivalent to f₂, then f₁a is
equal to f₂a. Thus application gives rise to a function
extfun_app:extfunαβ→(x:α)→βx. But for every f,
extfun_app(.mk_f) is definitionally equal to funx=>fx, which is
in turn definitionally equal to f. So, when f₁ and f₂ are
extensionally equal, we have the following chain of equalities:
Because Nonemptyα has type Prop and its constructor contains data, it can only eliminate to Prop.
In fact, Nonemptyα is equivalent to ∃x:α,True:
example(α:Typeu):Nonemptyα↔∃Variable name `x` is not explicitly referenced.Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:[apply]_xNote: This linter can be disabled with `set_option linter.unusedVariables false`x:α,True:=Iff.intro(fun⟨a⟩=>⟨a,trivial⟩)(fun⟨a,Variable name `h` is not explicitly referenced.Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:[apply]_hNote: This linter can be disabled with `set_option linter.unusedVariables false`h⟩=>⟨a⟩)
Our axiom of choice is now expressed simply as follows:
Given only the assertion h that α is nonempty, choiceh
magically produces an element of α. Of course, this blocks any
meaningful computation: by the interpretation of Prop, h
contains no information at all as to how to find such an element.
This is found in the Classical namespace, so the full name of the
theorem is Classical.choice. The choice principle is equivalent to
the principle of indefinite description, which can be expressed with
subtypes as follows:
Because it depends on choice, Lean cannot generate executable code for
indefiniteDescription, and so requires us to mark the definition
as noncomputable. Also in the Classical namespace, the
function choose and the property choose_spec decompose the two
parts of the output of indefiniteDescription:
Definition `inhabited_of_nonempty` of class type is semireducible. Most type class instances should be instance-reducible, so consider marking thisdefinition with `@[instance_reducible]`. If it is intentionally semireducible, this warning can be disabled with `set_option warn.classDefReducibility false`.noncomputabledefinhabited_of_nonempty(h:Nonemptyα):Inhabitedα:=choice(let⟨a⟩:=h;⟨⟨a⟩⟩)
In the next section, we will see that propext, funext, and
choice, taken together, imply the law of the excluded middle and
the decidability of all propositions. Using those, one can strengthen
the principle of indefinite description as follows:
Assuming the ambient type α is nonempty,
strongIndefiniteDescriptionp produces an element of α
satisfying p if there is one. The data component of this
definition is conventionally known as Hilbert's epsilon function:
Diaconescu's theorem states
that the axiom of choice is sufficient to derive the law of excluded
middle. More precisely, it shows that the law of the excluded middle
follows from Classical.choice, propext, and funext. We
sketch the proof that is found in the standard library.
First, we import the necessary axioms, and define two predicates U and V:
Each of U and V is a disjunction, so u_def and v_def
represent four cases. In one of these cases, u=True and
v=False, and in all the other cases, p is true. Thus we have:
havenot_uv_or_p:u≠v∨p:=bymatchu_def,v_defwith|Or.inrh,_=>p:PropU:Prop→Prop:=funx=>x=True∨pV:Prop→Prop:=funx=>x=False∨pexU:∃x,UxexV:∃x,Vxu:Prop:=chooseexUv:Prop:=chooseexVu_def:Uuv_def:Vvh:px✝:Vv⊢ u≠v∨pexactOr.inrhAll goals completed! 🐙|_,Or.inrh=>p:PropU:Prop→Prop:=funx=>x=True∨pV:Prop→Prop:=funx=>x=False∨pexU:∃x,UxexV:∃x,Vxu:Prop:=chooseexUv:Prop:=chooseexVu_def:Uuv_def:Vvx✝:Uuh:p⊢ u≠v∨pexactOr.inrhAll goals completed! 🐙|Or.inlhut,Or.inlhvf=>p:PropU:Prop→Prop:=funx=>x=True∨pV:Prop→Prop:=funx=>x=False∨pexU:∃x,UxexV:∃x,Vxu:Prop:=chooseexUv:Prop:=chooseexVu_def:Uuv_def:Vvhut:u=Truehvf:v=False⊢ u≠v∨papplyOr.inlp:PropU:Prop→Prop:=funx=>x=True∨pV:Prop→Prop:=funx=>x=False∨pexU:∃x,UxexV:∃x,Vxu:Prop:=chooseexUv:Prop:=chooseexVu_def:Uuv_def:Vvhut:u=Truehvf:v=False⊢ u≠vsimp[hvf,hut,This simp argument is unused:true_ne_falseHint: Omit it from the simp argument list.[apply]simp [hvf, hut]Note: This linter can be disabled with `set_option linter.unusedSimpArgs false`true_ne_false]p:PropU:Prop→Prop:=funx=>x=True∨pV:Prop→Prop:=funx=>x=False∨pexU:∃x,UxexV:∃x,Vxu:Prop:=chooseexUv:Prop:=chooseexVu_def:Uuv_def:Vvnot_uv_or_p:u≠v∨p⊢ p∨¬p
On the other hand, if p is true, then, by function extensionality
and propositional extensionality, U and V are equal. By the
definition of u and v, this implies that they are equal as well.
Consequences of excluded middle include double-negation elimination,
proof by cases, and proof by contradiction, all of which are described
in the section on classical logic.
The law of the excluded middle and propositional extensionality imply propositional completeness:
Together with choice, we also get the stronger principle that every
proposition is decidable. Recall that the class of Decidable
propositions is defined as follows:
In contrast to p∨¬p, which can only eliminate to Prop, the
type Decidablep is equivalent to the sum type Sum p (¬ p), which
can eliminate to any type. It is this data that is needed to write an
if-then-else expression.
As an example of classical reasoning, we use choose to show that if
f : α→β is injective and α is inhabited, then f has a
left inverse. To define the left inverse linv, we use a dependent
if-then-else expression. Recall that ifh:cthentelsee is
notation for ditec(funh:c=>t)(funh:¬c=>e). In the definition
of linv, choice is used twice: first, to show that
(∃a:α,fa=b) is “decidable,” and then to choose an a such that
fa=b. Notice that propDecidable is a scoped instance and is activated
by the openClassical command. We use this instance to justify
the if-then-else expression. (See also the discussion in
Decidable Propositions).
openClassicalDefinition `linv` is a proposition; use `theorem` instead of `def`Note: This linter can be disabled with `set_option linter.defProp false`noncomputabledeflinv[Inhabitedα](f:α→β):β→α:=funb:β=>ifex:(∃a:α,fa=b)thenchooseexelsedefaulttheoremlinv_comp_self{f:α→β}[Inhabitedα](inj:∀{ab},fa=fb→a=b):linvf∘f=id:=funextfuna=>haveex:∃a₁:α,fa₁=fa:=⟨a,rfl⟩havefeq:f(chooseex)=fa:=choose_specexcalclinvf(fa)_=chooseex:=rfl_=a:=injfeq
From a classical point of view, linv is a function. From a
constructive point of view, it is unacceptable; because there is no
way to implement such a function in general, the construction is not
informative.