| 1 |
(* license: GNU Lesser General Public License Version 2.1 or later *) |
| 2 |
(* ------------------------------------------------------------------------- *) |
| 3 |
|
| 4 |
Declare ML Module "rocq-elpi.tc". |
| 5 |
|
| 6 |
From elpi.apps.tc.elpi Extra Dependency "tc_aux.elpi" as tc_aux. |
| 7 |
(* From elpi.apps.tc.elpi Extra Dependency "compiler.elpi" as compiler. *) |
| 8 |
From elpi.apps.tc.elpi Extra Dependency "ho_precompile.elpi" as ho_precompile. |
| 9 |
From elpi.apps.tc.elpi Extra Dependency "ho_compile.elpi" as ho_compile. |
| 10 |
From elpi.apps.tc.elpi Extra Dependency "compiler1.elpi" as compiler1. |
| 11 |
From elpi.apps.tc.elpi Extra Dependency "modes.elpi" as modes. |
| 12 |
From elpi.apps.tc.elpi Extra Dependency "unif.elpi" as unif. |
| 13 |
From elpi.apps.tc.elpi Extra Dependency "ho_link.elpi" as ho_link. |
| 14 |
From elpi.apps.tc.elpi Extra Dependency "solver.elpi" as solver. |
| 15 |
From elpi.apps.tc.elpi Extra Dependency "create_tc_predicate.elpi" as create_tc_predicate. |
| 16 |
|
| 17 |
From elpi.apps Require Import db. |
| 18 |
From elpi.apps Require Export add_commands. |
| 19 |
|
| 20 |
(* Set Warnings "+elpi". *) |
| 21 |
|
| 22 |
Elpi Command TC.Print_instances. |
| 23 |
Elpi Accumulate Db tc.db. |
| 24 |
Elpi Accumulate lp:{{ |
| 25 |
func tc.list-printer-aux prop ->. |
| 26 |
tc.list-printer-aux (tc.instance _ InstGR _ Locality) :- |
| 27 |
coq.say InstGR "with locality" Locality. |
| 28 |
|
| 29 |
func tc.list-printer gref, list prop ->. |
| 30 |
tc.list-printer _ [] :- !. |
| 31 |
tc.list-printer ClassGR Instances :- |
| 32 |
coq.say "Instances list for" ClassGR "is:", |
| 33 |
std.forall Instances tc.list-printer-aux. |
| 34 |
|
| 35 |
main [str Class] :- |
| 36 |
std.assert! (coq.locate Class ClassGR) "The entered TC not exists", |
| 37 |
std.findall (tc.instance _ _ ClassGR _) Rules, |
| 38 |
tc.list-printer ClassGR Rules. |
| 39 |
main [] :- |
| 40 |
std.forall {coq.TC.db-tc} (ClassGR\ sigma Rules\ |
| 41 |
std.findall (tc.instance _ _ ClassGR _) Rules, |
| 42 |
tc.list-printer ClassGR Rules |
| 43 |
). |
| 44 |
}}. |
| 45 |
|
| 46 |
|
| 47 |
Elpi Tactic TC.Solver. |
| 48 |
Elpi Accumulate Db tc.db. |
| 49 |
Elpi Accumulate Db tc_options.db. |
| 50 |
Elpi Accumulate File tc_aux. |
| 51 |
Elpi Accumulate File unif. |
| 52 |
Elpi Accumulate File ho_link. |
| 53 |
(* Elpi Accumulate File compiler. *) |
| 54 |
Elpi Accumulate File ho_precompile. |
| 55 |
Elpi Accumulate File ho_compile. |
| 56 |
Elpi Accumulate File compiler1. |
| 57 |
Elpi Accumulate File modes. |
| 58 |
Elpi Accumulate File create_tc_predicate. |
| 59 |
Elpi Accumulate File solver. |
| 60 |
Elpi Query lp:{{ |
| 61 |
sigma Options\ |
| 62 |
tc.all-options Options, |
| 63 |
std.forall Options (x\ sigma L\ x L, |
| 64 |
if (coq.option.available? L _) true |
| 65 |
(coq.option.add L (coq.option.bool ff) ff)). |
| 66 |
}}. |
| 67 |
|
| 68 |
|
| 69 |
Elpi Query lp:{{ |
| 70 |
sigma Nums\ |
| 71 |
std.iota 1001 Nums, |
| 72 |
std.forall Nums (x\ sigma NumStr\ |
| 73 |
NumStr is int_to_string x, |
| 74 |
@global! => tc.add-tc-db NumStr (before "lastHook") (tc.hook NumStr) |
| 75 |
) |
| 76 |
}}. |
| 77 |
|
| 78 |
Elpi Command TC.Compiler. |
| 79 |
Elpi Accumulate Db tc.db. |
| 80 |
Elpi Accumulate Db tc_options.db. |
| 81 |
Elpi Accumulate File tc_aux. |
| 82 |
Elpi Accumulate File modes. |
| 83 |
Elpi Accumulate File create_tc_predicate. |
| 84 |
Elpi Accumulate File ho_precompile. |
| 85 |
Elpi Accumulate File ho_compile. |
| 86 |
Elpi Accumulate File unif. |
| 87 |
Elpi Accumulate File ho_link. |
| 88 |
Elpi Accumulate File compiler1. |
| 89 |
Elpi Accumulate lp:{{ |
| 90 |
|
| 91 |
/* |
| 92 |
Projections of a class that are coercions, are wrongly compiled: |
| 93 |
In the following code: |
| 94 |
```coq |
| 95 |
Class Animal. |
| 96 |
Class Bird := IsAnimal :> Animal. |
| 97 |
``` |
| 98 |
The instance IsAnimal of type Bird -> Animal, is compiled before the |
| 99 |
predicate for Bird; hence, Bird is not recognize as a premise of IsAnimal. |
| 100 |
This problem is due to the order in which the registers for Instance and |
| 101 |
Class creation are run. |
| 102 |
The solution is to do the following two jobs when a class C is created: |
| 103 |
1: for every projection P of C, if P is a coercion, the wrongly |
| 104 |
compiled instance is replaced with a `dummy` clause. |
| 105 |
2: the predicate for the class is created |
| 106 |
3: for every projection P of C, if P is a coercion, the correct |
| 107 |
instance is created and added to the db |
| 108 |
*/ |
| 109 |
|
| 110 |
shorten tc.class-coercion.{add, remove, loop-proj}. |
| 111 |
main [str "remove_coercions" | Proj] :- !, loop-proj remove Proj. |
| 112 |
main [str "add_coercions" | Proj] :- !, loop-proj add Proj. |
| 113 |
|
| 114 |
main [str "new_instance", str Inst, str Cl, str Locality, int Prio] :- !, |
| 115 |
tc.time-it _ (coq.locate Cl GRCl, |
| 116 |
coq.locate Inst GRInst, |
| 117 |
tc.add-inst GRInst GRCl Locality Prio) "Compiler for Instance". |
| 118 |
|
| 119 |
main [str "new_class", str Cl, str Modes] :- !, |
| 120 |
tc.time-it tc.oTC-time-compile-class ( |
| 121 |
coq.locate Cl GR, tc.add-class-gr tc.classic GR {rex.split " " Modes} |
| 122 |
) "Compiler for Class". |
| 123 |
|
| 124 |
% used to build ad-hoc instance for eta-reduction on the argument of |
| 125 |
% Cl that have function type |
| 126 |
main [str "default_instance", str Cl] :- !, |
| 127 |
tc.eta-reduction-aux.main Cl. |
| 128 |
|
| 129 |
main A :- coq.error "Fail in TC.Compiler: not a valid input entry" A. |
| 130 |
}}. |
| 131 |
|
| 132 |
|
| 133 |
(* Command allowing to set if a TC is deterministic. *) |
| 134 |
Elpi Command TC.Set_deterministic. |
| 135 |
Elpi Accumulate Db tc.db. |
| 136 |
Elpi Accumulate Db tc_options.db. |
| 137 |
Elpi Accumulate File tc_aux. |
| 138 |
Elpi Accumulate lp:{{ |
| 139 |
main [str ClassStr] :- |
| 140 |
coq.locate ClassStr ClassGR, |
| 141 |
std.assert! (coq.TC.class? ClassGR) "Should pass the name of a type class", |
| 142 |
std.assert! (tc.class ClassGR PredName _ Modes) "Cannot find `class ClassGR _ _` in the db", |
| 143 |
std.assert! (not (tc.instance _ _ ClassGR _)) "Cannot set deterministic a class with an already existing instance", |
| 144 |
tc.add-tc-db _ (after "0") (tc.class ClassGR PredName tc.deterministic Modes :- !). |
| 145 |
}}. |
| 146 |
|
| 147 |
|
| 148 |
Elpi Command TC.Get_class_info. |
| 149 |
Elpi Accumulate Db tc.db. |
| 150 |
Elpi Accumulate Db tc_options.db. |
| 151 |
Elpi Accumulate File tc_aux. |
| 152 |
Elpi Accumulate lp:{{ |
| 153 |
main [str ClassStr] :- |
| 154 |
coq.locate ClassStr ClassGR, |
| 155 |
tc.class ClassGR PredName SearchMode Modes, |
| 156 |
coq.say "[TC] For " ClassGR ":", |
| 157 |
coq.say " elpi predicate :" PredName, |
| 158 |
coq.say " search mode is :" SearchMode, |
| 159 |
coq.say " modes are :" Modes. |
| 160 |
main [str C] :- coq.error "[TC]" C "is not found in elpi db". |
| 161 |
main [A] :- std.assert! (str _ = A) "first argument should be a str". |
| 162 |
main [_|_] :- coq.error "[TC] Get_class_info accepts only one argument of type str". |
| 163 |
main [] :- coq.error "[TC] Get_class_info accepts only one argument of type str". |
| 164 |
main L :- coq.error "[TC] Uncaught error on input" L. |
| 165 |
}}. |
| 166 |
|
| 167 |
|
| 168 |
Elpi Command TC.Unfold. |
| 169 |
Elpi Accumulate Db tc_options.db. |
| 170 |
Elpi Accumulate Db tc.db. |
| 171 |
Elpi Accumulate File tc_aux. |
| 172 |
Elpi Accumulate lp:{{ |
| 173 |
func tc.add-unfold gref ->. |
| 174 |
tc.add-unfold (const C) :- !, |
| 175 |
if (tc.unfold-constant C) true |
| 176 |
(tc.add-tc-db _ _ (tc.unfold-constant C)). |
| 177 |
tc.add-unfold GR :- |
| 178 |
coq.error "[TC]" GR "is not a constant". |
| 179 |
main L :- |
| 180 |
ErrMsg = "[TC] TC.Unfold accepts a list of string is accepted", |
| 181 |
std.map L (x\r\ sigma R\ std.assert! (str R = x) ErrMsg, coq.locate R r) L', |
| 182 |
std.forall L' tc.add-unfold. |
| 183 |
}}. |
| 184 |
|
| 185 |
|
| 186 |
Elpi TC Solver Register TC.Solver. |
| 187 |
Elpi TC Solver Activate TC.Solver. |
| 188 |
Elpi TC Solver Override TC.Solver All. |
| 189 |
|
| 190 |
Elpi Register TC Compiler TC.Compiler. |
| 191 |
|
| 192 |
Elpi Export TC.Print_instances. |
| 193 |
Elpi Export TC.Solver. |
| 194 |
Elpi Export TC.Compiler. |
| 195 |
Elpi Export TC.Get_class_info. |
| 196 |
Elpi Export TC.Set_deterministic. |
| 197 |
Elpi Export TC.Unfold. |
| 198 |
|
| 199 |
Set Warnings "elpi". |
| 200 |
|
| 201 |
Elpi TC.AddAllClasses. |
| 202 |
Elpi TC.AddAllInstances. |