/* Unary parametricity translation (Realizability) */ /* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ % Author: Cyril Cohen shorten std.{forall, forall2, do!, rev, map2, map, append}. :before "subst-fun:fail" coq.subst-fun XS T TXS :- !, coq.mk-app T XS TXS. % this is outside the namespace since the predicate is also the db-one reali (sort prop as P) (fun `s` P x\ prod `s1` x _\ P) :- !. reali (sort _) (fun `s` (sort (typ U)) x\ prod `s1` x _\ (sort (typ V))) :- !, coq.univ.new U, coq.univ.new V. reali (fun N T B) (fun N T x\ fun N1 (TRsubst x) xR\ BR x xR) :- !, do! [ coq.name-suffix `P` N N1, reali T TR, (pi x xR\ reali x xR => reali (B x) (BR x xR)), (TRsubst = x\ {coq.subst-fun [x] TR}) ]. reali (prod N T P as Prod) ProdR :- !, do! [ coq.name-suffix `P` N N1, reali T TR, (pi x xR\ reali x xR => reali (P x) (PR x xR)), ProdR = fun `f` Prod f\ prod N T x\ prod N1 {coq.subst-fun [x] TR} xR\ {coq.subst-fun [{coq.mk-app f [x]}] (PR x xR)} ]. reali (app [A|Bs]) ARBsR :- !, do! [ reali A AR, derive.param1.reali-args Bs BsR, coq.mk-app AR BsR ARBsR ]. reali (let N T V B) LetR :- !, std.do! [ coq.name-suffix `P` N N1, reali T TR, reali V VR, (pi x xR\ reali x xR => reali (B x) (BR x xR)), LetR = let N T V x\ let N1 {coq.mk-app TR [x]} VR xR\ BR x xR ]. reali (match T P Bs) MR :- !, do! [ reali T TR, derive.param1.reali-match P PRM, reali T TR => derive.param1.reali-map Bs BsR, MR = match TR (PRM (x\ match x P Bs)) BsR ]. reali (fix N Rno T F as Fix) FixR :- !, std.do! [ RnoR is 2 * Rno + 1, RnoR1 is RnoR + 1, reali T TR, (pi x xR\ reali x xR => reali (F x) (FR x xR)), (TRsubst = f\ {coq.subst-fun [f] TR}), (pi f xR\ FixBody f xR = let N (TRsubst (F f)) (FR f xR) fr\ {paramX.mk-trivial-match RnoR (TRsubst f) [] fr}), (pi f xR\ coq.mk-eta RnoR1 (TRsubst f) (FixBody f xR) (EtaFixBody f xR)), coq.name-suffix N 1 N1, FixR = (let N T Fix f\ fix N1 RnoR (TRsubst f) xR\ EtaFixBody f xR), ]. namespace derive.param1 { pred reali-args o:list term, o:list term. reali-args [] []. reali-args [X|Xs] [X,XR|XsR] :- do! [ reali X XR, reali-args Xs XsR ]. pred reali-map o:list term, o:list term. reali-map [] []. reali-map [X|Xs] [XR|XsR] :- do! [ reali X XR, reali-map Xs XsR ]. % helpers for match return type pred reali-match i:term, o:((term -> term) -> term). reali-match (fun N T B) PRM :- pi x\ not (B x = fun _ _ _), !, do! [ reali T TR, (pi x xR\ reali x xR => reali (B x) (BR x xR)), coq.name-suffix `P` N N1, (pi z z1\ PRM z = fun N T x\ fun N1 {coq.subst-fun [x] TR} xR\ {coq.mk-app (BR x xR) [z x]}) ]. reali-match (fun N T B) PRM :- do! [ reali T TR, (pi x xR\ (reali x xR ==> reali-match (B x) (BR x xR))), coq.name-suffix N 1 N1, (pi z \ PRM z = fun N T x\ fun N1 {coq.subst-fun [x] TR} xR\ BR x xR z) ]. % Storage: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% func reali-store string, % Name suffix for the type class term, % Term term -> . % Translation reali-store N X XR :- !, Nreali is "reali_" ^ N, Args = [_, _, X, XR], T1 = app [{{ lib:@param1.store_reali }}|Args], std.assert-ok! (coq.typecheck T1 T2) "reali-store: T1 illtyped", coq.ensure-fresh-global-id Nreali FNreali, coq.env.add-const FNreali T1 T2 _ GR, @global! => coq.TC.declare-instance (const GR) 0. pred reali-store-indc i:string, i:constructor, i:constructor. reali-store-indc Prefix K XR :- reali-store {calc (Prefix ^ {coq.gref->id (indc K)})} (global (indc K)) (global (indc XR)). kind raw-mindt-spec type. type raw-mindt-spec inductive -> id -> bool -> term -> list constructor -> list (pair constructor id) -> list term -> raw-mindt-spec. func build-raw-mutual-spec string, inductive -> raw-mindt-spec. build-raw-mutual-spec Prefix GR (raw-mindt-spec GR FNewName IsInd Ty KNames KNamesR KTypes) :- coq.env.indt GR IsInd _ _ Ty KNames KTypes, coq.gref->id (indt GR) ID, std.map KNames (prefix-indc Prefix) KNamesR, NewName is Prefix ^ ID, coq.ensure-fresh-global-id NewName FNewName. func translate-mutual-spec raw-mindt-spec, inductive -> coq.indt-spec. translate-mutual-spec (raw-mindt-spec GR FNewName IsInd Ty KNames KNamesR KTypes) NewGR (coq.indt-spec NewGR FNewName IsInd TyOut KNamesR KTypesR) :- Ind = global (indt GR), reali Ty TyR, coq.mk-app TyR [Ind] TyOut, map2 KNames KTypes (k\ ty\ r\ sigma tyr\ reali ty tyr, coq.mk-app tyr [global (indc k)] r) KTypesR. func main-mblock list raw-mindt-spec, list raw-mindt-spec, list inductive -> list prop. main-mblock All [] Acc Clauses :- !, rev Acc NewGRs, main-mblock-aux All NewGRs Clauses. main-mblock All [raw-mindt-spec GR _ _ _ _ _ _|Rest] Acc Clauses :- % a fresh name for each inductive pi newGR\ (reali (global (indt GR)) (global (indt newGR)) :- !) => main-mblock All Rest [newGR|Acc] Clauses. func reali-store-mutual-indt list raw-mindt-spec, list inductive -> . reali-store-mutual-indt [] []. reali-store-mutual-indt [raw-mindt-spec GR FNewName _ _ _ _ _|MoreInd] [RealGR|MoreGR] :- reali-store FNewName (global (indt GR)) (global (indt RealGR)), reali-store-mutual-indt MoreInd MoreGR. func reali-store-mutual-indcs string, list constructor, list constructor -> . reali-store-mutual-indcs _ [] []. reali-store-mutual-indcs Prefix [K|KS] [KR|KRS] :- N is Prefix ^ {coq.gref->id (indc K)}, reali-store N (global (indc K)) (global (indc KR)), reali-store-mutual-indcs Prefix KS KRS. func reali-store-mutual-indc list raw-mindt-spec, list inductive -> . reali-store-mutual-indc [] []. reali-store-mutual-indc [raw-mindt-spec _ FNewName _ _ KNames _ _|Raws] [RealGR|RealGRs] :- coq.env.indt RealGR _ _ _ _ RealKNames _, Prefix1 is FNewName ^ "_", reali-store-mutual-indcs Prefix1 KNames RealKNames, reali-store-mutual-indc Raws RealGRs. func build-and-accumulate-clauses list raw-mindt-spec, list inductive -> list prop. build-and-accumulate-clauses [] [] []. build-and-accumulate-clauses [raw-mindt-spec GR _ _ _ KNames _ _|Raws] [RealGR|RealGRs] Clauses :- coq.env.indt RealGR _ _ _ _ RealKNames _, map2 KNames RealKNames (a\ b\ r\ r = (reali (global (indc a)) (global (indc b)) :- !)) CK, CDone = reali-done (indt GR), CReali = (reali (global (indt GR)) (global (indt RealGR)) :- !), CRealiR = (realiR (global (indt GR)) (global (indt RealGR)) :- !), coq.elpi.accumulate _ "derive.param1.db" (clause _ _ CDone), coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "realiR:fail") CRealiR), std.forall [CReali|CK] (x\coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") x)), build-and-accumulate-clauses Raws RealGRs Rest, append[CDone, CReali, CRealiR | CK] Rest Clauses. func main-mblock-aux list raw-mindt-spec, list inductive -> list prop. main-mblock-aux Raws NewGRs Clauses :- std.do! [ map2 Raws NewGRs translate-mutual-spec Specs, coq.build-mindt-decl Specs 0 0 DeclR, std.assert-ok! (coq.typecheck-indt-decl DeclR) "derive.param1 generates illtyped mutual inductive", coq.env.add-indt DeclR RealGR, coq.env.indt-block RealGR _ _ _ RealGRs _ _ _, reali-store-mutual-indt Raws RealGRs, reali-store-mutual-indc Raws RealGRs, build-and-accumulate-clauses Raws RealGRs Clauses, ]. func prefix-indc string, constructor -> pair constructor id. prefix-indc Prefix K (pr K FNewName) :- coq.gref->id (indc K) Name, NewName is Prefix ^ Name, coq.ensure-fresh-global-id NewName FNewName. % toplevel predicates %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% :index (1) func dispatch gref, % input of the translation string % the name -> list prop. % the clause dispatch (const GR) Prefix Clauses :- !, do! [ Term = global (const GR), Name is Prefix ^ {coq.gref->id (const GR)}, std.assert! (coq.env.const GR (some V) Ty) "param1: cannot handle axioms", reali V VR, reali Ty TyR, coq.mk-app TyR [Term] TyRV, % coq.typecheck is needed to add universe constraints std.assert-ok! (coq.typecheck TyRV _) "param1: illtyped param type", % apparently calling the type checker with the expected type is weaker in this case std.assert-ok! (coq.typecheck VR VRTy) "param1: illtyped constant", std.assert-ok! (coq.unify-leq VRTy TyRV) "param1: constant does not have the right type", coq.ensure-fresh-global-id Name FName, coq.env.add-const FName VR TyRV _ TermR, reali-store Name Term (global (const TermR)), C1 = (reali Term (global (const TermR)) :- !), coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") C1), C2 = (realiR Term (global (const TermR)) :- !), coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "realiR:fail") C2), coq.elpi.accumulate _ "derive.param1.db" (clause _ _ (reali-done (const GR))), Clauses = [C1, C2, reali-done (const GR)] ]. dispatch (indt GR) Prefix Clauses :- coq.env.indt-block GR _ Lno Luno GRS _ _ _, std.length GRS N, N > 1, !, do! [ map GRS (build-raw-mutual-spec Prefix) Raws, std.assert! (Lno = Luno) "derive.param1: Non-uniform parameters not supported", main-mblock Raws Raws [] Clauses ]. dispatch (indt GR) Prefix Clauses :- !, do! [ Ind = global (indt GR), coq.env.indt GR _ _ Lno Ty Knames Ktypes, LnoR is 2 * Lno, pi new_name\ sigma KnamesR KtypesR TyR\ ( (reali Ind (global (indt new_name)) ==> reali Ty TyR, map2 Knames Ktypes (k\ ty\ r\ sigma tyr\ reali ty tyr, coq.subst-fun [global (indc k)] tyr r) KtypesR), map Knames (prefix-indc Prefix) KnamesR, NewName is Prefix ^ {coq.gref->id (indt GR)}, coq.ensure-fresh-global-id NewName FNewName, coq.build-indt-decl (coq.indt-spec new_name FNewName tt {coq.subst-fun [Ind] TyR} KnamesR KtypesR) LnoR LnoR DeclR ), std.assert-ok! (coq.typecheck-indt-decl DeclR) "derive.param1 generates illtyped inductive", coq.env.add-indt DeclR GRR, reali-store NewName Ind (global (indt GRR)), coq.env.indt GRR _ _ _ _ RealNamesR _, Prefix1 is NewName ^ "_", forall2 Knames RealNamesR (reali-store-indc Prefix1), C1 = (reali Ind (global (indt GRR)) :- !), coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") C1), C2 = (realiR Ind (global (indt GRR)) :- !), coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "realiR:fail") C2), map2 Knames RealNamesR (a\ b\ r\ r = (reali (global (indc a)) (global (indc b)) :- !)) CK, forall CK (c\ coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") c)), coq.elpi.accumulate _ "derive.param1.db" (clause _ _ (reali-done (indt GR))), Clauses = [reali-done (indt GR), C1,C2|CK] ]. dispatch (indc _) _ _ :- coq.error "derive.param1: cannot translate a constructor". func main gref, string -> list prop. main T _ Clauses :- dispatch T "is_" Clauses. }