| 1 |
namespace rbuild { |
| 2 |
|
| 3 |
func unresolved_record->list term -> list term. |
| 4 |
unresolved_record->list {{ Stop }} []. |
| 5 |
unresolved_record->list {{ More _ lp:X lp:XS }} [X|Rest] :- unresolved_record->list XS Rest. |
| 6 |
|
| 7 |
func find constant, list term -> term, list term. |
| 8 |
find C [ {{ Label _ _ lp:P lp:T }} |TS] T TS :- |
| 9 |
coq.safe-dest-app P (global (const C)) _, !. |
| 10 |
find C [X|XS] T [X|YS] :- find C XS T YS. |
| 11 |
|
| 12 |
func check-no-label list term. |
| 13 |
check-no-label []. |
| 14 |
check-no-label [ {{ Label _ _ lp:P _ }} | _] :- !, coq.error "Unknown label" P. |
| 15 |
check-no-label [_|XS] :- check-no-label XS. |
| 16 |
|
| 17 |
func pad-with-holes int, list A -> list A. |
| 18 |
pad-with-holes N L L1 :- |
| 19 |
std.length L M, |
| 20 |
Diff is N - M, |
| 21 |
if2 (Diff = 0) (L = L1) |
| 22 |
(Diff > 0, not(L = []), var {std.last L}) (std.append L {coq.mk-n-holes Diff} L1) |
| 23 |
(if (Diff < 0) (coq.error "too many fields") (coq.error "not enough fields, maybe use « ... ; _ »")). |
| 24 |
|
| 25 |
% we the projection names to build a list of arguments of the right lenght |
| 26 |
% and we place in there the named ones, the others are placed in their order |
| 27 |
% in the remaining slots (carries by ACC) |
| 28 |
func reorder list (option constant), list term, list term -> list term. |
| 29 |
reorder [some P|PL] L ACC [X|XS] :- find P L X L', !, reorder PL L' ACC XS. |
| 30 |
reorder [_|PL] L ACC [X|XS] :- reorder PL L [X|ACC] XS. |
| 31 |
reorder [] L ACC [] :- check-no-label L, |
| 32 |
{std.rev ACC} = {pad-with-holes {std.length ACC} L}. |
| 33 |
|
| 34 |
func find-constructor term -> term, list (option constant). |
| 35 |
find-constructor Ty KParams PL :- not(var Ty), !, |
| 36 |
coq.safe-dest-app Ty Hd _, |
| 37 |
coq.env.global (indt I) Hd, |
| 38 |
coq.env.indt I _ N _ _ [K] _, |
| 39 |
coq.mk-n-holes N Params, |
| 40 |
coq.mk-app {coq.env.global (indc K)} Params KParams, |
| 41 |
coq.env.projections I PL. |
| 42 |
|
| 43 |
func find-inductive list term -> term. |
| 44 |
find-inductive [ {{ Label lp:S _ _ _ }} | _] S :- !. |
| 45 |
find-inductive [_|XS] S :- find-inductive XS S. |
| 46 |
|
| 47 |
func focus term, list constant -> term. |
| 48 |
focus {{ ssrfun.catcomp lp:G lp:F }} Acc R :- !, |
| 49 |
coq.safe-dest-app G P _, |
| 50 |
coq.env.global (const PC) P, |
| 51 |
coq.env.projection? PC _, |
| 52 |
focus F [PC|Acc] R. |
| 53 |
focus G Acc R :- |
| 54 |
coq.safe-dest-app G P _, |
| 55 |
coq.env.global (const PC) P, |
| 56 |
coq.env.projection? PC _, |
| 57 |
compose-lens [PC|Acc] R. |
| 58 |
|
| 59 |
func compose-lens list constant -> term. |
| 60 |
compose-lens [] _ :- coq.error "not a (composed) projection". |
| 61 |
compose-lens [PC] L :- !, find-lens PC L. |
| 62 |
compose-lens [PC|Rest] {{ lib:elpi.derive.lens.compose lp:L2 lp:L1 }} :- |
| 63 |
find-lens PC L1, |
| 64 |
compose-lens Rest L2. |
| 65 |
|
| 66 |
func find-lens constant -> term. |
| 67 |
find-lens PC TArgs :- |
| 68 |
std.assert! (lens-db _ PC C) "no Lens, did you derive the record?", |
| 69 |
coq.env.global (const C) T, |
| 70 |
coq.env.typeof (const C) Ty, |
| 71 |
coq.saturate Ty T TArgs. |
| 72 |
|
| 73 |
} |
| 74 |
|