| 1 |
/* Boolean comparison functions */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{assert!, do!, append}. |
| 6 |
|
| 7 |
namespace derive.eq { |
| 8 |
|
| 9 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 10 |
% Fills in the matrix with the truth values |
| 11 |
:index (_ 1 1) |
| 12 |
func body |
| 13 |
term, list term, list term, % constructor, arguments and their types |
| 14 |
term, list term, list term % constructor, arguments and their types |
| 15 |
-> term. %result |
| 16 |
|
| 17 |
:name "derive.eq.body:begin" % Extension point (e.g. to skip a subterm) |
| 18 |
body K [] _ K [] _ R :- !, % no arguments, same constructor |
| 19 |
R = {{ true }}. |
| 20 |
|
| 21 |
body K [X] [T1] K [Y] [T2] R :- !, % special case to avoid ".. && true" |
| 22 |
eq-db T1 T2 F, coq.mk-app F [X,Y] R. |
| 23 |
|
| 24 |
body K [X|XS] [T1|TS1] K [Y|YS] [T2|TS2] R :- !, |
| 25 |
% compare X with Y |
| 26 |
eq-db T1 T2 F, coq.mk-app F [X,Y] RX, |
| 27 |
% compare XS with YS |
| 28 |
body K XS TS1 K YS TS2 RXS, |
| 29 |
R = {{ (lp:RX && lp:RXS)%bool }}. |
| 30 |
|
| 31 |
body _ _ _ _ _ _ R :- !, % outside the diagonal it is always false |
| 32 |
R = {{ false }}. |
| 33 |
|
| 34 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 |
% Build the body |
| 36 |
|
| 37 |
:index (_ _ 1) |
| 38 |
func bo-idx |
| 39 |
prop, % eq-db clause for the current type (applied to params) |
| 40 |
term, % inductive type (applied to params) for the first argument |
| 41 |
term, % arity left for the first argument |
| 42 |
term, % inductive type (applied to params) for the second argument |
| 43 |
term, % arity left for the second argument |
| 44 |
int % accumulator to set Recno |
| 45 |
-> int, % Recno |
| 46 |
term, % Type of the term being built |
| 47 |
term. % Term buing built |
| 48 |
|
| 49 |
bo-idx C Ity1 (prod _ Src1 Tgt1) Ity2 (prod _ Src2 Tgt2) N M Rty R :- !, |
| 50 |
coq.name-suffix `i` 1 I1, |
| 51 |
coq.name-suffix `i` 2 I2, |
| 52 |
R = (fun I1 Src1 i\ fun I2 Src2 j\ Rrec i j), |
| 53 |
Rty = (prod I1 Src1 i\ prod I2 Src2 j\ Rtyrec i j), |
| 54 |
quantify-eq-db-idx C Crec, |
| 55 |
pi x y\ |
| 56 |
decl x `i` Src1 => |
| 57 |
decl y `j` Src2 => |
| 58 |
bo-idx Crec {coq.mk-app Ity1 [x]} (Tgt1 x) {coq.mk-app Ity2 [y]} (Tgt2 y) |
| 59 |
{calc (N + 2)} M (Rtyrec x y) (Rrec x y). |
| 60 |
|
| 61 |
bo-idx C Ity1 (sort _) Ity2 (sort _) N N Rty R :- !, |
| 62 |
Rty = {{ lp:Ity1 -> lp:Ity2 -> bool }}, |
| 63 |
R = {{ fun (x1 : lp:Ity1) (x2 : lp:Ity2) => lp:(Bo x1 x2) }}, |
| 64 |
pi x1 x2\ |
| 65 |
decl x1 `a` Ity1 => |
| 66 |
decl x2 `b` Ity2 => |
| 67 |
C => |
| 68 |
bo-matrix x1 Ity1 x2 Ity2 (Bo x1 x2). |
| 69 |
|
| 70 |
bo-idx C Ity1 X Ity2 Y N M Rty R :- whd1 X X1, whd1 Y Y1, !, |
| 71 |
bo-idx C Ity1 X1 Ity2 Y1 N M Rty R. |
| 72 |
|
| 73 |
func bo-matrix term, term, term, term -> term. |
| 74 |
bo-matrix X1 TyX1 X2 TyX2 R :- |
| 75 |
coq.build-match X1 TyX1 bo-ty (k1\ _\ a1\ ty1\ |
| 76 |
coq.build-match X2 TyX2 bo-ty (k2\ _\ a2\ ty2\ |
| 77 |
body k1 a1 ty1 k2 a2 ty2)) R. |
| 78 |
|
| 79 |
func bo-ty term, list term, list term -> term. |
| 80 |
bo-ty _ _ _ {{ bool }}. |
| 81 |
|
| 82 |
% Take in input all parameters and their comparison function %%%%%%%%%%%%%% |
| 83 |
func bo-param |
| 84 |
int, % Number of parameters, recursion fuel |
| 85 |
term, % Inductive type (applied to all parameters taken in input) |
| 86 |
term % Inductive type arity (parameters + indexes) |
| 87 |
-> term. % Comparison function |
| 88 |
|
| 89 |
bo-param 0 Ity Arity R :- coq.safe-dest-app Ity (global (indt GR)) _, coq.env.recursive? GR, !, |
| 90 |
Boidx = fix `f` Recno Rty Rbo, |
| 91 |
(pi f\ decl f `f` Rty => |
| 92 |
% We build the body (assuming the indexes are *not* the same) |
| 93 |
bo-idx (eq-db Ity Ity f :- []) Ity Arity Ity Arity 0 Recno Rty (Rbo f)), |
| 94 |
% We then pass to the body the terms with their indexes (that are the same) |
| 95 |
apply-idx Ity Arity Boidx R. |
| 96 |
bo-param 0 Ity Arity R :- !, |
| 97 |
bo-idx (pi f\eq-db Ity Ity f :- fail) Ity Arity Ity Arity 0 _ _ Boidx, |
| 98 |
apply-idx Ity Arity Boidx R. |
| 99 |
|
| 100 |
bo-param N Ity (prod _ Sty Rty) R :- !, M is N - 1, |
| 101 |
R = {{ fun (A : lp:Sty) (eqA : A -> A -> bool) => lp:(Bo A eqA) }}, |
| 102 |
pi a f\ sigma ItyA\ |
| 103 |
coq.mk-app Ity [a] ItyA, |
| 104 |
(eq-db a a f :- !) => |
| 105 |
decl a `a` Sty => |
| 106 |
decl f `f` {{ lp:a -> lp:a -> bool }} => |
| 107 |
bo-param M ItyA (Rty a) (Bo a f). |
| 108 |
|
| 109 |
bo-param N Ity X R :- whd1 X X1, !, |
| 110 |
bo-param N Ity X1 R. |
| 111 |
|
| 112 |
func apply-idx |
| 113 |
term, % Ity applied to parameters |
| 114 |
term, % Arity remaining after parameters |
| 115 |
term % Bo comparison with potentially different indexes as per bo-idx |
| 116 |
-> term. % fun idx1 idx2 (x1 : Ity ixd1) (x2 : Ity idx2) => Bo idx1 x1 idx2 x2 |
| 117 |
apply-idx _ (sort _) Bo Bo :- !. % no indexes -> avoid eta expansion of Bo |
| 118 |
apply-idx Ity Arity Bo R :- |
| 119 |
apply-idx.aux Ity Arity Bo R. |
| 120 |
apply-idx.aux Ity (prod N S T) Bo (fun N S x\ R x) :- !, |
| 121 |
pi x\ apply-idx.aux {coq.mk-app Ity [x]} (T x) {coq.mk-app Bo [x,x]} (R x). |
| 122 |
apply-idx.aux Ity (sort _) Bo (fun `x1` Ity x1\ fun `x2` Ity x2\ Bo1 x1 x2) :- !, |
| 123 |
pi x1 x2\ coq.mk-app Bo [x1,x2] (Bo1 x1 x2). |
| 124 |
|
| 125 |
func apply-idx.aux term, term, term -> term. |
| 126 |
apply-idx.aux Ity X Bo R :- whd1 X X1, !, |
| 127 |
apply-idx.aux Ity X1 Bo R. |
| 128 |
|
| 129 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 130 |
% Builds a clause for eq-db that fits the inductive arity: |
| 131 |
% - a premise per type parameter |
| 132 |
% - a simple argument for each index |
| 133 |
|
| 134 |
% Also used to load the context under the fix |
| 135 |
:index (1) |
| 136 |
func quantify-eq-db-idx prop -> prop. |
| 137 |
quantify-eq-db-idx (pi x y\ C x y) (pi x y\ C1 x y) :- |
| 138 |
pi x y\ quantify-eq-db-idx (C x y) (C1 x y). |
| 139 |
quantify-eq-db-idx (eq-db A B C :- L) |
| 140 |
(pi x y\ eq-db (A1 x) (B1 y) (C1 x y) :- L) :- |
| 141 |
pi x y\ coq.mk-app A [x] (A1 x), coq.mk-app B [y] (B1 y), coq.mk-app C [x,y] (C1 x y). |
| 142 |
|
| 143 |
func quantify-eq-db-param prop -> prop. |
| 144 |
quantify-eq-db-param (pi x f\ C x f) (pi x f\ C1 x f) :- |
| 145 |
pi x f\ quantify-eq-db-param (C x f) (C1 x f). |
| 146 |
quantify-eq-db-param (eq-db A B C :- (L : list prop)) |
| 147 |
(pi x f\ eq-db (A1 x) (B1 x) (C1 x f) :- (L1 x f : list prop)) :- |
| 148 |
pi x f\ |
| 149 |
append [eq-db x x f] L (L1 x f), |
| 150 |
coq.mk-app A [x] (A1 x), |
| 151 |
coq.mk-app B [x] (B1 x), |
| 152 |
coq.mk-app C [x,f] (C1 x f). |
| 153 |
|
| 154 |
func mk-clause int, term, prop -> prop. |
| 155 |
mk-clause Lno (prod _ _ Tx) C R :- Lno > 0, !, |
| 156 |
Lno1 is Lno - 1, |
| 157 |
quantify-eq-db-param C C1, |
| 158 |
pi x\ mk-clause Lno1 (Tx x) C1 R. |
| 159 |
mk-clause 0 (prod _ _ Tx) C R :- !, |
| 160 |
quantify-eq-db-idx C C1, |
| 161 |
pi x\ mk-clause 0 (Tx x) C1 R. |
| 162 |
mk-clause 0 (sort _) C C :- !. |
| 163 |
mk-clause N T A B :- whd1 T T1, !, mk-clause N T1 A B. |
| 164 |
|
| 165 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 166 |
|
| 167 |
func main inductive, string -> list prop. |
| 168 |
main GR Name [Clause1, Clause2] :- do! [ |
| 169 |
coq.env.indt GR Ind Lno Luno Arity _ _, |
| 170 |
assert! (Ind = tt) "derive.eq: Coinductive types are not supported", |
| 171 |
assert! (Lno = Luno) "derive.eq: Non-uniform parameters not supported", |
| 172 |
|
| 173 |
% we build the comparison function |
| 174 |
bo-param Lno (global (indt GR)) Arity RSkel, |
| 175 |
std.assert-ok! (coq.elaborate-skeleton RSkel RT R) "derive.eq generates illtyped term", |
| 176 |
coq.env.add-const Name R RT _ Cmp, |
| 177 |
|
| 178 |
% we register it as a clause |
| 179 |
mk-clause Lno Arity (eq-db (global (indt GR)) (global (indt GR)) (global (const Cmp)) :- [!]) Clause1, |
| 180 |
coq.elpi.accumulate _ "derive.eq.db" (clause _ (before "eq-db:fail") Clause1), |
| 181 |
Clause2 = eq-for GR Cmp, |
| 182 |
coq.elpi.accumulate _ "derive.eq.db" (clause _ _ Clause2), |
| 183 |
]. |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
% vim: set spelllang=: |