/* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ namespace tc { shorten tc.{search-mode}. % The predicate of a class is local if inside a section and readded at section end. % This way, section variables are generalized func get-class-locality -> list prop. get-class-locality [@local!] :- coq.env.current-section-path [_ | _], !. get-class-locality [@global!]. % [add-class-gr SearchMode ClassGR RModes] adds the predicate for the class with its corresponing mode % NOTE: the mode inside TC.declare_mode has the priority over Coq mode or #[mode=(...)] TC.Declare % RModes is a raw list of string containing rocq modes func add-class-gr search-mode, gref, list string ->. % the predicate for the class has alread been added add-class-gr _ ClassGR _ :- tc.class ClassGR _ _ _, !. add-class-gr SearchMode ClassGR SMR :- std.assert! (coq.TC.class? ClassGR) "Only gref of type classes can be added as new predicates", tc.get-elpi-mode ClassGR SMR EM SM, if (std.forall EM (m\ sigma a s\ m = pr a s, a = out)) (true) ( std.fold EM "" (m\s\r\ sigma a s'\ m = pr a s', if (a = in) (calc (s ^ " 10") r) (calc (s ^ " _") r)) Indexing), tc.gref->pred-name ClassGR PredName, get-class-locality Locality, Locality => ( coq.elpi.add-predicate "tc.db" Indexing PredName EM, tc.add-tc-db _ _ (tc.class ClassGR PredName SearchMode SM)). func add-class-str search-mode, string ->. add-class-str SearchMode ClassStr :- coq.locate ClassStr ClassGR, add-class-gr SearchMode ClassGR []. func attr->search-mode -> search-mode. attr->search-mode tc.deterministic :- get-option "deterministic" tt, !. attr->search-mode tc.classic. func attr->modes -> list hint-mode. attr->modes CoqModes :- get-option "mode" L, std.map L get-key-from-option RawModes, std.map RawModes tc.string->coq-mode CoqModes, !. attr->modes []. func get-key-from-option prop -> string. get-key-from-option (get-option A tt) A :- !. get-key-from-option (get-option "i" ff) "o" :- !. get-key-from-option (get-option "o" ff) "i" :- !. get-key-from-option A _ :- coq.error A "should be an option". func declare-class-in-coq gref ->. declare-class-in-coq ClassGR :- attr->modes CoqModes, if (CoqModes = []) ( tc.modes-of-class ClassGR EM, std.map EM tc.elpi->string-mode SM )( coq.hints.add-mode ClassGR "typeclass_instances" CoqModes, std.map CoqModes (x\y\tc.string->coq-mode y x) SM', std.append SM' ["o"] SM ), % CAVEAT: this triggers the observer coq.TC.declare-class ClassGR, attr->search-mode SearchMode, tc.gref->pred-name ClassGR PredName, % HACK: we override the clauses added by the observer, since it does not know % the SearchMode. get-class-locality Locality, Locality => tc.add-tc-db _ (after "0") (tc.class ClassGR PredName SearchMode SM :- !). func declare-class indt-decl ->. declare-class D :- !, coq.env.add-indt D I, coq.parse-attributes {attributes} [ att "mode" attlist, att "deterministic" bool ] Opts, Opts => declare-class-in-coq (indt I). % Contains some instruction that are executed just after the creation of % the predicate for the class namespace eta-reduction-aux { func is-functional term ->. is-functional (prod _ _ _). func replace list term, term -> list term, term. replace [] _ [] _ :- !. replace [(fun _ _ _ as T) | Xs] X' [X' | Xs] T :- !. replace [X | XS] NEW [X | YS] S :- replace XS NEW YS S. % a call to compile builds a new rule for the given class where only one % parameter P of function type is crafted. Compiled rules have only one % premise where P is possibly eta-contructed at runtime. The absence of bang % in the first rule (after is-functional T) allows to get all rules for and % functional parameter through the std.findall in the main predicate below. func compile gref, term, bool, list term -> prop. compile ClassGR (prod _ T Bo) ff L (pi name ty body\ Cl body ty name) :- is-functional T, !, pi name ty body x\ sigma Inp\ Inp = (fun name ty body), compile ClassGR (Bo x) tt [Inp|L] (Cl name ty body). compile ClassGR (prod _ _ Bo) S L (pi x\ Cl x) :- pi x\ compile ClassGR (Bo x) S [x|L] (Cl x). compile ClassGR (sort _) tt L (pi sol new-term\ Cl new-term sol) :- pi solution new-term\ sigma Args Args' Q Q'\ std.do![ tc.gref->pred-name ClassGR PredName, std.rev [solution | L] Args, replace Args new-term Args' T, coq.elpi.predicate PredName Args Q, coq.elpi.predicate PredName Args' Q', (Cl new-term solution) = (Q :- [coq.reduction.eta-contract T new-term, if (T == new-term) fail (Q')]) ]. % A debug predicate to print the genereted clauses func generated-clauses. func main string ->. main S :- coq.locate S ClassGR, coq.env.typeof ClassGR ClassTy, % a clause for eta contruction per argument of functional type is created % we get all the rules with the finall std.findall (compile ClassGR ClassTy ff [] _) SFindall, std.map SFindall (x\r\ compile _ _ _ _ r = x) SMap, if generated-clauses (coq.say SMap) true, std.forall SMap (tc.add-tc-db _ _). } }