| 1 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 2 |
/* ------------------------------------------------------------------------- */ |
| 3 |
|
| 4 |
namespace tc { |
| 5 |
% returns the classes on which the current gref depends |
| 6 |
func get-class-dependencies gref -> list gref. |
| 7 |
get-class-dependencies GR Res :- |
| 8 |
coq.env.dependencies GR _ DepSet, |
| 9 |
coq.gref.set.elements DepSet DepList, |
| 10 |
std.filter DepList coq.TC.class? Res. |
| 11 |
|
| 12 |
func has-context-deps gref -> . |
| 13 |
has-context-deps GR :- |
| 14 |
coq.env.section-variables SectionVars, |
| 15 |
coq.env.dependencies GR _ Deps, |
| 16 |
std.exists! SectionVars (x\ coq.gref.set.mem (const x) Deps). |
| 17 |
|
| 18 |
func get-locality string -> list prop. |
| 19 |
get-locality "Local" [@local!] :- !. |
| 20 |
get-locality _ [@local!] :- coq.env.current-section-path [_ | _], !. |
| 21 |
get-locality "Global" [@global!]. |
| 22 |
get-locality "Export" []. |
| 23 |
|
| 24 |
func add-inst.aux gref, gref, list prop, grafting ->. |
| 25 |
add-inst.aux Inst TC Locality Grafting :- |
| 26 |
coq.env.current-section-path SectionPath, |
| 27 |
tc.compile.instance-gr Inst Clause, |
| 28 |
tc.get-full-path Inst ClauseName, !, |
| 29 |
(Locality => ( |
| 30 |
tc.add-tc-db ClauseName Grafting Clause, |
| 31 |
tc.add-tc-db _ Grafting (tc.instance SectionPath Inst TC Locality))). |
| 32 |
add-inst.aux Inst _ _ _ :- |
| 33 |
(@global! => tc.add-tc-db _ _ (tc.banned Inst)), |
| 34 |
coq.error "Not-added" "TC_solver" "[TC] Not yet able to compile" Inst "...". |
| 35 |
|
| 36 |
func add-inst gref, gref, string, int ->. |
| 37 |
add-inst Inst TC LocalityStr Prio :- |
| 38 |
get-locality LocalityStr Locality, |
| 39 |
if (Prio = -1) (tc.get-inst-prio Inst Prio1) (Prio1 = Prio), |
| 40 |
Grafting is after (int_to_string Prio1), |
| 41 |
add-inst.aux Inst TC Locality Grafting. |
| 42 |
|
| 43 |
func is-local. |
| 44 |
is-local :- std.mem! {attributes} (attribute "local" _). |
| 45 |
|
| 46 |
func add-inst>db.aux gref ->. |
| 47 |
add-inst>db.aux Inst :- |
| 48 |
tc.get-inst-prio Inst Prio, |
| 49 |
tc.get-TC-of-inst-type {coq.env.typeof Inst} TC, |
| 50 |
% Note: this is an approximation of instance locality when added with |
| 51 |
% TC.AddAllInstances or TC.AddInstances InstName |
| 52 |
if (is-local; has-context-deps Inst) |
| 53 |
(LocalityStr = "Local") |
| 54 |
(LocalityStr = "Export"), |
| 55 |
add-inst Inst TC LocalityStr Prio. |
| 56 |
|
| 57 |
% [add-inst->db IgnoreClassDepL ForceAdd Inst] compiles and add the Inst to |
| 58 |
% the database if the instance is not tc.banned or if it does not depend on a TC |
| 59 |
% inside the list IgnoreClassDepL. |
| 60 |
func add-inst->db list gref, bool, gref. |
| 61 |
add-inst->db _ tt Inst :- !, add-inst>db.aux Inst. |
| 62 |
add-inst->db _ _ Inst :- |
| 63 |
tc.banned Inst, !, (coq.warning "tc.banned-inst" "TC-warning" Inst "is tc.banned"). |
| 64 |
add-inst->db _ _ Inst :- tc.instance _ Inst _ _, !. % the instance has already been added |
| 65 |
add-inst->db IgnoreClassDepL _ Inst :- |
| 66 |
get-class-dependencies Inst Dep, |
| 67 |
std.exists Dep (std.mem IgnoreClassDepL), !, |
| 68 |
coq.warning "invalid-dependency-inst" "TC-warning" Inst "depneds on a tc.banned TC". |
| 69 |
add-inst->db _ _ Inst :- !, add-inst>db.aux Inst. |
| 70 |
|
| 71 |
% add all the instances of a TC |
| 72 |
func add-inst-of-tc list gref, list gref, gref ->. |
| 73 |
add-inst-of-tc IgnoreDepClassGR IgnoreInstsGR GR:- |
| 74 |
tc.get-inst-by-tc-name GR InstL, |
| 75 |
std.filter InstL (x\ not (std.mem IgnoreInstsGR x)) InstLF, |
| 76 |
std.forall InstLF (add-inst->db IgnoreDepClassGR ff). |
| 77 |
|
| 78 |
func add-tc-or-inst-gr list string, list string, list gref ->. |
| 79 |
add-tc-or-inst-gr IgnoreDepClass IgnoreInsts Names :- |
| 80 |
std.map IgnoreDepClass coq.locate IgnoreDepClassGR, |
| 81 |
std.map IgnoreInsts coq.locate IgnoreInstsGR, |
| 82 |
std.forall Names (GR\ |
| 83 |
if2 (coq.TC.class? GR)(add-inst-of-tc IgnoreDepClassGR IgnoreInstsGR GR) |
| 84 |
(tc.is-instance-gr GR)(add-inst->db IgnoreDepClassGR ff GR) |
| 85 |
(coq.warning "not-inst-nor-tc" "TC-warning" GR "is neither a TC nor a instance") |
| 86 |
). |
| 87 |
|
| 88 |
func build-args term -> list term. |
| 89 |
build-args (prod _ _ Bo) [{{0}} | TL] :- !, build-args (Bo _) TL. |
| 90 |
build-args _ [{{0}}]. |
| 91 |
|
| 92 |
% [remove-inst GR] remove an instance from the DB |
| 93 |
func remove-inst gref ->. |
| 94 |
remove-inst InstGR :- |
| 95 |
tc.get-full-path InstGR ClauseName, |
| 96 |
std.once(tc.instance _ InstGR ClassGR Locality), |
| 97 |
tc.gref->pred-name ClassGR PredName, |
| 98 |
coq.env.typeof ClassGR ClassTy, |
| 99 |
coq.elpi.predicate PredName {build-args ClassTy} Clause, |
| 100 |
tc.remove-clause ClauseName Clause Locality. |
| 101 |
|
| 102 |
func is-in-path string, gref ->. |
| 103 |
is-in-path Path GR :- |
| 104 |
std.mem! {coq.gref->path GR} Path. |
| 105 |
|
| 106 |
% [add-path ClassName Path] adds only the instances from a given path name |
| 107 |
func add-path string, string ->. |
| 108 |
add-path ClassName Path :- |
| 109 |
coq.locate ClassName GR, |
| 110 |
std.filter {tc.get-inst-by-tc-name GR} (is-in-path Path) InstInPath, |
| 111 |
std.forall InstInPath (add-inst->db [] ff). |
| 112 |
|
| 113 |
namespace class-coercion { |
| 114 |
kind task type. |
| 115 |
type add task. |
| 116 |
type remove task. |
| 117 |
|
| 118 |
func do-task task, gref ->. |
| 119 |
do-task add GR :- add-inst->db [] tt GR. |
| 120 |
do-task remove GR :- remove-inst GR. |
| 121 |
|
| 122 |
:index (_ 1) |
| 123 |
func loop-proj task, list argument ->. |
| 124 |
loop-proj _ []. |
| 125 |
loop-proj Task [str Proj | TL] :- |
| 126 |
coq.locate Proj GRProj, tc.existing-instance GRProj, !, |
| 127 |
do-task Task GRProj, |
| 128 |
loop-proj Task TL. |
| 129 |
loop-proj Task [_|TL] :- loop-proj Task TL. |
| 130 |
|
| 131 |
} |
| 132 |
} |