| 1 |
From elpi Require Import elpi. |
| 2 |
From Stdlib Require Vector. |
| 3 |
|
| 4 |
(****** env **********************************) |
| 5 |
Elpi Command test. |
| 6 |
|
| 7 |
(* constant *) |
| 8 |
|
| 9 |
Elpi Query lp:{{ |
| 10 |
coq.locate "plus" (const GR), |
| 11 |
coq.env.const GR (some BO) TY, |
| 12 |
coq.locate "nat" GRNat, Nat = global GRNat, |
| 13 |
coq.locate "S" GRSucc, Succ = global GRSucc, |
| 14 |
TY = (prod _ Nat _\ prod _ Nat _\ Nat), |
| 15 |
BO = (fix _ 0 TY add\ |
| 16 |
fun _ Nat n\ fun _ Nat m\ |
| 17 |
match n (fun _ Nat _\ Nat) |
| 18 |
[ m |
| 19 |
, fun _ Nat w\ app[Succ, app[add,w,m]]]). |
| 20 |
}}. |
| 21 |
|
| 22 |
Axiom empty_nat : nat. |
| 23 |
|
| 24 |
Elpi Query lp:{{ |
| 25 |
coq.locate "empty_nat" (const GR), |
| 26 |
coq.env.const GR none TY. |
| 27 |
}}. |
| 28 |
|
| 29 |
Section Test. |
| 30 |
|
| 31 |
Variable A : nat. |
| 32 |
|
| 33 |
Elpi Query lp:{{ |
| 34 |
coq.locate "Vector.nil" GR1, |
| 35 |
coq.locate "nat" GR2, |
| 36 |
coq.locate "A" GR3, |
| 37 |
coq.env.typeof GR1 _, |
| 38 |
coq.env.typeof GR2 _, |
| 39 |
coq.env.typeof GR3 _. |
| 40 |
}}. |
| 41 |
|
| 42 |
End Test. |
| 43 |
|
| 44 |
Elpi Query lp:{{ |
| 45 |
coq.locate "plus" (const GR), |
| 46 |
coq.env.const GR (some BO) TY, |
| 47 |
coq.gref->id (const GR) S, |
| 48 |
Name is S ^ "_equal", |
| 49 |
coq.env.add-const Name BO TY @opaque! NGR, |
| 50 |
coq.env.opaque? NGR, |
| 51 |
coq.env.const NGR none _, coq.say {coq.gref->id (const NGR)}, |
| 52 |
coq.env.const-body NGR (some BO), |
| 53 |
rex_match "add_equal" {coq.gref->id (const NGR)}. |
| 54 |
}}. |
| 55 |
|
| 56 |
About add_equal. |
| 57 |
|
| 58 |
(* axiom *) |
| 59 |
|
| 60 |
Elpi Query lp:{{ |
| 61 |
coq.locate "False" F, |
| 62 |
coq.env.add-axiom "myfalse" (global F) GR, |
| 63 |
coq.env.opaque? GR, |
| 64 |
coq.env.const GR none _, |
| 65 |
coq.env.const-body GR none, |
| 66 |
coq.say GR. |
| 67 |
}}. |
| 68 |
|
| 69 |
Check myfalse. |
| 70 |
|
| 71 |
(* record *) |
| 72 |
|
| 73 |
Set Printing Universes. |
| 74 |
Elpi Query lp:{{ |
| 75 |
DECL = |
| 76 |
(parameter "T" _ {{Type}} t\ |
| 77 |
record "eq_class" {{Type}} "mk_eq_class" ( |
| 78 |
field [canonical ff, coercion regular] "eq_f" {{bool}} f\ |
| 79 |
field _ "eq_proof" {{lp:f = lp:f :> bool}} _\ |
| 80 |
end-record)), |
| 81 |
coq.say DECL, |
| 82 |
coq.env.add-indt DECL GR. |
| 83 |
}}. |
| 84 |
|
| 85 |
Print eq_class. |
| 86 |
Check (fun x : eq_class nat => (x : bool)). |
| 87 |
Axiom b : bool. |
| 88 |
Axiom p : b = b. |
| 89 |
Canonical xxx := mk_eq_class bool b p. |
| 90 |
Print Canonical Projections. |
| 91 |
Fail Check eq_refl _ : eq_f bool _ = b. |
| 92 |
|
| 93 |
Elpi Query lp:{{ |
| 94 |
DECL = |
| 95 |
(parameter "T" _ {{Type}} t\ |
| 96 |
record "prim_eq_class" {{Type}} "mk_prim_eq_class" ( |
| 97 |
field [canonical ff, coercion reversible] "prim_eq_f" {{bool}} f\ |
| 98 |
field _ "prim_eq_proof" {{lp:f = lp:f :> bool}} _\ |
| 99 |
end-record)), |
| 100 |
(@primitive! => coq.env.add-indt DECL GR), |
| 101 |
coq.env.projections GR [some _, some _]. |
| 102 |
}}. |
| 103 |
|
| 104 |
(* primitive records have eta *) |
| 105 |
Check fun r : prim_eq_class nat => |
| 106 |
eq_refl _ : r = mk_prim_eq_class _ (prim_eq_f _ r) (prim_eq_proof _ r). |
| 107 |
|
| 108 |
Module II. |
| 109 |
Arguments prim_eq_f : default implicits. |
| 110 |
Elpi Query lp:{{ |
| 111 |
coq.say {{ fun r : prim_eq_class nat => r.(prim_eq_f) }} |
| 112 |
}}. |
| 113 |
|
| 114 |
Definition pc (r : prim_eq_class nat) := r.(prim_eq_f). |
| 115 |
|
| 116 |
Elpi Query lp:{{ |
| 117 |
coq.locate "pc" (const C), |
| 118 |
coq.env.const C (some (fun _ _ r\ app[primitive _, r])) _ |
| 119 |
}}. |
| 120 |
|
| 121 |
Elpi Command primp. |
| 122 |
Elpi Accumulate lp:{{ |
| 123 |
main [const-decl _ (some (fun _ _ r\ app[primitive _, r])) _]. |
| 124 |
}}. |
| 125 |
Elpi primp Definition pc (r : prim_eq_class nat) := r.(prim_eq_f). |
| 126 |
|
| 127 |
End II. |
| 128 |
|
| 129 |
(* inductive *) |
| 130 |
|
| 131 |
Elpi Command indtest. |
| 132 |
Elpi Accumulate lp:{{ |
| 133 |
main _ :- |
| 134 |
DECL = |
| 135 |
(parameter "T" maximal {{Type}} t\ |
| 136 |
parameter "x" _ t x\ |
| 137 |
inductive "myind" _ (arity (prod `w` t _\ sort prop)) |
| 138 |
i\ [ constructor "K1" |
| 139 |
(arity (prod `y` t y\ prod _ (app[i,y]) _\app[i,x])) |
| 140 |
, constructor "K2" |
| 141 |
(arity (app[i,x])) |
| 142 |
] |
| 143 |
), |
| 144 |
coq.env.add-indt DECL _, |
| 145 |
coq.rename-indt-decl rename rename rename DECL DECL1, |
| 146 |
coq.env.add-indt DECL1 _. |
| 147 |
|
| 148 |
pred rename i:id, o:id. |
| 149 |
rename K S :- S is K ^ "1". |
| 150 |
}}. |
| 151 |
Elpi Query indtest lp:{{ main _ }}. |
| 152 |
|
| 153 |
Check myind true false : Prop. |
| 154 |
Check K2 true : myind true true. |
| 155 |
Check myind1 true false : Prop. |
| 156 |
Check K21 true : myind1 true true. |
| 157 |
|
| 158 |
Elpi Query lp:{{ |
| 159 |
coq.env.add-indt (parameter "X" _ {{Type}} x\ |
| 160 |
inductive "nuind" _ (parameter "n" _ {{ nat }} _\ arity {{ bool -> Type }}) i\ |
| 161 |
[constructor "k1" (parameter "n" _ {{nat}} n\ arity (app[i,n,{{true}}])) |
| 162 |
,constructor "k2" (parameter "n" _ {{nat}} n\ |
| 163 |
arity (prod `x` (app[i,{{1}},{{false}}]) _\ |
| 164 |
(app[i,n,{{false}}]))) |
| 165 |
]) _. |
| 166 |
}}. |
| 167 |
|
| 168 |
|
| 169 |
Check fun x : nuind nat 3 false => |
| 170 |
match x in nuind _ _ b return @eq bool b b with |
| 171 |
| k1 _ _ => (eq_refl : true = true) |
| 172 |
| k2 _ _ x => (fun w : nuind nat 1 false => (eq_refl : false = false)) x |
| 173 |
end. |
| 174 |
|
| 175 |
Fail Check fun x : nuind nat 3 false => |
| 176 |
match x in nuind _ i_cannot_name_this b return @eq bool b b with |
| 177 |
| k1 _ _ => (eq_refl : true = true) |
| 178 |
| k2 _ _ x => (fun w : nuind nat 1 false => (eq_refl : false = false)) x |
| 179 |
end. |
| 180 |
|
| 181 |
Elpi Query lp:{{ |
| 182 |
pi x\ (decl x `x` {{ nat }} => coq.typecheck x T ok), coq.say x T. |
| 183 |
}}. |
| 184 |
|
| 185 |
|
| 186 |
Elpi Query lp:{{ |
| 187 |
D = (parameter "A" _ {{ Type }} a\ |
| 188 |
inductive "tx" _ (parameter "y" _ {{nat}} _\ arity {{ bool -> Type }}) t\ |
| 189 |
[ constructor "K1x" (parameter "y" _ {{nat}} y\ arity {{ |
| 190 |
forall (x : lp:a) (n : nat) (p : @eq nat (S n) lp:y) (e : lp:t n true), |
| 191 |
lp:t lp:y true }}) |
| 192 |
, constructor "K2x" (parameter "y" _ {{nat}} y\ arity {{ |
| 193 |
lp:t lp:y false }}) ]), |
| 194 |
std.assert-ok! (coq.typecheck-indt-decl D) "illtyped", |
| 195 |
coq.env.add-indt D _. |
| 196 |
}}. |
| 197 |
|
| 198 |
Module HOAS. |
| 199 |
|
| 200 |
Inductive ind1 (A : Type) (a : A) | (B : Type) (b : B) : forall C : Type, C -> Type := |
| 201 |
| k1 : forall bb, ind1 (B * B)%type bb bool true -> ind1 B b unit tt |
| 202 |
| k2 : ind1 B b nat 1. |
| 203 |
|
| 204 |
Elpi Query lp:{{ |
| 205 |
|
| 206 |
coq.locate "ind1" (indt I), |
| 207 |
coq.env.indt-decl I D, |
| 208 |
D1 = |
| 209 |
(parameter "A" explicit (sort (typ UA)) c0 \ |
| 210 |
parameter "a" explicit c0 c1 \ |
| 211 |
inductive "ind1" tt |
| 212 |
(parameter "B" explicit (sort (typ UB1)) c2 \ |
| 213 |
parameter "b" explicit c2 c3 \ |
| 214 |
arity |
| 215 |
(prod `C` (sort (typ UC)) c4 \ prod `_` c4 c5 \ sort (typ U))) |
| 216 |
c2 \ |
| 217 |
[constructor "k1" |
| 218 |
(parameter "B" explicit (sort (typ UB2)) c3 \ |
| 219 |
parameter "b" explicit c3 c4 \ |
| 220 |
arity |
| 221 |
(prod `bb` {{ (lp:c3 * lp:c3)%type }} c5 \ |
| 222 |
prod `_` (app [c2, {{ (lp:c3 * lp:c3)%type }}, c5, {{ bool }}, {{ true }}]) c6 \ |
| 223 |
app [c2, c3, c4, {{ unit }}, {{ tt }}])), |
| 224 |
constructor "k2" |
| 225 |
(parameter "B" explicit (sort (typ UB3)) c3 \ |
| 226 |
parameter "b" explicit c3 c4 \ |
| 227 |
arity |
| 228 |
(app [c2, c3, c4, {{ nat }}, {{ 1 }}]))]), |
| 229 |
std.assert! (D = D1) "coq.env.indt-decl". |
| 230 |
|
| 231 |
}}. |
| 232 |
|
| 233 |
Arguments k1 A a B b [bb] _. |
| 234 |
|
| 235 |
Elpi Query lp:{{ |
| 236 |
|
| 237 |
coq.locate "ind1" (indt I), |
| 238 |
coq.env.indt-decl I D, |
| 239 |
D1 = |
| 240 |
(parameter "A" explicit (sort (typ UA)) c0 \ |
| 241 |
parameter "a" explicit c0 c1 \ |
| 242 |
inductive "ind1" tt |
| 243 |
(parameter "B" explicit (sort (typ UB1)) c2 \ |
| 244 |
parameter "b" explicit c2 c3 \ |
| 245 |
arity |
| 246 |
(prod `C` (sort (typ UC)) c4 \ prod `_` c4 c5 \ sort (typ U))) |
| 247 |
c2 \ |
| 248 |
[constructor "k1" |
| 249 |
(parameter "B" explicit (sort (typ UB2)) c3 \ |
| 250 |
parameter "b" explicit c3 c4 \ |
| 251 |
parameter "bb" implicit {{ (lp:c3 * lp:c3)%type }} c5 \ |
| 252 |
arity |
| 253 |
(prod `_` (app [c2, {{ (lp:c3 * lp:c3)%type }}, c5, {{ bool }}, {{ true }}]) c6 \ |
| 254 |
app [c2, c3, c4, {{ unit }}, {{ tt }}])), |
| 255 |
constructor "k2" |
| 256 |
(parameter "B" explicit (sort (typ UB3)) c3 \ |
| 257 |
parameter "b" explicit c3 c4 \ |
| 258 |
arity |
| 259 |
(app [c2, c3, c4, {{ nat }}, {{ 1 }}]))]), |
| 260 |
std.assert! (D = D1) "coq.env.indt-decl + implicits". |
| 261 |
}}. |
| 262 |
|
| 263 |
Record r1 (P : Type) (p : P) : Type := mk_r1 { |
| 264 |
f1 :> P -> P; |
| 265 |
#[canonical=no] f2 : p = f1 p; |
| 266 |
}. |
| 267 |
|
| 268 |
Elpi Query lp:{{ |
| 269 |
|
| 270 |
coq.locate "r1" (indt I), |
| 271 |
coq.env.indt-decl I D, |
| 272 |
D1 = |
| 273 |
(parameter "P" explicit (sort (typ UP)) c0 \ |
| 274 |
parameter "p" explicit c0 c1 \ |
| 275 |
record "r1" (sort (typ UR)) "mk_r1" |
| 276 |
(field [coercion reversible, canonical tt] "f1" {{ lp:c0 -> lp:c0 }} c2\ |
| 277 |
field [coercion off, canonical ff] "f2" {{ @eq lp:c0 lp:c1 (lp:c2 lp:c1) }} c3\ |
| 278 |
end-record) |
| 279 |
), |
| 280 |
std.assert! (D = D1) "coq.env.indt-decl + record". |
| 281 |
|
| 282 |
}}. |
| 283 |
|
| 284 |
#[warning="-uniform-inheritance"] Coercion f2 : r1 >-> eq. |
| 285 |
|
| 286 |
Elpi Query lp:{{ |
| 287 |
|
| 288 |
coq.locate "r1" (indt I), |
| 289 |
coq.env.indt-decl I D, |
| 290 |
D1 = |
| 291 |
(parameter "P" explicit (sort (typ UP)) c0 \ |
| 292 |
parameter "p" explicit c0 c1 \ |
| 293 |
record "r1" (sort (typ UR)) "mk_r1" |
| 294 |
(field [coercion reversible, canonical tt] "f1" {{ lp:c0 -> lp:c0 }} c2\ |
| 295 |
field [coercion regular, canonical ff] "f2" {{ @eq lp:c0 lp:c1 (lp:c2 lp:c1) }} c3\ |
| 296 |
end-record) |
| 297 |
), |
| 298 |
std.assert! (D = D1) "coq.env.indt-decl + record". |
| 299 |
|
| 300 |
}}. |
| 301 |
|
| 302 |
Elpi Query lp:{{ |
| 303 |
|
| 304 |
coq.locate "plus" GR, |
| 305 |
coq.env.dependencies GR _ L, |
| 306 |
coq.say L, |
| 307 |
coq.env.transitive-dependencies GR _ S, |
| 308 |
coq.say S, |
| 309 |
std.assert! (S = L) "plus wrong deps" |
| 310 |
|
| 311 |
}}. |
| 312 |
|
| 313 |
Module X. |
| 314 |
Definition a := 0. |
| 315 |
Definition b := a + a. |
| 316 |
End X. |
| 317 |
|
| 318 |
Elpi Query lp:{{ |
| 319 |
|
| 320 |
coq.locate "X.b" GR, |
| 321 |
coq.locate-module "X" M, |
| 322 |
coq.env.dependencies GR M L, |
| 323 |
coq.env.dependencies GR _ AllL, |
| 324 |
coq.say L AllL, |
| 325 |
std.assert! (coq.gref.set.subset L AllL) "??", |
| 326 |
std.assert! (coq.gref.set.elements L [{coq.locate "X.a"}]) "??", |
| 327 |
coq.env.transitive-dependencies GR M S, |
| 328 |
coq.env.transitive-dependencies GR _ AllS, |
| 329 |
coq.say S AllS, |
| 330 |
std.assert! (coq.gref.set.subset S AllS) "??" |
| 331 |
|
| 332 |
}}. |
| 333 |
|
| 334 |
End HOAS. |
| 335 |
|
| 336 |
From Stdlib Require Ranalysis5. |
| 337 |
|
| 338 |
Elpi Query lp:{{ |
| 339 |
|
| 340 |
coq.locate "Ranalysis5.derivable_pt_lim_CVU" GR, |
| 341 |
std.time (coq.env.transitive-dependencies GR _ S) T, |
| 342 |
std.assert! ({coq.gref.set.cardinal S} > 3000) "too few", |
| 343 |
std.assert! (T < 20.0) "too slow" % 0.5 here |
| 344 |
|
| 345 |
}}. |
| 346 |
|
| 347 |
Elpi Query lp:{{ |
| 348 |
|
| 349 |
T = {{ forall x : nat, x + 0 = x }}, |
| 350 |
coq.env.term-dependencies T S, |
| 351 |
std.assert! ({coq.gref.set.cardinal S} = 4) "wrong", |
| 352 |
not (coq.gref.set.mem {{:gref S }} S), |
| 353 |
(coq.gref.set.mem {{:gref O }} S), |
| 354 |
(coq.gref.set.mem {{:gref nat }} S), |
| 355 |
(coq.gref.set.mem {{:gref Nat.add }} S), |
| 356 |
(coq.gref.set.mem {{:gref eq }} S) |
| 357 |
|
| 358 |
}}. |
| 359 |
|
| 360 |
Set Universe Polymorphism. |
| 361 |
|
| 362 |
Elpi Query lp:{{ |
| 363 |
coq.env.begin-module "Test" none, |
| 364 |
coq.env.end-module _. |
| 365 |
}} lp:{{ |
| 366 |
coq.env.begin-module "Test" _, |
| 367 |
Decl = record "Rec" {{ Type }} "BuildRec" (field [] "f" {{ Type }} (_\ end-record)), |
| 368 |
(@univpoly! => coq.env.add-indt Decl _), |
| 369 |
coq.env.end-module _. |
| 370 |
}}. |
| 371 |
|
| 372 |
Set Printing Universes. Print Module Test. |
| 373 |
Check Test.f. |
| 374 |
|
| 375 |
From Stdlib Require Import ZArith. |
| 376 |
|
| 377 |
Elpi Query lp:{{ |
| 378 |
coq.locate-module "N2Z" MP, |
| 379 |
coq.locate-module "Znat" LP, |
| 380 |
coq.modpath->library MP LP |
| 381 |
}}. |