|
| 1 | +(** * SfLib: Software Foundations Library *) |
| 2 | + |
| 3 | +(* $Date: 2013-07-17 16:19:11 -0400 (Wed, 17 Jul 2013) $ *) |
| 4 | + |
| 5 | +(** Here we collect together several useful definitions and theorems |
| 6 | + from Basics.v, List.v, Poly.v, Ind.v, and Logic.v that are not |
| 7 | + already in the Coq standard library. From now on we can [Import] |
| 8 | + or [Export] this file, instead of cluttering our environment with |
| 9 | + all the examples and false starts in those files. *) |
| 10 | + |
| 11 | +(** * From the Coq Standard Library *) |
| 12 | + |
| 13 | +Require Omega. (* needed for using the [omega] tactic *) |
| 14 | +Require Export Bool. |
| 15 | +Require Export List. |
| 16 | +Export ListNotations. |
| 17 | +Require Export Arith. |
| 18 | +Require Export Arith.EqNat. (* Contains [beq_nat], among other things *) |
| 19 | + |
| 20 | +(** * From Basics.v *) |
| 21 | + |
| 22 | +Definition admit {T: Type} : T. Admitted. |
| 23 | + |
| 24 | +Require String. Open Scope string_scope. |
| 25 | + |
| 26 | +Ltac move_to_top x := |
| 27 | + match reverse goal with |
| 28 | + | H : _ |- _ => try move x after H |
| 29 | + end. |
| 30 | + |
| 31 | +Tactic Notation "assert_eq" ident(x) constr(v) := |
| 32 | + let H := fresh in |
| 33 | + assert (x = v) as H by reflexivity; |
| 34 | + clear H. |
| 35 | + |
| 36 | +Tactic Notation "Case_aux" ident(x) constr(name) := |
| 37 | + first [ |
| 38 | + set (x := name); move_to_top x |
| 39 | + | assert_eq x name; move_to_top x |
| 40 | + | fail 1 "because we are working on a different case" ]. |
| 41 | + |
| 42 | +Tactic Notation "Case" constr(name) := Case_aux Case name. |
| 43 | +Tactic Notation "SCase" constr(name) := Case_aux SCase name. |
| 44 | +Tactic Notation "SSCase" constr(name) := Case_aux SSCase name. |
| 45 | +Tactic Notation "SSSCase" constr(name) := Case_aux SSSCase name. |
| 46 | +Tactic Notation "SSSSCase" constr(name) := Case_aux SSSSCase name. |
| 47 | +Tactic Notation "SSSSSCase" constr(name) := Case_aux SSSSSCase name. |
| 48 | +Tactic Notation "SSSSSSCase" constr(name) := Case_aux SSSSSSCase name. |
| 49 | +Tactic Notation "SSSSSSSCase" constr(name) := Case_aux SSSSSSSCase name. |
| 50 | + |
| 51 | +Fixpoint ble_nat (n m : nat) : bool := |
| 52 | + match n with |
| 53 | + | O => true |
| 54 | + | S n' => |
| 55 | + match m with |
| 56 | + | O => false |
| 57 | + | S m' => ble_nat n' m' |
| 58 | + end |
| 59 | + end. |
| 60 | + |
| 61 | +Theorem andb_true_elim1 : forall b c, |
| 62 | + andb b c = true -> b = true. |
| 63 | +Proof. |
| 64 | + intros b c H. |
| 65 | + destruct b. |
| 66 | + Case "b = true". |
| 67 | + reflexivity. |
| 68 | + Case "b = false". |
| 69 | + rewrite <- H. reflexivity. Qed. |
| 70 | + |
| 71 | +Theorem andb_true_elim2 : forall b c, |
| 72 | + andb b c = true -> c = true. |
| 73 | +Proof. |
| 74 | +(* An exercise in Basics.v *) |
| 75 | +Admitted. |
| 76 | + |
| 77 | +Theorem beq_nat_sym : forall (n m : nat), |
| 78 | + beq_nat n m = beq_nat m n. |
| 79 | +(* An exercise in Lists.v *) |
| 80 | +Admitted. |
| 81 | + |
| 82 | +(** * From Props.v *) |
| 83 | + |
| 84 | +Inductive ev : nat -> Prop := |
| 85 | + | ev_0 : ev O |
| 86 | + | ev_SS : forall n:nat, ev n -> ev (S (S n)). |
| 87 | + |
| 88 | +(** * From Logic.v *) |
| 89 | + |
| 90 | +Theorem andb_true : forall b c, |
| 91 | + andb b c = true -> b = true /\ c = true. |
| 92 | +Proof. |
| 93 | + intros b c H. |
| 94 | + destruct b. |
| 95 | + destruct c. |
| 96 | + apply conj. reflexivity. reflexivity. |
| 97 | + inversion H. |
| 98 | + inversion H. Qed. |
| 99 | + |
| 100 | +Theorem false_beq_nat: forall n n' : nat, |
| 101 | + n <> n' -> |
| 102 | + beq_nat n n' = false. |
| 103 | +Proof. |
| 104 | +(* An exercise in Logic.v *) |
| 105 | +Admitted. |
| 106 | + |
| 107 | +Theorem ex_falso_quodlibet : forall (P:Prop), |
| 108 | + False -> P. |
| 109 | +Proof. |
| 110 | + intros P contra. |
| 111 | + inversion contra. Qed. |
| 112 | + |
| 113 | +Theorem ev_not_ev_S : forall n, |
| 114 | + ev n -> ~ ev (S n). |
| 115 | +Proof. |
| 116 | +(* An exercise in Logic.v *) |
| 117 | +Admitted. |
| 118 | + |
| 119 | +Theorem ble_nat_true : forall n m, |
| 120 | + ble_nat n m = true -> n <= m. |
| 121 | +(* An exercise in Logic.v *) |
| 122 | +Admitted. |
| 123 | + |
| 124 | +Theorem ble_nat_false : forall n m, |
| 125 | + ble_nat n m = false -> ~(n <= m). |
| 126 | +(* An exercise in Logic.v *) |
| 127 | +Admitted. |
| 128 | + |
| 129 | +Inductive appears_in (n : nat) : list nat -> Prop := |
| 130 | +| ai_here : forall l, appears_in n (n::l) |
| 131 | +| ai_later : forall m l, appears_in n l -> appears_in n (m::l). |
| 132 | + |
| 133 | +Inductive next_nat (n:nat) : nat -> Prop := |
| 134 | + | nn : next_nat n (S n). |
| 135 | + |
| 136 | +Inductive total_relation : nat -> nat -> Prop := |
| 137 | + tot : forall n m : nat, total_relation n m. |
| 138 | + |
| 139 | +Inductive empty_relation : nat -> nat -> Prop := . |
| 140 | + |
| 141 | +(** * From Later Files *) |
| 142 | + |
| 143 | +Definition relation (X:Type) := X -> X -> Prop. |
| 144 | + |
| 145 | +Definition deterministic {X: Type} (R: relation X) := |
| 146 | + forall x y1 y2 : X, R x y1 -> R x y2 -> y1 = y2. |
| 147 | + |
| 148 | +Inductive multi (X:Type) (R: relation X) |
| 149 | + : X -> X -> Prop := |
| 150 | + | multi_refl : forall (x : X), |
| 151 | + multi X R x x |
| 152 | + | multi_step : forall (x y z : X), |
| 153 | + R x y -> |
| 154 | + multi X R y z -> |
| 155 | + multi X R x z. |
| 156 | +Implicit Arguments multi [[X]]. |
| 157 | + |
| 158 | +Tactic Notation "multi_cases" tactic(first) ident(c) := |
| 159 | + first; |
| 160 | + [ Case_aux c "multi_refl" | Case_aux c "multi_step" ]. |
| 161 | + |
| 162 | +Theorem multi_R : forall (X:Type) (R:relation X) (x y : X), |
| 163 | + R x y -> multi R x y. |
| 164 | +Proof. |
| 165 | + intros X R x y r. |
| 166 | + apply multi_step with y. apply r. apply multi_refl. Qed. |
| 167 | + |
| 168 | +Theorem multi_trans : |
| 169 | + forall (X:Type) (R: relation X) (x y z : X), |
| 170 | + multi R x y -> |
| 171 | + multi R y z -> |
| 172 | + multi R x z. |
| 173 | +Proof. |
| 174 | + (* FILL IN HERE *) Admitted. |
| 175 | + |
| 176 | +(** Identifiers and polymorphic partial maps. *) |
| 177 | + |
| 178 | +Inductive id : Type := |
| 179 | + Id : nat -> id. |
| 180 | + |
| 181 | +Theorem eq_id_dec : forall id1 id2 : id, {id1 = id2} + {id1 <> id2}. |
| 182 | +Proof. |
| 183 | + intros id1 id2. |
| 184 | + destruct id1 as [n1]. destruct id2 as [n2]. |
| 185 | + destruct (eq_nat_dec n1 n2) as [Heq | Hneq]. |
| 186 | + Case "n1 = n2". |
| 187 | + left. rewrite Heq. reflexivity. |
| 188 | + Case "n1 <> n2". |
| 189 | + right. intros contra. inversion contra. apply Hneq. apply H0. |
| 190 | +Defined. |
| 191 | + |
| 192 | +Lemma eq_id : forall (T:Type) x (p q:T), |
| 193 | + (if eq_id_dec x x then p else q) = p. |
| 194 | +Proof. |
| 195 | + intros. |
| 196 | + destruct (eq_id_dec x x); try reflexivity. |
| 197 | + apply ex_falso_quodlibet; auto. |
| 198 | +Qed. |
| 199 | + |
| 200 | +Lemma neq_id : forall (T:Type) x y (p q:T), x <> y -> |
| 201 | + (if eq_id_dec x y then p else q) = q. |
| 202 | +Proof. |
| 203 | + (* FILL IN HERE *) Admitted. |
| 204 | + |
| 205 | +Definition partial_map (A:Type) := id -> option A. |
| 206 | + |
| 207 | +Definition empty {A:Type} : partial_map A := (fun _ => None). |
| 208 | + |
| 209 | +Notation "'\empty'" := empty. |
| 210 | + |
| 211 | +Definition extend {A:Type} (Gamma : partial_map A) (x:id) (T : A) := |
| 212 | + fun x' => if eq_id_dec x x' then Some T else Gamma x'. |
| 213 | + |
| 214 | +Lemma extend_eq : forall A (ctxt: partial_map A) x T, |
| 215 | + (extend ctxt x T) x = Some T. |
| 216 | +Proof. |
| 217 | + intros. unfold extend. rewrite eq_id; auto. |
| 218 | +Qed. |
| 219 | + |
| 220 | +Lemma extend_neq : forall A (ctxt: partial_map A) x1 T x2, |
| 221 | + x2 <> x1 -> |
| 222 | + (extend ctxt x2 T) x1 = ctxt x1. |
| 223 | +Proof. |
| 224 | + intros. unfold extend. rewrite neq_id; auto. |
| 225 | +Qed. |
| 226 | + |
| 227 | +Lemma extend_shadow : forall A (ctxt: partial_map A) t1 t2 x1 x2, |
| 228 | + extend (extend ctxt x2 t1) x2 t2 x1 = extend ctxt x2 t2 x1. |
| 229 | +Proof with auto. |
| 230 | + intros. unfold extend. destruct (eq_id_dec x2 x1)... |
| 231 | +Qed. |
| 232 | + |
| 233 | +(** -------------------- *) |
| 234 | + |
| 235 | +(** * Some useful tactics *) |
| 236 | + |
| 237 | +Tactic Notation "solve_by_inversion_step" tactic(t) := |
| 238 | + match goal with |
| 239 | + | H : _ |- _ => solve [ inversion H; subst; t ] |
| 240 | + end |
| 241 | + || fail "because the goal is not solvable by inversion.". |
| 242 | + |
| 243 | +Tactic Notation "solve" "by" "inversion" "1" := |
| 244 | + solve_by_inversion_step idtac. |
| 245 | +Tactic Notation "solve" "by" "inversion" "2" := |
| 246 | + solve_by_inversion_step (solve by inversion 1). |
| 247 | +Tactic Notation "solve" "by" "inversion" "3" := |
| 248 | + solve_by_inversion_step (solve by inversion 2). |
| 249 | +Tactic Notation "solve" "by" "inversion" := |
| 250 | + solve by inversion 1. |
0 commit comments