CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
What do we mean by formalizing programming languages?
CS720: Lecture 3 ❧ Tiago Cogumbreiro
What do we mean by formalizing programming languages?
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Basic.v
CoqIDE:
Console:
make Basics.vo
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem plus_n_O : forall n:nat, n = n + 0.Proof.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem plus_n_O : forall n:nat, n = n + 0.Proof.
Tactic simpl
does nothing.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem plus_n_O : forall n:nat, n = n + 0.Proof.
Tactic simpl
does nothing.
Tactic reflexivity
fails.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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
CS720: Lecture 3 ❧ Tiago Cogumbreiro
After proving the first, we get
1 subgoaln : nat______________________________________(1/1)S n = S n + 0
Applying simpl
yields:
1 subgoaln : nat______________________________________(1/1)S n = S (n + 0)
CS720: Lecture 3 ❧ Tiago Cogumbreiro
After proving the first, we get
1 subgoaln : nat______________________________________(1/1)S n = S n + 0
Applying simpl
yields:
1 subgoaln : nat______________________________________(1/1)S n = S (n + 0)
Tactic reflexivity
fails and there is nothing to rewrite.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
nat
For some property P
we want to prove.
Conclude that P(n) holds for all n.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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
withdestruct n
.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
After proving the first goal we get
1 subgoaln : natIHn : n = n + 0______________________________________(1/1)S n = S n + 0
applying simpl
yields
1 subgoaln : natIHn : n = n + 0______________________________________(1/1)S n = S (n + 0)
How do we conclude this proof?
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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.forall
.{
and }
to prove a sub-goal.CS720: Lecture 3 ❧ Tiago Cogumbreiro
ltac
proofs are imperative, assume the reader can step throughCS720: Lecture 3 ❧ Tiago Cogumbreiro
ltac
proofTheorem 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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
ltac
proofTheorem 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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
ltac
proofTheorem 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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Definition fst (p : natprod) : nat :=
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem surjective_pairing : forall (p : natprod), p = (fst p, snd p).Proof. intros p.
1 subgoalp : natprod______________________________________(1/1)p = (fst p, snd p)
Does
simpl
work? Doesreflexivity
work? Doesdestruct
work? What aboutinduction
?
destruct p as [x y]. reflexivity.Qed.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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).
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem nil_app_l : forall l:natlist, [] ++ l = l.Proof. intros l.
Can we prove this with
reflexivity
? Why?
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem nil_app_l : forall l:natlist, [] ++ l = l.Proof. intros l.
Can we prove this with
reflexivity
? Why?
reflexivity.Qed.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem nil_app_l : forall l:natlist, l ++ [] = l.Proof. intros l.
Can we prove this with
reflexivity
? Why?
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem nil_app_l : forall l:natlist, l ++ [] = l.Proof. intros l.
Can we prove this with
reflexivity
? Why?
In environmentl : natlistUnable to unify "l" with "l ++ [ ]".
How can we prove this result?
CS720: Lecture 3 ❧ Tiago Cogumbreiro
natlist
For some property P
we want to prove.
Conclude that P(l) holds for all l.
How do we know this principle? Hint: compare
natlist
withnat
.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Inductive natlist : Type := | O : natlist | A: T | S : nat -> nat. | B: T -> T
Inductive natlist : Type := | nil : natlist | A: T | cons : nat -> natlist -> natlist. | B: X -> T -> T
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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
If P([]) and ∀n,∀l,P(l)⟹P(n::l), then P(l) for any l.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Theorem nil_app_r : forall l:natlist, l ++ [] = l.Proof. intros l. induction l. - reflexivity. -
yields
1 subgoaln : natl : natlistIHl : l ++ [ ] = l______________________________________(1/1)(n :: l) ++ [ ] = n :: l
CS720: Lecture 3 ❧ Tiago Cogumbreiro
1 subgoaln : natl : natlistIHl : l ++ [ ] = l______________________________________(1/1)(n :: l) ++ [ ] = n :: l
CS720: Lecture 3 ❧ Tiago Cogumbreiro
1 subgoaln : natl : natlistIHl : 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)?
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Inductive natoption : Type := | Some : nat -> natoption | None : natoption.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Fixpoint indexof n (l:natlist) : natoption :=
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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".
CS720: Lecture 3 ❧ Tiago Cogumbreiro
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.
CS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
Search
to browse definitionsCS720: Lecture 3 ❧ Tiago Cogumbreiro
CS720: Lecture 3 ❧ Tiago Cogumbreiro
What do we mean by formalizing programming languages?
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 |