+ - 0:00:00
Notes for current slide
Notes for next slide

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

CS720

Logical Foundations of Computer Science

Lecture 7: Logical connectives in Coq

Tiago Cogumbreiro

1 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What have we learned so far

  • Comparing if two expressions are equal syntactically: e1 = e2
  • Implication P -> Q
  • Universal quantifier forall x, P

Is this all we can do?

2 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What have we learned so far

  • Comparing if two expressions are equal syntactically: e1 = e2
  • Implication P -> Q
  • Universal quantifier forall x, P

Is this all we can do? No.

We encoded predicates computationally:

  • In Basics.v we defined Nat.eqb: nat -> nat -> bool to compare if two naturals are equal.
  • In Basics.v we defined even: nat -> bool to check if a natural number is even

Computational predicates are limited in what they can describe (eg, functions in Coq have to be total), and are not very easy to reason about (ie, they are meant to compute/execute, not build logic statements).

2 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Today we will…

  • Logical connectives in Coq
PQPQP \land Q \qquad P \lor Q

Why are we learning this?

  • The building blocks of any interesting property
3 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Typing equality

What is the type of an equality?

Check beq_nat 2 2 = true.
Check forall (n m : nat), n + m = m + n.
4 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Typing equality

What is the type of an equality?

Check beq_nat 2 2 = true.
Check forall (n m : nat), n + m = m + n.

Both of these expressions have type Prop, for proposition.

4 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Are all propositions provable?

5 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Are all propositions provable?

Obviously no. How do you prove this proposition:

Check 0 = 1. (* Prints: 0 = 1: Prop *)
Goal 0 = 1.

Insights

  • We can write any proposition, even unprovable ones.
    We can write proposition 0 = 1, but we cannot prove it.

  • The fact that something is false is not the same as unprovable!
    We can prove that something is false (by showing it leads to false), eg, 0 = 1. .
    We cannot prove the law of the excluded middle in Coq.

  • In Coq, we must show evidence of what holds. (This is known as a constructive logic.)

5 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Propositions are still expressions (1/3)

What is the type of ex0:

Definition ex0 := beq_nat 2 2.
6 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Propositions are still expressions (1/3)

What is the type of ex0:

Definition ex0 := beq_nat 2 2.

What is the type of ex1? How can we use ex1?

Definition ex1 (n:nat) := beq_nat 2 n = true.
Check ex1.

ex1 is a function that returns a proposition, a parameterized proposition.
For which n is ex1 n provable?

6 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Propositions are still expressions (1/3)

What is the type of ex0:

Definition ex0 := beq_nat 2 2.

What is the type of ex1? How can we use ex1?

Definition ex1 (n:nat) := beq_nat 2 n = true.
Check ex1.

ex1 is a function that returns a proposition, a parameterized proposition.
For which n is ex1 n provable?

Lemma easy:
forall n, n = 2 -> ex1 n.
Proof.

(Done in class.)

6 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Propositions are still expressions (2/3)

What is the difference between ex1 and ex2?

Definition ex1 (n:nat) := beq_nat 2 n = true.
Theorem ex2: forall (n:nat), beq_nat 2 n = true.
7 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Propositions are still expressions (2/3)

What is the difference between ex1 and ex2?

Definition ex1 (n:nat) := beq_nat 2 n = true.
Theorem ex2: forall (n:nat), beq_nat 2 n = true.

ex1 defines a position (Prop), ex2 is a theorem definition and is expecting a proof.

What is the relation between ex3 and ex1, ex2?

Definition ex3 (n:nat) : beq_nat 2 n = true.
7 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Propositions are still expressions (2/3)

What is the difference between ex1 and ex2?

Definition ex1 (n:nat) := beq_nat 2 n = true.
Theorem ex2: forall (n:nat), beq_nat 2 n = true.

ex1 defines a position (Prop), ex2 is a theorem definition and is expecting a proof.

What is the relation between ex3 and ex1, ex2?

Definition ex3 (n:nat) : beq_nat 2 n = true.
  • Recall that Theorem and Definition are synonyms!
  • Thus, ex2 and ex3 are the same
7 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Logical connectives

8 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Conjunction

PQP \land Q

9 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \land Q?

  1. What is the type of PP?
10 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \land Q?

  1. What is the type of PP? Prop
  2. What is the type of QQ?
10 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \land Q?

  1. What is the type of PP? Prop
  2. What is the type of QQ? Prop
  3. What is the type of \land?
10 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \land Q?

  1. What is the type of PP? Prop
  2. What is the type of QQ? Prop
  3. What is the type of \land? Prop -> Prop -> Prop
10 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Split conjunctions in the goal

  • When a logical-and appears in the goal, use split

  • You need to prove both propositions

Goal 3 + 4 = 7 /\ 2 * 2 = 4.
Proof.
split.

(Done in class.)

11 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Conjunction example 1

More generally, we can show that if we have propositions AA and BB, we can conclude that we have ABA \land B.

Goal forall A B : Prop, A -> B -> A /\ B.
12 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Destruct conjunction in hypothesis

  • Case analysis A /\ B, how many proofs? how many goals?
Goal
forall x y,
3 + x = y /\ 2 * 2 = x ->
x = 4 /\ y = 7.
Proof.
intros x y Hconj.
destruct Hconj as [Hleft Hright].

(Done in class.)

13 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Conjunction example 2

Lemma correct_2 : forall A B : Prop, A /\ B -> A.
Proof.
Lemma correct_3 : forall A B : Prop, A /\ B -> B.
Proof.

(Done in class.)

14 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Disjunction

PQP \lor Q

15 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \lor Q?

  1. What is the type of PP?
16 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \lor Q?

  1. What is the type of PP? Prop
  2. What is the type of QQ?
16 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \lor Q?

  1. What is the type of PP? Prop
  2. What is the type of QQ? Prop
  3. What is the type of \lor?
16 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What is PQP \lor Q?

  1. What is the type of PP? Prop
  2. What is the type of QQ? Prop
  3. What is the type of \lor? Prop -> Prop -> Prop
16 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Choose disjunction in goal

  • Use left/right to pick what you want to prove
  • Only choose when you know you can prove it
Goal
forall n m : nat, Nat.beq n m = true \/ Nat.beq n m = false.
Proof.
17 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Destruct disjunction in hypothesis

  • Case analysis A \/ B, how many proofs? how many goals?
Lemma or_example :
forall n m : nat, n = 0 \/ m = 0 -> n * m = 0.
Proof.
intros n m Hor.
destruct Hor as [Heq | Heq].
18 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Falsehood

\bot

19 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Find contradiction, false in goal

  • False cannot be proved (we postpone how to our next lecture)
  • Equality contradictions can be handled via explosion principle (discriminate)
  • In this example we show that 1 = 2 is (leads to) false.
Goal
1 = 2 ->
False.
20 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Destruct false in hypothesis

  • Case analysis concludes any proof with False as assumption
Theorem ex_falso_quodlibet : forall (P:Prop),
False -> P.
Proof.
21 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Negation

¬P\neg P

22 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Not in assumption and contradictions

Definition not (P:Prop) := P -> False.
Notation "~ x" := (not x) : type_scope.
  • apply ~ P in P to reach contradiction
  • alternatively, use contradiction
Theorem contradiction_implies_anything : forall P Q : Prop,
(P /\ ~ P) -> Q.
Proof.
23 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

Negation in goal, proof by contradiction

  • Show ~ P by assuming P and reaching contradiction
Goal
~ False.
24 / 24

CS720: Lecture 7  𐫱  Tiago Cogumbreiro

What have we learned so far

  • Comparing if two expressions are equal syntactically: e1 = e2
  • Implication P -> Q
  • Universal quantifier forall x, P

Is this all we can do?

2 / 24
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow