| 1 |
/* Derive a function "isK t -> true" iif t is "K .." for K constructor */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{assert!, last, forall, map}. |
| 6 |
|
| 7 |
namespace derive.isK { |
| 8 |
|
| 9 |
func ty term, list term, list term -> term. |
| 10 |
ty _ _ _ {{ bool }}. |
| 11 |
|
| 12 |
% if the branch of the match (corresponding to KParams) is the one |
| 13 |
% for K (the constructor we are generating the isK function for) we say |
| 14 |
% true, else we say false. |
| 15 |
:index (1) |
| 16 |
func branch term, term, term, list term, list term -> term. |
| 17 |
branch K1 KParams _ _ _ {{ true }} :- |
| 18 |
coq.safe-dest-app KParams K2 _, |
| 19 |
coq.env.global (indc GRK1) K1, |
| 20 |
coq.env.global (indc GRK2) K2, |
| 21 |
GRK1 = GRK2, |
| 22 |
!. |
| 23 |
branch _ _ _ _ _ {{ false }}. |
| 24 |
|
| 25 |
func body term, term, list term, list term -> term. |
| 26 |
body K _ Vars Tys R :- |
| 27 |
last Vars X, |
| 28 |
last Tys TX, |
| 29 |
coq.build-match X TX ty (branch K) R. |
| 30 |
|
| 31 |
func main-K string, term, term, constructor -> prop. |
| 32 |
main-K Prefix Ity Arity GRK Clause :- |
| 33 |
coq.env.global (indc GRK) K, |
| 34 |
coq.bind-ind-arity Ity Arity (body K) TSek, |
| 35 |
(@keepunivs! => std.assert-ok! (coq.elaborate-skeleton TSek Ty T) "derive.isK generates illtyped term"), |
| 36 |
Name is Prefix ^ {coq.gref->id (indc GRK)}, |
| 37 |
coq.env.add-const Name T Ty _ IsK, |
| 38 |
Clause = (isK-db GRK (global (const IsK)) :- !). |
| 39 |
|
| 40 |
func main inductive, string -> list prop. |
| 41 |
main GR Prefix Clauses :- |
| 42 |
coq.env.global (indt GR) T, |
| 43 |
coq.env.indt GR _ _ _ Arity Kn _, |
| 44 |
map Kn (main-K Prefix T Arity) Clauses, |
| 45 |
forall Clauses (c\ coq.elpi.accumulate _ "derive.isK.db" (clause _ (before "isK-db:fail") c)). |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
% vim: set spelllang=: |