# Logic: Logic in Coq

Like its built-in programming language, Coq's built-in logic
is extremely small: universal quantification (forall) and
implication (->) are primitive, but all the other familiar
logical connectives -- conjunction, disjunction, negation,
existential quantification, even equality -- can be defined using
just these together with the Inductive definition facility.
In fact, -> and forall are the
Coq's -> notation is actually just a shorthand for forall.
The forall notation is more general, because it allows us to

# Quantification and Implication

*same*primitive!*name*the hypothesis. For example, consider this proposition:
If we had a proof term inhabiting this proposition, it would be a
function with two arguments: a number n and some evidence that
n is even. But the name E for this evidence is not used in
the rest of the statement of funny_prop1, so it's a bit silly to
bother making up a name. We could write it like this instead:

Or we can write it in more familiar notation:

This illustrates that "P -> Q" is just syntactic sugar for
"forall (_:P), Q".
The logical conjunction of propositions P and Q is
represented by the following inductively defined
proposition.

# Conjunction

Note that, like the definition of ev in the previous
chapter, this definition is parameterized; however, in this case,
the parameters are themselves propositions.
The intuition behind this definition is simple: to
construct evidence for and P Q, we must provide evidence
for P and evidence for Q. More precisely:
Since we'll be using conjunction a lot, let's introduce a more
familiar-looking infix notation for it.

- conj p q can be taken as evidence for and P Q if p
is evidence for P and q is evidence for Q; and
- this is the
*only*way to give evidence for and P Q -- that is, if someone gives us evidence for and P Q, we know it must have the form conj p q, where p is evidence for P and q is evidence for Q.

(The type_scope annotation tells Coq that this notation
will be appearing in propositions, not values.)
Consider the "type" of the constructor conj:

Notice that it takes 4 inputs, namely the propositions P,Q and
evidence of P, Q, and returns as output the evidence of P /\ Q.
Besides the elegance of building everything up from a tiny
foundation, what's nice about defining conjunction this way is
that we can prove statements involving conjunction using the
tactics that we already know. For example, if the goal statement
is a conjuction, we can prove it by applying the single
constructor conj, which (as can be seen from the type of conj)
solves the current goal and leaves the two parts of the
conjunction as subgoals to be proved separately.

Theorem and_example :

(ev 0) /\ (ev 4).

Proof.

apply conj.

(* Case "left". *) apply ev_0.

(* Case "right". *) apply ev_SS. apply ev_SS. apply ev_0. Qed.

Let's take a look at the proof object for the above theorem.

Print and_example.

(* ====> conj (ev 0) (ev 4) ev_0 (ev_SS 2 (ev_SS 0 ev_0))

: ev 0 /\ ev 4 *)

Note that the proof is of the form
Just for convenience, we can also use split as a shorthand for
apply conj.

conj (ev 0) (ev 4) (...pf of ev 0...) (...pf of ev 4...)

which is what you'd expect, given the type of conj.
#### Exercise: 1 star, optional

The Case tactics were commented out in the proof of and_example to avoid cluttering the proof object. What would you guess the proof object will look like if we uncomment them? Try it and see. ☐Theorem and_example' :

(ev 0) /\ (ev 4).

Proof.

split.

Case "left". apply ev_0.

Case "right". apply ev_SS. apply ev_SS. apply ev_0. Qed.

Conversely, the inversion tactic can be used to take a
conjunction hypothesis in the context, calculate what evidence
must have been used to build it, and put this evidence into the
proof context.

Theorem proj1 : forall P Q : Prop,

P /\ Q -> P.

Proof.

intros P Q H.

inversion H as [HP HQ].

apply HP. Qed.

☐

Theorem and_commut : forall P Q : Prop,

P /\ Q -> Q /\ P.

Proof.

(* WORKED IN CLASS *)

intros P Q H.

split.

Case "left". apply proj2 with (P:=P). apply H.

Case "right". apply proj1 with (Q:=Q). apply H. Qed.

#### Exercise: 2 stars

In the following proof, notice how the*nested pattern*in the inversion breaks H : P /\ (Q /\ R) down into HP: P, HQ : Q and HR : R. Finish the proof from there:

Theorem and_assoc : forall P Q R : Prop,

P /\ (Q /\ R) -> (P /\ Q) /\ R.

Proof.

intros P Q R H.

inversion H as [HP [HQ HR]].

(* FILL IN HERE *) Admitted.

☐

#### Exercise: 2 stars

Now we can prove the other direction of the equivalence of even and ev, which we left hanging in the last chapter. Notice that the left-hand conjunct here is the statement we are actually interested in; the right-hand conjunct is needed in order to make the induction hypothesis strong enough that we can carry out the reasoning in the inductive step. (To see why this is needed, try proving the left conjunct by itself and observe where things get stuck.)Theorem even_ev : forall n : nat,

(even n -> ev n) /\ (even (S n) -> ev (S n)).

Proof.

(* Hint: Use induction on n. *)

(* FILL IN HERE *) Admitted.

Definition iff (P Q : Prop) := (P -> Q) /\ (Q -> P).

Notation "P <-> Q" := (iff P Q) (at level 95, no associativity) : type_scope.

Theorem iff_implies : forall P Q : Prop,

(P <-> Q) -> P -> Q.

Proof.

intros P Q H.

inversion H as [HAB HBA]. apply HAB. Qed.

Theorem iff_sym : forall P Q : Prop,

(P <-> Q) -> (Q <-> P).

Proof.

(* WORKED IN CLASS *)

intros P Q H.

inversion H as [HAB HBA].

split.

Case "->". apply HBA.

Case "<-". apply HAB. Qed.

#### Exercise: 1 star (iff_properties)

Using the above proof that <-> is symmetric (iff_sym) as a guide, prove that it is also reflexive and transitive.
Hint: If you have an iff hypothesis in the context, you
can use inversion to break it into two separate
implications. (Think about why this works.)

Theorem iff_trans : forall P Q R : Prop,

(P <-> Q) -> (Q <-> R) -> (P <-> R).

Proof.

(* FILL IN HERE *) Admitted.

☐

#### Exercise: 2 stars

We have seen that the families of propositions MyProp and ev actually characterize the same set of numbers (the even ones). Prove that MyProp n <-> ev n for all n. Just for fun, write your proof as an explicit proof object, rather than using tactics. (*Hint*: you should only need a single line!)
☐
Unfortunately, propositions phrased with <-> are a bit
inconvenient to use as hypotheses or lemmas, because they have to
be deconstructed into their two directed components in order to be
applied. (The basic problem is that there's no way to apply an
iff proposition directly. If it's a hypothesis, you can invert
it, which is tedious; if it's a lemma, you have to destruct it
into hypotheses, which is worse.) It can be made much more
convenient using a Coq feature called "setoid rewriting," but
explaining how that works would take us too far afield. In the
rest of the course, we'll just avoid using iff very much.
Disjunction ("logical or") can also be defined as an
inductive proposition.

# Disjunction

Inductive or (P Q : Prop) : Prop :=

| or_introl : P -> or P Q

| or_intror : Q -> or P Q.

Notation "P \/ Q" := (or P Q) : type_scope.

Consider the "type" of the constructor or_introl:

It takes 3 inputs, namely the propositions P,Q and
evidence of P, and returns as output, the evidence of P /\ Q.
Next, look at the type of or_intror:

It is like or_introl but it requires evidence of Q
instead of evidence of P.
Intuitively, there are two ways of giving evidence for P \/ Q:

- give evidence for P (and say that it is P you are giving
evidence for! -- this is the function of the or_introl
constructor), or
- give evidence for Q, tagged with the or_intror constructor.

Theorem or_commut : forall P Q : Prop,

P \/ Q -> Q \/ P.

Proof.

intros P Q H.

inversion H as [HP | HQ].

Case "left". apply or_intror. apply HP.

Case "right". apply or_introl. apply HQ. Qed.

From here on, we'll use the shorthand tactics left and right
in place of apply or_introl and apply or_intror.

Theorem or_commut' : forall P Q : Prop,

P \/ Q -> Q \/ P.

Proof.

intros P Q H.

inversion H as [HP | HQ].

Case "left". right. apply HP.

Case "right". left. apply HQ. Qed.

Theorem or_distributes_over_and_1 : forall P Q R : Prop,

P \/ (Q /\ R) -> (P \/ Q) /\ (P \/ R).

Proof.

intros P Q R. intros H. inversion H as [HP | [HQ HR]].

Case "left". split.

SCase "left". left. apply HP.

SCase "right". left. apply HP.

Case "right". split.

SCase "left". right. apply HQ.

SCase "right". right. apply HR. Qed.

Theorem or_distributes_over_and_2 : forall P Q R : Prop,

(P \/ Q) /\ (P \/ R) -> P \/ (Q /\ R).

Proof.

(* FILL IN HERE *) Admitted.

(P \/ Q) /\ (P \/ R) -> P \/ (Q /\ R).

Proof.

(* FILL IN HERE *) Admitted.

Theorem or_distributes_over_and : forall P Q R : Prop,

P \/ (Q /\ R) <-> (P \/ Q) /\ (P \/ R).

Proof.

(* FILL IN HERE *) Admitted.

P \/ (Q /\ R) <-> (P \/ Q) /\ (P \/ R).

Proof.

(* FILL IN HERE *) Admitted.

☐
The induction principles for conjunction and disjunction are a
good illustration of Coq's way of generating simplified induction
principles for Inductively defined propositions, which we
discussed in the last chapter. You try first:

## Digression: Induction Principles for /\ and \/

#### Exercise: 1 star (and_ind_principle)

See if you can predict the induction principle for conjunction.
(* Check and_ind. *)

☐

#### Exercise: 1 star (or_ind_principle)

See if you can predict the induction principle for disjunction.
(* Check or_ind. *)

☐

From the inductive definition of the proposition and P Q
We've already seen several places where analogous structures
can be found in Coq's computational (Type) and logical (Prop)
worlds. Here is one more: the boolean operators andb and orb
are obviously analogs, in some sense, of the logical connectives
/\ and \/. This analogy can be made more precise by the
following theorems, which show how to translate knowledge about
andb and orb's behaviors on certain inputs into propositional
facts about those inputs.

Inductive and (P Q : Prop) : Prop :=

conj : P -> Q -> (and P Q).

we might expect Coq to generate this induction principle
conj : P -> Q -> (and P Q).

and_ind_max :

forall (P Q : Prop) (P0 : P /\ Q -> Prop),

(forall (a : P) (b : Q), P0 (conj P Q a b)) ->

forall a : P /\ Q, P0 a

but actually it generates this simpler and more useful one:
forall (P Q : Prop) (P0 : P /\ Q -> Prop),

(forall (a : P) (b : Q), P0 (conj P Q a b)) ->

forall a : P /\ Q, P0 a

and_ind :

forall P Q P0 : Prop,

(P -> Q -> P0) ->

P /\ Q -> P0

In the same way, when given the inductive definition of or P Q
forall P Q P0 : Prop,

(P -> Q -> P0) ->

P /\ Q -> P0

Inductive or (P Q : Prop) : Prop :=

| or_introl : P -> or P Q

| or_intror : Q -> or P Q.

instead of the "maximal induction principle"
| or_introl : P -> or P Q

| or_intror : Q -> or P Q.

or_ind_max :

forall (P Q : Prop) (P0 : P \/ Q -> Prop),

(forall a : P, P0 (or_introl P Q a)) ->

(forall b : Q, P0 (or_intror P Q b)) ->

forall o : P \/ Q, P0 o

what Coq actually generates is this:
forall (P Q : Prop) (P0 : P \/ Q -> Prop),

(forall a : P, P0 (or_introl P Q a)) ->

(forall b : Q, P0 (or_intror P Q b)) ->

forall o : P \/ Q, P0 o

or_ind :

forall P Q P0 : Prop,

(P -> P0) ->

(Q -> P0) ->

P \/ Q -> P0

forall P Q P0 : Prop,

(P -> P0) ->

(Q -> P0) ->

P \/ Q -> P0

## Relating /\ and \/ with andb and orb

Theorem andb_true__and : forall b c,

andb b c = true -> b = true /\ c = true.

Proof.

(* WORKED IN CLASS *)

intros b c H.

destruct b.

Case "b = true". destruct c.

SCase "c = true". apply conj. reflexivity. reflexivity.

SCase "c = false". inversion H.

Case "b = false". inversion H. Qed.

Theorem and__andb_true : forall b c,

b = true /\ c = true -> andb b c = true.

Proof.

(* WORKED IN CLASS *)

intros b c H.

inversion H.

rewrite H0. rewrite H1. reflexivity. Qed.

Theorem andb_false : forall b c,

andb b c = false -> b = false \/ c = false.

Proof.

(* FILL IN HERE *) Admitted.

Theorem orb_true : forall b c,

orb b c = true -> b = true \/ c = true.

Proof.

(* FILL IN HERE *) Admitted.

Theorem orb_false : forall b c,

orb b c = false -> b = false /\ c = false.

Proof.

(* FILL IN HERE *) Admitted.

andb b c = false -> b = false \/ c = false.

Proof.

(* FILL IN HERE *) Admitted.

Theorem orb_true : forall b c,

orb b c = true -> b = true \/ c = true.

Proof.

(* FILL IN HERE *) Admitted.

Theorem orb_false : forall b c,

orb b c = false -> b = false /\ c = false.

Proof.

(* FILL IN HERE *) Admitted.

☐
Logical falsehood can be represented in Coq as an inductively
defined proposition with no constructors.

# Falsehood

Intuition: False is a proposition for which there is no way
to give evidence.

#### Exercise: 1 star (False_ind_principle)

Can you predict the induction principle for falsehood?
(* Check False_ind. *)

☐
Since False has no constructors, inverting an assumption
of type False always yields zero subgoals, allowing us to
immediately prove any goal.

Theorem False_implies_nonsense :

False -> plus 2 2 = 5.

Proof.

intros contra.

inversion contra. Qed.

How does this work? The inversion tactic breaks contra into
each of its possible cases, and yields a subgoal for each case.
As contra is evidence for False, it has
Conversely, the only way to prove False is if there is already
something nonsensical or contradictory in the context:

*no*possible cases, hence, there are no possible subgoals and the proof is done.Theorem nonsense_implies_False :

plus 2 2 = 5 -> False.

Proof.

intros contra.

inversion contra. Qed.

Actually, since the proof of False_implies_nonsense
doesn't actually have anything to do with the specific nonsensical
thing being proved; it can easily be generalized to work for an
arbitrary P:

Theorem ex_falso_quodlibet : forall (P:Prop),

False -> P.

Proof.

intros P contra.

inversion contra. Qed.

The Latin
Since we have defined falsehood in Coq, we might wonder whether it
is possible to define truth in the same way. Naturally, the
answer is yes.

*ex falso quodlibet*means, literally, "from falsehood follows whatever you please." This theorem is also known as the*principle of explosion*.## Truth

#### Exercise: 2 stars

Define True as another inductively defined proposition. What induction principle will Coq generate for your definition? (The intution is that True should be a proposition for which it is trivial to give evidence. Alternatively, you may find it easiest to start with the induction principle and work backwards to the inductive definition.)(* FILL IN HERE *)

☐
However, unlike False, which we'll use extensively, True is
just a theoretical curiosity: it is trivial (and therefore
uninteresting) to prove as a goal, and it carries no useful
information as a hypothesis.
The logical complement of a proposition P is written not
P or, for shorthand, ~P:

# Negation

Definition not (P:Prop) := P -> False.

Notation "~ x" := (not x) : type_scope.

Check not.

(* ====> Prop -> Prop *)

The intuition is that, if P is not true, then anything at
all (even False) should follow from assuming P.
It takes a little practice to get used to working with
negation in Coq. Even though you can see perfectly well why
something is true, it can be a little hard at first to get things
into the right configuration so that Coq can see it! Here are
proofs of a few familiar facts about negation to get you warmed
up.

Theorem not_False :

~ False.

Proof.

unfold not. intros H. inversion H. Qed.

Theorem contradiction_implies_anything : forall P Q : Prop,

(P /\ ~P) -> Q.

Proof.

(* WORKED IN CLASS *)

intros P Q H. inversion H as [HP HNA]. unfold not in HNA.

apply HNA in HP. inversion HP. Qed.

Theorem double_neg : forall P : Prop,

P -> ~~P.

Proof.

(* WORKED IN CLASS *)

intros P H. unfold not. intros G. apply G. apply H. Qed.

#### Exercise: 2 stars (double_neg_inf)

Write an informal proof of double_neg:*Theorem*: P implies ~~P, for any proposition P.

*Proof*: (* FILL IN HERE *)

☐

#### Exercise: 2 stars

Theorem contrapositive : forall P Q : Prop,

(P -> Q) -> (~Q -> ~P).

Proof.

(* FILL IN HERE *) Admitted.

(P -> Q) -> (~Q -> ~P).

Proof.

(* FILL IN HERE *) Admitted.

☐

Theorem five_not_even :

~ ev 5.

Proof.

(* WORKED IN CLASS *)

unfold not. intros Hev5. inversion Hev5 as [|n Hev3 Heqn].

inversion Hev3 as [|n' Hev1 Heqn']. inversion Hev1. Qed.

#### Exercise: 1 star

Theorem five_not_even confirms the unsurprising fact that five is not an even number. Prove this more interesting fact:Theorem ev_not_ev_S : forall n,

ev n -> ~ ev (S n).

Proof.

unfold not. intros n H. induction H. (* not n! *)

(* FILL IN HERE *) Admitted.

☐

#### Exercise: 1 star, optional (informal_not_PNP)

Write an informal proof (in English) of the proposition forall P : Prop, ~(P /\ ~P).(* FILL IN HERE *)

☐

#### Exercise: 5 stars, optional (classical_axioms)

For those who like a challenge, here is an exercise taken from the Coq'Art book (p. 123). The following five statements are often considered as characterizations of classical logic (as opposed to constructive logic, which is what is "built in" to Coq). We can't prove them in Coq, but we can consistently add any one of them as an unproven axiom if we wish to work in classical logic. Prove that these five propositions are equivalent.Definition peirce := forall P Q: Prop,

((P->Q)->P)->P.

Definition classic := forall P:Prop,

~~P -> P.

Definition excluded_middle := forall P:Prop,

P \/ ~P.

Definition de_morgan_not_and_not := forall P Q:Prop,

~(~P/\~Q) -> P\/Q.

Definition implies_to_or := forall P Q:Prop,

(P->Q) -> (~P\/Q).

(* FILL IN HERE *)

Notation "x <> y" := (~ (x = y)) : type_scope.

Since inequality involves a negation, it again requires
a little practice to be able to work with it fluently. Here
is one very useful trick. If you are trying to prove a goal
that is nonsensical (e.g., the goal state is false = true),
apply the lemma ex_falso_quodlibet to change the goal to
False. This makes it easier to use assumptions of the form
~P that are available in the context -- in particular,
assumptions of the form x<>y.

Theorem not_false_then_true : forall b : bool,

b <> false -> b = true.

Proof.

intros b H. destruct b.

Case "b = true". reflexivity.

Case "b = false".

unfold not in H.

apply ex_falso_quodlibet.

apply H. reflexivity. Qed.

Theorem not_eq_beq_false : forall n n' : nat,

n <> n' ->

beq_nat n n' = false.

Proof.

(* FILL IN HERE *) Admitted.

n <> n' ->

beq_nat n n' = false.

Proof.

(* FILL IN HERE *) Admitted.

Theorem beq_false_not_eq : forall n m,

false = beq_nat n m -> n <> m.

Proof.

(* FILL IN HERE *) Admitted.

false = beq_nat n m -> n <> m.

Proof.

(* FILL IN HERE *) Admitted.

☐
Another critical logical connective is

# Existential Quantification

*existential quantification*. Here is one way of defining it.Module ExFirstTry.

Inductive ex : forall X:Type, (X->Prop) -> Prop :=

ex_intro : forall (X:Type) (P: X->Prop),

forall (witness:X),

P witness ->

ex X P.

That is, ex is a family of propositions indexed by a type X
and a property P over X. In order to give evidence for the
assertion "there exists an x for which the property P holds"
we must actually name a
For example, consider this existentially quantified proposition:

*witness*-- a specific value x -- and then give evidence for P x, i.e., evidence that x has the property P.
To prove this proposition, we need to choose a particular number
as witness -- say, 4 -- and give some evidence that that number is
even.

However, this is not actually the best way to define ex: if we
do it this way, Coq will generate the following rather complex
induction axiom:

Check ex_ind.

(* ===> forall Q : (forall X : Type, (X->Prop) -> Prop),

(forall (X : Type) (P : X->Prop) (witness : X),

P witness -> Q X P) ->

forall (X : Type) (P : X->Prop), ex X P -> Q X P

(modulo a little renaming...) *)

The reason this happened is that Coq uses the part of the
Inductive declaration between the : and the := as the
argument type T for the proposition P in the induction
principle "for all properties P over things of type T, if
...(some clause for each constructor)... then P holds for
everything of type T."

A much simpler induction axiom is generated if we rearrange the
definition so that the parameters X and P are introduced at
the very outside, rather than with the ex_intro constructor.

(Note that this is the version we saw in class. The first version
was added to the notes later.)
One way to state the effect of this change is to say that we are
now defining a
The ex_intro constructor gets exactly the same type as
before:

*parameterized inductively defined proposition*, rather than an*inductively defined parameterized proposition*. That is, the new ex can be thought of as a*function*that takes X and P and returns an inductively defined proposition: the whole inductive definition is inside the body of the function, rather than the function being inside the constructor ex_intro.
But the induction axiom is much simpler and easier to work with,
because the parameters X and P are bound once at the beginning
instead of appearing over and over:

Check ex_ind.

(* ===> forall (X:Type) (P: X->Prop) (Q: Prop),

(forall witness:X, P witness -> Q) ->

ex X P ->

Q *)

This principle can be understood as follows: If we have a
function f that can construct evidence for Q given
Next, Coq's notation definition facility can be used to
introduce more familiar notation for writing existentially
quantified propositions, exactly parallel to the built-in syntax
for universally quantified propositions. Instead of writing ex
nat ev to express the proposition that there exists some number
that is even, for example, we can write exists x:nat, ev x. (It
is not necessary to understand the details of how the Notation
definition works!)

*any*witness of type X together with evidence that this witness has property P, then from a proof of ex X P we can extract the witness and evidence that must have been supplied to the constructor, give these to f, and thus obtain a proof of Q.Notation "'exists' x , p" := (ex _ (fun x => p))

(at level 200, x ident, right associativity) : type_scope.

Notation "'exists' x : X , p" := (ex _ (fun x:X => p))

(at level 200, x ident, right associativity) : type_scope.

We can use the same set of tactics as always for
manipulating existentials. For example, if to prove an
existential, we apply the constructor ex_intro. Since the
premise of ex_intro involves a variable (witness) that does
not appear in its conclusion, we need to explicitly give its value
when we use apply.

Example exists_example_1 : exists n, plus n (mult n n) = 6.

Proof.

apply ex_intro with (witness:=2).

reflexivity. Qed.

Note, again, that we have to explicitly give the witness.
Or, instead of writing apply ex_intro with (witness:=e) all the
time, we can use the convenient shorthand exists e, which means
the same thing.

Conversely, if we have an existential hypothesis in the
context, we can eliminate it with inversion. Note the use
of the as... pattern to name the variable that Coq
introduces to name the witness value and get evidence that
the hypothesis holds for the witness. (If we don't
explicitly choose one, Coq will just call it witness, which
makes proofs confusing.)

Theorem exists_example_2 : forall n,

(exists m, n = plus 4 m) ->

(exists o, n = plus 2 o).

Proof.

intros n H.

inversion H as [m Hm].

exists (plus 2 m).

apply Hm. Qed.

(* FILL IN HERE *)

Complete the definition of the following proof object:

☐

#### Exercise: 1 star

Prove that "P holds for all x" and "there is no x for which P does not hold" are equivalent assertions.Theorem dist_not_exists : forall (X:Type) (P : X -> Prop),

(forall x, P x) -> ~ (exists x, ~ P x).

Proof.

(* FILL IN HERE *) Admitted.

☐

#### Exercise: 3 stars, optional

The other direction requires the classical "law of the excluded middle":
Theorem not_exists_dist :

excluded_middle ->

forall (X:Type) (P : X -> Prop),

~ (exists x, ~ P x) -> (forall x, P x).

Proof.

(* FILL IN HERE *) Admitted.

excluded_middle ->

forall (X:Type) (P : X -> Prop),

~ (exists x, ~ P x) -> (forall x, P x).

Proof.

(* FILL IN HERE *) Admitted.

Theorem dist_exists_or : forall (X:Type) (P Q : X -> Prop),

(exists x, P x \/ Q x) <-> (exists x, P x) \/ (exists x, Q x).

Proof.

(* FILL IN HERE *) Admitted.

☐
Even Coq's equality relation is not built in. It has the
following inductive definition. (We enclose the definition in a
module to avoid confusion with the standard library equality,
which we have used extensively already).

# Equality

Module MyEquality.

Inductive eq (P:Type) : P -> P -> Prop :=

refl_equal : forall x, eq P x x.

Notation "x = y" := (eq _ x y) (at level 70, no associativity) : type_scope.

This is a bit subtle. The way to think about it is that,
given a set P, it defines a
Now we are down to the bedrock: The primitive feature of Coq that
makes this definition work is the ability to make an Inductive
declaration with a constructor whose type only permits it to be
applied to three arguments where the second and third are the
same. (Strictly speaking, "the same
Here is an alternate definition -- the one that actually appears
in the Coq standard library.

*family*of propositions "x is equal to y," indexed by pairs of values (x and y) from P. There is just one way of constructing evidence for members of this family: by applying the constructor refl_equal to two identical arguments.*modulo simplification*.")Inductive eq' (P:Type) (x:P) : P -> Prop :=

refl_equal' : eq' P x x.

Notation "x =' y" := (eq' _ x y) (at level 70, no associativity) : type_scope.

Theorem two_defs_of_eq_coincide : forall (P:Type) x y,

eq P x y <-> eq' P x y.

Proof.

(* FILL IN HERE *) Admitted.

☐
The advantage of the second definition is that the induction
principle that Coq derives for it is precisely the familiar
principle of

*Leibniz equality*: what we mean when we say "x and y are equal" is that every property on P that is true of x is also true of y.Check eq'_ind.

(* ====> forall (P : Type) (x : P) (P0 : P -> Prop),

P0 x -> forall y : P, x =' y -> P0 y *)

End MyEquality.

## Inversion, Again

- takes a hypothesis H whose type is some inductively
defined proposition P
- for each constructor C in P's definition
- generates a new subgoal in which we assume H was
built with C
- adds the arguments (premises) of C to the context of
the subgoal as extra hypotheses
- matches the conclusion (result type) of C against the
current goal and calculates a set of equalities that must
hold in order for C to be applicable
- adds these equalities to the context of the subgoal
- if the equalities are not satisfiable (e.g., they involve things like S n = O, immediately solves the subgoal.

- generates a new subgoal in which we assume H was
built with C

*Example*: If we invert a hypothesis built with or, there are two constructors, so two subgoals get generated. The conclusion (result type) of the constructor (P \/ Q) doesn't place any restrictions on the form of P or Q, so we don't get any extra equalities in the context of the subgoal.

*Example*: If we invert a hypothesis built with and, there is only one constructor, so only one subgoal gets generated. Again, the conclusion (result type) of the constructor (P /\ Q) doesn't place any restrictions on the form of P or Q, so we don't get any extra equalities in the context of the subgoal. The constructor does have two arguments, though, and these can be seen in the context in the subgoal.

*Example*: If we invert a hypothesis built with eq, there is again only one constructor, so only one subgoal gets generated. Now, though, the form of the refl_equal constructor does give us some extra information: it tells us that the two arguments to eq must be equal! The inversion tactic adds this to the context.

# Relations as Propositions

*property*-- i.e., it defines a subset of nat, namely those numbers for which the proposition is provable. In the same way, a two-argument proposition can be thought of as a

*relation*-- i.e., it defines a set of pairs for which the proposition is provable. In the next few sections, we explore the consequences of this observation.

We've already seen an inductive definition of one
fundamental relation: equality. Another useful one is the "less
than or equal to" relation on numbers:
This definition should be fairly intuitive. It says that
there are two ways to give evidence that one number is less than
or equal to another: either observe that they are the same number,
or give evidence that the first is less than or equal to the
predecessor of the second.

Inductive le : nat -> nat -> Prop :=

| le_n : forall n, le n n

| le_S : forall n m, (le n m) -> (le n (S m)).

Check le_ind.

(* ===> forall P : nat -> nat -> Prop,

(forall n : nat, P n n) ->

(forall n m : nat, le n m -> P n m -> P n (S m)) ->

forall n n0 : nat, le n n0 -> P n n0 *)

End LeFirstTry.

This is a fine definition of the <= relation, but we can
streamline it a little by observing that the left-hand argument n
is the same everywhere in the definition, so we can actually make
it a "general parameter" to the whole definition, rather than an
argument to each constructor.

Inductive le (n:nat) : nat -> Prop :=

| le_n : le n n

| le_S : forall m, (le n m) -> (le n (S m)).

Notation "m <= n" := (le m n).

The second one is better, even though it looks less symmetric.
Why? Because it gives us a simpler induction principle.

Check le_ind.

(* ====> forall (n : nat) (P : nat -> Prop),

P n ->

(forall m : nat, n <= m -> P m -> P (S m)) ->

forall n0 : nat, n <= n0 -> P n0 *)

By contrast, the induction principle that Coq calculates for the
first definition has a lot of extra quantifiers, which makes it
messier to work with when proving things by induction. Here is
the induction principle for the first le:

(* Check le_ind. (the first one above) *)

(* ===> le_ind

: forall P : nat -> nat -> Prop,

(forall n : nat, P n n) ->

(forall n m : nat, LeFirstTry.le n m -> P n m -> P n (S m)) ->

forall n n0 : nat, LeFirstTry.le n n0 -> P n n0 *)

Proofs of facts about <= using the constructors le_n and
le_S follow the same patterns as proofs about properties, like
ev in the previous chapter. We can apply the constructors to
prove <= goals (e.g., to show that 3<=3 or 3<=6), and we can
use tactics like inversion to extract information from <=
hypotheses in the context (e.g., to prove that ~(2 <= 1).)
Here are some sanity checks on the definition. (Notice that,
although these are the same kind of simple "unit tests" as we gave
for the testing functions we wrote in the first few lectures, we
must construct their proofs explicitly -- simpl and
reflexivity don't do the job, because the proofs aren't just a
matter of simplifying computations.

Theorem test_le1 :

3 <= 3.

Proof.

(* WORKED IN CLASS *)

apply le_n. Qed.

Theorem test_le2 :

3 <= 6.

Proof.

(* WORKED IN CLASS *)

apply le_S. apply le_S. apply le_S. apply le_n. Qed.

Theorem test_le3 :

~ (2 <= 1).

Proof.

(* WORKED IN CLASS *)

intros H. inversion H. inversion H1. Qed.

The "strictly less than" relation n < m can now be defined
in terms of le.

A few more simple relations on numbers

Inductive square_of : nat -> nat -> Prop :=

sq : forall n:nat, square_of n (mult n n).

Inductive next_nat (n:nat) : nat -> Prop :=

| nn : next_nat n (S n).

Inductive next_even (n:nat) : nat -> Prop :=

| ne_1 : ev (S n) -> next_even n (S n)

| ne_2 : ev (S (S n)) -> next_even n (S (S n)).

#### Exercise: 2 stars

Define an inductive relation total_relation that holds between every pair of natural numbers.(* FILL IN HERE *)

(* FILL IN HERE *)

We can define three-place relations, four-place relations,
etc., in just the same way as binary relations. For example,
consider the following three-place relation on numbers:

Inductive R : nat -> nat -> nat -> Prop :=

| c1 : R 0 0 0

| c2 : forall m n o, R m n o -> R (S m) n (S o)

| c3 : forall m n o, R m n o -> R m (S n) (S o)

| c4 : forall m n o, R (S m) (S n) (S (S o)) -> R m n o

| c5 : forall m n o, R m n o -> R n m o.

- Which of the following propositions are provable?
- R 1 1 2
- R 2 2 6

- If we dropped constructor c5 from the definition of R,
would the set of provable propositions change? Briefly (1
sentence) explain your answer.
- If we dropped constructor c4 from the definition of R, would the set of provable propositions change? Briefly (1 sentence) explain your answer.

☐

#### Exercise: 3 stars, optional (R_fact)

State and prove an equivalent characterization of the relation R. That is, if R m n o is true, what can we say about m, n, and o, and vice versa?
(* FILL IN HERE *)

☐

#### Exercise: 4 stars (filter_challenge)

One of the main purposes of Coq is to prove that programs match their specifications. To this end, let's prove that our definition of filter matches a specification. Here is the specification, written out informally in English.
[1,4,6,2,3]

is an in-order merge of
[1,6,2]

and
[4,3].

(* FILL IN HERE *)

Inductive appears_in (X:Type) (a:X) : list X -> Prop :=

| ai_here : forall l, appears_in X a (a::l)

| ai_later : forall b l, appears_in X a l -> appears_in X a (b::l).

...gives us a precise way of saying that a value a appears at
least once as a member of a list l.
Use appears_in to define an inductive proposition no_repeats X
l, which should be provable exactly when l is a list (with
elements of type X) where every member is different from every
other. For example, no_repeats nat [1,2,3,4] and no_repeats bool
[] should be provable, while no_repeats nat [1,2,1] and
no_repeats bool [true,true] should not be.

(* FILL IN HERE *)

☐

#### Exercise: 3 stars (all_forallb)

Inductively define a property all of lists, parameterized by a type X and a property P : X -> Prop, such that all X P l asserts that P is true for every element of the list l.
Recall the function forallb, from the exercise
forall_exists_challenge in Poly.v:

Fixpoint forallb {X : Type} (test : X -> bool) (l : list X) : bool :=

match l with

| [] => true

| x :: l' => andb (test x) (forallb test l')

end.

Using the property all, write down a specification for forallb,
and prove that it satisfies the specification. Try to make your
specification as precise as possible.
Are there any important properties of the function forallb which
are not captured by your specification?

(* FILL IN HERE *)

☐
Let's pause briefly to record several facts about the <=
and < relations that we are going to need later in the
course. The proofs make good practice exercises.

## Digression: More facts about <= and <

#### Exercise: 2 stars, optional (le_exercises)

Theorem O_le_n : forall n,

0 <= n.

Proof.

(* FILL IN HERE *) Admitted.

Theorem n_le_m__Sn_le_Sm : forall n m,

n <= m -> S n <= S m.

Proof.

(* FILL IN HERE *) Admitted.

Theorem le_plus_l : forall a b,

a <= a + b.

Proof.

(* FILL IN HERE *) Admitted.

Theorem plus_lt : forall n1 n2 m,

plus n1 n2 < m ->

n1 < m /\ n2 < m.

Proof.

(* FILL IN HERE *) Admitted.

Theorem lt_S : forall n m,

n < m ->

n < S m.

Proof.

(* FILL IN HERE *) Admitted.

Theorem ble_nat_true : forall n m,

ble_nat n m = true -> n <= m.

Proof.

(* FILL IN HERE *) Admitted.

Theorem ble_nat_n_Sn_false : forall n m,

ble_nat n (S m) = false ->

ble_nat n m = false.

Proof.

(* FILL IN HERE *) Admitted.

Hint: Do the right induction!

Theorem ble_nat_false : forall n m,

ble_nat n m = false -> ~(n <= m).

Proof.

(* FILL IN HERE *) Admitted.

ble_nat n m = false -> ~(n <= m).

Proof.

(* FILL IN HERE *) Admitted.

☐
Q: What is the relation between a formal proof of a proposition
P and an informal proof of the same proposition P?
A: The latter should
Q: How much detail is needed?
A: There is no single right answer; rather, there is a range
of choices.
At one end of the spectrum, we can essentially give the
reader the whole formal proof (i.e., the informal proof
amounts to just transcribing the formal one into words).
This gives the reader the
At the other end of the spectrum, we can say "The theorem
is true and you can figure out why for yourself if you
think about it hard enough." This is also not a good
teaching strategy, because usually writing the proof
requires some deep insights into the thing we're proving,
and most readers will give up before they rediscover all
the same insights as we did.
In the middle is the golden mean -- a proof that includes
all of the essential insights (saving the reader the hard
part of work that we went through to find the proof in the
first place) and clear high-level suggestions for the more
routine parts to save the reader from spending too much
time reconstructing these parts (e.g., what the IH says and
what must be shown in each case of an inductive proof), but
not so much detail that the main ideas are obscured.
Another key point: if we're talking about a formal proof of a
proposition P and an informal proof of P, the proposition P
doesn't change. That is, formal and informal proofs are

# Informal Proofs, Again

*teach*the reader how to produce the former.*ability*to reproduce the formal one for themselves, but it doesn't*teach*them anything.*talking about the same world*and they must*play by the same rules*.