| 1 |
From elpi Require Import elpi. |
| 2 |
|
| 3 |
From elpi.tests Require test_API. |
| 4 |
|
| 5 |
Elpi Command declare.testX. |
| 6 |
Elpi Accumulate Db global.db. |
| 7 |
Elpi Accumulate lp:{{ |
| 8 |
|
| 9 |
main [str "mem", str I] :- |
| 10 |
std.assert! (declared I) "clause not present". |
| 11 |
main [str "length", int I] :- |
| 12 |
std.findall (declared J_) L, |
| 13 |
std.assert! (std.length L I) "wrong number of clauses". |
| 14 |
|
| 15 |
}}. |
| 16 |
|
| 17 |
|
| 18 |
Fail Elpi declare.testX "mem" "GLOBAL". |
| 19 |
Elpi declare.testX "length" 0. |
| 20 |
|
| 21 |
Import test_API. (* no dup *) |
| 22 |
|
| 23 |
Elpi declare.test "mem" "GLOBAL". |
| 24 |
Elpi declare.test "mem" "BOX". |
| 25 |
Elpi declare.test "length" 2. |
| 26 |
|
| 27 |
Fail Elpi declare.test "mem" "BOX.ClausesC". |
| 28 |
Import Box.ClausesC. |
| 29 |
Elpi declare.test "mem" "BOX.ClausesC". |
| 30 |
Elpi declare.test "length" 3. |
| 31 |
|
| 32 |
Elpi declare "library" "EXTRA". |
| 33 |
|
| 34 |
(** strategy *) |
| 35 |
Definition x1 := 3. |
| 36 |
Definition x2 := 3. |
| 37 |
Strategy opaque [x2]. |
| 38 |
Definition x3 := 3. |
| 39 |
Strategy expand [x3]. |
| 40 |
|
| 41 |
Elpi Command strategy. |
| 42 |
Elpi Query lp:{{ |
| 43 |
coq.locate "x1" (const X1), |
| 44 |
coq.locate "x2" (const X2), |
| 45 |
coq.locate "x3" (const X3), |
| 46 |
coq.strategy.get X1 (level 0), |
| 47 |
coq.strategy.get X2 opaque, |
| 48 |
coq.strategy.get X3 expand, |
| 49 |
coq.strategy.set [X3] (level 123), |
| 50 |
coq.strategy.get X3 (level 123). |
| 51 |
|
| 52 |
}}. |
| 53 |
|
| 54 |
Axiom P : nat -> Prop. |
| 55 |
|
| 56 |
Elpi Command mode. |
| 57 |
Elpi Query lp:{{ |
| 58 |
coq.hints.add-mode {{:gref P }} "core" [mode-input], |
| 59 |
coq.hints.add-mode {{:gref P }} "core" [mode-ground], |
| 60 |
coq.hints.modes {{:gref P }} "core" M, |
| 61 |
std.assert! (M = [[mode-ground],[mode-input]]) "wrong modes" |
| 62 |
}}. |
| 63 |
|
| 64 |
Elpi Command pv. |
| 65 |
Elpi Accumulate lp:{{ |
| 66 |
|
| 67 |
main [trm (primitive (uint63 P))] :- !, coq.say {coq.uint63->int P}. |
| 68 |
main [trm (primitive (float64 P))] :- !, coq.say {coq.float64->float P}. |
| 69 |
main X :- coq.error "not a primitive-value" X. |
| 70 |
|
| 71 |
}}. |
| 72 |
|
| 73 |
|
| 74 |
Elpi Command vp. |
| 75 |
Elpi Accumulate lp:{{ |
| 76 |
|
| 77 |
main [int I] :- !, coq.say {coq.int->uint63 I}. |
| 78 |
main [] :- !, coq.say {coq.float->float64 1.2}. |
| 79 |
|
| 80 |
}}. |
| 81 |
|
| 82 |
|
| 83 |
From Corelib Require Import PrimFloat PrimInt63. |
| 84 |
|
| 85 |
Open Scope uint63_scope. |
| 86 |
|
| 87 |
Elpi pv (1). |
| 88 |
Fail Elpi pv (4611686018427387904). (* max_int + 1 *) |
| 89 |
Elpi vp 1. |
| 90 |
Fail Elpi vp -1. |
| 91 |
|
| 92 |
Open Scope float_scope. |
| 93 |
|
| 94 |
Elpi pv (1.0). |
| 95 |
Elpi vp. |
| 96 |
|
| 97 |
Elpi Query lp:{{ |
| 98 |
|
| 99 |
coq.option.available? ["Primitive", "Projections"] ff |
| 100 |
|
| 101 |
}}. |
| 102 |
|
| 103 |
Elpi Query lp:{{ |
| 104 |
|
| 105 |
coq.option.set ["Primitive", "Projections"] (coq.option.bool tt), |
| 106 |
coq.option.get ["Primitive", "Projections"] (coq.option.bool tt), |
| 107 |
coq.option.set ["Primitive", "Projections"] (coq.option.bool ff), |
| 108 |
coq.option.get ["Primitive", "Projections"] (coq.option.bool ff) |
| 109 |
|
| 110 |
}}. |
| 111 |
|
| 112 |
Elpi Query lp:{{ |
| 113 |
|
| 114 |
coq.option.add ["Primitive", "Projections"] (coq.option.bool tt) _, |
| 115 |
coq.option.get ["Primitive", "Projections"] (coq.option.bool tt) |
| 116 |
|
| 117 |
}}. |
| 118 |
|
| 119 |
Elpi Query lp:{{ |
| 120 |
|
| 121 |
% done in test_API.v |
| 122 |
% coq.option.add ["Foo", "Bar"] (coq.option.string (some "x")) tt, |
| 123 |
coq.option.get ["Foo", "Bar"] (coq.option.string (some "x")) |
| 124 |
|
| 125 |
}}. |
| 126 |
|
| 127 |
Set Foo Bar "y". |
| 128 |
|
| 129 |
Elpi Query lp:{{ |
| 130 |
|
| 131 |
coq.option.get ["Foo", "Bar"] (coq.option.string (some "y")) |
| 132 |
|
| 133 |
}}. |
| 134 |
|
| 135 |
Unset Foo Bar. |
| 136 |
|
| 137 |
Elpi Query lp:{{ |
| 138 |
|
| 139 |
coq.option.get ["Foo", "Bar"] (coq.option.string none) |
| 140 |
|
| 141 |
}}. |
| 142 |
|
| 143 |
(* Hints transparent *) |
| 144 |
|
| 145 |
#[local] Hint Opaque plus : core. |
| 146 |
Definition times := plus. |
| 147 |
#[local] Hint Transparent times : core. |
| 148 |
|
| 149 |
Elpi Query lp:{{ |
| 150 |
|
| 151 |
{{ plus }} = global (const C1), |
| 152 |
coq.hints.opaque C1 "core" X1, |
| 153 |
std.assert!(X1 = @opaque!) "wrong opaque plus", |
| 154 |
{{ times }} = global (const C2), |
| 155 |
coq.hints.opaque C2 "core" X2, |
| 156 |
std.assert!(X2 = @transparent!) "wrong opaque times" |
| 157 |
|
| 158 |
}}. |
| 159 |
|
| 160 |
Definition x := 3. |
| 161 |
|
| 162 |
Elpi Query lp:{{ |
| 163 |
std.do! [ |
| 164 |
{{ x }} = global (const C1), |
| 165 |
coq.hints.opaque C1 "core" @opaque!, |
| 166 |
coq.hints.set-opaque C1 "core" @transparent!, |
| 167 |
coq.hints.opaque C1 "core" @transparent!, |
| 168 |
] |
| 169 |
|
| 170 |
}}. |
| 171 |
|
| 172 |
#[local] Hint Opaque x : core. |
| 173 |
|
| 174 |
Elpi Query lp:{{ |
| 175 |
std.do! [coq.env.begin-module "xx" none, coq.env.end-module XX, coq.env.import-module XX ] |
| 176 |
}} lp:{{ |
| 177 |
std.do! [ |
| 178 |
{{ x }} = global (const C1), |
| 179 |
coq.hints.opaque C1 "core" @opaque!, |
| 180 |
coq.env.begin-module "xx" _, |
| 181 |
(@local! ==> coq.hints.set-opaque C1 "core" @transparent!), |
| 182 |
coq.env.end-module M, |
| 183 |
coq.hints.opaque C1 "core" @opaque!, |
| 184 |
coq.env.import-module M, |
| 185 |
coq.hints.opaque C1 "core" @opaque!, |
| 186 |
] |
| 187 |
|
| 188 |
}}. |
| 189 |
|
| 190 |
Elpi Query lp:{{ |
| 191 |
std.do! [coq.env.begin-module "xx2" none, coq.env.end-module XX, coq.env.import-module XX] |
| 192 |
}} lp:{{ |
| 193 |
std.do! [ |
| 194 |
{{ x }} = global (const C1), |
| 195 |
coq.hints.opaque C1 "core" @opaque!, |
| 196 |
coq.env.begin-module "xx2" _, |
| 197 |
coq.hints.set-opaque C1 "core" @transparent!, |
| 198 |
coq.env.end-module M, |
| 199 |
coq.hints.opaque C1 "core" @opaque!, |
| 200 |
coq.env.import-module M, |
| 201 |
coq.hints.opaque C1 "core" @transparent!, |
| 202 |
] |
| 203 |
|
| 204 |
}}. |
| 205 |
|
| 206 |
#[local] Hint Opaque x : core. |
| 207 |
|
| 208 |
Elpi Query lp:{{ |
| 209 |
std.do! [coq.env.begin-module "xx3" none, coq.env.end-module _] |
| 210 |
}} lp:{{ |
| 211 |
std.do! [ |
| 212 |
{{ x }} = global (const C1), |
| 213 |
coq.hints.opaque C1 "core" @opaque!, |
| 214 |
coq.env.begin-module "xx3" _, |
| 215 |
(@global! => coq.hints.set-opaque C1 "core" @transparent!), |
| 216 |
coq.env.end-module M, |
| 217 |
coq.hints.opaque C1 "core" @transparent!, |
| 218 |
] |
| 219 |
|
| 220 |
}}. |
| 221 |
Fail Elpi Query lp:{{ |
| 222 |
|
| 223 |
{{ x }} = global (const C1), |
| 224 |
coq.hints.opaque C1 "corexx" T |
| 225 |
|
| 226 |
}}. |
| 227 |
|
| 228 |
|
| 229 |
(* Hints transparent *) |
| 230 |
|
| 231 |
Definition xeq T x y := x = y :> T. |
| 232 |
Axiom xxx : xeq _ 0 1. |
| 233 |
|
| 234 |
Elpi Query lp:{{ |
| 235 |
coq.hints.add-resolve {{:gref xxx }} "core" 0 _. |
| 236 |
}}. |
| 237 |
|
| 238 |
Goal 0 = 1. Fail solve [trivial]. Abort. |
| 239 |
|
| 240 |
Create HintDb xxx. |
| 241 |
|
| 242 |
Elpi Query lp:{{ |
| 243 |
coq.hints.add-resolve {{:gref xxx }} "xxx" 0 {{ 0 = _ }} |
| 244 |
}}. |
| 245 |
|
| 246 |
Print HintDb xxx. |
| 247 |
(* I could not test the pattern, but it is printed |
| 248 |
Goal 0 = 1. solve [debug eauto with xxx]. Abort. |
| 249 |
*) |
| 250 |
|
| 251 |
(* ------------- functor application ---------------- *) |
| 252 |
|
| 253 |
(*Module Type T. Axiom Inline(1) T : Type. End T.*) |
| 254 |
Elpi Query lp:{{ |
| 255 |
std.do! [ coq.env.begin-module-type "T", coq.env.end-module-type _, ] |
| 256 |
}} lp:{{ |
| 257 |
coq.env.begin-module-type "T", |
| 258 |
@inline-at! 1 ==> coq.env.add-axiom "T" {{ Type }} _, |
| 259 |
coq.env.end-module-type _ |
| 260 |
|
| 261 |
}}. |
| 262 |
|
| 263 |
(*Module F(P : T). Definition id (a : P.T) := a. End F.*) |
| 264 |
Elpi Query lp:{{ |
| 265 |
std.do! [coq.env.begin-module-functor "F" none [pr "P" {coq.locate-module-type "T"}], coq.env.end-module _] |
| 266 |
}} lp:{{ |
| 267 |
coq.env.begin-module-functor "F" _ _, |
| 268 |
coq.locate "P.T" GR, |
| 269 |
coq.env.add-const "id" (fun `a` (global GR) a\ a) _ _ _, |
| 270 |
coq.env.end-module _. |
| 271 |
|
| 272 |
}}. |
| 273 |
|
| 274 |
(*Module X. Definition T := nat. End X.*) |
| 275 |
Elpi Query lp:{{ |
| 276 |
std.do! [coq.env.begin-module "X" none, coq.env.end-module _] |
| 277 |
}} lp:{{ |
| 278 |
|
| 279 |
coq.env.begin-module "X" _, |
| 280 |
coq.env.add-const "T" {{ nat }} _ _ _, |
| 281 |
coq.env.end-module _. |
| 282 |
|
| 283 |
}}. |
| 284 |
|
| 285 |
(*Module G := F X [no inline].*) |
| 286 |
Elpi Query lp:{{ |
| 287 |
std.do! [coq.env.apply-module-functor "G" none {coq.locate-module "F"} [{coq.locate-module "X"}] coq.inline.no _] |
| 288 |
}} lp:{{ |
| 289 |
coq.env.apply-module-functor "G" _ _ _ _ G, |
| 290 |
coq.say G |
| 291 |
}}. |
| 292 |
Print Module G. |
| 293 |
|
| 294 |
(*Module H := F X [inline at leve 2].*) |
| 295 |
Elpi Query lp:{{ |
| 296 |
std.do! [coq.env.apply-module-functor "H" none {coq.locate-module "F"} [{coq.locate-module "X"}] (coq.inline.at 2) _] |
| 297 |
}} lp:{{ |
| 298 |
coq.env.apply-module-functor "H" _ _ _ _ H, |
| 299 |
coq.say H |
| 300 |
}}. |
| 301 |
Print Module H. |
| 302 |
|
| 303 |
Goal G.id 3%nat = 3%nat. |
| 304 |
Fail match goal with |- @Logic.eq nat _ _ => idtac end. |
| 305 |
match goal with |- @Logic.eq X.T _ _ => idtac end. |
| 306 |
Abort. |
| 307 |
|
| 308 |
Goal H.id 3%nat = 3%nat. |
| 309 |
match goal with |- @Logic.eq nat _ _ => idtac end. |
| 310 |
Fail match goal with |- @Logic.eq X.T _ _ => idtac end. |
| 311 |
Abort. |
| 312 |
|
| 313 |
(* ------------- functor type application ---------------- *) |
| 314 |
|
| 315 |
(*Module Type FT (P : T). Axiom idT : P.T -> P.T. End FT.*) |
| 316 |
Elpi Query lp:{{ |
| 317 |
std.do! [coq.env.begin-module-type-functor "FT" [pr "P" {coq.locate-module-type "T"}], coq.env.end-module-type _] |
| 318 |
}} lp:{{ |
| 319 |
|
| 320 |
coq.env.begin-module-type-functor "FT" _, |
| 321 |
coq.locate "P.T" GR, |
| 322 |
coq.env.add-axiom "idT" (prod _ (global GR) _\ (global GR)) _, |
| 323 |
coq.env.end-module-type _. |
| 324 |
|
| 325 |
}}. |
| 326 |
Print Module Type FT. |
| 327 |
|
| 328 |
(*Module Type GT := FT X [no inline].*) |
| 329 |
Elpi Query lp:{{ |
| 330 |
std.do! [coq.env.apply-module-type-functor "GT" {coq.locate-module-type "FT"} [{coq.locate-module "X"}] coq.inline.no _] |
| 331 |
}} lp:{{ |
| 332 |
coq.env.apply-module-type-functor "GT" _ _ _ G, |
| 333 |
coq.say G |
| 334 |
}}. |
| 335 |
Print Module Type GT. |
| 336 |
|
| 337 |
(*Module Type HT := FT X [inline at leve 2].*) |
| 338 |
Elpi Query lp:{{ |
| 339 |
std.do! [coq.env.apply-module-type-functor "HT" {coq.locate-module-type "FT"} [{coq.locate-module "X"}] (coq.inline.at 2) _] |
| 340 |
}} lp:{{ |
| 341 |
coq.env.apply-module-type-functor "HT" _ _ _ H, |
| 342 |
coq.say H |
| 343 |
}}. |
| 344 |
Print Module Type HT. |
| 345 |
|
| 346 |
Module Test (X : GT) (Y : HT). |
| 347 |
|
| 348 |
Goal X.idT 3%nat = 3%nat. |
| 349 |
Fail match goal with |- @Logic.eq nat _ _ => idtac end. |
| 350 |
match goal with |- @Logic.eq X.T _ _ => idtac end. |
| 351 |
Abort. |
| 352 |
|
| 353 |
Goal Y.idT 3%nat = 3%nat. |
| 354 |
match goal with |- @Logic.eq nat _ _ => idtac end. |
| 355 |
Fail match goal with |- @Logic.eq X.T _ _ => idtac end. |
| 356 |
Abort. |
| 357 |
|
| 358 |
End Test. |
| 359 |
|
| 360 |
Elpi Query lp:{{ |
| 361 |
|
| 362 |
coq.univ.variable.of-term (prod `_` (sort (typ U)) _\ sort (typ V)) S, |
| 363 |
coq.univ.variable U UV, |
| 364 |
coq.univ.variable V VV, |
| 365 |
coq.univ.variable.set.elements S L, |
| 366 |
( L = [UV,VV] ; L = [VV,UV] ) |
| 367 |
|
| 368 |
}}. |
| 369 |
(**********************************************) |
| 370 |
|
| 371 |
Elpi Command test.eta. |
| 372 |
Elpi Accumulate lp:{{ |
| 373 |
test T1 T2 :- |
| 374 |
coq.reduction.eta-contract T1 X, |
| 375 |
Msg is {coq.term->string T1} ^ "\n--eta-->\n" ^ {coq.term->string X} ^ "\n!=\n" ^ {coq.term->string T2} ^ "\n\n", |
| 376 |
std.assert! (X = T2) Msg. |
| 377 |
}}. |
| 378 |
Elpi Query lp:{{ |
| 379 |
std.do! [ |
| 380 |
test {{ fun x => x }} {{ fun x => x }}, |
| 381 |
test {{ fun x => S x }} {{ S }}, |
| 382 |
test {{ fun x y => plus x y }} {{ plus }}, |
| 383 |
test {{ fun x y => x x y }} {{ fun x y => x x y }}, |
| 384 |
test {{ fun z => S (fun x y => plus x y) z }} {{ S plus }}, |
| 385 |
test {{ fun z => S (fun x y => z x y) }} {{ S }}, |
| 386 |
test {{ fun z => S (fun x y => z z x y) }} {{ fun z => S (z z) }}, |
| 387 |
]. |
| 388 |
}}. |
| 389 |
|
| 390 |
|
| 391 |
Elpi Db foo.db lp:{{ |
| 392 |
pred p o:int. |
| 393 |
p 1. |
| 394 |
}}. |
| 395 |
Elpi Accumulate foo.db lp:{{ |
| 396 |
p 2. |
| 397 |
}}. |
| 398 |
Elpi Command query_foo. |
| 399 |
Elpi Accumulate Db Header foo.db. |
| 400 |
Elpi Accumulate lp:{{ |
| 401 |
main _ :- |
| 402 |
std.findall (p _) [p 1, p 2]. |
| 403 |
}}. |
| 404 |
Elpi Accumulate Db foo.db. |
| 405 |
|
| 406 |
Elpi query_foo. |
| 407 |
|
| 408 |
|
| 409 |
Elpi Command acc_foo. |
| 410 |
Elpi Accumulate Db Header foo.db. |
| 411 |
Elpi Accumulate lp:{{ |
| 412 |
pred r i:list int. |
| 413 |
main _ :- |
| 414 |
coq.elpi.accumulate _ "foo.db" (clause _ _ (r [])). |
| 415 |
}}. |
| 416 |
Elpi Accumulate Db foo.db. |
| 417 |
|
| 418 |
Fail Elpi acc_foo. (* since r has no type in foo.db *) |
| 419 |
|
| 420 |
|
| 421 |
Elpi File r.elpi lp:{{ pred r i:list int. }}. |
| 422 |
|
| 423 |
Elpi Accumulate foo.db File r.elpi. |
| 424 |
|
| 425 |
Elpi Command acc_foo2. |
| 426 |
Elpi Accumulate Db Header foo.db. |
| 427 |
Elpi Accumulate File r.elpi. |
| 428 |
Elpi Accumulate lp:{{ |
| 429 |
main _ :- |
| 430 |
coq.elpi.accumulate _ "foo.db" (clause _ _ (r [])). |
| 431 |
}}. |
| 432 |
Elpi Accumulate Db foo.db. |
| 433 |
|
| 434 |
Elpi acc_foo2. (* since r has no type in foo.db *) |
| 435 |
|
| 436 |
(* test goals that are TC are flagged as interactive *) |
| 437 |
|
| 438 |
Class Foo (A : Type) := { a : A }. |
| 439 |
|
| 440 |
Elpi Tactic spawn_tc_goal. |
| 441 |
Elpi Accumulate lp:{{ |
| 442 |
solve (goal _ _ Ty _ _ as G) GL :- |
| 443 |
T = {{ proj1 (conj _ (@a True _)) }}, |
| 444 |
(std.assert-ok! (coq.elaborate-skeleton T Ty T') "??"), |
| 445 |
refine T' G GL. |
| 446 |
|
| 447 |
}}. |
| 448 |
|
| 449 |
Goal 3+2=5. |
| 450 |
elpi spawn_tc_goal. |
| 451 |
Show. |
| 452 |
(* 2: shelve. Unshelve. *) |
| 453 |
easy. |
| 454 |
constructor. easy. |
| 455 |
Qed. |