namespace tc { shorten tc.{pending-mode}. typeabbrev elpi-mode (pair argument_mode string). typeabbrev elpi-modeL (list elpi-mode). typeabbrev string-modeL (list string). macro @pending-mode! :- "pending mode". func string->coq-mode -> string, hint-mode. string->coq-mode uvar uvar :- !, coq.error "[TC] string->coq-mode". string->coq-mode "!" mode-input :- !. string->coq-mode "+" mode-ground :- !. string->coq-mode "-" mode-output :- !. string->coq-mode "bang" mode-ground :- !. string->coq-mode "plus" mode-input :- !. string->coq-mode "minus" mode-output :- !. string->coq-mode "i" mode-input :- !. string->coq-mode "o" mode-output :- !. string->coq-mode A _ :- coq.error A "is not a valid mode". func coq->elpi-mode hint-mode -> elpi-mode. :name "coq->elpi-mode" coq->elpi-mode mode-ground (pr in "term"). % approximation /* The coq input-mode corresponds to the elpi mode out Instance : option_equiv : ∀ {A : Type}, Equiv A → Equiv (option A) Mode: Equiv ! Check _ : forall x, Equiv A -> Equiv (option _). In coq, the Check gives the instance option_equiv In elpi, we would have the rule: `tc-Equiv (option A) (option_equiv A B) :- tc-equiv A B.` since the argument in first position of the premise is flexible, then we have a failure */ coq->elpi-mode mode-input (pr in "term"). coq->elpi-mode mode-output (pr out "term"). func string->elpi-mode string -> elpi-mode. string->elpi-mode S R :- coq->elpi-mode {string->coq-mode S} R, !. string->elpi-mode _ (pr out "term"). func elpi->string-mode elpi-mode -> string. elpi->string-mode (pr in _) "i" :- !. elpi->string-mode (pr out _) "o". % Here we build the elpi modes a class CL. If CL has either zero or more than % one mode, then we consider all its parameter to be in output mode. If the % class has exactly one mode, then it is considered for the signature of the % predicate for CL func modes-of-class gref -> list (elpi-mode). modes-of-class ClassGR Modes :- coq.hints.modes ClassGR "typeclass_instances" CoqModesList, not (CoqModesList = []), not (CoqModesList = [_,_|_], coq.warning "TC.Modes" "At the moment we only allow TC with at most 1 hint Mode (caused by class" {coq.gref->string ClassGR} ")"), CoqModesList = [HintModesFst], std.append {std.map HintModesFst coq->elpi-mode} [pr out "term"] Modes, !. modes-of-class ClassGR Modes :- coq.env.typeof ClassGR ClassTy, N is {coq.count-prods ClassTy} + 1, % + 1 for the solution std.list.init N (x\r\ r = (pr out "term")) Modes. func add-pending-mode list string ->. add-pending-mode _ :- pending-mode _, !, coq.error "[TC] A pending mode already exists. Build a class with that mode to avoid this error". add-pending-mode M :- tc.add-tc-db @pending-mode! _ (pending-mode M). func remove-pending-mode ->. remove-pending-mode :- tc.remove-clause @pending-mode! (pending-mode []) []. func check-pending-mode-arity gref, list A ->. check-pending-mode-arity GR L :- coq.env.typeof GR Ty, coq.count-prods Ty N, N' is N + 1, std.assert! (std.length L N') "Pending mode invalid for class GR". % [get-elpi-mode GR RSM EM SM] % GR is a term gref. % RSM represents the raw modes of the GR that have been passed as attributes. % If the length of RSM is N and greater than 0, then the arity of the type of GR is exactly N. % Otherwise, it means that the user has not passed the attribute to the GR. % EM represents the Elpi modes. % SM represents the Rocq modes; it is RSM if RSM is not empty, otherwise it is a list [-,-,..,-] % with the length equal to the arity of the type of GR. func get-elpi-mode gref, string-modeL -> elpi-modeL, string-modeL. get-elpi-mode ClassGR _ EM SM :- pending-mode SM, !, check-pending-mode-arity ClassGR SM, remove-pending-mode, std.map SM string->elpi-mode EM. get-elpi-mode ClassGR [] EM SM :- !, modes-of-class ClassGR EM, std.map EM elpi->string-mode SM. get-elpi-mode ClassGR SM EM SM :- std.append {std.map SM string->elpi-mode} [pr out "term"] EM. func get-evars term -> list term. get-evars T L :- (pi hd T L\ fold-map T L _ [hd | L] :- var T hd _, !) => fold-map T [] _ L. pred evars o:term. func action-to-accumulate string, term -> list prop. action-to-accumulate _ T [evars HD] :- var T HD _, !. action-to-accumulate _ _ []. func mode-check string, term ->. % heuristic: if we are in "+" mode, then all evars in T should only be % in that argument mode-check "+" T :- !, std.findall-unary evars L, get-evars T EV, std.forall EV (x\ std.forall L (x'\ not (x == x'))). mode-check "!" uvar :- !, fail. mode-check _ _. func modes-check.aux list string, list term ->. modes-check.aux [] [_|_] :- coq.error "[TC] modes-check invalid list length". modes-check.aux [_|_] [] :- coq.error "[TC] modes-check invalid list length". modes-check.aux [] []. modes-check.aux [X|XS] [Y|YS] :- std.do![ mode-check X Y, % check on argument X wrt mode Y action-to-accumulate X Y P, % action P to accumulate P => modes-check.aux XS YS % accumulate P and iterate of XS YS ]. func modes-check term ->. modes-check (global _). modes-check (pglobal _ _). modes-check (app [ClassTerm | Args]) :- tc.class-gref ClassTerm ClassGR, tc.get-mode ClassGR M, std.drop-last 1 M M', modes-check.aux M' Args. }