| 1 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 2 |
/* ------------------------------------------------------------------------- */ |
| 3 |
|
| 4 |
namespace tc { |
| 5 |
shorten tc.{search-mode}. |
| 6 |
|
| 7 |
% The predicate of a class is local if inside a section and readded at section end. |
| 8 |
% This way, section variables are generalized |
| 9 |
func get-class-locality -> list prop. |
| 10 |
get-class-locality [@local!] :- coq.env.current-section-path [_ | _], !. |
| 11 |
get-class-locality [@global!]. |
| 12 |
|
| 13 |
% [add-class-gr SearchMode ClassGR RModes] adds the predicate for the class with its corresponing mode |
| 14 |
% NOTE: the mode inside TC.declare_mode has the priority over Coq mode or #[mode=(...)] TC.Declare |
| 15 |
% RModes is a raw list of string containing rocq modes |
| 16 |
func add-class-gr search-mode, gref, list string ->. |
| 17 |
% the predicate for the class has alread been added |
| 18 |
add-class-gr _ ClassGR _ :- tc.class ClassGR _ _ _, !. |
| 19 |
add-class-gr SearchMode ClassGR SMR :- |
| 20 |
std.assert! (coq.TC.class? ClassGR) "Only gref of type classes can be added as new predicates", |
| 21 |
tc.get-elpi-mode ClassGR SMR EM SM, |
| 22 |
if (std.forall EM (m\ sigma a s\ m = pr a s, a = out)) (true) ( |
| 23 |
std.fold EM "" (m\s\r\ sigma a s'\ m = pr a s', if (a = in) (calc (s ^ " 10") r) (calc (s ^ " _") r)) Indexing), |
| 24 |
tc.gref->pred-name ClassGR PredName, |
| 25 |
get-class-locality Locality, |
| 26 |
Locality => ( |
| 27 |
coq.elpi.add-predicate "tc.db" Indexing PredName EM, |
| 28 |
tc.add-tc-db _ _ (tc.class ClassGR PredName SearchMode SM)). |
| 29 |
|
| 30 |
func add-class-str search-mode, string ->. |
| 31 |
add-class-str SearchMode ClassStr :- |
| 32 |
coq.locate ClassStr ClassGR, |
| 33 |
add-class-gr SearchMode ClassGR []. |
| 34 |
|
| 35 |
func attr->search-mode -> search-mode. |
| 36 |
attr->search-mode tc.deterministic :- get-option "deterministic" tt, !. |
| 37 |
attr->search-mode tc.classic. |
| 38 |
|
| 39 |
func attr->modes -> list hint-mode. |
| 40 |
attr->modes CoqModes :- |
| 41 |
get-option "mode" L, |
| 42 |
std.map L get-key-from-option RawModes, |
| 43 |
std.map RawModes tc.string->coq-mode CoqModes, !. |
| 44 |
attr->modes []. |
| 45 |
|
| 46 |
func get-key-from-option prop -> string. |
| 47 |
get-key-from-option (get-option A tt) A :- !. |
| 48 |
get-key-from-option (get-option "i" ff) "o" :- !. |
| 49 |
get-key-from-option (get-option "o" ff) "i" :- !. |
| 50 |
get-key-from-option A _ :- coq.error A "should be an option". |
| 51 |
|
| 52 |
func declare-class-in-coq gref ->. |
| 53 |
declare-class-in-coq ClassGR :- |
| 54 |
attr->modes CoqModes, |
| 55 |
if (CoqModes = []) |
| 56 |
( |
| 57 |
tc.modes-of-class ClassGR EM, |
| 58 |
std.map EM tc.elpi->string-mode SM |
| 59 |
)( |
| 60 |
coq.hints.add-mode ClassGR "typeclass_instances" CoqModes, |
| 61 |
std.map CoqModes (x\y\tc.string->coq-mode y x) SM', |
| 62 |
std.append SM' ["o"] SM |
| 63 |
), |
| 64 |
% CAVEAT: this triggers the observer |
| 65 |
coq.TC.declare-class ClassGR, |
| 66 |
attr->search-mode SearchMode, |
| 67 |
tc.gref->pred-name ClassGR PredName, |
| 68 |
% HACK: we override the clauses added by the observer, since it does not know |
| 69 |
% the SearchMode. |
| 70 |
get-class-locality Locality, |
| 71 |
|
| 72 |
Locality => tc.add-tc-db _ (after "0") (tc.class ClassGR PredName SearchMode SM :- !). |
| 73 |
|
| 74 |
func declare-class indt-decl ->. |
| 75 |
declare-class D :- !, |
| 76 |
coq.env.add-indt D I, |
| 77 |
coq.parse-attributes {attributes} |
| 78 |
[ att "mode" attlist, att "deterministic" bool ] Opts, |
| 79 |
Opts => declare-class-in-coq (indt I). |
| 80 |
|
| 81 |
% Contains some instruction that are executed just after the creation of |
| 82 |
% the predicate for the class |
| 83 |
namespace eta-reduction-aux { |
| 84 |
func is-functional term ->. |
| 85 |
is-functional (prod _ _ _). |
| 86 |
|
| 87 |
func replace list term, term -> list term, term. |
| 88 |
replace [] _ [] _ :- !. |
| 89 |
replace [(fun _ _ _ as T) | Xs] X' [X' | Xs] T :- !. |
| 90 |
replace [X | XS] NEW [X | YS] S :- replace XS NEW YS S. |
| 91 |
|
| 92 |
% a call to compile builds a new rule for the given class where only one |
| 93 |
% parameter P of function type is crafted. Compiled rules have only one |
| 94 |
% premise where P is possibly eta-contructed at runtime. The absence of bang |
| 95 |
% in the first rule (after is-functional T) allows to get all rules for and |
| 96 |
% functional parameter through the std.findall in the main predicate below. |
| 97 |
func compile gref, term, bool, list term -> prop. |
| 98 |
compile ClassGR (prod _ T Bo) ff L (pi name ty body\ Cl body ty name) :- |
| 99 |
is-functional T, !, pi name ty body x\ sigma Inp\ |
| 100 |
Inp = (fun name ty body), |
| 101 |
compile ClassGR (Bo x) tt [Inp|L] (Cl name ty body). |
| 102 |
|
| 103 |
compile ClassGR (prod _ _ Bo) S L (pi x\ Cl x) :- |
| 104 |
pi x\ compile ClassGR (Bo x) S [x|L] (Cl x). |
| 105 |
|
| 106 |
compile ClassGR (sort _) tt L (pi sol new-term\ Cl new-term sol) :- |
| 107 |
pi solution new-term\ sigma Args Args' Q Q'\ |
| 108 |
std.do![ |
| 109 |
tc.gref->pred-name ClassGR PredName, |
| 110 |
std.rev [solution | L] Args, |
| 111 |
replace Args new-term Args' T, |
| 112 |
coq.elpi.predicate PredName Args Q, |
| 113 |
coq.elpi.predicate PredName Args' Q', |
| 114 |
(Cl new-term solution) = |
| 115 |
(Q :- [coq.reduction.eta-contract T new-term, |
| 116 |
if (T == new-term) fail (Q')]) |
| 117 |
]. |
| 118 |
|
| 119 |
% A debug predicate to print the genereted clauses |
| 120 |
func generated-clauses. |
| 121 |
|
| 122 |
func main string ->. |
| 123 |
main S :- |
| 124 |
coq.locate S ClassGR, |
| 125 |
coq.env.typeof ClassGR ClassTy, |
| 126 |
% a clause for eta contruction per argument of functional type is created |
| 127 |
% we get all the rules with the finall |
| 128 |
std.findall (compile ClassGR ClassTy ff [] _) SFindall, |
| 129 |
std.map SFindall (x\r\ compile _ _ _ _ r = x) SMap, |
| 130 |
if generated-clauses (coq.say SMap) true, |
| 131 |
std.forall SMap (tc.add-tc-db _ _). |
| 132 |
} |
| 133 |
} |