| {"text": "Require Import Arith.\n\n(* EXAMPLES *) \n(* The following function takes two arguments a and b\nwhich are numbers of type nat and returns b + 2 * (a + b) *)\nDefinition f_ex (a b : nat) := b + 2 * (a + b).\n\n(* When p is a pair, you can access its components by the \"pattern-matching\"\n construct illustrated in the following function. *)\nDefinition add_pair_components (p : nat * nat) :=\n match p with (a, b) => a + b end.\n\n(* f_ex is a function with two arguments. add_pair_components is a\n function with one argument, which is a pair. *)\n\n(* END OF EXAMPLES *)\n\n(* 1/ Define a function that takes two numbers as arguments and returns\n the sum of the squares of these numbers. *)\n\n(* 2/ Define a function that takes 5 arguments a b c d e, which are all\n numbers and returns the sum of all these numbers. *)\n\n(* 3/ Define a function named add5 that takes a number as argument and returns\n this number plus 5. *)\n\n(* The following should return 8 *)\n(* Compute add5 3.*)\n\n(* The following commands make it possible to find pre-defined functions\nSearch nat.\nSearch bool.\n *)\n\n(* 4/ Write a function swap of type list nat -> list nat such that\n swap (a::b::l) = (b::a::l) and\n swap l' = l' if l' has less than 2 elements. *)\n\n(* 5/ Write a function proc2 of type list nat -> nat, such that\n proc2 (a::b::l) = (b + 3) and\n proc2 l' = 0 if l' has less than 2 elements.\n\n In other words, proc2 only processes the 2nd argument of the list and\n returns that number plus 3. If there is no second argument to the list,\n proc2 returns 0. *)\n\n(* 6/ Write a function ms of type list nat -> list nat such that\n ms (a::b::...::nil) = a+2::b+2::...::nil\n For instance\n ms (2::3::5::nil) = (4::5::7::nil) *) \n\n(* 7/ Write a function sorted of type list nat -> bool, such that\n sorted l = true exactly when the natural numbers in\n l are in increasing order. *)\n\n(* 8/ Write a function p2 of type nat -> nat such that \n p2 n = 2 ^ n *)\n\n(* 9/ Write a function salt of type nat -> nat -> nat such that\n salt x n = x ^ n - x^ (n-1) + x^(n-2) .... + 1 or -1, if x <> 0,\n depending on the parity of n, thus\n salt x 3 = x^3 - x^2 + x - 1\n salt x 4 = x^4 - x^3 + x^2 - x + 1\n salt 2 3 = 5\n\n You may have to define auxiliary functions. *)\n\n\n(* 10/ Consider the following definition *)\n\nInductive btree : Type :=\n| leaf \n| bnode (n:nat)(t1 t2: btree).\n\n(* write a function that list the labels of a tree by infix order *)\n(** you may use the concatenation function app on lists \n (also written l1 ++ l2 ) *)\n\n\n(* write a boolean function that checks whether a tree is a binary \nsearch tree *)\n", "meta": {"author": "coq-community", "repo": "coq-art", "sha": "b3aaf69bc0c4809e482e931b633fa88ba1646996", "save_path": "github-repos/coq/coq-community-coq-art", "path": "github-repos/coq/coq-community-coq-art/coq-art-b3aaf69bc0c4809e482e931b633fa88ba1646996/ch2_types_expressions/SRC/ex1.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9603611626007843, "lm_q2_score": 0.9732407142435646, "lm_q1q2_score": 0.9346625838213675}} |
| {"text": "Require Import CpdtTactics.\n\n(** propositional logic *)\n\n(* (10 minutes) Propositional logic often comes with an A <=> B form that is\ntrue when A and B have the same truth values. Show how to define \"iff\" as\na Coq Prop. *)\n\nFunction iff (A : Prop) (B : Prop) : Prop := (A -> B) /\\ (B -> A).\n\nNotation \"a <=> b\" := (iff a b) (at level 77, right associativity).\nHint Unfold iff.\n\nTheorem iff_refl : forall P : Prop, P <=> P.\nProof.\n auto.\nQed.\n\nTheorem iff_sym : forall P Q : Prop, (P <=> Q) <=> (Q <=> P).\nProof.\n intros; constructor; intro; unfold iff in H; constructor; apply H.\nQed.\n\n(* How automated can you make these proofs? *)\n\n(* (15 minutes) In college-level logic classes, we learn that implication can\nbe expressed in terms of not and or operations (rather than in terms of\nforall). *)\nDefinition implies (A B : Prop) : Prop := ~A \\/ B.\nNotation \"a => b\" := (implies a b) (at level 75, right associativity).\nHint Unfold implies.\n\n(* One might wonder whether this form of implication is \"the same as\" Coq's\nversion that uses forall. State two theorems claiming that each kind of\nimplication can be converted into the other. Which one(s) can you prove? *)\n\nTheorem impl_implies : forall A B : Prop, A -> B -> A => B.\nProof. (* auto *)\n intros; unfold implies; unfold not; right; assumption.\nQed.\n\nTheorem implies_impl : forall A B : Prop, A => B -> A -> B.\nProof. \n intros; unfold implies in H; inversion H.\n unfold not in H1; apply H1 in H0; contradiction.\n assumption.\nQed.\n\n(* How about... *)\nAxiom nowai : forall P : Prop, P \\/ ~P.\n(* ...now? *)\n\n(* Some handy imports:\n Arith gives us nat, *, and +\n Arith.Even gives us even and odd\n Arith.Div2 gives us div2\n*)\nRequire Import Arith.\nRequire Import Arith.Even.\nRequire Import Arith.Div2.\nSet Implicit Arguments.\nHint Constructors even odd.\n\n(** propositions with implicit equality *)\n\n(* (35 minutes) The Collatz sequence starting at n is defined as the infinite\nsequence obtained by iterating the step function:\n\nstep(n) = n/2 if n is even\nstep(n) = 3n+1 if n is odd\n\nFor example, the sequence starting at 11 looks like\n\n11, 34, 17, 52, 26, 13, 40, 20, ...\n\nIt is conjectured that all collatz sequences eventually cycle (and in\nparticular reach 1 if they weren't 0 to begin with). Create an inductive\nproposition collatz_cycles which expresses the fact that a particular nat\neventually reaches a cycle, then state and prove the fact that the collatz\nsequence starting at 5 eventually cycles.\n\nDon't forget about \"Hint Constructors\" and \"auto n\" when creating proofs about\nconstants!\n*)\nPrint even.\n\n(* sequence, starting with n reaches k *)\nInductive collatz_reaches : nat -> nat -> Prop :=\n | obv : forall k : nat, collatz_reaches k k\n | evC : forall k n : nat, even n -> collatz_reaches k (n / 2) ->\n collatz_reaches k n\n | odC : forall k n : nat, odd n -> collatz_reaches k (3 * n + 1) ->\n collatz_reaches k n.\n\nInductive collatz_cycles (n : nat) : Prop :=\n | Cev : (exists k, even k /\\\n collatz_reaches k n /\\ collatz_reaches k (k / 2)) -> collatz_cycles n\n | Cod : (exists k, odd k /\\\n collatz_reaches k n /\\ collatz_reaches k (3 * k + 1)) -> collatz_cycles n.\n\nHint Constructors collatz_reaches.\n\nTheorem cc5 : collatz_cycles 5.\nProof.\n apply Cev; exists 4; split.\n auto 5. \n split. apply odC; auto 6.\n simpl; apply evC; auto 17.\n simpl; apply evC; auto 9.\n simpl; apply evC; auto 5.\n simpl; apply odC; auto 2.\nQed.\n\n(* (45 minutes) We say a list A is a \"subsequence\" of a list B when you can\nproduce list A by deleting some elements from list B. For example, [1,2,3] is\na subsequence of each of the lists\n[1,2,3]\n[1,1,1,2,2,3]\n[1,2,7,3]\n[5,6,1,9,9,2,7,3,8]\nbut it is not a subsequence of any of the lists:\n[1,2]\n[1,3]\n[5,6,2,1,7,3,8]\n\nDefine an inductive proposition subsequence that captures what it means to be\na subsequence, then prove that subsequence is transitive. Some hints on\nautomating this proof:\n\n1. Use Hint Constructors to add your inductive proposition's constructors to\nthe hint database.\n2. Choose which thing you do induction on carefully!\n3. Try \"crush\" and look where it fails -- then add that as a lemma and put it\nin your rewriting database. For example, on my first attempt, crush died here\nfirst:\n\n A : Type\n l1 : list A\n H : subsequence l1 nil\n l : list A\n ============================\n subsequence l1 l\n\nSo I added a lemma:\n\nLemma subsequence_nil_anything : forall A (l1 l2 : list A),\n subsequence l1 nil -> subsequence l1 l2.\n(* ... ;-) *)\nQed.\n\nHint Resolve subsequence_nil_anything.\n\nThat let crush get a bit farther, and suggested the next lemma I would need to\nprove.\n*)\n\nSection subsequence.\n Variable T : Type.\n\n(* 1st arg is subsequence of 2nd *)\nInductive subsequence : list T -> list T -> Prop :=\n | sNil : forall l, subsequence nil l\n | sTail : forall (b : T) (l l1 : list T), subsequence l l1 -> subsequence l (b :: l1)\n | sHead : forall (a b : T) (l l1 : list T), a = b -> subsequence l l1 -> subsequence (cons a l) (cons b l1).\n\nHint Constructors subsequence.\n\nHypothesis sub_nil_any : forall l, subsequence l nil -> forall l1, subsequence l l1.\nHypothesis sub_weak : forall a l l1, subsequence (a :: l) l1 -> subsequence l l1.\nHint Resolve sub_nil_any.\n\nTheorem subsequence_trans : forall l l1, subsequence l l1 -> forall l2, subsequence l1 l2 ->\n subsequence l l2.\nProof.\n induction 2; crush. inversion H; crush. inversion H2; crush. inversion H; crush.\n\nQed.\n\n(* I'm not really sure why we're not getting this notation for free, actually.\nBut we're not, se here it is for convenience. *)\nNotation \"[ ]\" := nil.\nNotation \"[ x ; .. ; y ]\" := (cons x .. (cons y nil) ..).\n\nDefinition sub123 := subsequence [1; 2; 3].\nDefinition notsub123 := fun l => ~(subsequence [1; 2; 3] l).\nHint Unfold sub123.\n\nExample e1 : sub123 [1; 2; 3]. auto 100. Qed.\nExample e2 : sub123 [1; 1; 1; 2; 2; 3]. auto 100. Qed.\nExample e3 : sub123 [1; 2; 7; 3]. auto 100. Qed.\nExample e4 : sub123 [5; 6; 1; 9; 9; 2; 7; 3; 8]. auto 100. Qed.\n(* optional; you'll like inversion and unfold a lot\nExample e5 : notsub123 [1; 2]. (* TODO *) Qed.\nExample e6 : notsub123 [1; 3]. (* TODO *) Qed.\nExample e7 : notsub123 [5; 6; 2; 1; 7; 3; 8]. (* TODO *) Qed.\n*)\n", "meta": {"author": "fyrchik", "repo": "cpdt_hw", "sha": "4b207b9edf448c344463700a48d83c09f72a8592", "save_path": "github-repos/coq/fyrchik-cpdt_hw", "path": "github-repos/coq/fyrchik-cpdt_hw/cpdt_hw-4b207b9edf448c344463700a48d83c09f72a8592/HW5.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9653811591688145, "lm_q2_score": 0.9525741234565097, "lm_q1q2_score": 0.9195971114966628}} |
| {"text": "(* nat --> An 'Inductive' Type, and not an 'enumerated' type [Ex: Bool] *)\nInductive nat : Type :=\n | O : nat\n | S : nat -> nat.\n\n(* Quick redefinition of : bool *)\nInductive bool : Type :=\n | true : bool\n | false : bool.\n\nDefinition negate_bool (b:bool) : bool :=\n match b with\n | true => false\n | false => true\nend.\n\nDefinition pred (n:nat) : nat :=\n match n with\n | O => O\n | S n' => n'\nend.\n\n(* Verify the Number 3 *)\nCheck (S (S (S O))).\n\n(* Using recusrion with : Fixpoint *)\nFixpoint odd_nat (n:nat) : bool :=\n match n with\n | O => false\n | S O => true \n | S (S n') => odd_nat n'\nend.\n\nFixpoint even_nat (n:nat) : bool :=\n match n with\n | O => true\n | S O => false\n | S (S n') => even_nat n'\nend.\n\nCompute (even_nat (S (S (S (S O))))).\nCheck even_nat.\nCheck pred.\nCheck S.\n\n(* A few Tests [Asserted as Proofs] *)\nExample test_even_nat1 : even_nat (S (S (S (S O)))) = true.\nProof. simpl. reflexivity. Qed.\nExample test_odd_nat1 : (odd_nat (S (S (S (S O))))) = false.\nProof. simpl. reflexivity. Qed.\n\n(* Definition of the plus, minus, mult and fac functions *)\nFixpoint plus (n:nat) (m:nat) : nat :=\n match n with\n | O => m\n | S n' => S (plus n' m)\nend.\n\nCompute (plus (S (S (S O))) (S (S O))).\n\nFixpoint minus (n m:nat) : nat :=\n match n, m with\n | O, _ => O\n | S _, O => n\n | S n', S m' => minus n' m'\nend.\n\nFixpoint minus_2 (n:nat) (m:nat) : nat :=\n match m with\n | O => n\n | S m' => minus_2 (pred n) m'\nend.\n\nCompute (minus_2 (S (S (S (S (S O))))) (S (S O))).\n\nFixpoint mult (n:nat) (m:nat) : nat :=\n match n with\n | O => O\n | S n' => plus m (mult n' m)\nend.\n\nFixpoint fac (n:nat) : nat :=\n match n with\n | O => S O\n | S O => S O\n | S n' => mult n (fac n')\nend.\n\n(* Simple Tests [Asserted as Proofs] for mult, etc. *)\nExample test_fac_1: (fac (S (S (S O)))) = (S (S (S (S (S (S O)))))).\nProof. simpl. reflexivity. Qed.\nExample test_mult_1: (mult (S (S O)) (S (S (S O)))) = (S (S (S( S( S( S O)))))).\nProof. simpl. reflexivity. Qed.\nExample test_minus_1: (minus (S (S (S (S (S O))))) (S (S (S O)))) = (S (S O)).\nProof. simpl. reflexivity. Qed.\nExample test_minus_2: (minus_2 (S (S (S (S (S O))))) (S (S (S O)))) = S (S O).\nProof. simpl. reflexivity. Qed.\n\n(* Introduce notations *)\nNotation \"x + y\" := (plus x y)\n (at level 50, left associativity)\n : nat_scope.\nNotation \"x - y\" := (minus x y)\n (at level 50, left associativity)\n : nat_scope.\nNotation \"x * y\" := (mult x y)\n (at level 40, left associativity)\n : nat_scope.\n\n(* Define Equality between 'nat' *)\nFixpoint beq_nat (n m : nat) : bool :=\n match n with\n | O => match m with \n | O => true\n | S m' => false\n end\n | S n' => match m with\n | O => false\n | S m' => beq_nat n' m'\n end\nend.\n\nExample test_beq_nat_1: (beq_nat (S (S (S O))) (S (S (S O)))) = true.\nProof. simpl. reflexivity. Qed.\nExample test_beq_nat_2: (beq_nat (S (S (S O))) (S (S O))) = false.\nProof. simpl. reflexivity. Qed.\n\n(* Define a function to test : n <= m, without using 'Fixpoint' *)\nFixpoint leq_nat (n m : nat) : bool :=\n match n, m with\n | O, _ => true\n | S _, O => false\n | S n', S m' => leq_nat n' m'\nend.\n\n(* Some computations of 'leq' before the Proofs *)\nCompute (leq_nat (S (S (S O))) (S (S (S O)))).\nCompute (leq_nat (S (S (S O))) (S (S (S (S O))))).\nCompute (leq_nat (S (S (S (S O)))) (S (S (S O)))).\n\nExample test_leq_nat1: (leq_nat (S (S (S O))) (S (S (S O)))) = true.\nProof. simpl. reflexivity. Qed.\nExample test_leq_nat2: (leq_nat (S (S (S O))) (S (S (S (S O))))) = true.\nProof. simpl. reflexivity. Qed.\nExample test_leq_nat3: (leq_nat (S (S (S (S O)))) (S (S (S O)))) = false.\nProof. simpl. reflexivity. Qed.\n\n(* Let us write a few Theorems regarding the type : nat *)\n\n(* Left addition of 0 *)\nTheorem plus_O_n_l : forall n: nat, O + n = n.\nProof.\n intros n. reflexivity. Qed. (* simpl is not needed *)\n\n(* To Coq, Lemma = Fact = Remark = Example = Theorem *)\n(* The difference is mostly Syntactic Sugar *)\n\n(* Left addition of 1 *)\nTheorem plus_1_n_l : forall n: nat, (S O) + n = S n.\nProof.\n intros n. reflexivity. Qed.\n\n(* Using rewrite *)\n\n(* Addition of equal nat *)\nTheorem plus_id : forall n m: nat,\nn = m ->\nn + n = m + m.\nProof.\n intros n m.\n intros H.\n rewrite -> H.\n reflexivity. Qed.\n\n(* Addition of transitively equal nat *)\nTheorem plus_id_transitive : forall n m o: nat,\nn = m -> m = o -> n + m = m + o.\nProof.\n intros n m o.\n intros H1.\n intros H2.\n rewrite -> H1.\n rewrite -> H2.\n reflexivity. Qed.\n\n(* We can also use rewrite using a _previously_ proved Theorem *)\nTheorem add_0_and_mult : forall n m: nat,\n(O + n) * m = n * m.\nProof.\n intros n m.\n rewrite -> plus_O_n_l.\n reflexivity. Qed.\n\n(* Proof for Right multiplication by Successor *)\nTheorem mult_S_n_1 : forall n m: nat,\nm = S n -> m * ((S O) + n) = m * m.\nProof.\n intros n m.\n intros H.\n rewrite -> plus_1_n_l.\n rewrite -> H.\n reflexivity. Qed.\n\n(* When the _evaluation_ of an argument itself is recursive, then\nproofs can't simply use _rewrite_ or _simpl_ -> Use _destruct_ *)\nTheorem plus_1_neq_O : forall n: nat,\nbeq_nat (n + (S O)) O = false.\nProof.\n intros n. destruct n as [|n'].\n - reflexivity.\n - reflexivity. Qed.\n\n(* Proof that 1 plus anything can never be zero in the realm of the natural numbers *)\nTheorem zero_nbeq_plus_1 : forall n: nat,\nbeq_nat O (n + (S O)) = false.\nProof.\nintros n. destruct n as [|n'].\n- reflexivity.\n- reflexivity. Qed.\n\n(* Exercise 1 : Show that the identity function applied twice is the same as it being applied once *)\nTheorem identity_fn_applied_twice : forall (f : bool -> bool),\n(forall (x: bool), f x = x) -> (forall (b: bool), f (f b) = b).\nProof.\nintros f H b.\nrewrite <- H.\nsimpl.\nrewrite <- H.\nreflexivity.\nQed.\n\nTheorem unnecess_brack_add : forall n m p: nat,\np + (n + m) = p + n + m.\nProof.\nintros n m p. induction p as [|p' IHp'].\n- simpl. reflexivity.\n- simpl. rewrite -> IHp'. simpl. reflexivity.\nQed.\n\n(* A List of Proofs about Functions of `Nat` from Chapter-3 (Induction) *)\n(* NOTE : Not all _require_ Induction. In fact, the goal is to skillfully be minimal in the proofs *)\nTheorem leq_nat_refl : forall n: nat,\ntrue = leq_nat n n.\nProof.\nintros n. induction n as [|n' IHn'].\n- simpl. reflexivity.\n- simpl. rewrite <- IHn'. reflexivity.\nQed.\n\nTheorem zero_nbeq_S : forall n: nat,\nbeq_nat O (S n) = false.\nProof.\nintros n.\nsimpl.\nreflexivity.\nQed.\n\nTheorem plus_ble_compat_l : forall n m p: nat,\nleq_nat n m = true -> leq_nat (p + n) (p + m) = true.\nProof.\nintros n m p H. induction p as [|p' IHp'].\n- simpl. rewrite -> H. reflexivity.\n- simpl. rewrite -> IHp'. reflexivity.\nQed.\n\nTheorem S_nbeq_O : forall n: nat,\n beq_nat (S n) O = false.\nProof.\nintros n.\nsimpl.\nreflexivity.\nQed.\n\n(* Temporary re-proof {already done in Induction.v} of plus_n_O *)\nTheorem plus_n_O : forall n: nat,\nn = n + O.\nProof.\nintros n. induction n as [|n' IHn'].\n- (* n = O *) reflexivity.\n- (* n = S n' *) simpl. rewrite <- IHn'. reflexivity.\nQed.\n\nTheorem mult_1_l : forall n: nat, \n(S O) * n = n.\nProof.\nintros n. induction n as [|n' IHn'].\n- simpl. reflexivity.\n- simpl. rewrite <- plus_n_O. reflexivity.\nQed.\n\nTheorem mult_plus_distr_r : forall n m p: nat,\n (n + m) * p = (n * p) + (m * p).\nProof.\nintros n m p. induction n as [|n' IHn'].\n- simpl. reflexivity.\n- simpl. rewrite -> IHn'. rewrite -> unnecess_brack_add. reflexivity.\nQed.\n\nTheorem beq_nat_refl : forall n: nat,\ntrue = beq_nat n n.\nProof.\nintros n. induction n as [|n' IHn'].\n- simpl. reflexivity.\n- simpl. rewrite -> IHn'. simpl. reflexivity.\nQed.\n", "meta": {"author": "jssandh2", "repo": "coq-types", "sha": "bc844def91356335816a0c97b556d98a202165f6", "save_path": "github-repos/coq/jssandh2-coq-types", "path": "github-repos/coq/jssandh2-coq-types/coq-types-bc844def91356335816a0c97b556d98a202165f6/src/Types/Nat/Nat.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9546474233166328, "lm_q2_score": 0.9572778071764596, "lm_q1q2_score": 0.9138627920192036}} |
| {"text": "Require Import Bool Arith List.\n\n(* 1. Define a recursive function that takes as input a list of numbers\n and returns the product of these numbers, *)\n\nFixpoint prodlist (l : list nat) :=\n match l with\n | nil => 1\n | x :: xs => x * prodlist xs\n end.\n\nCompute prodlist nil.\nCompute prodlist (0 :: 3 :: 7 :: nil).\nCompute prodlist (3 :: 4 :: 2 :: nil).\n\n(* 2. Define a recursive function that takes a list of numbers and\n returns true if and only if this list contains the number 0.\n Hint: pattern match on both the list and the elements of the list. *)\n\nFixpoint has_a_0 (l : list nat) :=\n match l with\n | nil => false\n | x :: xs => \n match x with\n | 0 => true\n | S _ => has_a_0 xs\n end\n end.\n\nCompute has_a_0 (2 :: 3 :: 0 :: 7 :: nil).\nCompute has_a_0 nil.\nCompute has_a_0 (3 :: 5 :: nil).\n\n(* 3. Define a recursive function that takes as input two numbers and\n returns true if and only if these two numbers are the same natural\n number (such a function already exists in the Coq libraries\n (function beq_nat, but how would you define such a function), using\n only pattern-matching and recursion. *)\n\nFixpoint beq (a b : nat) :=\n match a, b with\n | 0, 0 => true\n | 0, S _ => false\n | S _, 0 => false\n | S a1, S b1 => beq a1 b1\nend.\n\nCompute beq 4 5.\nCompute beq 7 7.\n\n(* 4. Define a recursive function that takes a number n and a number\n a as input and returns a list of numbers containing n elements\n that are all a. *)\n\nFixpoint mklist (n a : nat) (l: list nat) :=\n match n with\n | 0 => l\n | S n1 => mklist n1 a (a::l)\n end.\n\nCompute mklist 5 2 nil.\n(*\nFixpoint mklist (n a : nat) :=\n match n with\n | 0 => nil\n | S n1 => \n cons a (mklist n1 a)\n*)\n\n(* 5. Define a function that takes a number n and a number a and returns\n the list of n elements a ::a + 1:: · · · ::a + n:: nil. *)\n\nFixpoint mklist1 (n a : nat) :=\n match n with\n | 0 => nil\n | S n1 => cons (a) (mklist1 n1 (a + 1))\n end.\n\nCompute mklist1 5 2.\n\n(* 6. Define a function that takes as input a natural number and returns\n an element of option nat containing the predecessor if it exists or\n None if the input is 0. *)\nFixpoint pred (n : nat) :=\n match n with\n | 0 => None\n | S x => Some x\nend.\n\nCompute pred 0.\nCompute pred 6.\n\n(* 7. Define a recursive function that takes as input a list of numbers\n and returns the length of this list. *)\n\nFixpoint length (l : list nat) (n : nat) :=\n match l with\n | nil => n\n | x :: xs => length xs (n + 1)\n end.\n\nCompute length nil 0.\nCompute length (2 :: 3 :: nil) 0.\nCompute length (2 :: 3 :: 4 :: 4 :: nil) 0.\n\n(* 8. Can you write a recursive function values that takes as input a\n function f of type nat -> nat, an initial value a of type nat\n and a count n of type nat and produces the list\n a :: f a :: f (f a) :: ... *)\n\nFixpoint fold (f : nat -> nat) (a : nat) (n : nat) :=\n match n with\n | 0 => nil\n | S n1 => cons (a) (fold f (f a) n1)\n end.\n\nCompute fold (fun x : nat => x) 0 5.\nCompute fold (fun x : nat => x + 1) 3 4.\nCompute fold S 3 4.\n\n(* 9. To every natural number, we can associate the list of its digits\n from right to left. For instance, 239 should be associated to the list\n 9::3::2::nil. We also consider that 0 can be mapped to nil. If l is\n such a list, we can consider the successor of a list of digits.\n For instance, the successor of 9::3::2::nil is 0::4::2.\n Define the algorithm on lists of natural numbers that computes the\n successor of that list. *)\n\nFixpoint lsucc (l: list nat) :=\n match l with\n | nil => 1::nil\n | x :: xs =>\n match x with\n | 9 => 0 :: lsucc xs\n | _ => (x + 1) :: xs\n end\n end.\n\nCompute lsucc (3 :: 9 :: 1 :: nil).\nCompute lsucc (9 :: 3 :: 1 :: nil).\nCompute lsucc (9 :: 9 :: 1 :: nil).\nCompute lsucc nil.\nCompute lsucc (9 :: nil).\n\nCheck lsucc.\n(* 10. Assuming that lsuc is the function defined at the previous\n exercise, define the function nat_digits that maps every natural\n number to the corresponding list of digits (naive solutions are\n welcome, as long as they run). *)\n\nFixpoint nat_digits_aux (n i: nat) (li : list nat) :=\n match i with\n | 0 => li\n | S i1 => nat_digits_aux n i1 (lsucc li)\n end.\n\nDefinition nat_digits (n : nat) :=\n nat_digits_aux n n nil.\n\nCompute nat_digits 4532.\n\n(* 11. In the same context as the previous two exercises, define a function\n value that maps every list of digits to the natural number it\n represents. Thus, value (9::3::2::nil) should compute to 239. *)\n\nFixpoint value (l : list nat) :=\n match l with \n | nil => 0\n | x::xs => x + 10 * value(xs)\nend.\nCompute value (0 :: 2 :: 3 :: 4 :: nil).\n\n(* 12. In the same context as the previous exercises, define a function\n licit that tells whether a list of integers corresponds to the digits\n of natural number: no digit should be larger than 9. For instance,\n licit (9::3::2::0::nil) should compute to true and licit (239::nil)\n should compute to false. *)\n\nFixpoint licit (l : list nat) (b : bool):=\n match l, b with\n | nil, true => true\n | _, false => false\n | x::xs, true => licit xs (leb x 10)\nend.\n\nCompute licit (9 :: 3 :: 2 :: 0 :: 34 :: nil) true.\nCompute licit (2 :: 3 :: 9 :: nil) true.\n\n(* 13. In the same context as the previous exercises, define functions\n to add two lists of digits so that the result represents the sum of\n the numbers represented by the lists of digits. In other words\n addl (7::2::nil) (5::3::nil) should return 2::6::nil\n (Hint: you should implement the algorithm you learned in elementary\n school for adding numbers). *)\n\nFixpoint addl (l1 l2 l: list nat) (c : nat) :=\n match l1, l2 with\n | nil, nil => l\n | nil, x::xs => (x +)\n | _, nil => l1::l\n | x1::xs1, x2::xs2 => \n match leb (x1 + x2) 10 with\n | true => addl xs1 xs2 0\n | false => addl xs1 xs2 1\n end\nend.\n\nCompute addl (7 :: 2 :: nil) (5 :: 3 :: nil) nil 0.\n\n(* 14. In the same context as the previous exercises, define a function\n for multiplying a list by a small natural number (by successive\n additions) and then a function mull for multiplying two lists of\n digits. *)\n\n... mulln ...\n\nCompute mulln 3 (2 :: 1 :: nil).\n\n... mull ...\n\nCompute 14 * 13.\nCompute mull (4 :: 1 :: nil) (3 :: 1 :: nil).\nCompute 437 * 25.\nCompute mull (7 :: 3 :: 4 :: nil) (5 :: 2 :: nil).\nCompute 97 * 75.\nCompute mull (7 :: 9 :: nil) (5 :: 7 :: nil).\n", "meta": {"author": "vabh", "repo": "Coq", "sha": "75b90a61d518aead333de748a08b5ed04585ef9c", "save_path": "github-repos/coq/vabh-Coq", "path": "github-repos/coq/vabh-Coq/Coq-75b90a61d518aead333de748a08b5ed04585ef9c/ex1.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9465966671870766, "lm_q2_score": 0.9603611633098487, "lm_q1q2_score": 0.9090746764850065}} |
| {"text": "Inductive Nat :=\n |O : Nat\n |S : Nat -> Nat.\nCheck Nat_ind.\n\nFixpoint plus(n:Nat)(m:Nat) : Nat :=\n match n with\n |O => m\n |S v => S(plus v m)\n end.\n\nLemma O_plus_n_is_n:\n forall n, plus O n=n.\nProof.\n (* tactics *)\n intros n.\n simpl.\n reflexivity.\nQed.\n\nLemma n_plus_0_is_n:\n forall n, plus n O = n.\nProof.\n induction n.\n - simpl.\n reflexivity.\n - simpl.\n rewrite IHn.\n reflexivity.\nQed.\n\nLemma plus_comm:\n forall n m, plus n m = plus m n.\nProof.\n induction n.\n - intros m.\n simpl.\n rewrite n_plus_0_is_n.\n reflexivity.\n - induction m.\n + simpl.\n rewrite n_plus_0_is_n.\n reflexivity.\n + simpl.\n rewrite IHn.\n simpl.\n rewrite <- IHm.\n simpl.\n rewrite IHn.\n reflexivity.\nQed. \n\n", "meta": {"author": "IonitaCatalin", "repo": "programming-language-principle", "sha": "e6a5b4f5284f28127707dc1b8838bad29f215c69", "save_path": "github-repos/coq/IonitaCatalin-programming-language-principle", "path": "github-repos/coq/IonitaCatalin-programming-language-principle/programming-language-principle-e6a5b4f5284f28127707dc1b8838bad29f215c69/coq_arc/curs2.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.966914020657881, "lm_q2_score": 0.9381240198986809, "lm_q1q2_score": 0.9070852679559676}} |
| {"text": "Require Import List CpdtTactics.\nImport ListNotations.\n\n(* An abbreviation which given two nats n and m, \n decides whether {n <= m} or {n > m}. *)\nDefinition nat_lte := Compare_dec.le_gt_dec.\n\n(* Insert a number into a list. Assumes the list is\n sorted in in non-decreasing order. *)\nFixpoint insert (n:nat) (xs:list nat) : list nat := \n match xs with \n | [] => [n]\n | h::t => if nat_lte n h then n::h::t else h::(insert n t)\n end.\n\n(* Insertion sort. *)\nDefinition insert_sort (xs:list nat) : list nat := \n fold_right insert [] xs.\n\n(* Simple test. *)\nEval compute in insert_sort [3;2;5;1;7].\n\n(* A useful predicate on lists. list_all P xs holds \n when P holds on each element of xs. *)\nDefinition list_all {A:Type} (P:A->Prop) (xs:list A) : Prop := \n fold_right (fun h t => P h /\\ t) True xs.\n\n(* We can use list_all to define a notion of a sorted list. *)\nFixpoint sorted (xs:list nat) : Prop := \n match xs with \n | [] => True\n | h::t => sorted t /\\ list_all (le h) t\n end.\n\n(* Notice that this produces a predicate (which we can't prove!) *)\nEval compute in sorted [3;1;2].\n\n(* We can prove this predicate though. *)\nExample sorted_123 : sorted [1;2;3].\nProof.\n simpl. \n auto 100.\nQed.\n\n(* Here's an alternative definition of sorted using an\n couple of inductive definitions. *)\nInductive list_lte : nat -> list nat -> Prop := \n| nil_list_lte : forall n, list_lte n nil\n| cons_list_lte : forall n h t, n <= h -> list_lte n t -> list_lte n (h::t).\n\nInductive sorted' : list nat -> Prop := \n| nil_is_sorted : sorted' nil\n| cons_is_sorted : forall h t, list_lte h t -> sorted' t -> sorted' (h::t).\n\n(* And here's an example proof that the list [1;2;3] is sorted'. *)\nExample sorted'_123 : sorted' [1;2;3].\nProof.\n Hint Constructors list_lte sorted'.\n auto 100.\nQed. \n\n(* The two definitions of sorted'ness are equivalent. *)\nLemma list_lte_iff_list_all_lte : \n forall n (xs:list nat), \n list_lte n xs <-> list_all (le n) xs.\nProof.\n induction xs ; crush.\n inversion H1 ; intros ; subst.\n auto.\n inversion H1 ; intros ; subst.\n auto.\nQed.\n\nLemma sorted_iff_sorted' : \n forall (xs:list nat), \n sorted xs <-> sorted' xs.\nProof.\n Hint Constructors sorted'.\n induction xs ; crush.\n constructor. \n rewrite list_lte_iff_list_all_lte. auto. auto.\n inversion H1 ; intros ; subst. crush.\n rewrite <- list_lte_iff_list_all_lte.\n inversion H1 ; intros ; subst. auto.\nQed.\n\n(* Let's try to prove that insert_sort produces a sorted list. *)\nLemma insert_sort_sorted : forall xs, sorted (insert_sort xs).\nProof.\n induction xs ; crush.\n unfold sorted.\n (* We're stuck since Coq doesn't know if (insert a (insert_sort xs))\n is empty or a cons. Let's try to convince it. *)\n remember (insert a (insert_sort xs)) as sorted_xs.\n destruct sorted_xs.\n (* If (insert a (insert_sort xs)) is empty, we don't have to prove\n anything -- an empty list is already sorted. (But of course,\n (insert a (insert_sort xs)) is not empty. *)\n auto.\n (* Ugh! We're no better off than we were before. Let's abort... *)\n Abort.\n\n(* What we need to do first is prove something useful about \n insert. For example, if xs is sorted and we insert n into\n it, we should get back a sorted list. Before juming into\n that, let me define some useful lemmas. *)\n\n(* A useful lemma that lifts implication to list_all. It says\n that if P x -> Q x for any x, then list_all P xs -> list_all Q xs.\n*)\nLemma list_all_imp{A}: \n forall (P Q : A -> Prop),\n (forall (x:A), P x -> Q x) -> \n (forall (xs:list A), list_all P xs -> list_all Q xs).\nProof.\n intros P Q H.\n induction xs ; crush.\nQed.\n\n(* If n <= m and m <= each element of xs, then n <= each element of xs. *)\nLemma list_lte_nm (n m:nat) (xs:list nat) : \n n <= m -> list_all (le m) xs -> list_all (le n) xs.\nProof.\n intros.\n (* Aha! Now we can use the list_all_imp lemma to avoid\n reasining about the list, and reduce this to a single element. *)\n eapply (list_all_imp (le m) (le n)) ; [ intros ; omega | auto ].\nQed.\n\n(* So let's try to prove this fact now... *)\nLemma insert_sorted : forall n xs, sorted xs -> sorted (insert n xs).\nProof.\n induction xs ; crush.\n destruct (nat_lte n a) ; simpl.\n crush.\n (* Here's where our lemma above comes into play. *)\n eapply list_lte_nm ; eauto.\n crush.\n (* Ugh! How are we supposed to prove that a <= insert n xs?\n Intuitively it's true, but how can we show this? We need\n to know that if we insert n into xs, then the elements of\n the resulting list are either equal to n or one of the xs. *)\n Abort.\n\n(* An equivalent way to capture \"list_all\" *)\nLemma in_list_all {A} (P:A->Prop) (xs:list A) : \n (forall x, In x xs -> P x) <-> list_all P xs.\nProof.\n induction xs ; crush.\nQed.\n\n(* Now we can prove that if you insert n into xs, then\n any element of the resulting list is either equal to\n n or one of the xs. *)\nLemma in_insert :\n forall (xs:list nat) (n:nat), \n forall x, In x (insert n xs) -> x = n \\/ In x xs.\nProof.\n induction xs ; crush.\n destruct (nat_lte n a) ; crush.\n specialize (IHxs _ _ H0). crush.\nQed.\n\n(* The opposite fact will also be useful. *)\nLemma insert_in : \n forall (xs:list nat) (n:nat), \n forall x, x = n \\/ In x xs -> In x (insert n xs).\nProof.\n induction xs ; crush.\n destruct (nat_lte n a) ; crush.\n destruct (nat_lte n x) ; crush.\n destruct (nat_lte n a) ; crush.\nQed.\n\nLemma insert_sorted : forall n xs, sorted xs -> sorted (insert n xs).\nProof.\n induction xs ; crush.\n destruct (nat_lte n a) ; simpl.\n crush.\n eapply list_lte_nm ; eauto.\n crush.\n (* here's where in_list_all comes into play -- we turn the\n list_all into reasoning about a particular element in \n (insert n xs) which has to be either n or one of the xs. *)\n apply in_list_all.\n intros.\n generalize (in_insert xs n x H2). intro.\n destruct H3.\n crush.\n (* here's where the opposite lemma comes into play. *)\n rewrite <- in_list_all in H1.\n crush.\nQed.\n\n(* Once we've proved that insert produces a sorted list, we\n can easily prove that insert_sort produces a sorted list. *)\nLemma insert_sort_sorted : forall xs, sorted (insert_sort xs).\nProof.\n induction xs ; crush.\n apply insert_sorted ; auto.\nQed.\n\n(* However, note that the following function also produces\n a sorted list:\n*)\nDefinition bogus_sort (xs:list nat) : list nat := nil.\n\nLemma bogus_sort_sorted (xs:list nat) : sorted (bogus_sort xs).\n apply I.\nQed.\n\n(* Here's an attempt to capture what it means for a sort\n function to actually be correct. The output should\n be sorted, the length of the input should equal the\n length of the output, and every member of the input \n should be in the output (and vice versa, though this\n can be shown given that the lengths are the same.)\n*)\nDefinition sublist {A} (xs ys:list A) : Prop := \n forall (x:A), In x xs -> In x ys.\n\nDefinition sort_corr (xs ys:list nat) : Prop := \n sorted ys /\\ sublist xs ys /\\ length xs = length ys.\n\n(* There are, of course, alternative definitions. For\n instance, we might specify that the output is a sorted\n permutation of the input. \n\n Often, you can't predict what will be the most useful\n specification. That depends largely on who'se using\n the code and what they need to know. For instance,\n we might be using the sort routine as part of a set\n implementation. In that case, these properties would\n be good enough, though we'd probably want to build\n some derived properties. For instance, we might \n like to know that \n\n (sort_corr xs ys) -> (sort_corr xs zs) -> ys = zs\n\n which *cannot* be proven from the spec above (and in \n fact is not true.) \n*)\n\n(* Let's prove now that our insertion sort is correct,\n according to the definition we gave above. We have\n already shown insert_sort produces a sorted list. \n Now we just need to establish the other two properties: *)\nLemma insert_sort_sublist : forall xs, sublist xs (insert_sort xs).\nProof.\n unfold sublist.\n induction xs ; crush ; apply insert_in ; crush.\nQed.\n\nLemma insert_length : forall xs n, length (insert n xs) = S (length xs).\nProof.\n induction xs ; crush.\n destruct (nat_lte n a) ; crush.\nQed.\n\nLemma insert_sort_length : forall xs, length xs = length (insert_sort xs).\nProof.\n induction xs ; crush.\n rewrite insert_length. auto.\nQed.\n\n(* And finally, we can show insertion sort is correct. *)\nLemma insert_sort_corr : forall xs, sort_corr xs (insert_sort xs).\nProof.\n unfold sort_corr. intro.\n split.\n apply insert_sort_sorted.\n split.\n apply insert_sort_sublist.\n apply insert_sort_length.\nQed.\n\n(********************************************************)\n\n(* Of course, we don't want to use an O(n^2) sort in practice.\n So here, I develop a merge sort. This shows off something\n new -- defining a recursive function using something besides\n structural induction to establish termination.\n*)\n\n\n(* First, we need to define a function to merge two (already\n sorted lists). Now normally, we'd write this as:\n\n Fixpoint merge (xs ys:list nat) {struct xs} : list nat := \n match xs, ys with \n | [], ys => ys\n | xs, [] => xs\n | h1::t1, h2::t2 => if nat_lte h1 h2 then \n h1 :: (merge t1 ys)\n else\n h2 :: (merge xs t2)\n end.\n\n But unfortunately, Coq will reject this because it's\n not the case that xs is always getting smaller, nor\n the case that ys is always getting smaller. Of course,\n *one* of them is always getting smaller, so eventually,\n this will terminate. \n\n But in this case, we can hack around the problem by\n simply re-organizing the function as follows:\n*)\nFixpoint merge (xs:list nat) : list nat -> list nat := \n match xs with \n | nil => fun ys => ys\n | (x::xs') => \n (fix inner_merge (ys:list nat) : list nat := \n match ys with \n | nil => x::xs'\n | y::ys' => if nat_lte x y then \n x :: (merge xs' (y::ys'))\n else \n y :: (inner_merge ys')\n end)\n end.\n(* Note that for the out loop, we only call it with a\n smaller xs, and for the inner loop, we only call it\n with a smaller ys. So Coq can see by structural\n induction the loops that the definition terminates.\n\n Note that if you tried to pull inner_merge out and\n define it as a top-level function, Coq would no \n longer be able to tell that merge terminates. \n In this sense, Coq's termination checking isn't\n modular.\n*)\n\n(* Test that merge works. *)\nEval compute in merge [1;3;5] [2;4;6].\nEval compute in merge [3] [1;4].\n\n(* This function takes a list of lists of nats, and \n merges each pair of lists. See the example below. *)\nFixpoint merge_pairs (xs:list (list nat)) : list (list nat) := \n match xs with \n | h1::h2::t => (merge h1 h2) :: (merge_pairs t)\n | xs' => xs'\n end.\n\nEval compute in merge_pairs [[1;3];[2;4;9];[0];[2;3]].\nEval compute in merge_pairs [[1;3];[2;4;9];[0]].\n\n(* To define our actualy merge sort, we want to take the\n initial list [n1;n2;...;nm] and turn it into a list of \n singleton lists: [[n1];[n2];...;[nm]] and then successively\n call merge_pairs until we get a single list out. *)\n\n(* This function takes a list and turns it into a list\n of singleton lists of the elements. *)\nDefinition make_lists (xs:list nat) : list (list nat) := \n List.map (fun x => x::nil) xs.\n\nEval compute in make_lists [5; 1; 4; 2; 3].\nEval compute in merge_pairs (merge_pairs (merge_pairs (make_lists [5; 1; 4; 2; 3]))).\n(* As with merge, I would like to write the following function\n which is intended to iterate merging the pairs of a list of\n lists until we get a single list out:\n\n Fixpoint merge_iter (xs:list (list nat)) : list nat := \n match xs with \n | [] => []\n | [h] => h\n | h1::h2::xs' => merge_iter (merge_pairs (h1::h2::xs'))\n end.\n\n But Coq can't tell that this terminates. The problem is\n that we are calling merge_iter on (merge_pairs (h1::h2::xs'))\n instead of xs'. Since in principle, merge_pairs could return\n a list no smaller than the input, Coq rejects the definition.\n\n All is not lost, though. In Coq, we can define functions\n that use an arbitrary measure (or really, any well-founded\n relation) and show that the measure is always decreasing\n to convince Coq the function terminates. \n\n Before doing that, I need to establish a lemma that:\n\n length (merge_pairs (h1::h2::xs)) < length (h1::h2::xs)\n\n This is a little tricky to prove since we are peeling off\n 2 elements instead of one. One way to prove it is using\n so-called \"strong-induction\", but here's another way:\n*)\nLemma merge_pairs_length' : \n forall xs, (forall h1 h2, \n length (merge_pairs (h1::h2::xs)) < length (h1::h2::xs)) /\\\n (forall h, \n length (merge_pairs (h::xs)) <= length (h::xs)).\nProof.\n induction xs ; crush.\nQed.\n\n(* What I did in merge_pairs_length' is generalize my desired\n property to one that holds regardless of the number of elements\n in the list. Try doing it without the second case and see what\n goes wrong... *)\n\n(* Anyway, my decreasing measure is now easy to establish from the\n lemma above. *)\nLemma merge_pairs_length : \n forall h1 h2 xs, length (merge_pairs (h1::h2::xs)) < length (h1::h2::xs).\nProof.\n intros.\n specialize (merge_pairs_length' xs). \n intros [H _].\n apply H.\nQed.\n\n(* Now I'm going to define my merge_iter function. Since it's\n not structurally recursive, but rather, defined using a measure,\n I first need to import a couple of libraries. *)\nRequire Import Program.\nRequire Import Wf.\nRequire Import Recdef.\n\n(* The [Program Fixpoint] construct is similar to the [Fixpoint]\n construct. The big difference is that I'm required to state\n a [{measure ...}] clause to convince Coq as to what's going\n down. In this case, the length of the argument list is always\n going down when we do a recursive call.\n*)\nFunction merge_iter (xs : list (list nat)) {measure length} : \n list nat :=\n match xs with \n | nil => nil\n | h::nil => h\n | h1::h2::xs' => merge_iter (merge_pairs (h1::h2::xs'))\n end.\nintros.\napply merge_pairs_length.\nDefined.\n\nPrint merge_iter.\n\n(* If we print out merge_iter... *)\nPrint merge_iter.\n(* ...we see that the [Program Fixpoint] did a lot of work for us\n to translate our definition into the real core of Coq. It's\n using a special function Fix_sub, along with some other hidden\n definitions that you can print out if you like. *)\nPrint Fix_sub.\n(* Fix_sub is defined in terms of Fix_F_sub. *)\nPrint Fix_F_sub.\n(* Fix_F_sub is defined to be:\n\n fun (A : Type) (R : A -> A -> Prop) (P : A -> Type)\n (F_sub : forall x : A, (forall y : {y : A | R y x}, P (` y)) -> P x) =>\n fix Fix_F_sub (x : A) (r : Acc R x) {struct r} : P x :=\n F_sub x\n\n If you look carefully, this is proceeding by (structural) induction\n on r and is just a seemingly infinite loop. But since r must be\n getting smaller each time around the loop, the loop actually\n terminates.\n\n What is r? It's a proof that the value x is \"accessible\" (Acc) with\n respect to the relation R. In our case, R is essentially the \n relation between the length of the original input\n the length of the list we are recursing on (a < relation on the\n lengths of the lists.) The \"accessible\" notion is capturing the\n idea that our relation has no infinite descending chains. In the\n case of <, there is a least element, namely 0. So if we are always\n going down in the relation, we will eventually get to 0. \n\n TL;DR: You don't need to understand all of this. You just need\n to be able to use the [Program Fixpoint ... {measure ...}] construct\n to write functions and argue that the generated obligations are\n met (i.e., that your measure really is decreasing.)\n*)\n\n(* Once we've defined our merge_iter, we can finally define\n our merge_sort: *)\nDefinition merge_sort (xs:list nat) := \n merge_iter (make_lists xs).\n\n(* Let's test that it's working... *)\nEval compute in merge_sort [7;8;3;5;1;2;6;4].\nEval compute in merge_sort [3;2;7;8].\n\n(* Exercises:\n\n1. Show that\n\n a. sorted xs -> sorted ys -> sorted (merge xs ys)\n\n*)\n\nLemma merge_list_all : forall P xs ys, list_all P xs -> list_all P ys -> list_all P (merge xs ys).\nintros P xs.\ninduction xs; intros.\nsimpl. assumption.\nsimpl merge.\ninduction ys.\nassumption.\nremember (nat_lte a a0) as cond. destruct cond.\nsimpl. destruct H. split. assumption. eapply IHxs; assumption.\ndestruct H0. apply IHys in H1.\nsimpl. split; assumption.\nQed.\n\nLemma list_all_le : forall a n ys, a <= n -> list_all (le n) ys -> list_all (le a) ys.\nintros.\ninduction ys.\nsimpl. reflexivity.\ndestruct H0.\napply IHys in H1.\nsimpl. split. rewrite H0 in H. assumption.\nassumption.\nQed.\n\nTheorem merged_sorted : forall xs ys, sorted xs -> sorted ys -> sorted (merge xs ys).\n intro xs.\n induction xs; intros.\n simpl. assumption.\n induction ys. assumption.\n simpl merge.\n destruct (nat_lte a a0); simpl; split.\n (* a <= a0 *)\n eapply IHxs.\n crush. crush.\n eapply merge_list_all.\n crush.\n simpl.\n split. assumption. apply list_all_le with (n:=a0); crush.\n (* a > a0 *)\n simpl in H0.\n destruct H0.\n apply IHys in H0.\n simpl in H0.\n assumption.\n simpl in H0.\n eapply merge_list_all with (P:=(le a0)) (xs:=(a :: xs)) (ys:= ys); intros.\n simpl.\n split.\n crush.\n simpl in H. destruct H. apply list_all_le with (n:=a); crush.\n crush.\nQed.\n\n(*\n b. (length xs + length ys) = length (merge xs ys)\n*)\n\nRequire Import Arith.\n\nTheorem merge_adds : forall xs ys, (length xs + length ys) = length (merge xs ys).\n induction xs. crush.\n induction ys. crush.\n simpl. destruct (nat_lte a a0).\n simpl. rewrite <- IHxs with (ys:=(a0 :: ys)).\n crush. simpl in *. rewrite <- IHys. crush.\nQed.\n\n(*\n c. In x xs \\/ In x ys <-> In (merge xs ys)\n*)\n\nTheorem merged_in : forall x xs ys, In x xs \\/ In x ys <-> In x (merge xs ys).\n induction xs. crush.\n induction ys. crush.\n simpl. destruct (nat_lte a a0).\n simpl. rewrite <- IHxs with (ys:=(a0 :: ys)). crush. \n simpl in *. rewrite <- IHys. crush.\nQed.\n\n(*\n\n2. Show that \n\n a. list_all sorted xs -> list_all sorted (merge_pairs xs)\n\n*)\n\nRequire Import Arith.Even.\n\nLemma length0 : forall A (xs : list A), length xs = 0 <-> xs = [].\ninduction xs. crush. simpl. split; crush.\nQed.\n\nLemma length0' : forall A (xs : list A), 0 = length xs <-> xs = [].\ninduction xs. crush. simpl. split; crush.\nQed.\n\nLemma not_odd_and_even : forall n, odd n -> even n -> False.\ncrush. apply not_even_and_odd with (n:=n); assumption.\nQed.\n\nLemma oddS : forall m, odd (S m) -> even m.\nintros. destruct m.\ncrush. specialize even_odd_dec with (n:=(S m)).\nintros. destruct H0. assumption. contradict H.\nintro. apply not_even_and_odd with (n:=(S (S m))).\nconstructor. assumption. assumption.\nQed.\n\nLemma evenS : forall m, even (S m) -> odd m.\nintros. induction m.\ncontradict H. crush. assert (odd 1). crush.\napply not_even_and_odd with (n:=1); assumption.\nspecialize even_odd_dec with (n:=(S m)).\nintros. destruct H0. apply IHm in e.\ncontradict H. intro. apply not_even_and_odd with (n:=(S (S m))).\nassumption. constructor. constructor. assumption. assumption.\nQed.\n \nLemma evenSn: forall m, (even m -> (exists n, m = 2*n)) /\\ (odd m -> exists n, m = S(2*n)).\ninduction m.\nsplit. intros. exists 0. simpl. reflexivity.\nintro. contradict H. crush. assert (even 0). crush. \napply not_even_and_odd with (n:=0); assumption.\nsplit. destruct IHm.\nintro. apply evenS in H1. apply H0 in H1.\ndestruct H1. rewrite H1. exists (S x). crush.\ndestruct IHm. intro. apply oddS in H1. apply H in H1.\ndestruct H1. rewrite H1. exists x. crush.\nQed.\n\nLemma evenSn2: forall m, (exists n, m = 2*n) -> even m.\nintros. destruct H. simpl in H. assert( x + (x + 0) = x+x). crush. rewrite H0 in H. clear H0.\nspecialize even_odd_dec with (n:=x).\nintros. rewrite H. destruct H0.\napply even_even_plus; assumption.\napply odd_even_plus; assumption.\nQed.\n\nLemma test: forall A (xs: list A), even (length xs) -> (length xs <> 0) -> (exists a b t, (xs = a :: b :: t) /\\ (even (length t))).\nintros.\ndestruct xs. contradict H0.\nsimpl. reflexivity.\ndestruct xs.\ncontradict H.\nsimpl. intro.\nassert (odd 1). crush.\napply not_even_and_odd with (n:=1); assumption.\nexists a. exists a0. exists xs. split. reflexivity.\nsimpl in H. apply evenS in H. apply oddS in H. assumption.\nQed.\n\nLemma neq : forall n, n<>0 <-> exists m, n = S m.\ncrush. destruct n. crush. exists n. reflexivity. Qed.\n\nLemma list_all_tail : forall A P (l:A) (r: list A), list_all P r /\\ P l <-> list_all P (r ++ [l]).\nintros. induction r; simpl; crush.\nQed.\n\nLemma app_cons : forall A (x:A) xs, x :: xs = [x] ++ xs.\ncrush.\nQed.\n\nLemma merge_pairs_app: forall xs l, even (length xs) -> merge_pairs (xs ++ [l]) = (merge_pairs xs) ++ [l].\n Lemma merge_pairs_app': forall n xs l, (n*2) = length xs -> merge_pairs (xs ++ [l]) = (merge_pairs xs) ++ [l].\n induction n.\n intros. simpl in H. apply eq_sym in H. rewrite length0 in H. rewrite H.\n simpl. reflexivity.\n intros.\n pose proof test.\n assert (even (length xs)). apply evenSn2. exists (S n).\n rewrite mult_comm. apply eq_sym. assumption.\n specialize H0 with (xs:=xs).\n apply H0 in H1. destruct H1. destruct H1. destruct H1.\n destruct H1. rewrite H1. simpl.\n assert (merge_pairs x1 ++ [l] = merge_pairs (x1 ++ [l])).\n apply eq_sym. apply IHn.\n rewrite H1 in H. simpl in H. injection H. intros. assumption.\n rewrite H3. reflexivity.\n apply neq. rewrite <- H. simpl. exists (S (n * 2)). reflexivity.\n Qed.\n intros. apply evenSn in H. destruct H.\n apply merge_pairs_app' with (n:=x). crush.\nQed.\n\n\nLemma odd0 : odd 0 -> False. intro. assert (even 0). crush. apply not_even_and_odd with (n:=0); assumption. Qed.\n\nLtac mymagic := match goal with\n | [ H: odd 0 |- _ ] => apply odd0 in H; crush\n | [ H: odd (length []) |- _ ] => simpl in H; apply odd0 in H; crush\n | [ H: context[ _ + length [_]] |- _ ] => simpl length in H; rewrite plus_comm in H; simpl plus in H\n | [ H: context[(length (_ ++ [_]))] |- _ ] => rewrite app_length in H; mymagic\n | [ H: 0 = length ?xs |- _ ] => apply eq_sym in H; apply length0 in H\nend.\n\nLemma evenSn' : forall A (Q: list A -> Prop), (forall n (xs : list A), 2*n = length xs -> Q xs) <-> (forall (xs : list A), (even (length xs)) -> Q xs).\n intros. split.\n intros. apply evenSn in H0. destruct H0. apply eq_sym in H0. apply H in H0. assumption.\n intros. eapply H. apply evenSn2. exists n. crush.\nQed.\n\nLemma list_odd : forall A (xs:list A), (odd (length xs)) -> (exists (ys:list A) (t:A), xs = ys ++ [t] /\\ even (length ys)).\n intros.\n assert (xs <> []). crush. mymagic.\n exists (removelast xs). apply exists_last in H0.\n destruct H0. destruct s. exists x0. rewrite e. rewrite removelast_app. simpl.\n rewrite app_nil_r. split. reflexivity.\n rewrite e in H. mymagic. apply oddS in H. assumption. crush.\nQed.\n\nLtac third := let HH:=fresh in let H2:=fresh \"X\" in intro n; induction n; [\n (* Base Case *)\n simpl mult; try (split; intros x HH); try rewrite length0' in HH; subst; simpl in *; [ crush; try mymagic; crush | .. ]; try assumption |\n (* Start of Recursive Case *)\n intros xs Hy;\n remember (2 * S n) as m;\n assert (even m /\\ m <> 0) as Hx; [ split; [ apply evenSn2; exists (S n); assumption | crush ] | ];\n specialize test with (xs:=xs); intros Hm; rewrite <- Hy in Hm;\n destruct Hx; destruct Hm as [x H2]; [ assumption | assumption | .. ] ];\n destruct H2 as [y H2]; destruct H2 as [z H2]; destruct H2; subst.\n\nTheorem merge_pairs_sorted : forall xs, list_all sorted xs -> list_all sorted (merge_pairs xs).\n Lemma merge_pairs_sorted_even : forall xs, (even (length xs)) -> list_all sorted xs -> list_all sorted (merge_pairs xs).\n erewrite <- evenSn'. third. crush. apply merged_sorted; assumption.\n\n Qed.\n intros.\n remember (length xs) as n.\n specialize even_odd_dec with (n:=n). intros Hx. destruct Hx. \n + apply merge_pairs_sorted_even; [ crush | .. ]; try assumption.\n + specialize list_odd with (xs:=xs). intros Hy. subst. apply Hy in o.\n destruct o eqn:Hz. destruct Hz eqn:Ha. destruct e eqn:Hb. crush.\n rewrite merge_pairs_app; [ | assumption ].\n (* apply eq_sym. rewrite fold_right_app. *)\n rewrite H1 in H. apply list_all_tail in H. destruct H.\n apply list_all_tail; (try split); try assumption.\n apply merge_pairs_sorted_even. assumption. assumption.\nQed.\n\n(*\n b. (sum of lengths of lists in xs) = \n (sum of lengths of lists in merge_pairs xs)\n*)\n\nDefinition sum_length (a:list nat) (b:nat) := (plus (length a) b).\n\nHint Unfold sum_length.\n\nTheorem sum_merge_pairs : forall (xs: list (list nat)), (fold_right sum_length 0 xs) = (fold_right sum_length 0 (merge_pairs xs)).\n Lemma sum_merge_pairs_even : forall d xs, (even (length xs)) -> (fold_right sum_length d xs) = (fold_right sum_length d (merge_pairs xs)).\n intro d. erewrite <- evenSn'.\n third. crush. unfold sum_length. fold sum_length. rewrite <- merge_adds. erewrite <- IHn; [ | crush]. crush.\n Qed.\n intros.\n remember (length xs) as n.\n specialize even_odd_dec with (n:=n). intros Hx. destruct Hx. \n + apply sum_merge_pairs_even; [ crush | .. ]; try assumption.\n + specialize list_odd with (xs:=xs). intros Hy. subst. apply Hy in o.\n destruct o eqn:Hz. destruct Hz eqn:Ha. destruct e eqn:Hb. \n destruct a as [aa ab]. rewrite aa.\n rewrite merge_pairs_app; [ | assumption ].\n (* End Generic Skeleton *)\n rewrite fold_right_app. apply eq_sym. rewrite fold_right_app.\n rewrite <- sum_merge_pairs_even; [ | assumption ].\n subst. reflexivity.\nQed.\n\n(*\n c. (x is in one of the lists in xs) <->\n (x is in one of the lists in merge_pairs xs)\n\n*)\nDefinition InOneOf (x:nat) (xs: list (list nat)) : Prop :=\n exists subl, (In x subl) /\\ (In subl xs).\n\nHint Unfold InOneOf.\n\nLemma In_app : forall A (x: A) xs ys, In x (xs ++ ys) <-> In x xs \\/ In x ys.\n intros. induction xs; crush.\nQed.\n\nLemma InOneOf_app : forall x xs ys, InOneOf x (xs ++ ys) <-> InOneOf x xs \\/ InOneOf x ys.\n intros. unfold InOneOf.\n split; intros.\n do 2 destruct H; rewrite In_app in H0. \n { destruct H0.\n + left. exists x0. split; assumption.\n + right. exists x0. split; assumption. \n }\n do 2 destruct H; exists x0; rewrite In_app; crush.\nQed.\n\nLemma InOneOf_one : forall x y, InOneOf x [y] <-> In x y.\n crush. unfold InOneOf in H. crush. unfold InOneOf. exists y. crush.\nQed.\n\nLemma InOneOf_cons : forall x xs y, InOneOf x (y :: xs) <-> In x y \\/ InOneOf x xs.\n intros. rewrite app_cons. rewrite <- InOneOf_one. eapply InOneOf_app.\nQed. \n\nTheorem InPairs : forall (x:nat) (xs: list (list nat)), InOneOf x xs <-> InOneOf x (merge_pairs xs).\n Lemma InPairs_even : forall x xs, (even (length xs)) -> (InOneOf x xs <-> InOneOf x (merge_pairs xs)).\n intro d. erewrite <- evenSn'. third.\n simpl merge_pairs. repeat rewrite InOneOf_cons.\n rewrite <- merged_in. rewrite IHn; [ | crush ].\n crush.\n Qed.\n intros.\n remember (length xs) as n.\n specialize even_odd_dec with (n:=n). intros Hx. destruct Hx. \n + apply InPairs_even; [ crush | .. ]; try assumption.\n + specialize list_odd with (xs:=xs). intros Hy. subst. apply Hy in o.\n destruct o eqn:Hz. destruct Hz eqn:Ha. destruct e eqn:Hb.\n destruct a as [aa ab]. rewrite aa.\n rewrite merge_pairs_app; [ | assumption ].\n (* End Generic Skeleton *)\n rewrite InOneOf_app. apply iff_sym. rewrite InOneOf_app. \n rewrite InPairs_even with (xs:=x0); [ | assumption ].\n reflexivity.\nQed.\n\n(*\n\n3. Show that\n\n a. list_all sorted xs -> sorted (merge_iter xs)\n\n*)\n\nTheorem merge_iter_sorted : forall xs, list_all sorted xs -> sorted (merge_iter xs).\nintros.\nfunctional induction (merge_iter xs).\ncrush. crush. apply merge_pairs_sorted in H. apply IHl in H. assumption.\nQed.\n\n(*\n b. (sum of lengths of lists in xs) = length of (merge_iter xs)\n*)\n\nTheorem merge_iter_length : forall xs, (fold_right sum_length 0 xs) = length (merge_iter xs).\nintros.\nfunctional induction (merge_iter xs).\ncrush. crush. unfold sum_length. crush.\nrewrite sum_merge_pairs. assumption.\nQed.\n\n(*\n c. (x is in one of the lists in xs) <-> In x (merge_iter xs)\n*)\n\nTheorem merge_iter_in : forall x xs, InOneOf x xs <-> In x (merge_iter xs).\nintros.\nfunctional induction (merge_iter xs).\ncrush. unfold InOneOf in H. destruct H. crush.\ncrush. unfold InOneOf in H. crush.\nunfold InOneOf. exists h. crush.\nrewrite InPairs. assumption.\nQed.\n\n(*\n4. Show that\n a. make_lists xs is sorted\n b. In x xs <-> (x is in one of the lists in make_lists xs)\n c. length xs = (sum of the lengths of the lists in make_lists xs)\n*)\n\nTheorem make_lists_sorted : forall xs, list_all sorted (make_lists xs).\ninduction xs. crush. simpl. crush.\nQed.\n\nTheorem make_lists_in : forall x xs, In x xs <-> InOneOf x (make_lists xs).\nintros. induction xs.\ncrush. unfold InOneOf in H. destruct H. crush.\nsimpl. rewrite app_cons. rewrite InOneOf_app.\nsplit. intros.\ndestruct H. left. rewrite H. unfold InOneOf. exists [x]. crush.\napply IHxs in H. right. assumption.\nintros. destruct H. left. unfold InOneOf in H. destruct H. crush. crush.\nQed.\n\nTheorem make_lists_length : forall xs, length xs = (fold_right sum_length 0 (make_lists xs)).\nintros. induction xs. crush.\nsimpl. unfold sum_length. fold sum_length. crush.\nQed.\n\n (*\n\n5. Show that sort_corr xs (merge_sort xs)\n\n*)\n\nTheorem merge_corr: forall xs, sort_corr xs (merge_sort xs).\ncrush. unfold sort_corr. unfold merge_sort.\nsplit. apply merge_iter_sorted. apply make_lists_sorted.\nsplit. unfold sublist. intros.\napply merge_iter_in. apply make_lists_in. assumption.\nrewrite <- merge_iter_length. apply make_lists_length.\nQed.\n\n(*\n\n6. Define a better definition of correctness for sorts (sort_corr').\n\n*)\n\nRequire Import Permutation.\n\nDefinition sort_corr' (xs ys:list nat) : Prop :=\n sorted ys /\\ Permutation xs ys.\n\n(* This is better because *)\nGoal sort_corr ([1;1;2;2;3;3]) ([1;1;2;2;2;3]).\n unfold sort_corr.\n unfold sorted.\n unfold sublist.\n simpl.\n crush.\nQed.\n\nGoal ~ sort_corr' ([1;1;2;2;3;3]) ([1;1;2;2;2;3]). \n unfold sort_corr'. crush. clear -H1.\n repeat apply Permutation_cons_inv in H1.\n apply Permutation_length_2_inv in H1.\n destruct H1; crush.\nQed.\n\n(* Let's show that this is a stronger condition *)\nTheorem sort_corr'_strong : forall xs ys, sort_corr' xs ys -> sort_corr xs ys.\n intros. unfold sort_corr. unfold sort_corr' in H. crush.\n - unfold sublist. intros. apply Permutation_in with (l:=xs); assumption.\n - apply Permutation_length; assumption.\nQed.\n\n(* And let's show that merge still satisfies this *)\nFixpoint flatten {A: Type} (xs: list (list A)) : list A :=\n match xs with\n | [] => []\n | A::B => (app A (flatten B))\n end.\n\nLemma flatten_app : forall x (xs: list (list nat)), flatten (xs ++ [x]) = (flatten xs) ++ x.\n intros. induction xs; crush.\nQed.\n\nLemma make_lists_inv : forall xs, xs = (flatten (make_lists xs)).\n induction xs. crush. crush.\nQed.\n\nTheorem merge_pairs_permutation : forall xs, Permutation (flatten xs) (flatten (merge_pairs xs)).\nLemma merge_pairs_permutation_even : forall xs, even (length xs) -> Permutation (flatten xs) (flatten (merge_pairs xs)).\n Lemma merge_pairs_permutation_even' : forall n xs, 2*n=length xs -> Permutation (flatten xs) (flatten (merge_pairs xs)).\n Lemma merge_permutation : forall xs ys, Permutation (xs++ys) (merge xs ys).\n induction xs. crush.\n induction ys. \n - crush. apply perm_skip. specialize IHxs with (ys:=[]). \n rewrite app_nil_r in IHxs. apply Permutation_sym. assumption.\n - simpl. destruct (nat_lte a a0).\n apply perm_skip. eapply IHxs.\n apply Permutation_trans with (l' := (a0 :: a :: xs ++ ys)).\n + admit.\n + unfold merge in IHxs. fold merge in IHxs. apply perm_skip. apply IHys.\n Qed.\n third.\n specialize IHn with (xs:=z).\n assert (2*n = length z). crush.\n apply IHn in H1. clear IHn.\n crush.\n rewrite app_assoc.\n apply Permutation_app.\n apply merge_permutation.\n assumption.\n Qed.\n erewrite <- evenSn'. apply merge_pairs_permutation_even'.\n Qed.\n Show.\n intros.\n remember (length xs) as n.\n specialize even_odd_dec with (n:=n). intros Hx.\n destruct Hx. apply merge_pairs_permutation_even. crush.\n specialize list_odd with (xs:=xs). intros Hy. subst. apply Hy in o.\n destruct o eqn:Hz. destruct Hz eqn:Ha. destruct e eqn:Hb. crush.\n rewrite merge_pairs_app.\n remember (fold_right sum_length 0 (x1 ++ [x2])).\n do 2 rewrite flatten_app.\n apply Permutation_app_tail.\n apply merge_pairs_permutation_even. assumption.\n assumption.\nQed.\n\nTheorem merge_iter_perm : forall xs, Permutation (merge_iter xs) (flatten xs).\n crush. functional induction (merge_iter xs).\n crush. crush. rewrite app_nil_r. apply Permutation_refl.\n apply Permutation_trans with (l' := flatten (merge_pairs (h1 :: h2 :: xs'))).\n assumption.\n apply Permutation_sym. apply merge_pairs_permutation.\nQed.\n\nTheorem sort_corr'_merge : forall xs, sort_corr' xs (merge_sort xs).\n intros. unfold sort_corr'.\n specialize merge_corr with (xs:=xs). \n split. unfold sort_corr in H. crush.\n unfold merge_sort.\n unfold sort_corr in H.\n unfold merge_sort.\n destruct (merge_sort xs).\n crush. apply length0 in H2. crush.\n rewrite make_lists_inv with (xs:=xs) at 1. \n apply Permutation_sym.\n apply merge_iter_perm.\nQed.\n", "meta": {"author": "Keno", "repo": "CS250", "sha": "5865c43b99d3acee956d610475445894851397f6", "save_path": "github-repos/coq/Keno-CS250", "path": "github-repos/coq/Keno-CS250/CS250-5865c43b99d3acee956d610475445894851397f6/pset3/pset3.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9553191297273498, "lm_q2_score": 0.948917261778908, "lm_q1q2_score": 0.9065188127058862}} |
| {"text": "(*\nExercise: 3 stars (binary)\nConsider a different, more efficient representation of natural numbers using a binary rather\nthan unary system. That is, instead of saying that each natural number is either zero or the\nsuccessor of a natural number, we can say that each binary number is either\n\n zero,\n twice a binary number, or\n one more than twice a binary number.\n\n(a) First, write an inductive definition of the type bin corresponding to this description of\nbinary numbers.\n\n(Hint: Recall that the definition of nat from class,\n Inductive nat : Type :=\n | O : nat\n | S : nat → nat.\n\nsays nothing about what O and S \"mean.\" It just says \"O is in the set called nat, and if n is\nin the set then so is S n.\" The interpretation of O as zero and S as successor/plus one comes\nfrom the way that we use nat values, by writing functions to do things with them, proving \nthings about them, and so on. Your definition of bin should be correspondingly simple; it is\nthe functions you will write next that will give it mathematical meaning.)\n\n(b) Next, write an increment function for binary numbers, and a function to convert binary\nnumbers to unary numbers.\n(c) Write some unit tests for your increment and binary-to-unary functions. Notice that\nincrementing a binary number and then converting it to unary should yield the same result as\nfirst converting it to unary and then incrementing.\n\n *)\n", "meta": {"author": "kisom", "repo": "sf", "sha": "36ec076f140a79a382f298b277f19d4d9dc236a2", "save_path": "github-repos/coq/kisom-sf", "path": "github-repos/coq/kisom-sf/sf-36ec076f140a79a382f298b277f19d4d9dc236a2/basics/bin.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.953966098909522, "lm_q2_score": 0.9489172659321807, "lm_q1q2_score": 0.9052349023692119}} |
|
|