| 1 |
From elpi Require Import tc. |
| 2 |
|
| 3 |
Elpi Accumulate TC.Compiler lp:{{ |
| 4 |
:before "0" |
| 5 |
tc.add-class-gr _ A SM :- |
| 6 |
coq.say "Adding predicate for" A, |
| 7 |
coq.say "with mode" SM, fail, !. |
| 8 |
|
| 9 |
func old-version string, int, int, int ->. |
| 10 |
old-version _ 9 0 _. |
| 11 |
old-version _ 9 1 _. |
| 12 |
old-version _ 9 2 _. |
| 13 |
|
| 14 |
pred get-old-mode o:string. |
| 15 |
|
| 16 |
:after "0" |
| 17 |
main [str "new_class", str Cl, str _] :- old-version {coq.version}, get-old-mode Modes, not (var Modes), !, |
| 18 |
tc.time-it tc.oTC-time-compile-class ( |
| 19 |
coq.locate Cl GR, tc.add-class-gr tc.classic GR {rex.split " " Modes} |
| 20 |
) "Compiler for Class". |
| 21 |
|
| 22 |
}}. |
| 23 |
|
| 24 |
Module ES3. |
| 25 |
|
| 26 |
(* No explicit mode in the class, |
| 27 |
the type of the predicate for add is `pred -> term, term`. |
| 28 |
i.e. the nat is considered as an output |
| 29 |
*) |
| 30 |
Class Add (I: nat). |
| 31 |
|
| 32 |
Instance addNat: Add 0. Qed. |
| 33 |
|
| 34 |
(* No problem in apply _ since the evar can be unified with the pattern 0 *) |
| 35 |
Goal exists x, (Add x). |
| 36 |
Proof. eexists; now apply _. Qed. |
| 37 |
|
| 38 |
End ES3. |
| 39 |
|
| 40 |
Module ES3'. |
| 41 |
|
| 42 |
#[mode = "-"]Class Add (I: nat). |
| 43 |
|
| 44 |
Instance addNat: Add 0. Qed. |
| 45 |
|
| 46 |
(* No problem in apply _ since the evar can be unified with the pattern 0 *) |
| 47 |
Goal exists x, (Add x). |
| 48 |
Proof. eexists; now apply _. Qed. |
| 49 |
|
| 50 |
End ES3'. |
| 51 |
|
| 52 |
Module ES4. |
| 53 |
Section S. |
| 54 |
Elpi Accumulate TC.Compiler lp:{{ |
| 55 |
get-old-mode "+". |
| 56 |
}}. |
| 57 |
(* Plus is mapped to the elpi input mode, i.e. |
| 58 |
the type of the predicate for add is `pred term -> term`. |
| 59 |
*) |
| 60 |
#[mode = "+"] Class Add (I: nat). |
| 61 |
|
| 62 |
Instance addNat: Add 0. Qed. |
| 63 |
|
| 64 |
(* Failure in apply _ since the evar does not match the pattern 0 *) |
| 65 |
Goal exists x, (Add x). |
| 66 |
Proof. eexists. Fail apply _. Abort. |
| 67 |
End S. |
| 68 |
End ES4. |
| 69 |
|
| 70 |
|
| 71 |
Module m1. |
| 72 |
Elpi TC.Pending_mode +. |
| 73 |
Class C (i : nat). |
| 74 |
Instance C0 : C 0. Qed. |
| 75 |
Goal exists x, C x. eexists. Fail apply _. Abort. |
| 76 |
Class C' (i : nat). |
| 77 |
Instance C0' : C' 0. Qed. |
| 78 |
Goal exists x, C' x. eexists. apply _. Abort. |
| 79 |
|
| 80 |
Elpi TC.Pending_mode +. |
| 81 |
Fail Elpi TC.Pending_mode +. |
| 82 |
|
| 83 |
Class C'' (i : nat). |
| 84 |
Instance C0'' : C'' 0. Qed. |
| 85 |
Goal exists x, C'' x. eexists. Fail apply _. Abort. |
| 86 |
End m1. |
| 87 |
|
| 88 |
Module ground. |
| 89 |
Elpi TC.Pending_mode +. |
| 90 |
Class C (i : Type). |
| 91 |
Instance i : C (list nat). Qed. |
| 92 |
|
| 93 |
Goal exists (x : Type), C (list x). |
| 94 |
eexists. |
| 95 |
Fail apply _. |
| 96 |
Abort. |
| 97 |
End ground. |
| 98 |
|
| 99 |
Module ground1. |
| 100 |
Elpi TC.Pending_mode +. |
| 101 |
Class C (i : Type). |
| 102 |
Instance i x: C x. Qed. |
| 103 |
|
| 104 |
Goal exists (x : Type), C (list x). |
| 105 |
eexists. |
| 106 |
apply _. |
| 107 |
Abort. |
| 108 |
End ground1. |
| 109 |
|
| 110 |
Module ground2. |
| 111 |
Elpi TC.Pending_mode +. |
| 112 |
Class C (i : Type). |
| 113 |
Instance i (x: Type): C (list x). Qed. |
| 114 |
|
| 115 |
Goal exists (x : Type), C (list x). |
| 116 |
eexists. |
| 117 |
apply _. |
| 118 |
Abort. |
| 119 |
End ground2. |
| 120 |
|
| 121 |
Module ground3. |
| 122 |
Elpi TC.Pending_mode + +. |
| 123 |
Class C {i : Type} (f : i -> i -> Prop). |
| 124 |
Instance i {X : Type}: C (@eq X). Qed. |
| 125 |
|
| 126 |
Goal exists (X : Type), C (@eq X). |
| 127 |
eexists. |
| 128 |
apply _. |
| 129 |
Unshelve. |
| 130 |
apply nat. |
| 131 |
Qed. |
| 132 |
End ground3. |
| 133 |
|
| 134 |
Module ground4. |
| 135 |
Elpi TC.Pending_mode - +. |
| 136 |
Class C {i : Type} (f : i -> i -> Prop). |
| 137 |
Instance i {X : Type}: C (@eq X). Qed. |
| 138 |
Hint Mode C ! ! : typeclass_instances. |
| 139 |
|
| 140 |
Goal exists (X : Type), @C (list X) eq. |
| 141 |
eexists. |
| 142 |
apply _. |
| 143 |
Abort. |
| 144 |
End ground4. |
| 145 |
|
| 146 |
Module rigid_head1. |
| 147 |
Elpi TC.Pending_mode !. |
| 148 |
Class C (i : Type). |
| 149 |
Instance i: C (list nat). Qed. |
| 150 |
Hint Mode C ! : typeclass_instances. |
| 151 |
|
| 152 |
Goal exists (x : Type), C (list x). |
| 153 |
eexists. |
| 154 |
Fail apply _. |
| 155 |
Abort. |
| 156 |
|
| 157 |
Goal exists (x : Type), C x. |
| 158 |
eexists. |
| 159 |
Fail apply _. |
| 160 |
Abort. |
| 161 |
End rigid_head1. |
| 162 |
|
| 163 |
Module rigid_head2. |
| 164 |
Elpi TC.Pending_mode ! !. |
| 165 |
Class C {i : Type} (f : i -> i -> Prop). |
| 166 |
Instance i {X : Type}: C (@eq X). Qed. |
| 167 |
Hint Mode C ! ! : typeclass_instances. |
| 168 |
|
| 169 |
Goal exists (X : Type), C (@eq X). |
| 170 |
eexists. |
| 171 |
apply _. |
| 172 |
Unshelve. |
| 173 |
apply nat. |
| 174 |
Qed. |
| 175 |
End rigid_head2. |
| 176 |
|
| 177 |
Module simplEq. |
| 178 |
|
| 179 |
TC.Pending_mode "!". |
| 180 |
Class MyEqb A : Type := eqb : A -> A -> bool. |
| 181 |
(* Global Hint Mode MyEqb ! : typeclass_instances. *) |
| 182 |
|
| 183 |
Notation " x == y " := (eqb x y) (no associativity, at level 70). |
| 184 |
|
| 185 |
Global Instance eqU : MyEqb unit := { eqb x y := true }. |
| 186 |
Global Instance eqB : MyEqb bool := { eqb x y := if x then y else negb y }. |
| 187 |
Global Instance eqP {A B} `{MyEqb A} `{MyEqb B} : MyEqb (A * B) := { |
| 188 |
eqb x y := andb (fst x == fst y) (snd x == snd y) }. |
| 189 |
|
| 190 |
Fail Goal exists T: Type, forall n m : T, eqb n m = false. |
| 191 |
Goal forall n m : bool, eqb n m = false. Abort. |
| 192 |
End simplEq. |
| 193 |
|
| 194 |
Module force_input_link. |
| 195 |
TC.Pending_mode "+". |
| 196 |
Class A (i: nat -> nat -> nat). |
| 197 |
|
| 198 |
Global Hint Mode A + : typeclass_instances. |
| 199 |
|
| 200 |
Axiom (f : nat -> nat -> nat). |
| 201 |
Instance instA: A f := {}. |
| 202 |
|
| 203 |
Class B (i: nat). |
| 204 |
Instance instB : forall R, A R -> forall x y, B (R x y) := {}. |
| 205 |
|
| 206 |
Goal B (f 0 0). |
| 207 |
apply _. |
| 208 |
Qed. |
| 209 |
End force_input_link. |
| 210 |
|
| 211 |
Module force_input_link_HO_var1. |
| 212 |
|
| 213 |
TC.Pending_mode !. |
| 214 |
Class A (i: Type). |
| 215 |
Global Hint Mode A ! : typeclass_instances. (*Mode also added in elpi*) |
| 216 |
|
| 217 |
Axiom f : Type -> Type. |
| 218 |
Axiom g : Type -> Type. |
| 219 |
|
| 220 |
Class B (i :Type). |
| 221 |
|
| 222 |
(* Elpi Trace Browser. *) |
| 223 |
Instance i: forall X (Y: Type), (forall Y, A (X Y)) -> B (X Y) := {}. |
| 224 |
Instance a : forall x, A (g x) := {}. |
| 225 |
Instance b x : A (f x) -> A (f x) := {}. |
| 226 |
|
| 227 |
|
| 228 |
Goal B (g nat). |
| 229 |
apply _. |
| 230 |
Qed. |
| 231 |
|
| 232 |
End force_input_link_HO_var1. |
| 233 |
|
| 234 |
Module force_input_link_HO_var2. |
| 235 |
|
| 236 |
TC.Pending_mode !. |
| 237 |
Class A (i: Type). |
| 238 |
Global Hint Mode A ! : typeclass_instances. (*Mode also added in elpi*) |
| 239 |
|
| 240 |
Axiom f : Type -> Type. |
| 241 |
Axiom g : Type -> Type. |
| 242 |
|
| 243 |
Class B (i :Type). |
| 244 |
|
| 245 |
Instance i: forall X (Y: Type), A (X Y) -> B (X Y) := {}. |
| 246 |
Instance a : A (g nat) := {}. |
| 247 |
Instance b x : A (f x) -> A (f x) | 0 := {}. |
| 248 |
|
| 249 |
Goal B (g nat). |
| 250 |
apply _. |
| 251 |
Abort. |
| 252 |
|
| 253 |
End force_input_link_HO_var2. |
| 254 |
|
| 255 |
Module force_input_link_HO_var3. |
| 256 |
|
| 257 |
TC.Pending_mode !. |
| 258 |
Class A (i: Type). |
| 259 |
Global Hint Mode A ! : typeclass_instances. (*Mode also added in elpi*) |
| 260 |
|
| 261 |
Axiom g : Type -> Type. |
| 262 |
|
| 263 |
Class B (i :Type). |
| 264 |
|
| 265 |
(* TODO: This instance should not be compilable ? |
| 266 |
The premise has always a flex term while its mode is rigid head *) |
| 267 |
Instance i: forall X (Y: Type), (A X) -> B nat := {}. |
| 268 |
Instance b x : A x -> A x := {}. |
| 269 |
|
| 270 |
Goal B nat. |
| 271 |
(* TODO: this should not loop, note that we have no way to stop it in elpi |
| 272 |
since the current modes on arguments do not filter out instance b |
| 273 |
*) |
| 274 |
Fail Timeout 1 apply _. |
| 275 |
Abort. |
| 276 |
|
| 277 |
End force_input_link_HO_var3. |