| 1 |
(* -*- mode: coq; coq-prog-args: ("-allow-rewrite-rules") -*- *) |
| 2 |
|
| 3 |
(* Simple first example *) |
| 4 |
Symbol pplus : nat -> nat -> nat. |
| 5 |
Notation "a ++ b" := (pplus a b). |
| 6 |
|
| 7 |
Rewrite Rules plus_rew := |
| 8 |
| ?n ++ 0 => ?n |
| 9 |
| ?n ++ S ?n' => S (?n ++ ?n') |
| 10 |
| 0 ++ ?n => ?n |
| 11 |
| S ?n ++ ?n' => S (?n ++ ?n'). |
| 12 |
|
| 13 |
Check eq_refl : 5 ++ 10 = 15. |
| 14 |
Check (fun _ _ => eq_refl) : forall n n', 2 + n ++ 3 + n' = 5 + (n ++ n'). |
| 15 |
|
| 16 |
(* Test deep pattern matching *) |
| 17 |
Eval lazy in fun n n' => 2 + n ++ 3 + n'. |
| 18 |
Eval cbv in fun n n' => 2 + n ++ 3 + n'. |
| 19 |
Eval cbn in fun n n' => 2 + n ++ 3 + n'. |
| 20 |
Eval simpl in fun n n' => 2 + n ++ 3 + n'. (* Does not reduce *) |
| 21 |
|
| 22 |
(* Example with more pattern constructions and higher-order in patterns *) |
| 23 |
#[unfold_fix] Symbol raise : forall P: Type, P. |
| 24 |
|
| 25 |
Rewrite Rules raise_rew := |
| 26 |
raise (forall (x : ?A), ?P) => fun x => raise ?P |
| 27 |
|
| 28 |
| raise (?A * ?B) => (raise ?A, raise ?B) |
| 29 |
|
| 30 |
| raise unit => tt |
| 31 |
|
| 32 |
| match raise bool as b return ?P with |
| 33 |
true => _ | false => _ |
| 34 |
end => raise ?P@{b := raise bool} |
| 35 |
|
| 36 |
| match raise nat as n return ?P with |
| 37 |
0 => ?p | S n => ?p' |
| 38 |
end => raise ?P@{n := raise nat} |
| 39 |
|
| 40 |
| match raise (@eq ?A ?a ?b) as e in _ = b return ?P with |
| 41 |
| eq_refl => _ |
| 42 |
end => raise ?P@{b := _; e := raise (?a = ?b)} |
| 43 |
|
| 44 |
| match raise (list ?A) as l return ?P with |
| 45 |
| nil => _ | cons _ _ => _ |
| 46 |
end => raise ?P@{l := raise (list ?A)} |
| 47 |
|
| 48 |
| match raise False as e return ?P with |
| 49 |
end => raise ?P@{e := raise False} |
| 50 |
|
| 51 |
| match raise (?A + ?B) as e return ?P with |
| 52 |
| inl _ => _ | inr _ => _ |
| 53 |
end => raise ?P@{e := raise (?A + ?B)}. |
| 54 |
(* There is currently no way to write these rules without the universe inconcistency *) |
| 55 |
|
| 56 |
Eval simpl in match raise bool with true | false => 0 end. (* Does not reduce *) |
| 57 |
|
| 58 |
Eval lazy in match (raise nat * 5 + 3 :: 0 :: nil)%list with cons 0 l => tt | _ => tt end. |
| 59 |
|
| 60 |
Eval lazy in raise nat + 5. |
| 61 |
Eval cbv in raise nat + 5. |
| 62 |
Eval cbn in raise nat + 5. |
| 63 |
Eval simpl in raise nat + 5. (* Does not reduce *) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
Set Primitive Projections. |
| 70 |
Record primprod (A B : Type) := { fst: A; snd: B }. |
| 71 |
|
| 72 |
(* Example with even more pattern constructions, mostly for terms *) |
| 73 |
Universe idu. |
| 74 |
#[unfold_fix, universes(polymorphic)] Symbol id@{q; } : forall A : Type@{q;idu}, A -> A. |
| 75 |
|
| 76 |
Rewrite Rules id_rew := |
| 77 |
| @{q;u+|+} |- id _ Type@{q;u} => Type@{q;u} |
| 78 |
|
| 79 |
| @{q;u+|+} |- id Type@{q;u} (forall (x : ?A), ?P) => forall x, id Type@{q;u} ?P |
| 80 |
| id (forall (x : ?A), ?P) ?f => fun (x : ?A) => id ?P (?f x) |
| 81 |
|
| 82 |
| @{u+} |- id Type@{u} (?A * ?B)%type => (id Type@{u} ?A * id Type@{u} ?B)%type |
| 83 |
| id (?A * ?B) (?a, ?b) => (id _ ?a, id _ ?b) |
| 84 |
|
| 85 |
| id _ unit => unit |
| 86 |
| id _ tt => tt |
| 87 |
|
| 88 |
| id _ nat => nat |
| 89 |
| id _ 0 => 0 |
| 90 |
| id _ (S ?n) => S (id _ ?n) |
| 91 |
| id _ (fun (n : ?A) => S ?n) => fun n => S (id _ ?n) |
| 92 |
| id (primprod ?A ?B) {| fst := ?a; snd := ?b |} => {| fst := id _ ?a; snd := id _ ?b |}. |
| 93 |
|
| 94 |
Fail Rewrite Rule id_rew_fail := Datatypes.id _ ?x => ?x. (* Subterm not recognised as pattern: Datatypes.id *) |
| 95 |
Fail Rewrite Rule id_rew_fail := 0 => 0. (* Head head-pattern is not a symbol. *) |
| 96 |
Fail Rewrite Rule id_rew_fail := id _ (?x ?y) => ?x ?y. (* Subterm not recognised as pattern: ?x *) |
| 97 |
Fail Rewrite Rule id_rew_fail := id _ _ => ?x. (* Unknown existential variable. *) |
| 98 |
Fail Rewrite Rule id_rew_fail := @{u} |- id _ ?x => ?x. (* Not all universe level variables appear in the pattern. *) |
| 99 |
Fail Rewrite Rule id_rew_fail := id _ (?x, ?x) => ?x. (* Variable ?x is bound multiple times in the pattern (holes number 1 and 2). *) |
| 100 |
Fail Rewrite Rule id_rew_fail := @{u+} |- id _ (Type@{u}, Type@{u}) => ?x. (* Universe variable u is bound multiple times in the pattern (holes number 0 and 1). *) |
| 101 |
Fail Rewrite Rule id_rew_fail := id _ (?x, ?y) => (?x, ?y). (* The replacement term contains unresolved implicit arguments: (?x, ?y) *) |
| 102 |
Fail Rewrite Rule id_rew_fail := id _ Type => Type. (* Universe rewrule.xxx is unbound. *) |
| 103 |
Fail Rewrite Rule id_rew_fail := id _ (forall x, ?P) => ?P. (* Cannot interpret ?P in current context: no binding for x. *) |
| 104 |
|
| 105 |
|
| 106 |
Symbol idS : forall (A : SProp), A -> A. |
| 107 |
Inductive unitS : SProp := ttS. |
| 108 |
Rewrite Rule id_rew' := idS _ ttS => ttS. |
| 109 |
(* Warning: This subpattern is irrelevant and can never be matched against. *) |
| 110 |
|
| 111 |
Symbol vararity : forall n, (fix f n := match n with 0 => unit | S n => unit -> f n end) n. |
| 112 |
Check vararity (4 + _) tt tt tt _. |
| 113 |
Rewrite Rule vararity_rew := id _ (vararity _) => 0. |
| 114 |
(* Warning: This subpattern has a yet unknown type, which may be a product type, but pattern-matching is not done modulo eta, so this rule may not trigger at required times *) |
| 115 |
|
| 116 |
Module MLTTmap. |
| 117 |
|
| 118 |
Symbol map : forall A B, (A -> B) -> list A -> list B. |
| 119 |
|
| 120 |
Rewrite Rule map_rew := |
| 121 |
| map _ _ (fun x => x) ?l => ?l |
| 122 |
| map _ ?C ?f (map ?A _ ?g ?l) => map ?A ?C (fun x => ?f (?g x)) ?l |
| 123 |
| map ?A ?B ?f (@nil _) => @nil ?B |
| 124 |
| map ?A ?B ?f (@cons _ ?a ?l) => @cons ?B (?f ?a) (map _ _ ?f ?l). |
| 125 |
|
| 126 |
Definition idA {A: Type} := fun (x : A) => x. |
| 127 |
|
| 128 |
Eval lazy in fun l => (map _ _ idA l). |
| 129 |
Eval cbv in fun l => (map _ _ idA l). |
| 130 |
Eval cbn in fun l => (map _ _ idA l). |
| 131 |
Eval simpl in fun l => (map _ _ idA l). (* Does not reduce *) |
| 132 |
|
| 133 |
Eval lazy in fun l => (map _ _ (fun f x => f x) l). |
| 134 |
(* Does not reduce because there is no support for eta *) |
| 135 |
|
| 136 |
End MLTTmap. |
| 137 |
|
| 138 |
(* Example where ignore holes are necessary *) |
| 139 |
Symbol J : forall (A : Type) (a : A) (P : A -> Type), P a -> forall (a' : A), @eq A a a' -> P a'. |
| 140 |
Rewrite Rule a := J _ _ _ ?H _ (@eq_refl _ _) => ?H. |
| 141 |
|
| 142 |
|
| 143 |
Module omega. |
| 144 |
(* Example of a broken extension *) |
| 145 |
#[unfold_fix] Symbol omega : nat. |
| 146 |
Rewrite Rule omega_rew := match omega with S n => ?P | 0 => _ end => ?P@{n := omega}. |
| 147 |
Theorem omega_spec : S omega = omega. |
| 148 |
Proof. |
| 149 |
symmetry. |
| 150 |
change omega with (Nat.pred omega) at 2. |
| 151 |
remember omega as omeg eqn:e. |
| 152 |
destruct omeg. 2: reflexivity. |
| 153 |
apply (f_equal (fun n => match n with 0 => 0 | S _ => 1 end)) in e. |
| 154 |
apply e. |
| 155 |
Qed. |
| 156 |
|
| 157 |
Theorem omega_contradiction : False. |
| 158 |
Proof. |
| 159 |
assert (forall n, S n = n -> False) as X. |
| 160 |
2: eapply X, omega_spec. |
| 161 |
induction n. |
| 162 |
1: discriminate. |
| 163 |
now intros [=]. |
| 164 |
Qed. |
| 165 |
|
| 166 |
End omega. |
| 167 |
|
| 168 |
|
| 169 |
Module stream. |
| 170 |
|
| 171 |
(* Subtle interaction between rewriting and the guard-checker *) |
| 172 |
Inductive stream := T (_ : stream). |
| 173 |
|
| 174 |
Fixpoint f s : False := f match s with T s' => s' end. |
| 175 |
Fixpoint g s : False := match s with T s' => g s' end. |
| 176 |
|
| 177 |
Rewrite Rule raise_rew_stream := |
| 178 |
| match raise _ as s return ?P with T _ => _ end => raise ?P@{s := raise _}. |
| 179 |
|
| 180 |
Goal forall s, f s = g s. |
| 181 |
unfold f, g. |
| 182 |
induction s. assumption. |
| 183 |
Defined. |
| 184 |
|
| 185 |
Eval lazy in g (raise _). |
| 186 |
|
| 187 |
End stream. |
| 188 |
|
| 189 |
Module context. |
| 190 |
(* Test whether context extensions work correctly (here, with constructor arrguments)*) |
| 191 |
Symbol id : forall A, A -> A. |
| 192 |
Axioms (aa ee : nat). |
| 193 |
Inductive A := C (a := aa) (b : unit) (c := (a, b)) (d : True) (e := ee). |
| 194 |
|
| 195 |
Rewrite Rule raise_rew_C := match raise _ with C a b c d e => id (_ * _) ?P end => ?P@{a := _; b := raise _; c := _; d := raise _; e := _}. |
| 196 |
|
| 197 |
Eval lazy in match raise _ with C a b c d e => id _ (a, b, c, d, e) end. |
| 198 |
Eval cbv in match raise _ with C a b c d e => id _ (a, b, c, d, e) end. |
| 199 |
Eval cbn in match raise _ with C a b c d e => id _ (a, b, c, d, e) end. |
| 200 |
Eval simpl in match raise _ with C a b c d e => id _ (a, b, c, d, e) end. |
| 201 |
End context. |
| 202 |
|
| 203 |
(* Non-confluent rules prove False *) |
| 204 |
Symbol Devil : bool -> bool. |
| 205 |
|
| 206 |
Rewrite Rule devil := |
| 207 |
| Devil ?b => false |
| 208 |
| Devil true => true. |
| 209 |
|
| 210 |
Lemma Devil_false b : Devil b = false. |
| 211 |
Proof. reflexivity. Defined. |
| 212 |
|
| 213 |
Lemma Devil_true : Devil true = true. |
| 214 |
Proof. reflexivity. Defined. |
| 215 |
|
| 216 |
Lemma ministry_of_truth : true = false. |
| 217 |
Proof. |
| 218 |
transitivity (Devil true). |
| 219 |
- symmetry;exact Devil_true. |
| 220 |
- apply Devil_false. |
| 221 |
Defined. |
| 222 |
|
| 223 |
Corollary contradiction : False. |
| 224 |
Proof. |
| 225 |
pose proof ministry_of_truth; discriminate. |
| 226 |
Defined. |
| 227 |
|
| 228 |
Definition successor_of_nothing : nat := |
| 229 |
match ministry_of_truth in eq _ b return if b then bool else nat with |
| 230 |
eq_refl => false |
| 231 |
end. |
| 232 |
|
| 233 |
(* Such mistyped terms would break the VM, hence why it must be disabled *) |
| 234 |
Eval vm_compute in pred successor_of_nothing. |
| 235 |
|
| 236 |
Definition ignore {A} (x:A) := tt. |
| 237 |
|
| 238 |
Definition beginning_of_the_world : ignore (pred successor_of_nothing) = tt. |
| 239 |
Proof. lazy;reflexivity. Qed. |
| 240 |
|
| 241 |
Lemma end_of_the_world : tt = tt. |
| 242 |
Proof. |
| 243 |
vm_compute. |
| 244 |
exact beginning_of_the_world. |
| 245 |
Defined. |
| 246 |
(* This computation would run in the VM from the kernel, which is dangerous *) |
| 247 |
|
| 248 |
|
| 249 |
(* Having a common supertype is not enough to preserve SR *) |
| 250 |
Universe u. |
| 251 |
Symbol idTy@{i} : Type@{i} -> Type@{u}. |
| 252 |
|
| 253 |
Rewrite Rule idTy_id := idTy ?t => ?t. |
| 254 |
(* Warning: This rewrite rule breaks subject reduction (universe inconsistency). *) |
| 255 |
|
| 256 |
Definition U : Type@{u} := idTy Type@{u}. |
| 257 |
Check U : U. |
| 258 |
|
| 259 |
Definition id'@{i} : Type@{i} -> Type@{u} := fun (t: Type@{i}) => t. |
| 260 |
Fail Definition U' : Type@{u} := id' Type@{u}. |
| 261 |
|
| 262 |
Require Import TestSuite.hurkens. |
| 263 |
Goal False. |
| 264 |
apply (TypeNeqSmallType.paradox U eq_refl). |
| 265 |
Defined. |
| 266 |
|
| 267 |
|
| 268 |
(* Test substitution on context extensions *) |
| 269 |
Definition a : 0 = 0. |
| 270 |
set (test := let n := 0 in @eq_trans _ n n n (raise _) (raise _)). |
| 271 |
lazy delta in test. |
| 272 |
lazy beta in test. |
| 273 |
set (test_lazy := test). |
| 274 |
lazy delta zeta in test_lazy. |
| 275 |
set (test_cbv := test). |
| 276 |
cbv delta zeta in test_cbv. |
| 277 |
set (test_cbn := test). |
| 278 |
cbn delta zeta in test_cbn. |
| 279 |
set (test_simpl := test). |
| 280 |
unfold test in test_simpl. simpl in test_simpl. |
| 281 |
Abort. |
| 282 |
|
| 283 |
Definition test_subst_context := |
| 284 |
Eval cbv delta zeta in |
| 285 |
let n := 0 in |
| 286 |
match raise (n = n) in (_ = a) return (n = a) with |
| 287 |
| eq_refl => raise _ |
| 288 |
end. |