namespace rbuild { func unresolved_record->list term -> list term. unresolved_record->list {{ Stop }} []. unresolved_record->list {{ More _ lp:X lp:XS }} [X|Rest] :- unresolved_record->list XS Rest. func find constant, list term -> term, list term. find C [ {{ Label _ _ lp:P lp:T }} |TS] T TS :- coq.safe-dest-app P (global (const C)) _, !. find C [X|XS] T [X|YS] :- find C XS T YS. func check-no-label list term. check-no-label []. check-no-label [ {{ Label _ _ lp:P _ }} | _] :- !, coq.error "Unknown label" P. check-no-label [_|XS] :- check-no-label XS. func pad-with-holes int, list A -> list A. pad-with-holes N L L1 :- std.length L M, Diff is N - M, if2 (Diff = 0) (L = L1) (Diff > 0, not(L = []), var {std.last L}) (std.append L {coq.mk-n-holes Diff} L1) (if (Diff < 0) (coq.error "too many fields") (coq.error "not enough fields, maybe use « ... ; _ »")). % we the projection names to build a list of arguments of the right lenght % and we place in there the named ones, the others are placed in their order % in the remaining slots (carries by ACC) func reorder list (option constant), list term, list term -> list term. reorder [some P|PL] L ACC [X|XS] :- find P L X L', !, reorder PL L' ACC XS. reorder [_|PL] L ACC [X|XS] :- reorder PL L [X|ACC] XS. reorder [] L ACC [] :- check-no-label L, {std.rev ACC} = {pad-with-holes {std.length ACC} L}. func find-constructor term -> term, list (option constant). find-constructor Ty KParams PL :- not(var Ty), !, coq.safe-dest-app Ty Hd _, coq.env.global (indt I) Hd, coq.env.indt I _ N _ _ [K] _, coq.mk-n-holes N Params, coq.mk-app {coq.env.global (indc K)} Params KParams, coq.env.projections I PL. func find-inductive list term -> term. find-inductive [ {{ Label lp:S _ _ _ }} | _] S :- !. find-inductive [_|XS] S :- find-inductive XS S. func focus term, list constant -> term. focus {{ ssrfun.catcomp lp:G lp:F }} Acc R :- !, coq.safe-dest-app G P _, coq.env.global (const PC) P, coq.env.projection? PC _, focus F [PC|Acc] R. focus G Acc R :- coq.safe-dest-app G P _, coq.env.global (const PC) P, coq.env.projection? PC _, compose-lens [PC|Acc] R. func compose-lens list constant -> term. compose-lens [] _ :- coq.error "not a (composed) projection". compose-lens [PC] L :- !, find-lens PC L. compose-lens [PC|Rest] {{ lib:elpi.derive.lens.compose lp:L2 lp:L1 }} :- find-lens PC L1, compose-lens Rest L2. func find-lens constant -> term. find-lens PC TArgs :- std.assert! (lens-db _ PC C) "no Lens, did you derive the record?", coq.env.global (const C) T, coq.env.typeof (const C) Ty, coq.saturate Ty T TArgs. }