(* Don't worry about the following definition. It is a trick allowing the teacher to state all exercises without solving the ones which are supposed to be solved below it. *) Definition admit {T: Type} : T. Admitted. (* A common notation for pattern matching on bool *) Definition returns_1_or_3 : bool -> nat := fun b => match b with | true => 1 | false => 3 end. Print returns_1_or_3. (* Coq 8.4 presents this function as: returns_1_or_3 = fun b : bool => if b then 1 else 3 More generally, if b then X else Y denotes match b with true => X | false => Y end *) (* This notation can be used in user-defined functions *) Definition another_1_or_3 : bool -> nat := fun b => if b then 1 else 3. (* You can redefine in this way - Boolean negation (negb) - Boolean conjunction (andb) - Boolean disjunction (orb). Such functions are actually available from the pre-loaded Coq library and will be used below. *) Print negb. Print andb. Print orb. (* More on recursive programming *) (* Lists of Booleans *) Inductive listbool : Set := | Nilb : listbool | Consb : bool -> listbool -> listbool. (* Some examples to be used in tests *) Definition lbt := Consb true Nilb. Definition lbtt := Consb true lbt. Definition lbftt := Consb false lbtt. Definition lbttt := Consb true lbtt. Definition lbf := Consb false Nilb. Definition lbff := Consb false lbf. Definition lbtff := Consb true lbff. Compute lbttt. Compute lbftt. (* The typical scheme for programming on lists is Fixpoint some_program (l: listbool) : type_of_result := match l with | Nilb => ... | Consb h t => ... h ... (some_program t) ... end. Note that t is structurally smaller than l when l = Consb h t See similar remark on nat in the new version of lecture02a.v and in lecture02a_hint.v *) (* Example: length *) Fixpoint lengthb (l: listbool) : nat := match l with | Nilb => 0 | Consb h t => S (lengthb t) end. Example test_lengthb_lbttt: lengthb lbttt = 3. Proof. reflexivity. Qed. (* Exercises *) (* For the following, find 2 solutions: - one using only pattern matching (including "if... then... else") - one using functions on Booleans, such as negb, andb, orb. *) Fixpoint alltrue (l: listbool) : bool := (* FILL IN HERE *) admit. Example test_alltrue_lbttt : alltrue lbttt = true. Proof. (*reflexivity. Qed.*) Admitted. Example test_alltrue_lbftt : alltrue lbftt = false. Proof. (*reflexivity. Qed.*) Admitted. (* Number of true in a listbool *) Fixpoint nb_true (l: listbool) : nat := (* FILL IN HERE *) admit. Example test_nb_true_lbftt : nb_true lbftt = 2. Proof. (*reflexivity. Qed.*) Admitted. (* Switch all values in a list *) Fixpoint switchall (l: listbool) : listbool := (* FILL IN HERE *) admit. Example test_switchall_lbftt : switchall lbftt = lbtff. Proof. (*reflexivity. Qed.*) Admitted. (* We generalize to lists of anything *) Inductive list (A: Set) : Set := | Nil : list A | Cons : A -> list A -> list A. (* Some examples to be used in tests *) Definition lt := Cons bool true (Nil bool). Definition ltt := Cons bool true lt. (* The argument bool can be guessed from the context, so we can write _ instead of bool *) Definition lftt := Cons _ false ltt. Definition lttt := Cons _ true ltt. Definition lf := Cons _ false (Nil _). Definition lff := Cons _ false lf. Definition ltff := Cons _ true lff. (* In the following context, _ stands for nat *) Definition l0 := Cons _ 0 (Nil nat). Definition l10 := Cons _ 1 l0. Definition l210 := Cons _ 2 l10. Compute lttt. Compute l210. (* The typical scheme for programming on lists is Fixpoint some_program (A: Set) (l: list) : type_of_result := match l with | Nilb => ... | Consb h t => ... h ... (some_program A t) ... end. Note that in pattern matching, the argument A is removed (Roughly, this is because this argument is homogeneously used in Inductive list (A: Set) : Set := etc.) Note that t is structurally smaller than l when l = Cons bool h t *) (* Example: length *) Fixpoint length (A: Set) (l: list A) : nat := match l with | Nil => 0 | Cons h t => S (length A t) end. Example test_lengthb_lttt: length bool lttt = 3. Proof. reflexivity. Qed. (* The previous function alltrue can be expressed on (list bool) instead of listbool. *) Fixpoint specializes (l: list bool) : listbool := match l with | Nil => Nilb | Cons h t => Consb h (specializes t) end. Example test_specializes_ftt: specializes lftt = lbftt. Proof. reflexivity. Qed. (* Exercises *) Fixpoint alltrue_again (l: list bool) : bool := (* FILL IN HERE *) admit. Example test_alltrue_again_lttt : alltrue_again lttt = true. Proof. (*reflexivity. Qed.*) Admitted. Example test_alltrue_again_lftt : alltrue_again lftt = false. Proof. (*reflexivity. Qed.*) Admitted. (* The following function returns true if and only if all its elements satisfy a Boolean condition given as an argument *) Fixpoint holds_everywhere (A: Set) (cond: A -> bool) (l: list A) : bool := (* FILL IN HERE *) admit. (** The following line imports all definitions from tests_nat.v. For it to work, you need to compile tests_nat.v into tests_nat.vo. (This is like making a .o file from a .c file.) Here are two ways to compile your code: - CoqIDE 1. Open tests_nat.v. 2. In the "Compile" menu, click on "Compile Buffer". - Command line 1. Run: coqc tests_nat.v *) Require Import tests_nat. (* beq_nat, ble_nat and evenb are now available *) Definition le10 (n: nat) := ble_nat n 10. Example holds_everywhere1: holds_everywhere nat le10 l210 = true. Proof. (*reflexivity. Qed.*) Admitted. Example holds_everywhere2: holds_everywhere _ (fun x => ble_nat x 5) l210 = true. Proof. (*reflexivity. Qed.*) Admitted. (* Number of 1 in a list nat *) Fixpoint nb_1 (l: list nat) : nat := (* FILL IN HERE *) admit. Example test_nb_1_l210 : nb_1 l210 = 1. Proof. (*reflexivity. Qed.*) Admitted. (* Returns the list of positions (marked as "true") of even numbers *) Fixpoint evenpos (l: list nat) : list bool := (* FILL IN HERE *) admit. Example test_evenpos : evenpos l210 = Cons _ true (Cons _ false (Cons _ true (Nil _))). Proof. (*reflexivity. Qed.*) Admitted. (* Generalisation: apply a given function to all elements of a list *) Fixpoint map (A B: Set) (f: A->B) (l: list A) : list B := (* FILL IN HERE *) admit. Example test_map : map nat bool evenb l210 = evenpos l210. Proof. (*reflexivity. Qed.*) Admitted.