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

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

CS720

Logical Foundations of Computer Science

Lecture 5: Tactics

Tiago Cogumbreiro

1 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Tactics.v

2 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Exercise 1: transitivity over equals

Theorem eq_trans : forall (T:Type) (x y z : T),
x = y -> y = z -> x = z.
Proof.
intros T x y z eq1 eq2.
rewrite -> eq1.

yields

1 subgoal
T : Type
x, y, z : T
eq1 : x = y
eq2 : y = z
______________________________________(1/1)
y = z

How do we conclude this proof?

3 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Exercise 1: transitivity over equals

Theorem eq_trans : forall (T:Type) (x y z : T),
x = y -> y = z -> x = z.
Proof.
intros T x y z eq1 eq2.
rewrite -> eq1.

yields

1 subgoal
T : Type
x, y, z : T
eq1 : x = y
eq2 : y = z
______________________________________(1/1)
y = z

How do we conclude this proof? Yes, rewrite -> eq2. reflexivity. works.

3 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Exercise 1: introducing apply

Apply takes an hypothesis/lemma to conclude the goal.

apply eq2.
Qed.

apply takes ?X to conclude a goal ?X (resolves foralls in the hypothesis).

1 subgoal
T : Type
x, y, z : T
eq1 : x = y
eq2 : y = z
______________________________________(1/1)
y = z
4 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Applying conditional hypothesis

apply uses an hypothesis/theorem of format H1 -> ... -> Hn -> G, then solves goal G, and produces new goals H1, …, Hn.

Theorem eq_trans_2 : forall (T:Type) (x y z: T),
(x = y -> y = z -> x = z) -> (* eq1 *)
x = y -> (* eq2 *)
y = z -> (* eq3 *)
x = z.
Proof.
intros T x y z eq1 eq2 eq3.
apply eq1. (* x = y -> y = z -> x = z *)

(Done in class.)

5 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Rewriting conditional hypothesis

apply uses an hypothesis/theorem of format H1 -> ... -> Hn -> G, then solves goal G, and produces new goals H1, …, Hn.

Theorem eq_trans_3 : forall (T:Type) (x y z: T),
(x = y -> y = z -> x = z) -> (* eq1 *)
x = y -> (* eq2 *)
y = z -> (* eq3 *)
x = z.
Proof.
intros T x y z eq1 eq2 eq3.
rewrite -> eq1. (* x = y -> y = z -> x = z *)

(Done in class.)

Notice that there are 2 conditions in eq1, so we get 3 goals to solve.

6 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Recap

What's the difference between reflexivity, rewrite, and apply?

  1. reflexivity solves goals that can be simplified as an equality like ?X = ?X
  2. rewrite -> H takes an hypothesis H of type H1 -> ... -> Hn -> ?X = ?Y, finds any sub-term of the goal that matches ?X and replaces it by ?Y; it also produces goals H1,…, Hn. rewrite does not care about what your goal is, just that the goal must contain a pattern ?X.
  3. apply H takes an hypothesis H of type H1 -> ... -> Hn -> G and solves goal G; it creates goals H1, …, Hn.
7 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Apply with/Rewrite with

Theorem eq_trans_nat : forall (x y z: nat),
x = 1 ->
x = y ->
y = z ->
z = 1.
Proof.
intros x y z eq1 eq2 eq3.
assert (eq4: x = z). {
apply eq_trans.

outputs

Unable to find an instance for the variable y.

We can supply the missing arguments using the keyword with: apply eq_trans with (y:=y).

Can we solve the same theorem but use rewrite instead?

8 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Symmetry

What about this exercise?

Theorem eq_trans_nat : forall (x y z: nat),
x = 1 ->
x = y ->
y = z ->
1 = z.
Proof.
intros x y z eq1 eq2 eq3.
assert (eq4: x = z). {
9 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Symmetry

What about this exercise?

Theorem eq_trans_nat : forall (x y z: nat),
x = 1 ->
x = y ->
y = z ->
1 = z.
Proof.
intros x y z eq1 eq2 eq3.
assert (eq4: x = z). {

We can rewrite a goal ?X = ?Y into ?Y = ?X with symmetry.

9 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Apply in example

Theorem silly3' : forall (n : nat),
(Nat.eqb n 5 = true -> Nat.eqb (S (S n)) 7 = true) ->
true = Nat.eqb n 5 ->
true = Nat.eqb (S (S n)) 7.
Proof.
intros n eq H.
symmetry in H.
apply eq in H.

(Done in class.)

10 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Targetting hypothesis

  • rewrite -> H1 in H2
  • symmetry in H
  • apply H1 in H2
11 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Forward vs backward reasoning

If we have a theorem L: C1 -> C2 -> G:

  • Goal takes last: apply to goal of type G and replaces G by C1 and C2
  • Assumption takes first: apply to hypothesis L to an hypothesis H: C1 and rewrites H:C2 -> G

Proof styles:

  • Forward reasoning: (apply in hypothesis) manipulate the hypothesis until we reach a goal.
    Standard in math textbooks.
  • Backward reasoning: (apply to goal) manipulate the goal until you reach a state where you can apply the hypothesis.
    Idiomatic in Coq.
12 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Recall our encoding of natural numbers

Inductive nat : Type :=
| O : nat
| S : nat -> nat.
  1. Does the equation S n = 0 hold? Why?
13 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Recall our encoding of natural numbers

Inductive nat : Type :=
| O : nat
| S : nat -> nat.
  1. Does the equation S n = 0 hold? Why?
    No the constructors are implicitly disjoint.

  2. If S n = S m, can we conclude something about the relation between n and m?

13 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Recall our encoding of natural numbers

Inductive nat : Type :=
| O : nat
| S : nat -> nat.
  1. Does the equation S n = 0 hold? Why?
    No the constructors are implicitly disjoint.

  2. If S n = S m, can we conclude something about the relation between n and m?
    Yes, constructor S is injective. That is, if S n = S m, then n = m holds.

These two principles are available to all inductive definitions! How do we use these two properties in a proof?

13 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Proving that S is injective (1/2)

Theorem S_injective : forall (n m : nat),
S n = S m ->
n = m.
Proof.
intros n m eq1.
injection eq1 as eq2.

If we run injection, we get:

1 subgoal
n, m : nat
eq1 : S n = S m
eq2 : n = m
______________________________________(1/1)
m = m
14 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Disjoint constructors

Theorem Nat.eqb_0_l : forall n,
Nat.eqb 0 n = true -> n = 0.
Proof.
intros n eq1.
destruct n.

(To do in class.)

15 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Principle of explosion

Ex falso (sequitur) quodlibet

discriminate concludes absurd hypothesis, where there is an equality between different constructors. Use discriminate eq1 to conclude the proof below.

1 subgoal
n : nat
eq1 : false = true
______________________________________(1/1)
S n = 0
16 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

What we learned…

Tactics.v

  • Exploding principle
  • Forward and backward proof styles
  • New tactics: apply H and apply H in
  • Differences between apply and rewrite
  • New tactics: symmetry
  • New capability: rewrite ... in ...
  • New capability: simpl in ...
  • Constructors are disjoint and injective
17 / 17

CS720: Lecture 5  𐫱  Tiago Cogumbreiro

Tactics.v

2 / 17
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