Exercise sheet 1: Coq Core Topics and Tactics
From mathcomp Require Import all_ssreflect.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Definition fixme {T} : T. Admitted.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Definition fixme {T} : T. Admitted.
Exercise: complete the definition of these functions
Definition apply_two_parameters (f : nat → nat) (n : nat) : nat := fixme.
Definition apply_two_parameters_using_fun : (nat → nat) → nat → nat := fixme.
Definition apply_two_parameters_using_fun : (nat → nat) → nat → nat := fixme.
Exercise: use proof by computation to test the functions behave as intended,
for a return value of 4
- Exercise: write a function that takes two functions and returns their composition
Exercise: Implement xor, write a test for it
Exercise: complete proj2, which will return the proof of B from A ∧ B
As an example, we provide proj1 which returns the proof of A from A ∧ B
Definition proj1 (A B : Prop) (H : and A B) : A :=
match H with
| conj pA pB ⇒ pA
end.
Definition proj2 (A B : Prop) (H : and A B) : B := fixme.
match H with
| conj pA pB ⇒ pA
end.
Definition proj2 (A B : Prop) (H : and A B) : B := fixme.
Exercise: complete the function ex_wit which extracts the witness from an existential
Exercise: complete the following function
Definition forall_dist T (P Q : T → Prop) :
(∀ (x : T), P x ∧ Q x) →
(∀ (x : T), P x) ∧ (∀ (x : T), Q x) := fixme.
(∀ (x : T), P x ∧ Q x) →
(∀ (x : T), P x) ∧ (∀ (x : T), Q x) := fixme.
Exercise: complete the following function
Exercise: recall the weird_natural definition from the
lesson. Do you think it could work as a more efficient enconding than
the usualy nat unary one? If so, how would that enconding be? Rename
the constructors accordingly.
Inductive weird_natural : Type :=
| zero' : weird_natural
| succ' : weird_natural → weird_natural
| weird : weird_natural → weird_natural → weird_natural.
| zero' : weird_natural
| succ' : weird_natural → weird_natural
| weird : weird_natural → weird_natural → weird_natural.
Exercise: Complete the below function from weird_natural to nat
Exercise: Complete the below function for addition of weird_natural
Exercise: Write a test ensuring that weird_add and to_nat commute
Exercise: complete a division / remainder function
Exercise: write a test for the above function using nat_mul
Exercise: complete the following function that transforms a list pointwise
Exercise: complete the following function that filters a list
Exercise: filter a list of naturals to select the even numbers
Exercise: complete the following function that concatenates 2 lists
Exercise: complete the following function that reverses a list
Inductive types
Exercise: syntax for propositional logic. Complete the below inductive to add cases for and and or
Exercise: eval. Complete the below evalution function for the extended syntax
Fixpoint eval_form (f : form) : bool :=
match f with
| TT ⇒ true
| FF ⇒ true
| Not f ⇒ eval_form f
end.
Compute (eval_form (Not FF)).
match f with
| TT ⇒ true
| FF ⇒ true
| Not f ⇒ eval_form f
end.
Compute (eval_form (Not FF)).
Exercise: the above evalution function has some bugs; write a test
that showcases it. What do you need to assume? Fix the evaluation function.
Fixpoint eval_form' (f : form) : bool :=
match f with
| TT ⇒ true
| FF ⇒ true
| Not f ⇒ eval_form f
end.
State passing
Exercise: the definition of a function with state S from A to B is given below.
Complete the definition of function composition.
Definition fn_st S A B := A → S → B × S.
Definition fn_comp S A B C (f : fn_st S A B) (g: fn_st S B C) : fn_st S A C := fixme.
Definition fn_comp S A B C (f : fn_st S A B) (g: fn_st S B C) : fn_st S A C := fixme.
Exercise: prove that orC (orC P) = P
Exercise: prove
Exercise:
- look up the definition of iter
- prove this statement by induction
Exercise:
- look up the definition of iter (note there is an accumulator varying during recursion)
- prove the following statement by induction
Exercise:
- prove this statement using only rewrite and by
- use Search to find the relevant lemmas (all are good but for
ltn_neqAle) or browse the online doc
- proof sketch:
m < n = ~~ (n <= m) = ~~ (n == m || n < m) = n != m && ~~ (n < m) = ...
- proof sketch:
Exercise: use rewrite {pos}H to prove the following lemma:
Exercise: rewrite !H will rewrite with H until the goal doesn't change.
This can easily lead to infinite loops, for example when doing
rewrite addnC. Use a rewrite pattern
rewrite ![X in pat]addnC to avoid that and prove the below lemma:
Exercise:
- which is the size of an empty sequence?
- Use lemmas about size and filter to prove the below
Exercise: prove filter_all using induction