| 1 |
namespace tc { |
| 2 |
shorten tc.{pending-mode}. |
| 3 |
|
| 4 |
typeabbrev elpi-mode (pair argument_mode string). |
| 5 |
typeabbrev elpi-modeL (list elpi-mode). |
| 6 |
typeabbrev string-modeL (list string). |
| 7 |
macro @pending-mode! :- "pending mode". |
| 8 |
|
| 9 |
func string->coq-mode -> string, hint-mode. |
| 10 |
string->coq-mode uvar uvar :- !, coq.error "[TC] string->coq-mode". |
| 11 |
string->coq-mode "!" mode-input :- !. |
| 12 |
string->coq-mode "+" mode-ground :- !. |
| 13 |
string->coq-mode "-" mode-output :- !. |
| 14 |
string->coq-mode "bang" mode-ground :- !. |
| 15 |
string->coq-mode "plus" mode-input :- !. |
| 16 |
string->coq-mode "minus" mode-output :- !. |
| 17 |
string->coq-mode "i" mode-input :- !. |
| 18 |
string->coq-mode "o" mode-output :- !. |
| 19 |
string->coq-mode A _ :- coq.error A "is not a valid mode". |
| 20 |
|
| 21 |
func coq->elpi-mode hint-mode -> elpi-mode. |
| 22 |
:name "coq->elpi-mode" |
| 23 |
coq->elpi-mode mode-ground (pr in "term"). % approximation |
| 24 |
/* |
| 25 |
The coq input-mode corresponds to the elpi mode out |
| 26 |
Instance : option_equiv : ∀ {A : Type}, Equiv A → Equiv (option A) |
| 27 |
Mode: Equiv ! |
| 28 |
Check _ : forall x, Equiv A -> Equiv (option _). |
| 29 |
In coq, the Check gives the instance option_equiv |
| 30 |
In elpi, we would have the rule: `tc-Equiv (option A) (option_equiv A B) :- tc-equiv A B.` |
| 31 |
since the argument in first position of the premise is flexible, then we have a failure |
| 32 |
*/ |
| 33 |
coq->elpi-mode mode-input (pr in "term"). |
| 34 |
coq->elpi-mode mode-output (pr out "term"). |
| 35 |
|
| 36 |
func string->elpi-mode string -> elpi-mode. |
| 37 |
string->elpi-mode S R :- coq->elpi-mode {string->coq-mode S} R, !. |
| 38 |
string->elpi-mode _ (pr out "term"). |
| 39 |
|
| 40 |
func elpi->string-mode elpi-mode -> string. |
| 41 |
elpi->string-mode (pr in _) "i" :- !. |
| 42 |
elpi->string-mode (pr out _) "o". |
| 43 |
|
| 44 |
% Here we build the elpi modes a class CL. If CL has either zero or more than |
| 45 |
% one mode, then we consider all its parameter to be in output mode. If the |
| 46 |
% class has exactly one mode, then it is considered for the signature of the |
| 47 |
% predicate for CL |
| 48 |
func modes-of-class gref -> list (elpi-mode). |
| 49 |
modes-of-class ClassGR Modes :- |
| 50 |
coq.hints.modes ClassGR "typeclass_instances" CoqModesList, |
| 51 |
not (CoqModesList = []), |
| 52 |
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} ")"), |
| 53 |
CoqModesList = [HintModesFst], |
| 54 |
std.append {std.map HintModesFst coq->elpi-mode} [pr out "term"] Modes, !. |
| 55 |
modes-of-class ClassGR Modes :- |
| 56 |
coq.env.typeof ClassGR ClassTy, |
| 57 |
N is {coq.count-prods ClassTy} + 1, % + 1 for the solution |
| 58 |
std.list.init N (x\r\ r = (pr out "term")) Modes. |
| 59 |
|
| 60 |
func add-pending-mode list string ->. |
| 61 |
add-pending-mode _ :- |
| 62 |
pending-mode _, !, |
| 63 |
coq.error "[TC] A pending mode already exists. Build a class with that mode to avoid this error". |
| 64 |
add-pending-mode M :- |
| 65 |
tc.add-tc-db @pending-mode! _ (pending-mode M). |
| 66 |
|
| 67 |
func remove-pending-mode ->. |
| 68 |
remove-pending-mode :- |
| 69 |
tc.remove-clause @pending-mode! (pending-mode []) []. |
| 70 |
|
| 71 |
func check-pending-mode-arity gref, list A ->. |
| 72 |
check-pending-mode-arity GR L :- |
| 73 |
coq.env.typeof GR Ty, |
| 74 |
coq.count-prods Ty N, |
| 75 |
N' is N + 1, |
| 76 |
std.assert! (std.length L N') "Pending mode invalid for class GR". |
| 77 |
|
| 78 |
% [get-elpi-mode GR RSM EM SM] |
| 79 |
% GR is a term gref. |
| 80 |
% RSM represents the raw modes of the GR that have been passed as attributes. |
| 81 |
% If the length of RSM is N and greater than 0, then the arity of the type of GR is exactly N. |
| 82 |
% Otherwise, it means that the user has not passed the attribute to the GR. |
| 83 |
% EM represents the Elpi modes. |
| 84 |
% SM represents the Rocq modes; it is RSM if RSM is not empty, otherwise it is a list [-,-,..,-] |
| 85 |
% with the length equal to the arity of the type of GR. |
| 86 |
func get-elpi-mode gref, string-modeL -> elpi-modeL, string-modeL. |
| 87 |
get-elpi-mode ClassGR _ EM SM :- |
| 88 |
pending-mode SM, !, |
| 89 |
check-pending-mode-arity ClassGR SM, |
| 90 |
remove-pending-mode, |
| 91 |
std.map SM string->elpi-mode EM. |
| 92 |
get-elpi-mode ClassGR [] EM SM :- !, |
| 93 |
modes-of-class ClassGR EM, |
| 94 |
std.map EM elpi->string-mode SM. |
| 95 |
get-elpi-mode ClassGR SM EM SM :- |
| 96 |
std.append {std.map SM string->elpi-mode} [pr out "term"] EM. |
| 97 |
|
| 98 |
func get-evars term -> list term. |
| 99 |
get-evars T L :- |
| 100 |
(pi hd T L\ fold-map T L _ [hd | L] :- var T hd _, !) => |
| 101 |
fold-map T [] _ L. |
| 102 |
|
| 103 |
pred evars o:term. |
| 104 |
|
| 105 |
func action-to-accumulate string, term -> list prop. |
| 106 |
action-to-accumulate _ T [evars HD] :- var T HD _, !. |
| 107 |
action-to-accumulate _ _ []. |
| 108 |
|
| 109 |
func mode-check string, term ->. |
| 110 |
% heuristic: if we are in "+" mode, then all evars in T should only be |
| 111 |
% in that argument |
| 112 |
mode-check "+" T :- !, |
| 113 |
std.findall-unary evars L, |
| 114 |
get-evars T EV, |
| 115 |
std.forall EV (x\ std.forall L (x'\ not (x == x'))). |
| 116 |
mode-check "!" uvar :- !, fail. |
| 117 |
mode-check _ _. |
| 118 |
|
| 119 |
func modes-check.aux list string, list term ->. |
| 120 |
modes-check.aux [] [_|_] :- coq.error "[TC] modes-check invalid list length". |
| 121 |
modes-check.aux [_|_] [] :- coq.error "[TC] modes-check invalid list length". |
| 122 |
modes-check.aux [] []. |
| 123 |
modes-check.aux [X|XS] [Y|YS] :- std.do![ |
| 124 |
mode-check X Y, % check on argument X wrt mode Y |
| 125 |
action-to-accumulate X Y P, % action P to accumulate |
| 126 |
P => modes-check.aux XS YS % accumulate P and iterate of XS YS |
| 127 |
]. |
| 128 |
|
| 129 |
func modes-check term ->. |
| 130 |
modes-check (global _). |
| 131 |
modes-check (pglobal _ _). |
| 132 |
modes-check (app [ClassTerm | Args]) :- |
| 133 |
tc.class-gref ClassTerm ClassGR, |
| 134 |
tc.get-mode ClassGR M, |
| 135 |
std.drop-last 1 M M', |
| 136 |
modes-check.aux M' Args. |
| 137 |
} |