Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file app_encode.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259(* This file is free software, part of Zipperposition. See file "license" for more details. *)(** {1 Applicative Encoding} *)openLogtkopenLibzipperpositionletsection=Util.Section.make~parent:Const.section"app_encode"letmode_:[`None|`Extensional|`Intensional]ref=ref`NonemoduleT=TypedSTerm(* Replacement for the function type in the encoding: *)letid_ae_fun=ID.make"app_encode_fun"letty_ae_fun=T.app_builtin~ty:T.tTypeBuiltin.Arrow[T.tType;T.tType;T.tType]letfunction_type=T.const~ty:ty_ae_funid_ae_fun(* Replacement for application in the encoding: *)letid_ae_app=ID.make"app_encode_app"letty_ae_app=letalpha=Var.make~ty:T.tType(ID.make"alpha")inletbeta=Var.make~ty:T.tType(ID.make"beta")inT.bind~ty:T.tTypeBinder.ForallTyalpha(T.bind~ty:T.tTypeBinder.ForallTybeta(T.app_builtin~ty:T.tTypeBuiltin.Arrow[T.varbeta;T.app~ty:T.tType(function_type)[T.varalpha;T.varbeta];T.varalpha]))letconst_ae_app=T.const~ty:ty_ae_appid_ae_app(* skolem for the extensionality axiom: *)letid_ext_diff=ID.make"app_encode_ext_diff"letty_ext_diff=letalpha=Var.make~ty:T.tType(ID.make"alpha")inletbeta=Var.make~ty:T.tType(ID.make"beta")inT.bind~ty:T.tTypeBinder.ForallTyalpha(T.bind~ty:T.tTypeBinder.ForallTybeta(T.app_builtin~ty:T.tTypeBuiltin.Arrow[T.varalpha;T.app~ty:T.tType(function_type)[T.varalpha;T.varbeta];T.app~ty:T.tType(function_type)[T.varalpha;T.varbeta]]))letconst_ext_diff=T.const~ty:ty_ext_diffid_ext_diff(** Type declarations for these new symbols *)letty_decls=letae_fun_decl=Statement.ty_decl~proof:Proof.Step.trivialid_ae_funty_ae_funinletae_app_decl=Statement.ty_decl~proof:Proof.Step.trivialid_ae_appty_ae_appinIter.of_list[ae_fun_decl;ae_app_decl]letty_decl_ext_diff=Statement.ty_decl~proof:Proof.Step.trivialid_ext_diffty_ext_diff(** Encode a type *)letrecapp_encode_tyty=letty'=matchT.viewtywith|T.App(f,args)->assert(not(T.equalffunction_type));T.app~ty:T.tType(app_encode_tyf)(CCList.mapapp_encode_tyargs)|T.AppBuiltin(Builtin.Arrow,ret::args)when(not(T.Ty.is_tTyperet))->letret_ty=CCList.fold_right(funargt->T.app~ty:T.tTypefunction_type[app_encode_tyarg;t])args(app_encode_tyret)inret_ty|T.AppBuiltin(f,args)->T.app_builtin~ty:T.tTypef(CCList.mapapp_encode_tyargs)|T.Const_->ty|T.Var_->ty|T.Meta_->failwith"Not implemented: Meta"|T.Bind(b,v,t)->T.bind~ty:T.tTypebv(app_encode_tyt)|T.Ite(_,_,_)->failwith"Not implemented: Ite"|T.Let_->failwith"Not implemented: Let"|T.Match(_,_)->failwith"Not implemented: Match"|T.Multiset_->failwith"Not implemented: Multiset"|T.Record(_,_)->failwith"Not implemented: Record"inUtil.debugf~section5"Encoded type @[%a@] into @[%a@]"(funk->kT.pptyT.ppty');ty'(** Is a term a type? i.e. is a term of type tType? *)letis_typea=T.Ty.is_tType(T.ty_exna)(** Encode a variable *)letapp_encode_varvar=letty=app_encode_ty(Var.tyvar)in(Var.update_tyvar~f:(fun_->ty))(** Encode a term *)letrecapp_encode_termtoplevelt=iftoplevelthenUtil.debugf~section3"Encoding toplevel term @[%a@]"(funk->kT.ppt);letty=app_encode_ty(CCOpt.get_exn(T.tyt))inlett'=matchT.viewtwith|T.App(f,[])->app_encode_termfalsef|T.App(f,args)->Util.debugf~section5"Attempting to encode application: %a"(funk->kT.pp_with_tyt);CCList.fold_left(funtermarg->Util.debugf~section5"Encoding application of %a to %a"(funk->kT.pp_with_tytermT.pparg);matchT.view(T.ty_exnterm)with|T.App(f,types)->assert(T.equalffunction_type);assert(List.lengthtypes==2);assert(not(is_typearg));letarg'=app_encode_termfalsearginT.app~ty:(List.nthtypes1)const_ae_app[CCOpt.get_exn(T.tyarg');List.nthtypes1;term;arg']|T.Bind(Binder.ForallTy,var,t)->assert(is_typearg);letarg'=app_encode_tyarginlett'=T.Subst.eval_nonrec(Var.Subst.singletonvararg')tinT.app~ty:t'term[arg']|T.AppBuiltin(Builtin.Arrow,ret_ty::arg_tys)->(* mandatory arguments *)letarg'=app_encode_termfalsearginletty'=beginmatcharg_tyswith|[]->assertfalse|_::[]->ret_ty|_::arg_tys_head::arg_tys_tail->T.app_builtin~ty:T.tTypeBuiltin.Arrow(ret_ty::arg_tys_head::arg_tys_tail)endinT.app~ty:ty'term[arg']|_->failwith"Expected quantified or function type")(app_encode_termfalsef)args|T.AppBuiltin(f,ts)->Util.debugf~section5"AppBuiltin-Term: %a"(funk->kT.ppt);failwith"Not implemented: AppBuiltin"|T.Constc->T.const~tyc|T.Varv->T.var(app_encode_varv)|T.Bind(Binder.Forall,_,_)->failwith"Not implemented: Forall"|T.Bind(Binder.Exists,_,_)->failwith"Not implemented: Exist"|T.Bind(Binder.Lambda,_,_)->failwith"Not implemented: Lambda"|_->failwith"Not implemented: Other kind of term"inUtil.debugf~section5"Encoded term @[%a@] into @[%a@]"(funk->kT.pptT.ppt');t'(** Encode a literal *)letapp_encode_litlit=Util.debugf~section2"# Encoding Literal %a"(funk->k(SLiteral.ppT.pp)lit);SLiteral.map(app_encode_termtrue)lit(** Encode a clause *)letapp_encode_litslits=List.mapapp_encode_litlitsexceptionE_iof((T.tSLiteral.t)list,T.t,T.t)Statement.tletpp_inpp_fpp_tpp_ty=function|Output_format.O_zf->Statement.ZF.pppp_fpp_tpp_ty|Output_format.O_tptp->Statement.TPTP.pppp_fpp_tpp_ty|Output_format.O_normal->Statement.pppp_fpp_tpp_ty|Output_format.O_none->CCFormat.silentletpp_clause_ino=letpp_t=T.pp_inoinpp_in(Util.pp_list~sep:" ∨ "(SLiteral.pp_inopp_t))pp_tpp_toletres_tc=Proof.Result.make_tc~of_exn:(functionE_ic->Somec|_->None)~to_exn:(funi->E_ii)~compare:compare~pp_in:pp_clause_in~is_stmt:true~name:Statement.name~to_form:(fun~ctxst->letconv_c(c:(T.tSLiteral.t)list):_=c|>List.mapSLiteral.to_form|>T.Form.or_inStatement.Seq.formsst|>Iter.mapconv_c|>Iter.to_list|>T.Form.and_)()(** encode a statement *)letapp_encode_stmtstmt=letas_proof=Proof.S.mk(Statement.proof_stepstmt)(Proof.Result.makeres_tcstmt)inletproof=Proof.Step.esa~rule:(Proof.Rule.mk"app_encode")[as_proof|>Proof.Parent.from]inmatchStatement.viewstmtwith|Statement.Data_->failwith"Not implemented: Data"|Statement.Lemma_->failwith"Not implemented: Lemma"|Statement.Goallits->failwith"Not implemented: Goal"|Statement.Defdefs->letmap_single=Statement.map_def~form:app_encode_lits~term:(app_encode_termtrue)~ty:app_encode_tyinStatement.def~proof(List.mapmap_singledefs)|Statement.Rewritedef->letnew_def=Statement.map_def_rule~form:app_encode_lits~term:(app_encode_termtrue)~ty:app_encode_tydefinStatement.rewrite~proofnew_def|Statement.NegatedGoal(skolems,clauses)->letskolems=List.map(fun(id,ty)->(id,app_encode_tyty))skolemsinStatement.neg_goal~proof~skolems(List.mapapp_encode_litsclauses)|Statement.Assertlits->Statement.assert_~proof(app_encode_litslits)|Statement.TyDecl(id,ty)->Statement.ty_decl~proof:Proof.Step.trivialid(app_encode_tyty)letextensionality_axiom=letalpha=Var.make~ty:T.tType(ID.make"alpha")inletbeta=Var.make~ty:T.tType(ID.make"beta")inletfun_alpha_beta=T.app~ty:T.tType(function_type)[T.varalpha;T.varbeta]inletx=Var.make~ty:fun_alpha_beta(ID.make"x")inlety=Var.make~ty:fun_alpha_beta(ID.make"y")inletdiff=T.app~ty:(T.varalpha)const_ext_diff[T.varalpha;T.varbeta;T.varx;T.vary]inletxdiff=T.app~ty:(T.varbeta)const_ae_app[T.varalpha;T.varbeta;T.varx;diff]inletydiff=T.app~ty:(T.varbeta)const_ae_app[T.varalpha;T.varbeta;T.vary;diff]inStatement.assert_~proof:Proof.Step.trivial[SLiteral.neqxdiffydiff;SLiteral.eq(T.varx)(T.vary)]letextension=letmodifierseq=if!mode_!=`Nonethen(Util.debug~section2"Start applicative encoding";(* Encode statements *)letseq=Iter.mapapp_encode_stmtseqin(* Add type declarations *)letseq=Iter.appendty_declsseqin(* Add extensionality axiom *)letseq=if!mode_=`ExtensionalthenIter.appendseq(Iter.of_list[ty_decl_ext_diff;extensionality_axiom])elseseqinUtil.debug~section2"Finished applicative encoding";seq)elseseqinExtensions.({defaultwithname="app_encode";post_cnf_modifiers=[modifier];})letoptions=["none",`None;"extensional",`Extensional;"intensional",`Intensional]let()=Options.add_opts["--app-encode",Arg.Symbol(List.mapfstoptions,funo->mode_:=List.assocooptions)," enable applicative encoding"]