| 1 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 2 |
/* ------------------------------------------------------------------------- */ |
| 3 |
|
| 4 |
namespace tc { |
| 5 |
|
| 6 |
namespace lettify { |
| 7 |
func replace-args term, list term, list term, (func term -> term) -> term. |
| 8 |
replace-args Hd [] L K R :- std.rev L L1, K (app [Hd|L1]) R. |
| 9 |
replace-args Hd [X|Xs] L K (let `_` Ty X T') :- |
| 10 |
coq.typecheck X Ty ok, |
| 11 |
pi x\ replace-args Hd Xs [x|L] K (T' x). |
| 12 |
|
| 13 |
func replace-top-level list term, term, list term -> term. |
| 14 |
replace-top-level [] Hd L (app [Hd | L']) :- std.rev L L'. |
| 15 |
replace-top-level [app[X|Xs]|As] Hd L R :- |
| 16 |
replace-args X Xs [] (x\ replace-top-level As Hd [x|L]) R. |
| 17 |
replace-top-level [(fun _ _ _ as A)|As] Hd L (let `_` Ty A T') :- |
| 18 |
coq.typecheck A Ty ok, |
| 19 |
pi x\ replace-top-level As Hd [x|L] (T' x). |
| 20 |
|
| 21 |
func main term -> term. |
| 22 |
main (prod N Ty Bo) (prod N Ty Bo') :- !, |
| 23 |
pi x\ main (Bo x) (Bo' x). |
| 24 |
main (app [X|Xs]) T :- is-instance-term X, !, |
| 25 |
replace-top-level Xs X [] T. |
| 26 |
main T T. |
| 27 |
} |
| 28 |
|
| 29 |
% return if a gref is an existsing instance |
| 30 |
% TODO: this could be replaced with an API |
| 31 |
% coq.TC.get-class-of-inst i:gref, o:gref |
| 32 |
func existing-instance gref ->. |
| 33 |
existing-instance InstGR :- |
| 34 |
coq.env.typeof InstGR InstTy, |
| 35 |
get-TC-of-inst-type InstTy ClassGR, |
| 36 |
coq.TC.class? ClassGR, |
| 37 |
coq.TC.db-for ClassGR DB, |
| 38 |
std.mem! DB (tc-instance InstGR _). |
| 39 |
|
| 40 |
func under-p-global term -> gref. |
| 41 |
under-p-global (global GR) GR. |
| 42 |
under-p-global (pglobal GR _) GR. |
| 43 |
|
| 44 |
func class-gref term -> gref. |
| 45 |
class-gref T ClassGR :- |
| 46 |
under-p-global T ClassGR, |
| 47 |
coq.TC.class? ClassGR. |
| 48 |
|
| 49 |
% returns the TC from the type of an instance |
| 50 |
% TODO: This could be replaced with an api |
| 51 |
% coq.TC.get-class-of-inst i:gref, o:gref |
| 52 |
func get-TC-of-inst-type term -> gref. |
| 53 |
get-TC-of-inst-type (prod _ _ A) ClassGR:- !, |
| 54 |
pi x\ get-TC-of-inst-type (A x) ClassGR. |
| 55 |
get-TC-of-inst-type (let _ _ _ A) ClassGR:- !, |
| 56 |
pi x\ get-TC-of-inst-type (A x) ClassGR. |
| 57 |
get-TC-of-inst-type T ClassGR :- |
| 58 |
coq.safe-dest-app T HD _, |
| 59 |
not (var HD), class-gref HD ClassGR. |
| 60 |
|
| 61 |
func instances-of-current-section list gref ->. |
| 62 |
:name "MySectionEndHook" |
| 63 |
instances-of-current-section InstsFiltered :- |
| 64 |
coq.env.current-section-path SectionPath, |
| 65 |
std.findall (tc.instance SectionPath _ _ _) Insts, |
| 66 |
coq.env.section-variables SectionVars, |
| 67 |
std.map-filter Insts (x\r\ sigma X\ tc.instance _ r _ _ = x, const X = r, not(std.mem SectionVars X)) InstsFiltered. |
| 68 |
|
| 69 |
func is-instance-gr gref ->. |
| 70 |
is-instance-gr GR :- |
| 71 |
coq.env.typeof GR Ty, |
| 72 |
is-instance-term Ty. |
| 73 |
|
| 74 |
func is-instance-term term ->. |
| 75 |
is-instance-term T :- |
| 76 |
get-TC-of-inst-type T TC, |
| 77 |
coq.TC.class? TC. |
| 78 |
|
| 79 |
% adds a clause to the tc.db DB at the passed grafting |
| 80 |
func add-tc-db id, grafting, prop ->. |
| 81 |
add-tc-db _ _ Clause :- not (ground_term Clause), coq.error "[TC] anomaly: open rule:" Clause, !. |
| 82 |
add-tc-db ClauseName Graft Clause :- coq.elpi.accumulate _ "tc.db" (clause ClauseName Graft Clause), !. |
| 83 |
add-tc-db _ _ Clause :- coq.error "cannot add " PR " to tc.db". |
| 84 |
|
| 85 |
% takes a tc-instance and return the gref of the inst |
| 86 |
func inst->gref tc-instance -> gref. |
| 87 |
inst->gref (tc-instance Res _) Res. |
| 88 |
|
| 89 |
% returns all the instances of the passed ClassName |
| 90 |
func get-inst-by-tc-name gref -> list gref. |
| 91 |
get-inst-by-tc-name TC GRL:- |
| 92 |
coq.TC.db-for TC Inst, |
| 93 |
std.map Inst inst->gref GRL', |
| 94 |
std.rev GRL' GRL. |
| 95 |
|
| 96 |
func app-has-class term ->. |
| 97 |
app-has-class T :- |
| 98 |
get-TC-of-inst-type T Hd, |
| 99 |
coq.TC.class? Hd. |
| 100 |
|
| 101 |
% TC preds are on the form tc-[PATH_TO_TC].tc-[TC-Name] |
| 102 |
func gref->pred-name gref -> string. |
| 103 |
gref->pred-name Gr S :- |
| 104 |
if (tc.is-option-active tc.oTC-clauseNameShortName) |
| 105 |
(Path = "") |
| 106 |
(coq.gref->path Gr [Hd | Tl], |
| 107 |
if (Hd = "Coq") (Hd' = "Corelib") (Hd' = Hd), |
| 108 |
std.string.concat "." [Hd'|Tl] Path', |
| 109 |
Path is Path' ^ ".tc-"), |
| 110 |
% CAVEAT : Non-ascii caractars can't be part of a pred |
| 111 |
% name, we replace ö with o |
| 112 |
rex.replace "ö" "o" {coq.gref->id Gr} GrStr, |
| 113 |
S is "tc-" ^ Path ^ GrStr. |
| 114 |
|
| 115 |
func get-mode gref -> list string. |
| 116 |
get-mode ClassGR M :- tc.class ClassGR _ _ M, !. |
| 117 |
get-mode ClassGR _ :- coq.error "[TC]" ClassGR "is an unknown class". |
| 118 |
|
| 119 |
/* |
| 120 |
[make-tc.aux B Sol Head Body Rule] builds the rule with the given Head and body |
| 121 |
paying attention to the positivity of the |
| 122 |
clause |
| 123 |
Note: if the Rule being constructed is negative (B = ff), then Rules returns a |
| 124 |
solution Sol used inside the proof. If the solution is already given, we |
| 125 |
do not run the premise. This would ask Sol to be ground (ground_term S). |
| 126 |
Here, for performance issues, we simply check that the solution is not a |
| 127 |
flexible term |
| 128 |
*/ |
| 129 |
:index (1 _ _ 1) |
| 130 |
func make-tc.aux bool, term, prop, list prop -> prop. |
| 131 |
make-tc.aux tt _ Head [] Head :- !. |
| 132 |
make-tc.aux ff Sol Head [] P :- !, P = if (var Sol) Head true. |
| 133 |
make-tc.aux tt _ Head Body (Head :- Body) :- !. |
| 134 |
make-tc.aux ff Sol Head Body P :- !, P = if (var Sol) (Body => Head) true. |
| 135 |
|
| 136 |
func make-tc term, term, list prop, bool -> prop. |
| 137 |
make-tc Goal Sol RuleBody IsPositive Rule :- |
| 138 |
coq.safe-dest-app Goal Class Args, |
| 139 |
get-TC-of-inst-type Class ClassGR, |
| 140 |
gref->pred-name ClassGR ClassStr, |
| 141 |
std.append Args [Sol] ArgsSol, |
| 142 |
coq.elpi.predicate ClassStr ArgsSol RuleHead, |
| 143 |
make-tc.aux IsPositive Sol RuleHead RuleBody Rule. |
| 144 |
|
| 145 |
% returns the priority of an instance from the gref of an instance |
| 146 |
func get-inst-prio gref -> int. |
| 147 |
get-inst-prio InstGR Prio :- |
| 148 |
coq.env.typeof InstGR InstTy, |
| 149 |
get-TC-of-inst-type InstTy ClassGR, |
| 150 |
coq.TC.get-inst-prio ClassGR InstGR Prio. |
| 151 |
|
| 152 |
func get-full-path gref -> string. |
| 153 |
get-full-path Gr Res' :- |
| 154 |
coq.gref->string Gr Path, |
| 155 |
coq.env.current-section-path SectionPath, |
| 156 |
std.fold SectionPath "" (e\acc\r\ r is acc ^ "." ^ e) Res, |
| 157 |
Res' is Res ^ Path. |
| 158 |
|
| 159 |
func constant->redflag prop -> coq.redflag. |
| 160 |
constant->redflag (tc.unfold-constant C) (coq.redflags.const C). |
| 161 |
|
| 162 |
func normalize-ty term -> term. |
| 163 |
:name "normalize-ty" |
| 164 |
normalize-ty G1 G2 :- |
| 165 |
std.findall (tc.unfold-constant _) UnfoldFindall, |
| 166 |
std.map UnfoldFindall constant->redflag UnfoldRF, |
| 167 |
coq.redflags.add coq.redflags.nored [coq.redflags.beta, coq.redflags.delta | UnfoldRF] RF, |
| 168 |
@redflags! RF => coq.reduction.lazy.norm G1 G2. |
| 169 |
|
| 170 |
func remove-clause string, prop, list prop ->. |
| 171 |
remove-clause ClauseName P Locality :- |
| 172 |
%Locality => add-tc-db _ (remove ClauseName) P. |
| 173 |
Locality => add-tc-db _ (replace ClauseName) (P :- fail, !). |
| 174 |
|
| 175 |
% [section-var->decl.aux L R] auxiliary function for `section-var->decl` |
| 176 |
func section-var->decl.aux list constant -> list prop. |
| 177 |
section-var->decl.aux [] []. |
| 178 |
section-var->decl.aux [X|XS] [(decl (global (const X)) _ Ty)|YS] :- |
| 179 |
coq.env.typeof (const X) Ty, |
| 180 |
section-var->decl.aux XS YS. |
| 181 |
|
| 182 |
% [section-var->decl L] decl representing seciton variables with their types |
| 183 |
func section-var->decl -> list prop. |
| 184 |
section-var->decl L :- |
| 185 |
section-var->decl.aux {coq.env.section-variables} L. |
| 186 |
|
| 187 |
func time-is-active (func (list string) ->) ->. |
| 188 |
:name "time-is-active" |
| 189 |
time-is-active _ :- coq.option.get ["Time", "TC", "Bench"] (coq.option.bool tt), !. |
| 190 |
time-is-active Opt :- tc.is-option-active Opt. |
| 191 |
|
| 192 |
func time-pp string, float ->. |
| 193 |
time-pp Msg Time :- !, coq.debug "[TC] - Time of" Msg "is" Time. |
| 194 |
|
| 195 |
pred give-res i:prop, o:prop. |
| 196 |
give-res P true :- P. |
| 197 |
give-res _ fail. |
| 198 |
|
| 199 |
pred time-res i:prop, o:float, o:prop. |
| 200 |
time-res P T Res :- std.time (give-res P Res) T. |
| 201 |
|
| 202 |
func build-msg prop, string -> string. |
| 203 |
build-msg true S S :- !. |
| 204 |
build-msg fail S S' :- S' is S ^ " fail". |
| 205 |
|
| 206 |
pred time-it i:(list string -> prop), i:prop, i:string. |
| 207 |
time-it Opt P Msg :- time-is-active Opt, !, time-res P Time Res, build-msg Res Msg Msg', time-pp Msg' Time, Res. |
| 208 |
time-it _ P _ :- P. |
| 209 |
|
| 210 |
kind range-arity type. |
| 211 |
type r-ar nat -> nat -> range-arity. |
| 212 |
|
| 213 |
type prod-range |
| 214 |
term -> % The current qunatified uvar |
| 215 |
range-arity -> % Its minimum and maximal application |
| 216 |
term. |
| 217 |
|
| 218 |
type maybe-eta-tm |
| 219 |
term -> % The current precompiled subterm |
| 220 |
list term -> % The list of FV in the precomp subterm |
| 221 |
term. |
| 222 |
|
| 223 |
type maybe-llam-tm |
| 224 |
term -> % The current precompiled subterm: shape is app[app[X,PF],NPF] |
| 225 |
list term -> % The eta-expanded version of X, from X^{len(PF)} to X^{len(PF)+len(NPF)} |
| 226 |
term. |
| 227 |
} |