Debian Codegraph @

apps/derive/elpi/derive.elpi

raw
1 /* Entry point for all derivations */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 namespace derive {
6
7 func exists-indc inductive, (func constructor ->).
8 exists-indc I P :-
9 coq.env.indt I _ _ _ _ KL _,
10 std.exists! KL P.
11
12 func if-verbose (func) ->.
13 if-verbose P :- (get-option "verbose" tt ; get-option "recursive" tt), !, P.
14 if-verbose _.
15
16 pred dep o:string, o:string.
17 dep X Y :- dep1 X Y.
18 dep X Y :- dep1 X Z, dep Z Y.
19
20 func selected string ->.
21 selected Name :- get-option "only" Map, !,
22 Map => (get-option Name _; (get-option X _, dep X Name)), !.
23 selected _.
24
25 func validate-only gref, list derive ->.
26 validate-only T LD :- get-option "only" Map, !, std.forall Map (known-option T LD).
27 validate-only _ _.
28
29 func known-option gref, list derive, prop ->.
30 known-option T L (get-option X _) :-
31 if (std.mem! L (derive X _ _)) true
32 (coq.error "Derivation" X "unknown or not applicable to input" T).
33
34 :index (_ 1)
35 func chain gref, list derive -> list prop.
36 chain _ [] [].
37 chain T [derive Name _ _|FS] CL :- not(selected Name), !,
38 if-verbose (coq.say "Skipping derivation" Name "on" T "since the user did not select it"),
39 chain T FS CL.
40 chain T [derive Name _ AlreadyDone|FS] CL :- ((pi x\ stop x :- !, fail) ==> AlreadyDone), !,
41 if-verbose (coq.say "Skipping derivation" Name "on" T "since it has been already run"),
42 chain T FS CL.
43 chain T [derive Name F _|FS] CL :- get-option "only" _, !, % request this one
44 if-verbose (coq.say "Derivation" Name "on" T),
45 (@dropunivs! ==> std.time (F C) Time),
46 if-verbose (coq.say "Derivation" Name "on" T "took" Time),
47 (C ==> chain T FS CS),
48 std.append C CS CL.
49 chain T [derive Name F _|FS] CL :- % all are selected, we can fail
50 if-verbose (coq.say "Derivation" Name "on" T),
51 ((pi x\ stop x :- !, fail) ==> @dropunivs! ==> std.time (F C) Time), !,
52 if-verbose (coq.say "Derivation" Name "on" T "took" Time),
53 (C ==> chain T FS CS),
54 std.append C CS CL.
55 chain T [derive F _ _|FS] CL :-
56 if-verbose (coq.say "Derivation" F "on" T "failed, continuing"),
57 chain T FS CL.
58
59 func toposort list derive -> list derive.
60 toposort L SL :-
61 std.findall (dep1 _ _) Deps,
62 topo L Deps SL.
63
64 func not-a-src list prop, derive ->.
65 not-a-src Deps (derive A _ _) :- not(std.mem! Deps (dep1 A _)).
66
67 :index (1)
68 func tgt-is-not-in list derive, prop ->.
69 tgt-is-not-in [] _.
70 tgt-is-not-in [derive Tgt _ _|_] (dep1 _ Tgt) :- !, fail.
71 tgt-is-not-in [_|L] D :- tgt-is-not-in L D.
72
73 func topo list derive, list prop -> list derive.
74 topo [] _ [] :- !.
75 topo L Deps SL :-
76 std.partition L (not-a-src Deps) LNoDeps Other,
77 if (LNoDeps = []) (coq.error "derive: no topological order:" L Deps) true,
78 std.filter Deps (tgt-is-not-in LNoDeps) NewDeps,
79 topo Other NewDeps SOther,
80 std.append LNoDeps SOther SL.
81
82 func export? prop -> prop.
83 export? (export M) (coq.env.export-module M).
84
85 func indt-or-const gref ->.
86 indt-or-const (indt _).
87 indt-or-const (const _).
88
89 func main gref -> list prop.
90 main GR CL :- get-option "module" M, !,
91 if (M = "") (coq.gref->id GR Mod) (Mod = M),
92 if-verbose (coq.say "Starting module" Mod),
93 coq.env.begin-module Mod none,
94 main-derive GR tt CL,
95 coq.env.end-module _.
96 main GR CL :-
97 main-derive GR ff CL.
98
99 func main-derive gref, bool -> list prop.
100 main-derive GR InModule CL :- get-option "recursive" tt, !,
101 coq.env.dependencies GR _ AllDeps,
102 coq.gref.set.elements AllDeps AllDepsL,
103 std.filter AllDepsL indt-or-const Deps,
104 main.aux InModule Deps [] CL1,
105 (CL1 ==> main1 GR InModule CL2),
106 std.append CL1 CL2 CL.
107 main-derive GR InModule CL :- main1 GR InModule CL.
108
109 :index (_ 1)
110 func main.aux bool, list gref, list prop -> list prop.
111 main.aux _ [] X X.
112 main.aux InModule [GR|GRS] Acc CL :-
113 ((pi X\get-option "only" X :- !, fail) ==> Acc ==> main-derive GR InModule CL1),
114 main.aux InModule GRS {std.append CL1 Acc} CL.
115
116 func validate-recursive prop -> derive.
117 validate-recursive (derivation _ _ tt _) _ :- get-option "recursive" tt, !,
118 coq.error "Synterp actions not supported in recursive derive.".
119 validate-recursive (derivation _ _ _ R) R.
120 func main1 gref, bool -> list prop.
121 main1 GR InModule CL :-
122 if (get-option "prefix" PFX)
123 (Prefix = PFX)
124 (if (InModule is ff) (Prefix is {coq.gref->id GR} ^ "_") (Prefix = "")),
125 std.findall (derivation GR Prefix _ _) L,
126 if (L = [])
127 (coq.error "no derivation found, did you Import derive.std?")
128 true,
129 std.map L validate-recursive DL,
130 validate-only GR DL,
131 toposort DL SortedDL,
132 chain GR SortedDL CL.
133
134 func decl+main string, indt-decl ->.
135 decl+main TypeName DS :- std.do! [
136 if (get-option "module" M)
137 (if (M = "") (ModName = TypeName) (ModName = M), HasModule = tt)
138 (HasModule = ff),
139 if (HasModule = tt)
140 (if-verbose (coq.say "Starting module" ModName),
141 coq.env.begin-module ModName none)
142 true,
143 std.assert-ok! (coq.elaborate-indt-decl-skeleton DS D) "Inductive type declaration illtyped",
144 if-verbose (coq.say "Declaring inductive" D),
145 coq.env.add-indt D I,
146 if-verbose (coq.say "Deriving"),
147 main-derive (indt I) HasModule CL,
148 if-verbose (coq.say "Done"),
149 if (HasModule = tt)
150 (coq.env.end-module _,
151 decl+main.post TypeName I DS CL)
152 check-no-no-alias
153 ].
154
155 func check-no-no-alias.
156 check-no-no-alias :- get-option "no_alias" tt, !,
157 coq.error "The no_alias attribute only has an effect when a wrapper module is generated.".
158 check-no-no-alias.
159
160 func decl+main.post string, inductive, indt-decl -> list prop.
161 decl+main.post TypeName I DS CL :- std.do! [
162 coq.env.indt I _ _ _ _ KS _,
163 std.map KS (k\r\ r = indc k) KGRS,
164 std.map KGRS coq.gref->id KNS,
165 std.map KGRS (gr\r\ r = global gr) KTS,
166
167 std.forall2 [TypeName|KNS] [global (indt I)|KTS] short-alias,
168
169 coq.indt-decl->implicits DS IndImpls KsImpls,
170 if (coq.any-implicit? IndImpls)
171 (@global! ==> coq.arguments.set-implicit (indt I) [IndImpls])
172 true,
173 std.forall2 KsImpls KS (i\k\
174 if (coq.any-implicit? i)
175 (@global! ==> coq.arguments.set-implicit (indc k) [i])
176 true
177 ),
178 std.map-filter CL export? P,
179 std.do! P,
180 ].
181
182 func short-alias id, term ->.
183 short-alias _ _ :- get-option "no_alias" tt, !.
184 short-alias ID T :- @global! ==> coq.notation.add-abbreviation ID 0 T ff _.
185
186 }