/* Derive a function "isK t -> true" iif t is "K .." for K constructor */ /* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ shorten std.{assert!, last, forall, map}. namespace derive.isK { func ty term, list term, list term -> term. ty _ _ _ {{ bool }}. % if the branch of the match (corresponding to KParams) is the one % for K (the constructor we are generating the isK function for) we say % true, else we say false. :index (1) func branch term, term, term, list term, list term -> term. branch K1 KParams _ _ _ {{ true }} :- coq.safe-dest-app KParams K2 _, coq.env.global (indc GRK1) K1, coq.env.global (indc GRK2) K2, GRK1 = GRK2, !. branch _ _ _ _ _ {{ false }}. func body term, term, list term, list term -> term. body K _ Vars Tys R :- last Vars X, last Tys TX, coq.build-match X TX ty (branch K) R. func main-K string, term, term, constructor -> prop. main-K Prefix Ity Arity GRK Clause :- coq.env.global (indc GRK) K, coq.bind-ind-arity Ity Arity (body K) TSek, (@keepunivs! => std.assert-ok! (coq.elaborate-skeleton TSek Ty T) "derive.isK generates illtyped term"), Name is Prefix ^ {coq.gref->id (indc GRK)}, coq.env.add-const Name T Ty _ IsK, Clause = (isK-db GRK (global (const IsK)) :- !). func main inductive, string -> list prop. main GR Prefix Clauses :- coq.env.global (indt GR) T, coq.env.indt GR _ _ _ Arity Kn _, map Kn (main-K Prefix T Arity) Clauses, forall Clauses (c\ coq.elpi.accumulate _ "derive.isK.db" (clause _ (before "isK-db:fail") c)). } % vim: set spelllang=: