| 1 |
From elpi.apps.rbuild.elpi Extra Dependency "rbuild.elpi" as rbuild. |
| 2 |
|
| 3 |
From elpi Require Import elpi. |
| 4 |
From elpi.apps.derive Require Export lens. |
| 5 |
From elpi.apps Require Export coercion. |
| 6 |
Require ssr.ssrfun. |
| 7 |
|
| 8 |
Inductive unresolved_record := |
| 9 |
| Stop |
| 10 |
| More (T : Type) : T -> unresolved_record -> unresolved_record. |
| 11 |
Inductive named_field (S: Type) := |
| 12 |
| Label (T : Type) : (S -> T) -> T -> named_field. |
| 13 |
|
| 14 |
Declare Scope label_scope. |
| 15 |
Delimit Scope label_scope with lbl. |
| 16 |
Declare Scope labeled_scope. |
| 17 |
Delimit Scope labeled_scope with lbled. |
| 18 |
Notation " a # b" := (ssrfun.catcomp a b) (at level 0, b at level 0) : label_scope. |
| 19 |
|
| 20 |
Notation " l .= v " := (Label _ _ l%lbl v) (at level 51, v at level 98) : labeled_scope. |
| 21 |
Notation "« x ; .. ; y »" := |
| 22 |
(More _ x%lbled .. (More _ y%lbled Stop) .. (* stage 1 : field rearranging / padding *) |
| 23 |
|
| 24 |
:> (* ignition: More .. is not a type *) |
| 25 |
More _ x%lbled .. (More _ y%lbled Stop) ..) (* stage 0 : indtype resolution *) |
| 26 |
(at level 0, x, y at level 100, only parsing). |
| 27 |
|
| 28 |
Notation "« 'unresolved record' »" := (More _ _ _) (at level 0, only printing). |
| 29 |
|
| 30 |
Inductive update := With (T : Type) : T -> named_field T -> update. |
| 31 |
|
| 32 |
Notation "« x 'with' l .= v »" := (With _ x (Label _ _ l%lbl v) : With _ x (Label _ _ l%lbl v)) |
| 33 |
(at level 0, x at level 100, l at level 0, v at level 98, only parsing). |
| 34 |
|
| 35 |
|
| 36 |
Elpi Accumulate coercion Db derive.lens.db. |
| 37 |
|
| 38 |
Elpi Accumulate coercion File rbuild. |
| 39 |
|
| 40 |
|
| 41 |
Elpi Accumulate coercion.db lp:{{ |
| 42 |
|
| 43 |
% workaround: this should work: Elpi Accumulate coercion.db File Signature rbuild. |
| 44 |
func rbuild.find-inductive list term -> term. |
| 45 |
func rbuild.reorder list (option constant), list term, list term -> list term. |
| 46 |
func rbuild.unresolved_record->list term -> list term. |
| 47 |
func rbuild.focus term, list constant -> term. |
| 48 |
func rbuild.find-constructor term -> term, list (option constant). |
| 49 |
|
| 50 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 51 |
% stage 0: we figure out the inductive type and move to stage 1 |
| 52 |
|
| 53 |
% we have labels, we use them to infer the type |
| 54 |
coercion _ L {{ unresolved_record }} (sort _) R :- |
| 55 |
rbuild.find-inductive {rbuild.unresolved_record->list L} R, !. |
| 56 |
|
| 57 |
% no labels, we hope the type is imposed by the context |
| 58 |
coercion _ _ {{ unresolved_record }} (sort _) R :- |
| 59 |
R = {{ unresolved_record }}. |
| 60 |
|
| 61 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 62 |
% stage 1: we find the constructor from the type and we apply it |
| 63 |
coercion _ L {{ unresolved_record }} Ty R :- rbuild.find-constructor Ty K PL, !, |
| 64 |
rbuild.unresolved_record->list L Args, |
| 65 |
rbuild.reorder PL Args [] SortedArgs, |
| 66 |
coq.mk-app K SortedArgs Candidate, |
| 67 |
std.assert-ok! (coq.typecheck Candidate Ty) "Illtyped record", |
| 68 |
R = Candidate. |
| 69 |
% this type checks R again, maybe we can do as in refine to avoid that |
| 70 |
|
| 71 |
% stage 0, we schedule the update for stage 1 |
| 72 |
coercion _ {{ With lp:R _ _}} {{ update }} (sort _) R. |
| 73 |
|
| 74 |
% stage 1, we update X via a lens on L |
| 75 |
coercion _ {{ With lp:R lp:X (Label _ _ lp:L lp:V )}} {{ update }} R O :- std.do! [ |
| 76 |
rbuild.focus L [] Focus, |
| 77 |
Candidate = {{ over lp:Focus (fun _ => lp:V) lp:X }}, |
| 78 |
std.assert-ok! (coq.typecheck Candidate R) "illtyped update", |
| 79 |
O = Candidate, |
| 80 |
]. |
| 81 |
|
| 82 |
}}. |