File: test_API_env.v

package info (click to toggle)
coq-elpi 2.5.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,176 kB
  • sloc: ml: 13,016; python: 331; makefile: 102; sh: 34
file content (381 lines) | stat: -rw-r--r-- 10,462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
From elpi Require Import elpi.
From elpi_stdlib Require Vector.

(****** env **********************************)
Elpi Command test.

(* constant *)

Elpi Query lp:{{
  coq.locate "plus" (const GR),
  coq.env.const GR (some BO) TY,
  coq.locate "nat" GRNat, Nat = global GRNat,
  coq.locate "S" GRSucc, Succ = global GRSucc,
  TY = (prod _ Nat _\ prod _ Nat _\ Nat),
  BO = (fix _ 0 TY add\
         fun _ Nat n\ fun _ Nat m\
         match n (fun _ Nat _\ Nat)
         [ m
         , fun _ Nat w\ app[Succ, app[add,w,m]]]).
}}.

Axiom empty_nat : nat.

Elpi Query lp:{{
  coq.locate "empty_nat" (const GR),
  coq.env.const GR none TY.
}}.

Section Test.

Variable A : nat.

Elpi Query lp:{{
  coq.locate "Vector.nil" GR1,
  coq.locate "nat"        GR2,
  coq.locate "A"          GR3,
  coq.env.typeof GR1 _,
  coq.env.typeof GR2 _,
  coq.env.typeof GR3 _.
}}.

End Test.

Elpi Query lp:{{
  coq.locate "plus" (const GR),
  coq.env.const GR (some BO) TY,
  coq.gref->id (const GR) S,
  Name is S ^ "_equal",
  coq.env.add-const Name BO TY @opaque! NGR,
  coq.env.opaque? NGR,
  coq.env.const NGR none _, coq.say {coq.gref->id (const NGR)},
  coq.env.const-body NGR (some BO),
  rex_match "add_equal" {coq.gref->id (const NGR)}.
}}.

About add_equal.

(* axiom *)

Elpi Query lp:{{
  coq.locate "False" F,
  coq.env.add-axiom "myfalse" (global F) GR,
  coq.env.opaque? GR,
  coq.env.const GR none _,
  coq.env.const-body GR none,
  coq.say GR.
}}.

Check myfalse.

(* record *)

Set Printing Universes.
Elpi Query lp:{{
  DECL = 
    (parameter "T" _ {{Type}} t\
       record "eq_class" {{Type}} "mk_eq_class" (
            field [canonical ff, coercion regular]     "eq_f"     {{bool}} f\
            field _ "eq_proof" {{lp:f = lp:f :> bool}} _\
       end-record)),
 coq.say DECL,
 coq.env.add-indt DECL GR.
}}.

Print eq_class.
Check (fun x : eq_class nat => (x : bool)).
Axiom b : bool.
Axiom p : b = b.
Canonical xxx := mk_eq_class bool b p.
Print Canonical Projections.
Fail Check eq_refl _ : eq_f bool _ = b.

Elpi Query lp:{{
  DECL = 
    (parameter "T" _ {{Type}} t\
       record "prim_eq_class" {{Type}} "mk_prim_eq_class" (
            field [canonical ff, coercion reversible]     "prim_eq_f"     {{bool}} f\
            field _ "prim_eq_proof" {{lp:f = lp:f :> bool}} _\
       end-record)),
 (@primitive! => coq.env.add-indt DECL GR),
 coq.env.projections GR [some _, some _].
}}.

(* primitive records have eta *)
Check fun r : prim_eq_class nat =>
  eq_refl _ : r = mk_prim_eq_class _ (prim_eq_f _ r) (prim_eq_proof _ r).

Module II.
Arguments prim_eq_f : default implicits.
Elpi Query lp:{{
  coq.say {{ fun r : prim_eq_class nat => r.(prim_eq_f) }}
}}.

Definition pc (r : prim_eq_class nat) := r.(prim_eq_f).

Elpi Query lp:{{
  coq.locate "pc" (const C),
  coq.env.const C (some (fun _ _ r\ app[primitive _, r])) _
}}.

Elpi Command primp.
Elpi Accumulate lp:{{
  main [const-decl _ (some (fun _ _ r\ app[primitive _, r])) _].
}}.
Elpi primp Definition pc (r : prim_eq_class nat) := r.(prim_eq_f).

End II.

(* inductive *)

Elpi Command indtest.
Elpi Accumulate lp:{{
main _ :-
  DECL =
      (parameter "T" maximal {{Type}} t\
         parameter "x" _ t x\
           inductive "myind" _ (arity (prod `w` t _\ sort prop))
             i\ [ constructor "K1"
                   (arity (prod `y` t y\ prod _ (app[i,y]) _\app[i,x]))
                , constructor "K2"
                    (arity (app[i,x]))
                ]
            ),
 coq.env.add-indt DECL _,
 coq.rename-indt-decl rename rename rename DECL DECL1,
 coq.env.add-indt DECL1 _.

pred rename i:id, o:id.
rename K S :- S is K ^ "1".
}}.
Elpi Query indtest lp:{{ main _ }}.

Check myind true false : Prop.
Check K2 true : myind true true.
Check myind1 true false : Prop.
Check K21 true : myind1 true true.

Elpi Query lp:{{
  coq.env.add-indt (parameter "X" _ {{Type}} x\
                      inductive "nuind" _ (parameter "n" _ {{ nat }} _\ arity {{ bool -> Type }}) i\
                       [constructor "k1" (parameter "n" _ {{nat}} n\ arity (app[i,n,{{true}}]))
                       ,constructor "k2" (parameter "n" _ {{nat}} n\
                                             arity (prod `x` (app[i,{{1}},{{false}}]) _\
                                              (app[i,n,{{false}}])))
                       ]) _.
}}.


Check fun x : nuind nat 3 false =>
       match x in nuind _ _ b return @eq bool b b with
       | k1 _ _ => (eq_refl : true = true)
       | k2 _ _ x => (fun w : nuind nat 1 false => (eq_refl : false = false)) x
       end.

Fail Check fun x : nuind nat 3 false =>
       match x in nuind _ i_cannot_name_this b return @eq bool b b with
       | k1 _ _ => (eq_refl : true = true)
       | k2 _ _ x => (fun w : nuind nat 1 false => (eq_refl : false = false)) x
       end.

Elpi Query lp:{{
  pi x\ (decl x `x` {{ nat }} => coq.typecheck x T ok), coq.say x T.
}}.


Elpi Query lp:{{
  D = (parameter "A" _ {{ Type }} a\
     inductive "tx" _ (parameter "y" _ {{nat}} _\ arity {{ bool -> Type }}) t\
       [ constructor "K1x" (parameter "y" _ {{nat}} y\ arity {{
           forall (x : lp:a) (n : nat) (p : @eq nat (S n) lp:y) (e : lp:t n true),
           lp:t lp:y true }})
       , constructor "K2x" (parameter "y" _ {{nat}} y\ arity {{
           lp:t lp:y false }}) ]),
  coq.typecheck-indt-decl D ok,
  coq.env.add-indt D _.
}}.

Module HOAS.

Inductive ind1 (A : Type) (a : A) | (B : Type) (b : B) : forall C : Type, C -> Type :=
  | k1 : forall bb, ind1 (B * B)%type bb bool true -> ind1 B b unit tt
  | k2 : ind1 B b nat 1.

Elpi Query lp:{{

  coq.locate "ind1" (indt I),
  coq.env.indt-decl I D,
  D1 =
    (parameter "A" explicit (sort (typ UA)) c0 \
     parameter "a" explicit c0 c1 \
       inductive "ind1" tt 
          (parameter "B" explicit (sort (typ UB1)) c2 \
           parameter "b" explicit c2 c3 \
           arity
             (prod `C` (sort (typ UC)) c4 \ prod `_` c4 c5 \ sort (typ U)))
       c2 \
   [constructor "k1"
     (parameter "B" explicit (sort (typ UB2)) c3 \
       parameter "b" explicit c3 c4 \
        arity
         (prod `bb` {{ (lp:c3 * lp:c3)%type }} c5 \
           prod `_` (app [c2, {{ (lp:c3 * lp:c3)%type }}, c5, {{ bool }}, {{ true }}]) c6 \
            app [c2, c3, c4, {{ unit }},  {{ tt }}])), 
    constructor "k2"
     (parameter "B" explicit (sort (typ UB3)) c3 \
       parameter "b" explicit c3 c4 \
        arity
         (app [c2, c3, c4, {{ nat }}, {{ 1 }}]))]),
  std.assert! (D = D1) "coq.env.indt-decl".

}}.

Arguments k1 A a B b [bb] _.

Elpi Query lp:{{

  coq.locate "ind1" (indt I),
  coq.env.indt-decl I D,
  D1 =
    (parameter "A" explicit (sort (typ UA)) c0 \
     parameter "a" explicit c0 c1 \
       inductive "ind1" tt 
          (parameter "B" explicit (sort (typ UB1)) c2 \
           parameter "b" explicit c2 c3 \
           arity
             (prod `C` (sort (typ UC)) c4 \ prod `_` c4 c5 \ sort (typ U)))
       c2 \
   [constructor "k1"
     (parameter "B" explicit (sort (typ UB2)) c3 \
       parameter "b" explicit c3 c4 \
       parameter "bb" implicit {{ (lp:c3 * lp:c3)%type }} c5 \
        arity
          (prod `_` (app [c2, {{ (lp:c3 * lp:c3)%type }}, c5, {{ bool }}, {{ true }}]) c6 \
            app [c2, c3, c4, {{ unit }},  {{ tt }}])), 
    constructor "k2"
     (parameter "B" explicit (sort (typ UB3)) c3 \
       parameter "b" explicit c3 c4 \
        arity
         (app [c2, c3, c4, {{ nat }}, {{ 1 }}]))]),
   std.assert! (D = D1) "coq.env.indt-decl + implicits".
}}.

Record r1 (P : Type) (p : P) : Type := mk_r1 {
  f1 :> P -> P;
  #[canonical=no] f2 : p = f1 p;
}.

Elpi Query lp:{{

  coq.locate "r1" (indt I),
  coq.env.indt-decl I D,
  D1 =
    (parameter "P" explicit (sort (typ UP)) c0 \
     parameter "p" explicit c0 c1 \
       record "r1" (sort (typ UR)) "mk_r1" 
         (field [coercion reversible, canonical tt] "f1" {{ lp:c0 -> lp:c0 }} c2\
          field [coercion off, canonical ff] "f2" {{ @eq lp:c0 lp:c1 (lp:c2 lp:c1) }} c3\
          end-record)
    ),
  std.assert! (D = D1) "coq.env.indt-decl + record".

}}.

#[warning="-uniform-inheritance"] Coercion f2 : r1 >-> eq.

Elpi Query lp:{{

  coq.locate "r1" (indt I),
  coq.env.indt-decl I D,
  D1 =
    (parameter "P" explicit (sort (typ UP)) c0 \
     parameter "p" explicit c0 c1 \
       record "r1" (sort (typ UR)) "mk_r1" 
         (field [coercion reversible, canonical tt] "f1" {{ lp:c0 -> lp:c0 }} c2\
          field [coercion regular, canonical ff] "f2" {{ @eq lp:c0 lp:c1 (lp:c2 lp:c1) }} c3\
          end-record)
    ),
  std.assert! (D = D1) "coq.env.indt-decl + record".

}}.

Elpi Query lp:{{

  coq.locate "plus" GR,
  coq.env.dependencies GR _ L,
  coq.say L,
  coq.env.transitive-dependencies GR _ S,
  coq.say S,
  std.assert! (S = L) "plus wrong deps"

}}.

Module X.
  Definition a := 0.
  Definition b := a + a.
End X.

Elpi Query lp:{{

  coq.locate "X.b" GR,
  coq.locate-module "X" M,
  coq.env.dependencies GR M L,
  coq.env.dependencies GR _ AllL,
  coq.say L AllL,
  std.assert! (coq.gref.set.subset L AllL) "??",
  std.assert! (coq.gref.set.elements L [{coq.locate "X.a"}]) "??",
  coq.env.transitive-dependencies GR M S,
  coq.env.transitive-dependencies GR _ AllS,
  coq.say S AllS,
  std.assert! (coq.gref.set.subset S AllS) "??"
  
}}.

End HOAS.

From elpi_stdlib Require Ranalysis5.

Elpi Query lp:{{

  coq.locate "Ranalysis5.derivable_pt_lim_CVU" GR,
  std.time (coq.env.transitive-dependencies GR _ S) T,
  std.assert! ({coq.gref.set.cardinal S} > 3000) "too few",
  std.assert! (T < 25.0) "too slow" % 0.5 here

}}.

Elpi Query lp:{{

  T = {{ forall x : nat, x + 0 = x }},
  coq.env.term-dependencies T S,
  std.assert! ({coq.gref.set.cardinal S} = 4) "wrong",
  not (coq.gref.set.mem {{:gref S }}       S),
      (coq.gref.set.mem {{:gref O }}       S),
      (coq.gref.set.mem {{:gref nat }}     S),
      (coq.gref.set.mem {{:gref Nat.add }} S),
      (coq.gref.set.mem {{:gref eq }}      S)

}}.

Set Universe Polymorphism.

Elpi Query lp:{{
  coq.env.begin-module "Test" none,
  coq.env.end-module _.
}} lp:{{
  coq.env.begin-module "Test" _,
  Decl = record "Rec" {{ Type }} "BuildRec" (field [] "f" {{ Type }} (_\ end-record)),
  (@univpoly! => coq.env.add-indt Decl _),
  coq.env.end-module _.
}}.

Set Printing Universes. Print Module Test.
Check Test.f.

From elpi_stdlib Require Import ZArith.

Elpi Query lp:{{
  coq.locate-module "N2Z" MP,
  coq.locate-module "Znat" LP,
  coq.modpath->library MP LP
}}.