| 1 |
/* eq.axiom for each constructor */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{assert!, do!, last, map2}. |
| 6 |
|
| 7 |
namespace derive.eqK { |
| 8 |
|
| 9 |
% links a term x, a comparison cmp, and H : eq_axiom cmp x |
| 10 |
func axiom-db term -> term, term. |
| 11 |
|
| 12 |
|
| 13 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 14 |
% case split on the second constructor |
| 15 |
|
| 16 |
% same constructor, we use the bcongr lemma |
| 17 |
func branch term, term, term, list term, list term -> term. |
| 18 |
branch K1 K2 _ V _ R :- |
| 19 |
coq.safe-dest-app K1 (global (indc GR)) Args1, |
| 20 |
coq.safe-dest-app K2 (global (indc GR)) LArgs2, !, do! [ |
| 21 |
bcongr-db GR Lem, |
| 22 |
call Lem Args1 LArgs2 V R, |
| 23 |
]. |
| 24 |
|
| 25 |
% different constructor, ReflectF + discriminate |
| 26 |
branch K1 K2 T V _ R :- coq.safe-dest-app K2 _ _, !, do! [ |
| 27 |
coq.mk-app K2 V K2A, |
| 28 |
Eq = {{ lib:@elpi.eq lp:T lp:K1 lp:K2A }}, |
| 29 |
R = {{ lib:@elpi.ReflectF lp:Eq (fun abs : lp:Eq => lp:(Bo abs)) }}, |
| 30 |
(pi abs\ ltac.discriminate abs Eq {{ lib:elpi.False }} (Bo abs)), |
| 31 |
]. |
| 32 |
|
| 33 |
func call term, list term, list term, list term -> term. |
| 34 |
call X [] [] [] X. |
| 35 |
call X [P|P1] [_|Q1] V R :- |
| 36 |
coq.mk-app X [P] XP, call XP P1 Q1 V R. |
| 37 |
call X [A|AS] [] [B|BS] R :- |
| 38 |
if (axiom-db A F P) true (M is "derive.eqK: no proved comparison for " ^ {coq.term->string A}, stop M), |
| 39 |
coq.mk-app X [A,B,{coq.mk-app F [A,B]},{coq.mk-app P [B]}] XAB, |
| 40 |
call XAB AS [] BS R. |
| 41 |
|
| 42 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 43 |
% bind the arguments of the constructor |
| 44 |
func args term, term -> term. |
| 45 |
args (prod _ S T) K R :- !, |
| 46 |
std.assert! (eq-db S S F) "derive.eqK: cannot find an eq test for constructor argument", |
| 47 |
R = (fun `x` S x\ |
| 48 |
fun `h` {{ lib:elpi.derive.eq_axiom_at lp:S lp:F lp:x }} (Bo x)), |
| 49 |
@pi-decl `x` S x\ |
| 50 |
@pi-decl `h` {{ lib:elpi.derive.eq_axiom_at lp:S lp:F lp:x }} h\ |
| 51 |
axiom-db x F h => |
| 52 |
args (T x) {coq.mk-app K [x]} (Bo x h). |
| 53 |
|
| 54 |
args T K (fun `x` T R) :- (T = global (indt _) ; T = app[global (indt _)|_]), !, |
| 55 |
eq-db T T Cmp, |
| 56 |
@pi-decl `x` T x\ |
| 57 |
coq.build-match x T (oty Cmp K) (branch K) (R x). |
| 58 |
|
| 59 |
args T K R :- whd1 T T1, !, args T1 K R. |
| 60 |
|
| 61 |
args T _ _ :- |
| 62 |
M is "derive.eqK: cannot find an eq test for " ^ {coq.term->string T}, |
| 63 |
stop M. |
| 64 |
|
| 65 |
func oty term, term, term, list term, list term -> term. |
| 66 |
oty Cmp K _ V VT R :- |
| 67 |
last V X, |
| 68 |
last VT T, |
| 69 |
R = {{ lib:elpi.derive.eq_axiom_on lp:T lp:Cmp lp:K lp:X }}. |
| 70 |
|
| 71 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 |
% For each parameter take in input a comparison function |
| 73 |
|
| 74 |
func param int, term, term -> term. |
| 75 |
|
| 76 |
param 0 T K R :- !, args T K R. |
| 77 |
|
| 78 |
param L (prod N S T) K R :- L > 0, !, |
| 79 |
R = (fun N S a\ |
| 80 |
fun `f` {{ lp:a -> lp:a -> bool }} (Bo a)), |
| 81 |
M is L - 1, |
| 82 |
@pi-decl N S a\ |
| 83 |
@pi-decl `f` {{ lp:a -> lp:a -> bool }} f\ |
| 84 |
(eq-db a a f :- !) => |
| 85 |
param M (T a) {coq.mk-app K [a]} (Bo a f). |
| 86 |
|
| 87 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 88 |
% for each constructor we generate the case split lemma |
| 89 |
|
| 90 |
func main-constructor int, string, constructor, term -> prop. |
| 91 |
main-constructor Lno Prefix K Kty C :- do! [ |
| 92 |
assert! (bcongr-db K _) "derive.eqK: run derive.bcongr first", |
| 93 |
|
| 94 |
% generate the K-split lemma |
| 95 |
param Lno Kty (global (indc K)) RSkel, |
| 96 |
|
| 97 |
% add to the environment |
| 98 |
std.assert-ok! (coq.elaborate-skeleton RSkel RT R) "derive.eqK generates illtyped term", |
| 99 |
coq.gref->id (indc K) Kname, |
| 100 |
Name is Prefix ^ Kname, |
| 101 |
coq.env.add-const Name R RT @opaque! EqK, |
| 102 |
|
| 103 |
% add the clause to the db |
| 104 |
C = (eqK-db K (global (const EqK)) :- !), |
| 105 |
coq.elpi.accumulate _ "derive.eqK.db" (clause _ (before "eqK-db:fail") C) |
| 106 |
]. |
| 107 |
|
| 108 |
func main inductive, string -> list prop. |
| 109 |
main GR Prefix Clauses :- |
| 110 |
|
| 111 |
coq.env.indt GR _ Lno _ _ Kns Ktys, |
| 112 |
|
| 113 |
map2 Kns Ktys (main-constructor Lno Prefix) Clauses. |
| 114 |
|
| 115 |
} |
| 116 |
|