Debian Codegraph @

apps/derive/elpi/lens.elpi

raw
1 /* A lens, to see better */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 namespace derive.lens {
6
7 :index (1 2)
8 func build-lens record-decl, list (option constant), int, term -> list (pair id constant), list term.
9 build-lens end-record [] _ _ [] [].
10 build-lens (field _ _ _ Fields) [none|Projs] N RTy IDL BOL :-
11 if (pi x\ occurs x (Fields x))
12 (stop "derive.lens: dependent records not supported")
13 true,
14 M is N + 1,
15 Dummy = sort prop,
16 build-lens (Fields Dummy) Projs M RTy IDL BOL.
17
18 build-lens (field _ ID Ty Fields) [some P|Projs] N RTy [pr ID P|IDL] [Lens|BOL] :-
19 if (pi x\ occurs x (Fields x))
20 (stop "derive.lens: dependent records not supported")
21 true,
22
23 View = {{ fun (r : lp:RTy) => lp:{{ % Coq term
24 {coq.build-match {{r}} RTy (constant-rty Ty) (build-view N)} % Elpi "function" to build a match, see coq-lib.elpi
25 }} }},
26
27 % we let Coq infer the type of f at type checking time
28 Over = {{ fun f (r : lp:RTy) => lp:{{
29 {coq.build-match {{r}} RTy (constant-rty RTy) (build-over N {{f}})}
30 }} }},
31
32 Lens = {{ lib:@elpi.derive.lens.make _ _ _ _ lp:View lp:Over }},
33
34 M is N + 1,
35 Dummy = sort prop,
36 build-lens (Fields Dummy) Projs M RTy IDL BOL.
37
38 % builds the return type of the match
39 func constant-rty term, term, list term, list term -> term.
40 constant-rty X _ _ _ X.
41
42 % builds the match branch for view, L is the list of arguments to
43 % the record constructor
44 func build-view int, term, term, list term, list term -> term.
45 build-view N _ _ L _ R :- std.nth N L R.
46
47 % builds the match branch for over, K is the record constructor (already
48 % applied) to the record parameters
49 func build-over int, term, term, term, list term, list term -> term.
50 build-over N F K _ L _ R :-
51 std.split-at N L Before [X|After],
52 coq.mk-app K {std.append Before [{{ lp:F lp:X }}|After]} R.
53
54 % moves under the paramters of the inductive type type and binds them back
55 % as a lambd abstraction over all the lenses that were built
56 :index (1)
57 func params indt-decl, list (option constant), term -> list (pair id constant), list term.
58 params (parameter ID _ Ty Decl) Projs RTy IL TL :-
59 coq.id->name ID Name,
60 (@pi-decl Name Ty x\ params (Decl x) Projs {coq.mk-app RTy [x]} IL (BL x)),
61 distribute-abstraction Name Ty BL TL.
62 params (record _ _ _ RD) Projs RTy IL TL :- build-lens RD Projs 0 RTy IL TL.
63 params (inductive ID _ _ _) _ _ _ _ :-
64 M is "derive.lens: " ^ ID ^ " is not a record",
65 stop M.
66
67 % distributes a lambda abstraction to all items in the list
68 func distribute-abstraction name, term, (term -> list term) -> list term.
69 distribute-abstraction _ _ (_\[]) [] :- !.
70 distribute-abstraction Name Ty (x\[T x|L x]) [fun Name Ty T|L1] :-
71 distribute-abstraction Name Ty L L1.
72
73 % typechecks and declares the lens
74 func declare-lens string, inductive, pair id constant, term -> prop.
75 declare-lens Prefix I (pr FieldName Proj) RawBody (lens-db I Proj C):-
76 Name is Prefix ^ FieldName,
77 coq.env.indt I _ Nparams _ _ _ _,
78 % In order to support primitive records we call the elaborator, which
79 % eventually compiles the match into primitive projections
80 std.assert-ok! (coq.elaborate-skeleton RawBody Ty Body) "derive.lens generates illtyped term",
81 ((pi P P1 N\ copy (primitive (proj P N)) (primitive (proj P1 N)) :- coq.primitive.projection-unfolded P1 P, !) => copy Body Body1),
82 coq.env.add-const Name Body1 Ty @transparent! C,
83 std.map {std.iota Nparams} (_\r\ r = maximal) Implicits,
84 if (Nparams > 0)
85 (@global! => coq.arguments.set-implicit (const C) [Implicits, []])
86 true.
87
88 func main inductive, string -> list prop.
89 main I Prefix Clauses :- std.do! [
90 coq.env.indt-decl I Decl, % easy to recurse on
91 coq.env.projections I Projs,
92 params Decl Projs (global (indt I)) Names_Projs Defs,
93 std.map2 Names_Projs Defs (declare-lens Prefix I) Clauses,
94 std.forall Clauses (c\coq.elpi.accumulate _ "derive.lens.db" (clause _ _ c)),
95 ].
96
97 }