PropPropositions and Evidence
Require Export MoreCoq.
In previous chapters, we have seen many examples of factual
claims (propositions) and ways of presenting evidence of their
truth (proofs). In particular, we have worked extensively with
equality propositions of the form e1 = e2, with
implications (P → Q), and with quantified propositions
(∀ x, P).
In this chapter we take a deeper look at the way propositions are
expressed in Coq and at the structure of the logical evidence that
we construct when we carry out proofs.
Some of the concepts in this chapter may seem a bit abstract on a
first encounter. We've included a lot of exercises, most of
which should be quite approachable even if you're still working on
understanding the details of the text. Try to work as many of
them as you can, especially the one-starred exercises.
Inductively Defined Propositions
- Rule b_0: The number 0 is beautiful.
- Rule b_3: The number 3 is beautiful.
- Rule b_5: The number 5 is beautiful.
- Rule b_sum: If n and m are both beautiful, then so is their sum.
(b_0) | |
beautiful 0 |
(b_3) | |
beautiful 3 |
(b_5) | |
beautiful 5 |
beautiful n beautiful m | (b_sum) |
beautiful (n+m) |
----------- (b_3) ----------- (b_5)
beautiful 3 beautiful 5
------------------------------- (b_sum)
beautiful 8
Of course, there are other ways of using these rules to argue that
8 is beautiful, for instance:
beautiful 3 beautiful 5
------------------------------- (b_sum)
beautiful 8
----------- (b_5) ----------- (b_3)
beautiful 5 beautiful 3
------------------------------- (b_sum)
beautiful 8
beautiful 5 beautiful 3
------------------------------- (b_sum)
beautiful 8
Exercise: 1 star (varieties_of_beauty)
How many different ways are there to show that 8 is beautiful?(* FILL IN HERE *)
☐
In Coq, we can express the definition of beautiful as
follows:
Inductive beautiful : nat → Prop :=
b_0 : beautiful 0
| b_3 : beautiful 3
| b_5 : beautiful 5
| b_sum : ∀n m, beautiful n → beautiful m → beautiful (n+m).
The first line declares that beautiful is a proposition — or,
more formally, a family of propositions "indexed by" natural
numbers. (That is, for each number n, the claim that "n is
beautiful" is a proposition.) Such a family of propositions is
often called a property of numbers. Each of the remaining lines
embodies one of the rules for beautiful numbers.
We can use Coq's tactic scripting facility to assemble proofs that
particular numbers are beautiful.
Theorem three_is_beautiful: beautiful 3.
Proof.
(* This simply follows from the axiom b_3. *)
apply b_3.
Qed.
Theorem eight_is_beautiful: beautiful 8.
Proof.
(* First we use the rule b_sum, telling Coq how to
instantiate n and m. *)
apply b_sum with (n:=3) (m:=5).
(* To solve the subgoals generated by b_sum, we must provide
evidence of beautiful 3 and beautiful 5. Fortunately we
have axioms for both. *)
apply b_3.
apply b_5.
Qed.
Proof Objects
propositions ~ types proofs ~ data valuesMany useful insights follow from this connection. To begin with, it gives us a natural interpretation of the type of b_sum constructor:
Check b_sum.
(* ===> b_sum : forall n m,
beautiful n ->
beautiful m ->
beautiful (n+m) *)
This can be read "b_sum is a constructor that takes four
arguments — two numbers, n and m, and two values, of types
beautiful n and beautiful m — and yields evidence for the
proposition beautiful (n+m)."
In view of this, we might wonder whether we can write an
expression of type beautiful 8 by applying b_sum to
appropriate arguments. Indeed, we can:
Check (b_sum 3 5 b_3 b_5).
(* ===> beautiful (3 + 5) *)
The expression b_sum 3 5 b_3 b_5 can be thought of as
instantiating the parameterized constructor b_sum with the
specific arguments 3 5 and the corresponding proof objects for
its premises beautiful 3 and beautiful 5 (Coq is smart enough
to figure out that 3+5=8). Alternatively, we can think of b_sum
as a primitive "evidence constructor" that, when applied to two
particular numbers, wants to be further applied to evidence that
those two numbers are beautiful; its type,
[
∀ n m, beautiful n → beautiful m → beautiful (n+m),
]
expresses this functionality, in the same way that the polymorphic
type ∀ X, list X in the previous chapter expressed the fact
that the constructor nil can be thought of as a function from
types to empty lists with elements of that type.
This gives us an alternative way to write the proof that 8 is
beautiful:
Theorem eight_is_beautiful': beautiful 8.
Proof.
apply (b_sum 3 5 b_3 b_5).
Qed.
Notice that we're using apply here in a new way: instead of just
supplying the name of a hypothesis or previously proved theorem
whose type matches the current goal, we are supplying an
expression that directly builds evidence with the required
type.
Proof Scripts and Proof Objects
Theorem eight_is_beautiful'': beautiful 8.
Proof.
Show Proof.
apply b_sum with (n:=3) (m:=5).
Show Proof.
apply b_3.
Show Proof.
apply b_5.
Show Proof.
Qed.
At any given moment, Coq has constructed a term with some
"holes" (indicated by ?1, ?2, and so on), and it knows what
type of evidence is needed at each hole. In the Show Proof
output, lines of the form ?1 → beautiful n record these
requirements. (The → here has nothing to do with either
implication or function types — it is just an unfortunate choice
of concrete syntax for the output!)
Each of the holes corresponds to a subgoal, and the proof is
finished when there are no more subgoals. At this point, the
Theorem command gives a name to the evidence we've built and
stores it in the global context.
Tactic proofs are useful and convenient, but they are not
essential: in principle, we can always construct the required
evidence by hand. Indeed, we don't even need the Theorem
command: we can instead use Definition to directly give a global
name to a piece of evidence.
Definition eight_is_beautiful''' : beautiful 8 :=
b_sum 3 5 b_3 b_5.
All these different ways of building the proof lead to exactly the
same evidence being saved in the global environment.
Print eight_is_beautiful.
(* ===> eight_is_beautiful = b_sum 3 5 b_3 b_5 : beautiful 8 *)
Print eight_is_beautiful'.
(* ===> eight_is_beautiful' = b_sum 3 5 b_3 b_5 : beautiful 8 *)
Print eight_is_beautiful''.
(* ===> eight_is_beautiful'' = b_sum 3 5 b_3 b_5 : beautiful 8 *)
Print eight_is_beautiful'''.
(* ===> eight_is_beautiful''' = b_sum 3 5 b_3 b_5 : beautiful 8 *)
Exercise: 1 star (six_is_beautiful)
Give a tactic proof and a proof object showing that 6 is beautiful.Theorem six_is_beautiful :
beautiful 6.
Proof.
(* FILL IN HERE *) Admitted.
Definition six_is_beautiful' : beautiful 6 :=
(* FILL IN HERE *) admit.
☐
Exercise: 1 star (nine_is_beautiful)
Give a tactic proof and a proof object showing that 9 is beautiful.Theorem nine_is_beautiful :
beautiful 9.
Proof.
(* FILL IN HERE *) Admitted.
Definition nine_is_beautiful' : beautiful 9 :=
(* FILL IN HERE *) admit.
☐
Implications and Functions
Theorem b_plus3: ∀n, beautiful n → beautiful (3+n).
Proof.
intros n H.
apply b_sum.
apply b_3.
apply H.
Qed.
What is the proof object corresponding to b_plus3?
We're looking for an expression whose type is ∀ n,
beautiful n → beautiful (3+n) — that is, a function that
takes two arguments (one number and a piece of evidence) and
returns a piece of evidence! Here it is:
Definition b_plus3' : ∀n, beautiful n → beautiful (3+n) :=
fun n => fun H : beautiful n =>
b_sum 3 n b_3 H.
Check b_plus3'.
(* ===> b_plus3' : forall n, beautiful n -> beautiful (3+n) *)
Recall that fun n => blah means "the function that, given n,
yields blah." Another equivalent way to write this definition is:
Definition b_plus3'' (n : nat) (H : beautiful n) : beautiful (3+n) :=
b_sum 3 n b_3 H.
Check b_plus3''.
(* ===> b_plus3'' : forall n, beautiful n -> beautiful (3+n) *)
Theorem b_times2: ∀n, beautiful n → beautiful (2*n).
Proof.
(* FILL IN HERE *) Admitted.
Proof.
(* FILL IN HERE *) Admitted.
Definition b_times2': ∀n, beautiful n → beautiful (2*n) :=
(* FILL IN HERE *) admit.
Theorem b_timesm: ∀n m, beautiful n → beautiful (m*n).
Proof.
(* FILL IN HERE *) Admitted.
Proof.
(* FILL IN HERE *) Admitted.
☐
Induction Over Proof Objects
- E is b_0 (and n is O),
- E is b_3 (and n is 3),
- E is b_5 (and n is 5), or
- E is b_sum n1 n2 E1 E2 (and n is n1+n2, where E1 is evidence that n1 is beautiful and E2 is evidence that n2 is beautiful).
Inductive gorgeous : nat → Prop :=
g_0 : gorgeous 0
| g_plus3 : ∀n, gorgeous n → gorgeous (3+n)
| g_plus5 : ∀n, gorgeous n → gorgeous (5+n).
Exercise: 1 star (gorgeous_tree)
Write out the definition of gorgeous numbers using inference rule notation.☐
Theorem gorgeous__beautiful : ∀n,
gorgeous n → beautiful n.
Proof.
intros n H.
induction H as [|n'|n'].
Case "g_0".
apply b_0.
Case "g_plus3".
apply b_sum. apply b_3.
apply IHgorgeous.
Case "g_plus5".
apply b_sum. apply b_5. apply IHgorgeous.
Qed.
Notice that the argument proceeds by induction on the evidence H!
Let's see what happens if we try to prove this by induction on n
instead of induction on the evidence H.
Theorem gorgeous__beautiful_FAILED : ∀n,
gorgeous n → beautiful n.
Proof.
intros. induction n as [| n'].
Case "n = 0". apply b_0.
Case "n = S n'". (* We are stuck! *)
Abort.
The problem here is that doing induction on n doesn't yield a
useful induction hypothesis. Knowing how the property we are
interested in behaves on the predecessor of n doesn't help us
prove that it holds for n. Instead, we would like to be able to
have induction hypotheses that mention other numbers, such as n -
3 and n - 5. This is given precisely by the shape of the
constructors for gorgeous.
Exercise: 1 star (gorgeous_plus13)
Theorem gorgeous_plus13: ∀n,
gorgeous n → gorgeous (13+n).
Proof.
(* FILL IN HERE *) Admitted.
gorgeous n → gorgeous (13+n).
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 2 stars, optional (gorgeous_plus13_po):
Give the proof object for theorem gorgeous_plus13 above.Definition gorgeous_plus13_po: ∀n, gorgeous n → gorgeous (13+n):=
(* FILL IN HERE *) admit.
Theorem gorgeous_sum : ∀n m,
gorgeous n → gorgeous m → gorgeous (n + m).
Proof.
(* FILL IN HERE *) Admitted.
gorgeous n → gorgeous m → gorgeous (n + m).
Proof.
(* FILL IN HERE *) Admitted.
Theorem beautiful__gorgeous : ∀n, beautiful n → gorgeous n.
Proof.
(* FILL IN HERE *) Admitted.
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 3 stars, optional (b_times2)
Prove the g_times2 theorem below without using gorgeous__beautiful. You might find the following helper lemma useful.Lemma helper_g_times2 : ∀x y z, x + (z + y)= z + x + y.
Proof.
(* FILL IN HERE *) Admitted.
Theorem g_times2: ∀n, gorgeous n → gorgeous (2*n).
Proof.
intros n H. simpl.
induction H.
(* FILL IN HERE *) Admitted.
☐
From Boolean Functions to Propositions
Definition even (n:nat) : Prop :=
evenb n = true.
That is, we can define "n is even" to mean "the function evenb
returns true when applied to n."
Another alternative is to define the concept of evenness
directly. Instead of going via the evenb function ("a number is
even if a certain computation yields true"), we can say what the
concept of evenness means by giving two different ways of
presenting evidence that a number is even.
Inductive ev : nat → Prop :=
| ev_0 : ev O
| ev_SS : ∀n:nat, ev n → ev (S (S n)).
This definition says that there are two ways to give
evidence that a number m is even. First, 0 is even, and
ev_0 is evidence for this. Second, if m = S (S n) for some
n and we can give evidence e that n is even, then m is
also even, and ev_SS n e is the evidence.
Exercise: 1 star (double_even)
Construct a tactic proof of the following proposition.Theorem double_even : ∀n,
ev (double n).
Proof.
(* FILL IN HERE *) Admitted.
☐
We have seen that the proposition "n is even" can be
phrased in two different ways — indirectly, via a boolean testing
function evenb, or directly, by inductively describing what
constitutes evidence for evenness. These two ways of defining
evenness are about equally easy to state and work with. Which we
choose is basically a question of taste.
However, for many other properties of interest, the direct
inductive definition is preferable, since writing a testing
function may be awkward or even impossible.
One such property is beautiful. This is a perfectly sensible
definition of a set of numbers, but we cannot translate its
definition directly into a Coq Fixpoint (or into a recursive
function in any other common programming language). We might be
able to find a clever way of testing this property using a
Fixpoint (indeed, it is not too hard to find one in this case),
but in general this could require arbitrarily deep thinking. In
fact, if the property we are interested in is uncomputable, then
we cannot define it as a Fixpoint no matter how hard we try,
because Coq requires that all Fixpoints correspond to
terminating computations.
On the other hand, writing an inductive definition of what it
means to give evidence for the property beautiful is
straightforward.
Exercise: 4 stars, optional (double_even_pfobj)
Try to predict what proof object is constructed by the above tactic proof. (Before checking your answer, you'll want to strip out any uses of Case, as these will make the proof object look a bit cluttered.) ☐Discussion: Computational vs. Inductive Definitions
Inversion on Proof Objects
Theorem ev_minus2: ∀n,
ev n → ev (pred (pred n)).
Proof.
intros n E.
destruct E as [| n' E'].
Case "E = ev_0". simpl. apply ev_0.
Case "E = ev_SS n' E'". simpl. apply E'. Qed.
(* FILL IN HERE *)
☐
Exercise: 1 star (ev__even)
Here is a proof that the inductive definition of evenness implies the computational one.Theorem ev__even : ∀n,
ev n → even n.
Proof.
intros n E. induction E as [| n' E'].
Case "E = ev_0".
unfold even. reflexivity.
Case "E = ev_SS n' E'".
unfold even. apply IHE'.
Qed.
Could this proof also be carried out by induction on n instead
of E? If not, why not?
(* FILL IN HERE *)
☐
The induction principle for inductively defined propositions does
not follow quite the same form as that of inductively defined
sets. For now, you can take the intuitive view that induction on
evidence ev n is similar to induction on n, but restricts our
attention to only those numbers for which evidence ev n could be
generated. We'll look at the induction principle of ev in more
depth below, to explain what's really going on.
(* FILL IN HERE *)
☐
Exercise: 1 star (l_fails)
The following proof attempt will not succeed.
Theorem l : ∀n,
ev n.
Proof.
intros n. induction n.
Case "O". simpl. apply ev_0.
Case "S".
...
Intuitively, we expect the proof to fail because not every
number is even. However, what exactly causes the proof to fail?
ev n.
Proof.
intros n. induction n.
Case "O". simpl. apply ev_0.
Case "S".
...
☐
Exercise: 2 stars (ev_sum)
Here's another exercise requiring induction.Theorem ev_sum : ∀n m,
ev n → ev m → ev (n+m).
Proof.
(* FILL IN HERE *) Admitted.
☐
Another situation where we want to analyze evidence for evenness
is when proving that, if n+2 is even, then n is.
Our first idea might be to use destruct for this kind of case
analysis:
Theorem SSev_ev_firsttry : ∀n,
ev (S (S n)) → ev n.
Proof.
intros n E.
destruct E as [| n' E'].
(* Stuck: destruct gives us an unprovable subgoal here! *)
Abort.
In the first sub-goal, we've lost the information that n is 0.
We could have used remember, but then we still need inversion
on both cases.
Theorem SSev_ev_secondtry : ∀n,
ev (S (S n)) → ev n.
Proof.
intros n E. remember (S (S n)) as n2.
destruct E as [| n' E'].
Case "n = 0". inversion Heqn2.
Case "n = S n'". inversion Heqn2. rewrite ← H0. apply E'.
Qed.
There is a much simpler way to do this. We can use
inversion directly on the inductively defined proposition
ev (S (S n)).
Theorem SSev__even : ∀n,
ev (S (S n)) → ev n.
Proof.
intros n E. inversion E as [| n' E']. apply E'. Qed.
This use of inversion may seem a bit mysterious at first.
Until now, we've only used inversion on equality
propositions, to utilize injectivity of constructors or to
discriminate between different constructors. But we see here
that inversion can also be applied to analyzing evidence
for inductively defined propositions.
Here's how inversion works in general. Suppose the name
I refers to an assumption P in the current context, where
P has been defined by an Inductive declaration. Then,
for each of the constructors of P, inversion I generates
a subgoal in which I has been replaced by the exact,
specific conditions under which this constructor could have
been used to prove P. Some of these subgoals will be
self-contradictory; inversion throws these away. The ones
that are left represent the cases that must be proved to
establish the original goal.
In this particular case, the inversion analyzed the construction
ev (S (S n)), determined that this could only have been
constructed using ev_SS, and generated a new subgoal with the
arguments of that constructor as new hypotheses. (It also
produced an auxiliary equality, which happens to be useless here.)
We'll begin exploring this more general behavior of inversion in
what follows.
Exercise: 1 star (inversion_practice)
Theorem SSSSev__even : ∀n,
ev (S (S (S (S n)))) → ev n.
Proof.
(* FILL IN HERE *) Admitted.
ev (S (S (S (S n)))) → ev n.
Proof.
(* FILL IN HERE *) Admitted.
The inversion tactic can also be used to derive goals by showing
the absurdity of a hypothesis.
Theorem even5_nonsense :
ev 5 → 2 + 2 = 9.
Proof.
(* FILL IN HERE *) Admitted.
☐
We can generally use inversion on inductive propositions.
This illustrates that in general, we get one case for each
possible constructor. Again, we also get some auxiliary
equalities that are rewritten in the goal but not in the other
hypotheses.
Theorem ev_minus2': ∀n,
ev n → ev (pred (pred n)).
Proof.
intros n E. inversion E as [| n' E'].
Case "E = ev_0". simpl. apply ev_0.
Case "E = ev_SS n' E'". simpl. apply E'. Qed.
Exercise: 3 stars, advanced (ev_ev__ev)
Finding the appropriate thing to do induction on is a bit tricky here:Theorem ev_ev__ev : ∀n m,
ev (n+m) → ev n → ev m.
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 3 stars, optional (ev_plus_plus)
Here's an exercise that just requires applying existing lemmas. No induction or even case analysis is needed, but some of the rewriting may be tedious. You'll want the replace tactic used for plus_swap' in Basics.vTheorem ev_plus_plus : ∀n m p,
ev (n+m) → ev (n+p) → ev (m+p).
Proof.
(* FILL IN HERE *) Admitted.
☐
Building Proof Objects Incrementally (Optional)
Definition b_16_atmpt_1 : beautiful 16 := admit.
Maybe we can use b_sum to construct a term of type beautiful 16?
Recall that b_sum is of type
If we can demonstrate the beauty of 5 and 11, we should
be done.
∀n m : nat, beautiful n → beautiful m → beautiful (n + m)
Definition b_16_atmpt_2 : beautiful 16 := b_sum 5 11 admit admit.
In the attempt above, we've omitted the proofs of the propositions
that 5 and 11 are beautiful. But the first of these is already
axiomatized in b_5:
Definition b_16_atmpt_3 : beautiful 16 := b_sum 5 11 b_5 admit.
What remains is to show that 11 is beautiful. We repeat the
procedure:
Definition b_16_atmpt_4 : beautiful 16 :=
b_sum 5 11 b_5 (b_sum 5 6 admit admit).
Definition b_16_atmpt_5 : beautiful 16 :=
b_sum 5 11 b_5 (b_sum 5 6 b_5 admit).
Definition b_16_atmpt_6 : beautiful 16 :=
b_sum 5 11 b_5 (b_sum 5 6 b_5 (b_sum 3 3 admit admit)).
And finally, we can complete the proof object:
Definition b_16 : beautiful 16 :=
b_sum 5 11 b_5 (b_sum 5 6 b_5 (b_sum 3 3 b_3 b_3)).
To recap, we've been guided by an informal proof that we have in
our minds, and we check the high level details before completing
the intricacies of the proof. The admit term allows us to do
this.
Additional Exercises
Exercise: 4 stars (palindromes)
A palindrome is a sequence that reads the same backwards as forwards.- Define an inductive proposition pal on list X that
captures what it means to be a palindrome. (Hint: You'll need
three cases. Your definition should be based on the structure
of the list; just having a single constructor
c : ∀l, l = rev l → pal lmay seem obvious, but will not work very well.)
- Prove that
∀l, pal (l ++ rev l).
- Prove that
∀l, pal l → l = rev l.
(* FILL IN HERE *)
☐
Exercise: 5 stars, optional (palindrome_converse)
Using your definition of pal from the previous exercise, prove that
∀l, l = rev l → pal l.
(* FILL IN HERE *)
☐
Exercise: 4 stars, advanced (subsequence)
A list is a subsequence of another list if all of the elements in the first list occur in the same order in the second list, possibly with some extra elements in between. For example,
[1,2,3]
is a subsequence of each of the lists
[1,2,3]
[1,1,1,2,2,3]
[1,2,7,3]
[5,6,1,9,9,2,7,3,8]
but it is not a subsequence of any of the lists
[1,1,1,2,2,3]
[1,2,7,3]
[5,6,1,9,9,2,7,3,8]
[1,2]
[1,3]
[5,6,2,1,7,3,8]
[1,3]
[5,6,2,1,7,3,8]
- Define an inductive proposition subseq on list nat that
captures what it means to be a subsequence. (Hint: You'll need
three cases.)
- Prove that subsequence is reflexive, that is, any list is a
subsequence of itself.
- Prove that for any lists l1, l2, and l3, if l1 is a
subsequence of l2, then l1 is also a subsequence of l2 ++
l3.
- (Optional, harder) Prove that subsequence is transitive — that is, if l1 is a subsequence of l2 and l2 is a subsequence of l3, then l1 is a subsequence of l3. Hint: choose your induction carefully!
(* FILL IN HERE *)
☐
☐
Exercise: 2 stars, optional (R_provability)
Suppose we give Coq the following definition:
Inductive R : nat → list nat → Prop :=
| c1 : R 0 []
| c2 : ∀n l, R n l → R (S n) (n :: l)
| c3 : ∀n l, R (S n) l → R n l.
Which of the following propositions are provable?
| c1 : R 0 []
| c2 : ∀n l, R n l → R (S n) (n :: l)
| c3 : ∀n l, R (S n) l → R n l.
- R 2 [1,0]
- R 1 [1,2,1,0]
- R 6 [3,2,1,0]
(* $Date: 2013-03-10 13:20:46 -0400 (Sun, 10 Mar 2013) $ *)