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

CS720: Lecture 3  ❧  Tiago Cogumbreiro

CS720

Logical Foundations of Computer Science

Lecture 3: induction

Tiago Cogumbreiro

1 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Recap

  • We are currently learning the Logical Foundations (volume 1 of the SF book)
  • We are learning a programming language that allows us formalize programming languages

What do we mean by formalizing programming languages?

2 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Recap

  • We are currently learning the Logical Foundations (volume 1 of the SF book)
  • We are learning a programming language that allows us formalize programming languages

What do we mean by formalizing programming languages?

  1. A way to describe the abstract syntax (do we know how to do this?)
  2. A way to describe how language executes (do we know how to do this?)
  3. A way to describe properties of the language (do we know how to do this?)
2 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Today we will learn…

  • about proofs with recursive data structures
  • how to use induction in Coq
  • how to infer the induction principle
  • about the difference between informal and mechanized proofs
3 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Compile Basic.v

CoqIDE:

  • Open Basics.v. In the "Compile" menu, click on "Compile Buffer".

Console:

  • make Basics.vo
4 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (1/4)

Theorem plus_n_O : forall n:nat,
n = n + 0.
Proof.
5 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (1/4)

Theorem plus_n_O : forall n:nat,
n = n + 0.
Proof.

Tactic simpl does nothing.

5 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (1/4)

Theorem plus_n_O : forall n:nat,
n = n + 0.
Proof.

Tactic simpl does nothing. Tactic reflexivity fails.

5 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (1/4)

Theorem plus_n_O : forall n:nat,
n = n + 0.
Proof.

Tactic simpl does nothing. Tactic reflexivity fails. Apply destruct n.

2 subgoals
______________________________________(1/2)
0 = 0 + 0
______________________________________(2/2)
S n = S n + 0
5 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (2/4)

After proving the first, we get

1 subgoal
n : nat
______________________________________(1/1)
S n = S n + 0

Applying simpl yields:

1 subgoal
n : nat
______________________________________(1/1)
S n = S (n + 0)
6 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (2/4)

After proving the first, we get

1 subgoal
n : nat
______________________________________(1/1)
S n = S n + 0

Applying simpl yields:

1 subgoal
n : nat
______________________________________(1/1)
S n = S (n + 0)

Tactic reflexivity fails and there is nothing to rewrite.

6 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

We need an induction principle of nat

For some property P we want to prove.

  • Show that P(0)P(0) holds.
  • Given the induction hypothesis P(n)P(n), show that P(n+1)P(n+1) holds.

Conclude that P(n)P(n) holds for all nn.

7 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (3/4)

Apply induction n.

2 subgoals
______________________________________(1/2)
0 = 0 + 0
______________________________________(2/2)
S n = S n + 0

How do we prove the first goal?

Compare induction n with destruct n.

8 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Example: prove this lemma (4/4)

After proving the first goal we get

1 subgoal
n : nat
IHn : n = n + 0
______________________________________(1/1)
S n = S n + 0

applying simpl yields

1 subgoal
n : nat
IHn : n = n + 0
______________________________________(1/1)
S n = S (n + 0)

How do we conclude this proof?

9 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Intermediary results

Theorem mult_0_plus' : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m.
assert (H: 0 + n = n). { reflexivity. }
rewrite -> H.
reflexivity. Qed.
  • H is a variable name, you can pick whichever you like.
  • Your intermediary result will capture all of the existing hypothesis.
  • It may include forall.
  • We use braces { and } to prove a sub-goal.
10 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Formal versus informal proofs

  • The objective of a mechanical (formal) proofs is to convince the proof checker.
  • The objective of an informal proof is to convince (logically) the reader.
  • ltac proofs are imperative, assume the reader can step through
  • In informal proofs we want to help the reader reconstruct the proof state.
11 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

An example of an ltac proof

Theorem plus_assoc : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p. induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite -> IHn'. reflexivity. Qed.
  1. The proof follows by induction on nn.
12 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

An example of an ltac proof

Theorem plus_assoc : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p. induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite -> IHn'. reflexivity. Qed.
  1. The proof follows by induction on nn.
  2. In the base case, we have that n=0n = 0. We need to show 0+(m+p)=0+m+p0 + (m + p) = 0 + m + p, which follows by the definition of ++.
12 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

An example of an ltac proof

Theorem plus_assoc : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p. induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite -> IHn'. reflexivity. Qed.
  1. The proof follows by induction on nn.
  2. In the base case, we have that n=0n = 0. We need to show 0+(m+p)=0+m+p0 + (m + p) = 0 + m + p, which follows by the definition of ++.
  3. In the inductive case, we have n=S nn = \mathtt{S}\ n' and must show Sn+(m+p)=Sn+m+pS n' + (m + p) = S n' + m + p.
    From the definition of ++ it follows that S (n+(m+p))=S (n+m+p)\texttt{S}\ (n' + (m + p)) = \texttt{S}\ (n' + m + p).
    The proof concludes by applying the induction hypothesis n+(m+p)=n+m+pn' + (m + p) = n' + m + p.
12 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we define a data structure that holds two nats?

13 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

A pair of nats

Inductive natprod : Type :=
| pair : nat -> nat -> natprod.
Notation "( x , y )" := (pair x y).

Explicit vs implicit: be cautious when declaring notations, they make your code harder to understand.

14 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we read the contents of a pair?

15 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Accessors of a pair

16 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Accessors of a pair

Definition fst (p : natprod) : nat :=
16 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Accessors of a pair

Definition fst (p : natprod) : nat :=
match p with
| pair x y => x
end.
Definition snd (p : natprod) : nat :=
match p with
| (x, y) => y (* using notations in a pattern to be matched *)
end.
17 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we prove the correctness of our accessors?

(What do we expect fst/snd to do?)

18 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Proving the correctness of our accessors:

Theorem surjective_pairing : forall (p : natprod),
p = (fst p, snd p).
Proof.
intros p.
1 subgoal
p : natprod
______________________________________(1/1)
p = (fst p, snd p)

Does simpl work? Does reflexivity work? Does destruct work? What about induction?

19 / 39
destruct p as [x y].
reflexivity.
Qed.

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we define a list of nats?

20 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

A list of nats

Inductive natlist : Type :=
| nil : natlist
| cons : nat -> natlist -> natlist.
(* You don't need to learn notations, just be aware of its existence:*)
Notation "x :: l" := (cons x l) (at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := (cons x .. (cons y nil) ..).
Compute cons 1 (cons 2 (cons 3 nil)).

outputs:

= [1; 2; 3]
: list nat
21 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we concatenate two lists?

22 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Concatenating two lists

Fixpoint app (l1 l2 : natlist) : natlist :=
match l1 with
| nil => l2
| h :: t => h :: (app t l2)
end.
Notation "x ++ y" := (app x y) (right associativity, at level 60).
23 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Proving results on list concatenation

Theorem nil_app_l : forall l:natlist,
[] ++ l = l.
Proof.
intros l.

Can we prove this with reflexivity? Why?

24 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Proving results on list concatenation

Theorem nil_app_l : forall l:natlist,
[] ++ l = l.
Proof.
intros l.

Can we prove this with reflexivity? Why?

reflexivity.
Qed.
24 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Nil is a neutral element wrt app

Theorem nil_app_l : forall l:natlist,
l ++ [] = l.
Proof.
intros l.

Can we prove this with reflexivity? Why?

25 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Nil is a neutral element wrt app

Theorem nil_app_l : forall l:natlist,
l ++ [] = l.
Proof.
intros l.

Can we prove this with reflexivity? Why?

In environment
l : natlist
Unable to unify "l" with "l ++ [ ]".

How can we prove this result?

25 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

We need an induction principle of natlist

For some property P we want to prove.

  • Show that P([])P([]) holds.
  • Given the induction hypothesis P(l)P(l) and some number nn, show that P(n::l)P(n::l) holds.

Conclude that P(l)P(l) holds for all ll.

How do we know this principle? Hint: compare natlist with nat.

26 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Comparing nats with natlists

Inductive natlist : Type :=
| O : natlist | A: T
| S : nat -> nat. | B: T -> T
  1. P(A)\vdash P(A)
  2. t:T,P(t)P(B t)t:T, P(t) \vdash P(B\ t)
Inductive natlist : Type :=
| nil : natlist | A: T
| cons : nat -> natlist -> natlist. | B: X -> T -> T
  1. P(A)\vdash P(A)
  2. x:X,t:T,P(t)P(B t)x:X, t:T, P(t) \vdash P(B\ t)
27 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we know the induction principle?

Use search

Search natlist.

which outputs

nil: natlist
cons: nat -> natlist -> natlist
(* trimmed output *)
natlist_ind:
forall P : natlist -> Prop,
P [] ->
(forall (n : nat) (l : natlist), P l -> P (n::l)) -> forall n : natlist, P n
28 / 39

If P([])P([]) and n,l,P(l)    P(n::l)\forall n, \forall l, P(l) \implies P(n::l), then P(l)P(l) for any ll.

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Nil is neutral on the right (1/2)

Theorem nil_app_r : forall l:natlist,
l ++ [] = l.
Proof.
intros l.
induction l.
- reflexivity.
-

yields

1 subgoal
n : nat
l : natlist
IHl : l ++ [ ] = l
______________________________________(1/1)
(n :: l) ++ [ ] = n :: l
29 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Nil is neutral on the right (2/2)

1 subgoal
n : nat
l : natlist
IHl : l ++ [ ] = l
______________________________________(1/1)
(n :: l) ++ [ ] = n :: l
30 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Nil is neutral on the right (2/2)

1 subgoal
n : nat
l : natlist
IHl : l ++ [ ] = l
______________________________________(1/1)
(n :: l) ++ [ ] = n :: l
simpl. (* app (n::l) [] = n :: (app l []) *)
rewrite -> IHl. (* n :: (app l []) = n :: l *)
(* ^^^^^^^^ ^ *)
reflexivity. (* conclude *)

Can we apply rewrite directly without simplifying?

Hint: before and after stepping through a tactic show/hide notations.

How do we state a theorem that leads to the same proof state (without ltac)?

30 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we signal failure in a functional language?

31 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Partial functions

How declare a function that is not defined for empty lists?

(* Pairs the head and the list *)
Fixpoint indexof n (l:natlist) :=
match l with
| [] => ???
| h :: t =>
match beq_nat h n with
| true => 0
| false => S (indexof t)
end
end.
32 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Optional results

Inductive natoption : Type :=
| Some : nat -> natoption
| None : natoption.
33 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we declare indexof with optional types?

Fixpoint indexof n (l:natlist) : natoption :=
34 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we declare indexof with optional types?

Fixpoint indexof n (l:natlist) : natoption :=
match l with
| [] => None
| h :: t =>
match beq_nat h n with
| true => Some 0
| false => S (indexof n t)
end
end.
35 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we declare indexof with optional types?

Fixpoint indexof n (l:natlist) : natoption :=
match l with
| [] => None
| h :: t =>
match beq_nat h n with
| true => Some 0
| false => S (indexof n t)
end
end.
| false => S (indexof n t)
^^^^^^^^^^^
The term "indexof n t" has type "natoption" while it is expected to have type "nat".
35 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

How do we declare indexof with optional types?

Fixpoint indexof (n:nat) (l:natlist) : natoption :=
match l with
| [] => None
| h :: t =>
match beq_nat h n with
| true => Some 0 (* element found at the head *)
| false =>
match indexof n t with (* check for error *)
| Some i => Some (S i) (* increment successful result *)
| None => None (* propagate error *)
end
end
end.
36 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Summary

37 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Summary

  • implemented containers: pair, list, option
  • partial functions via option types
  • reviewed case analysis, proof by induction
  • used Search to browse definitions
37 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Next class: read Poly.v

38 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Ltac vocabulary

(Nothing new from Lesson 2.)

39 / 39

CS720: Lecture 3  ❧  Tiago Cogumbreiro

Recap

  • We are currently learning the Logical Foundations (volume 1 of the SF book)
  • We are learning a programming language that allows us formalize programming languages

What do we mean by formalizing programming languages?

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