The last chapter introduced you to methods that construct proofs of
statements involving the propositional connectives. In this chapter,
we extend the repertoire of logical constructions to include the
universal and existential quantifiers, and the equality relation.
Notice that if α is any type, we can represent a unary predicate
p on α as an object of type α→Prop. In that case, given
x : α, px denotes the assertion that p holds of
x. Similarly, an object r : α→α→Prop denotes a binary
relation on α: given xy : α, rxy denotes the assertion
that x is related to y.
The universal quantifier, ∀x:α,px is supposed to denote the
assertion that “for every x : α, px” holds. As with the
propositional connectives, in systems of natural deduction, “forall”
is governed by an introduction and elimination rule. Informally, the
introduction rule states:
Given a proof of px, in a context where x : α is arbitrary, we obtain a proof ∀x:α,px.
The elimination rule states:
Given a proof ∀x:α,px and any term t : α, we obtain a proof of pt.
As was the case for implication, the propositions-as-types
interpretation now comes into play. Remember the introduction and
elimination rules for dependent arrow types:
Given a term t of type βx, in a context where x : α is arbitrary, we have (funx:α=>t):(x:α)→βx.
The elimination rule states:
Given a term s : (x:α)→βx and any term t : α, we have st:βt.
In the case where px has type Prop, if we replace
(x:α)→βx with ∀x:α,px, we can read these as the correct rules
for building proofs involving the universal quantifier.
The Calculus of Constructions therefore identifies dependent arrow
types with forall-expressions in this way. If p is any expression,
∀x:α,p is nothing more than alternative notation for
(x:α)→p, with the idea that the former is more natural than the latter
in cases where p is a proposition. Typically, the expression p
will depend on x:α. Recall that, in the case of ordinary
function spaces, we could interpret α→β as the special case of
(x:α)→β in which β does not depend on x. Similarly, we
can think of an implication p→q between propositions as the
special case of ∀x:p,q in which the expression q does not
depend on x.
Here is an example of how the propositions-as-types correspondence gets put into practice.
As a notational convention, we give the universal quantifier the
widest scope possible, so parentheses are needed to limit the
quantifier over x to the hypothesis in the example above. The
canonical way to prove ∀y:α,py is to take an arbitrary y,
and prove py. This is the introduction rule. Now, given that
h has type ∀x:α,px∧qx, the expression hy has type
py ∧ qy. This is the elimination rule. Taking the left conjunct
gives the desired conclusion, py.
Remember that expressions which differ up to renaming of bound
variables are considered to be equivalent. So, for example, we could
have used the same variable, x, in both the hypothesis and
conclusion, and instantiated it by a different variable, z, in the
proof:
Think about what is going on here. When we instantiate trans_r at
the values abc, we end up with a proof of rab → rbc → rac.
Applying this to the “hypothesis” hab:rab, we get a proof
of the implication rbc → rac. Finally, applying it to the
hypothesis hbc yields a proof of the conclusion rac.
In situations like this, it can be tedious to supply the arguments
abc, when they can be inferred from habhbc. For that reason, it
is common to make these arguments implicit:
The advantage is that we can simply write trans_rhabhbc as a
proof of rac. A disadvantage is that Lean does not have enough
information to infer the types of the arguments in the expressions
trans_r and trans_rhab. The output of the first #check
command is r ?m.1 ?m.2 → r ?m.2 ?m.3 → r ?m.1 ?m.3, indicating
that the implicit arguments are unspecified in this case.
Here is an example of how we can carry out elementary reasoning with an equivalence relation:
To get used to using universal quantifiers, you should try some of the
exercises at the end of this section.
It is the typing rule for dependent arrow types, and the universal
quantifier in particular, that distinguishes Prop from other
types. Suppose we have α : Sorti and β : Sortj, where the
expression β may depend on a variable x : α. Then
(x:α)→β is an element of Sort(imaxij), where imax i j is the
maximum of i and j if j is not 0, and 0 otherwise.
The idea is as follows. If j is not 0, then (x:α)→β is
an element of Sort(maxij). In other words, the type of
dependent functions from α to β “lives” in the universe whose
index is the maximum of i and j. Suppose, however, that β
is of Sort0, that is, an element of Prop. In that case,
(x:α)→β is an element of Sort0 as well, no matter which
type universe α lives in. In other words, if β is a
proposition depending on α, then ∀x:α,β is again a
proposition. This reflects the interpretation of Prop as the type
of propositions rather than data, and it is what makes Propimpredicative.
The term “predicative” stems from foundational developments around the
turn of the twentieth century, when logicians such as Poincaré and
Russell blamed set-theoretic paradoxes on the “vicious circles” that
arise when we define a property by quantifying over a collection that
includes the very property being defined. Notice that if α is any
type, we can form the type α→Prop of all predicates on α
(the “power type of α”). The impredicativity of Prop means that we
can form propositions that quantify over α→Prop. In particular,
we can define predicates on α by quantifying over all predicates
on α, which is exactly the type of circularity that was once
considered problematic.
Let us now turn to one of the most fundamental relations defined in
Lean's library, namely, the equality relation. In the chapter on inductive types,
we will explain how equality is defined from the primitives of Lean's logical framework.
In the meanwhile, here we explain how to use it.
Of course, a fundamental property of equality is that it is an equivalence relation:
Reflexivity is more powerful than it looks. Recall that terms in the
Calculus of Constructions have a computational interpretation, and
that the logical framework treats terms with a common reduct as the
same. As a result, some nontrivial identities can be proved by
reflexivity:
Equality is much more than an equivalence relation, however. It has
the important property that every assertion respects the equivalence,
in the sense that we can substitute equal expressions without changing
the truth value. That is, given h1 : a=b and h2 : pa, we
can construct a proof for pb using substitution:
Eq.substh1h2.
The triangle in the second presentation is a macro built on top of
Eq.subst and Eq.symm, and you can enter it by typing \t.
The rule Eq.subst is used to define the following auxiliary rules,
which carry out more explicit substitutions. They are designed to deal
with applicative terms, that is, terms of form st. Specifically,
congrArg can be used to replace the argument, congrFun can be
used to replace the term that is being applied, and congr can be
used to replace both at once.
Note that Nat.mul_add and Nat.add_mul are alternative names
for Nat.left_distrib and Nat.right_distrib, respectively. The
properties above are stated for the natural numbers (type Nat).
Here is an example of a calculation in the natural numbers that uses
substitution combined with associativity and distributivity.
Notice that the second implicit parameter to Eq.subst, which
provides the context in which the substitution is to occur, has type
α→Prop. Inferring this predicate therefore requires an instance
of higher-order unification. In full generality, the problem of
determining whether a higher-order unifier exists is undecidable, and
Lean can at best provide imperfect and approximate solutions to the
problem. As a result, Eq.subst doesn't always do what you want it
to. The macro h▸e uses more effective heuristics for computing
this implicit parameter, and often succeeds in situations where
applying Eq.subst fails.
Because equational reasoning is so common and important, Lean provides
a number of mechanisms to carry it out more effectively. The next
section offers syntax that allow you to write calculational proofs in
a more natural and perspicuous way. But, more importantly, equational
reasoning is supported by a term rewriter, a simplifier, and other
kinds of automation. The term rewriter and simplifier are described
briefly in the next section, and then in greater detail in the next
chapter.
A calculational proof is just a chain of intermediate results that are
meant to be composed by basic principles such as the transitivity of
equality. In Lean, a calculational proof starts with the keyword
calc, and has the following syntax:
This style of writing proofs is most effective when it is used in
conjunction with the simp and rw tactics, which are
discussed in greater detail in the next chapter. For example, using
rw for rewrite, the proof above could be written
as follows:
Essentially, the rw tactic uses a given equality (which can be a
hypothesis, a theorem name, or a complex term) to “rewrite” the
goal. If doing so reduces the goal to an identity t=t, the
tactic applies reflexivity to prove it.
Rewrites can be applied sequentially, so that the proof above can be
shortened to this:
The simp tactic, instead, rewrites the goal by applying the given
identities repeatedly, in any order, anywhere they are applicable in a
term. It also uses other rules that have been previously declared to
the system, and applies commutativity wisely to avoid looping. As a
result, we can also prove the theorem as follows:
You can “teach” calc new transitivity theorems by adding new instances
of the Trans type class. Type classes are introduced later, but the following
small example demonstrates how to extend the calc notation using new Trans instances.
The example above also makes it clear that you can use calc even if you do not have an infix
notation for your relation. Lean already includes the standard Unicode notation for divisibility
(using ∣, which can be entered as \dvd or \mid), so the example above uses the ordinary
vertical bar to avoid a conflict. In practice, this is not a good idea, as it risks confusion with
the ASCII | used in the match ... with expression.
With calc, we can write the proof in the last section in a more
natural and perspicuous way.
The alternative calc notation is worth considering here. When the
first expression is taking this much space, using _ in the first
relation naturally aligns all relations:
Here the left arrow before Nat.add_assoc tells rewrite to use the
identity in the opposite direction. (You can enter it with \l or
use the ASCII equivalent, <-.) If brevity is what we are after,
both rw and simp can do the job on their own:
Finally, consider the existential quantifier, which can be written as
either existsx:α,px or ∃x:α,px. Both versions are
actually notationally convenient abbreviations for a more long-winded
expression, Exists(funx:α=>px), defined in Lean's library.
As you should by now expect, the library includes both an introduction
rule and an elimination rule. The introduction rule is
straightforward: to prove ∃x:α,px, it suffices to provide a
suitable term t and a proof of pt. Here are some examples:
Note that Exists.intro has implicit arguments: Lean has to infer
the predicate p : α→Prop in the conclusion ∃x,px. This
is not a trivial affair. For example, if we have
hg : g00=0 and write Exists.intro0hg, there are many possible values
for the predicate p, corresponding to the theorems ∃x,gxx=x,
∃x,gxx=0, ∃x,gx0=x, etc. Lean uses the
context to infer which one is appropriate. This is illustrated in the
following example, in which we set the option pp.explicit to true
to ask Lean's pretty-printer to show the implicit arguments.
We can view Exists.intro as an information-hiding operation, since
it hides the witness to the body of the assertion. The existential
elimination rule, Exists.elim, performs the opposite operation. It
allows us to prove a proposition q from ∃x:α,px, by
showing that q follows from pw for an arbitrary value
w. Roughly speaking, since we know there is an x satisfying
px, we can give it a name, say, w. If q does not mention
w, then showing that q follows from pw is tantamount to
showing that q follows from the existence of any such x. Here
is an example:
It may be helpful to compare the exists-elimination rule to the
or-elimination rule: the assertion ∃x:α,px can be thought of
as a big disjunction of the propositions pa, as a ranges over
all the elements of α. Note that the anonymous constructor
notation ⟨w,hw.right,hw.left⟩ abbreviates a nested constructor
application; we could equally well have written ⟨w, ⟨hw.right, hw.left⟩⟩.
Notice that an existential proposition is very similar to a sigma
type, as described in dependent types section. The difference is that
existential propositions are propositions, while sigma types are types.
Otherwise, they are very similar. Given a predicate p : α→Prop and a family of types β : α→Type,
for a term a : α with h : pa and h':βa, the term Exists.introah has
type (∃x:α,px):Prop, while Sigma.mkah' has type
(Σx:α,βx). The similarity between ∃ and Σ is another
instance of the Curry-Howard isomorphism.
Lean provides a more convenient way to eliminate from an existential
quantifier with the match expression:
The match expression is part of Lean's function definition system,
which provides convenient and expressive ways of defining complex
functions. Once again, it is the Curry-Howard isomorphism that allows
us to co-opt this mechanism for writing proofs as well. The match
statement “destructs” the existential assertion into the components
w and hw, which can then be used in the body of the statement
to prove the proposition. We can annotate the types used in the match
for greater clarity:
Using the various gadgets described in this chapter—the match
statement, anonymous constructors, and the rewrite tactic, we can
write this proof concisely as follows:
Just as the constructive “or” is stronger than the classical “or,” so,
too, is the constructive “exists” stronger than the classical
“exists”. For example, the following implication requires classical
reasoning because, from a constructive standpoint, knowing that it is
not the case that every x satisfies ¬p is not the same as
having a particular x that satisfies p.
What follows are some common identities involving the existential
quantifier. In the exercises below, we encourage you to prove as many
as you can. We also leave it to you to determine which are
nonconstructive, and hence require some form of classical reasoning.
Notice that the second example and the last two examples require the
assumption that there is at least one element a of type α.
Here are solutions to two of the more difficult ones:
openClassicalvariable(α:Type)(pq:α→Prop)variable(a:α)variable(r:Prop)example:(∃x,px∨qx)↔(∃x,px)∨(∃x,qx):=Iff.intro(fun⟨a,(h1:pa∨qa)⟩=>Or.elimh1(funhpa:pa=>Or.inl⟨a,hpa⟩)(funhqa:qa=>Or.inr⟨a,hqa⟩))(funh:(∃x,px)∨(∃x,qx)=>Or.elimh(fun⟨a,hpa⟩=>⟨a,(Or.inlhpa)⟩)(fun⟨a,hqa⟩=>⟨a,(Or.inrhqa)⟩))example:(∃x,px→r)↔(∀x,px)→r:=Iff.intro(fun⟨b,(hb:pb→r)⟩=>funh2:∀x,px=>showrfromhb(h2b))(funh1:(∀x,px)→r=>show∃x,px→rfrombyCases(funhap:∀x,px=>⟨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]_h'Note: This linter can be disabled with `set_option linter.unusedVariables false`h'=>h1hap⟩)(funhnap:¬∀x,px=>byContradiction(funhnex:¬∃x,px→r=>havehap:∀x,px:=funx=>byContradiction(funhnp:¬px=>havehex:∃x,px→r:=⟨x,(funhp=>absurdhphnp)⟩showFalsefromhnexhex)showFalsefromhnaphap)))
We have seen that keywords like fun, have, and show make
it possible to write formal proof terms that mirror the structure of
informal mathematical proofs. In this section, we discuss some
additional features of the proof language that are often convenient.
To start with, we can use anonymous have expressions to introduce an
auxiliary goal without having to label it. We can refer to the last
expression introduced in this way using the keyword this:
This tells Lean to use the assumption tactic, which, in turn,
proves the goal by finding a suitable hypothesis in the local
context. We will learn more about the assumption tactic in the
next chapter.
We can also ask Lean to fill in the proof by writing ‹p›, where
p is the proposition whose proof we want Lean to find in the
context. You can type these corner quotes using \f< and \f>,
respectively. The letter “f” is for “French,” since the Unicode
symbols can also be used as French quotation marks. In fact, the
notation is defined in Lean as follows:
This approach is more robust than using byassumption, because the
type of the assumption that needs to be inferred is given
explicitly. It also makes proofs more readable. Here is a more
elaborate example:
Keep in mind that you can use the French quotation marks in this way
to refer to anything in the context, not just things that were
introduced anonymously. Its use is also not limited to propositions,
though using it for data is somewhat odd:
You should also try to understand why the reverse implication is not derivable in the last example.
It is often possible to bring a component of a formula outside a
universal quantifier, when it does not depend on the quantified
variable. Try proving these (one direction of the second of these
requires classical logic):
Consider the “barber paradox,” that is, the claim that in a certain
town there is a (male) barber that shaves all and only the men who
do not shave themselves. Prove that this is a contradiction:
Remember that, without any parameters, an expression of type
Prop is just an assertion. Fill in the definitions of prime
and Fermat_prime below, and construct each of the given
assertions. For example, you can say that there are infinitely many
primes by asserting that for every natural number n, there is a
prime number greater than n. Goldbach's weak conjecture states
that every odd number greater than 5 is the sum of three
primes. Look up the definition of a Fermat prime or any of the
other statements, if necessary.