Nominal/NewParser.thy
changeset 2034 837b889fcf59
parent 2027 68b2d2d7b4ed
child 2035 3622cae9b10e
equal deleted inserted replaced
2033:74bd7bfb484b 2034:837b889fcf59
   157 in
   157 in
   158   map (map (map rawify_bclause)) bclauses
   158   map (map (map rawify_bclause)) bclauses
   159 end
   159 end
   160 *}
   160 *}
   161 
   161 
   162 text {* What does the prep_bn code do? Cezary's Function? *}
   162 (* strip_bn_fun takes a bn function defined by the user as a union or
   163 
   163    append of elements and returns those elements together with bn functions
       
   164    applied *)
   164 ML {*
   165 ML {*
   165 fun strip_bn_fun t =
   166 fun strip_bn_fun t =
   166   case t of
   167   case t of
   167     Const (@{const_name sup}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
   168     Const (@{const_name sup}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
   168   | Const (@{const_name append}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
   169   | Const (@{const_name append}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
   256   ||>> pair raw_bclauses
   257   ||>> pair raw_bclauses
   257   ||>> pair raw_bns
   258   ||>> pair raw_bns
   258 end
   259 end
   259 *}
   260 *}
   260 
   261 
       
   262 lemma equivp_hack: "equivp x"
       
   263 sorry
       
   264 ML {*
       
   265 fun equivp_hack ctxt rel =
       
   266 let
       
   267   val thy = ProofContext.theory_of ctxt
       
   268   val ty = domain_type (fastype_of rel)
       
   269   val cty = ctyp_of thy ty
       
   270   val ct = cterm_of thy rel
       
   271 in
       
   272   Drule.instantiate' [SOME cty] [SOME ct] @{thm equivp_hack}
       
   273 end
       
   274 *}
       
   275 
       
   276 ML {* val cheat_alpha_eqvt = Unsynchronized.ref false *}
       
   277 ML {* val cheat_equivp = Unsynchronized.ref false *}
       
   278 ML {* val cheat_fv_rsp = Unsynchronized.ref false *}
       
   279 ML {* val cheat_alpha_bn_rsp = Unsynchronized.ref false *}
       
   280 ML {* val cheat_supp_eq = Unsynchronized.ref false *}
       
   281 
       
   282 ML {*
       
   283 fun remove_loop t =
       
   284   let val _ = HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of t)) in t end
       
   285   handle TERM _ => @{thm eqTrueI} OF [t]
       
   286 *}
   261 
   287 
   262 text {* 
   288 text {* 
   263   nominal_datatype2 does the following things in order:
   289   nominal_datatype2 does the following things in order:
   264 
   290 
   265 Parser.thy/raw_nominal_decls
   291 Parser.thy/raw_nominal_decls
   333     raw_nominal_decls dts bn_funs bn_eqs bclauses lthy
   359     raw_nominal_decls dts bn_funs bn_eqs bclauses lthy
   334 
   360 
   335   val dtinfo = Datatype.the_info (ProofContext.theory_of lthy1) (hd raw_dt_names);
   361   val dtinfo = Datatype.the_info (ProofContext.theory_of lthy1) (hd raw_dt_names);
   336   val {descr, sorts, ...} = dtinfo;
   362   val {descr, sorts, ...} = dtinfo;
   337   val raw_tys = map (fn (i, _) => nth_dtyp descr sorts i) descr;
   363   val raw_tys = map (fn (i, _) => nth_dtyp descr sorts i) descr;
   338   val all_typs = map (fn i => typ_of_dtyp descr sorts (DtRec i)) (map fst descr)
   364   val all_typs = map (fn i => Datatype_Aux.typ_of_dtyp descr sorts (Datatype_Aux.DtRec i)) (map fst descr)
   339 
   365 
   340   val all_full_tnames = map (fn (_, (n, _, _)) => n) descr;
   366   val all_full_tnames = map (fn (_, (n, _, _)) => n) descr;
   341   val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy1)) all_full_tnames;
   367   val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy1)) all_full_tnames;
   342   val inject = flat (map #inject dtinfos);
   368   val inject = flat (map #inject dtinfos);
   343   val distincts = flat (map #distinct dtinfos);
   369   val distincts = flat (map #distinct dtinfos);
   378   val rel_dists_bn = flat (map (distinct_rel lthy4 alpha_cases)
   404   val rel_dists_bn = flat (map (distinct_rel lthy4 alpha_cases)
   379     ((map (fn i => nth rel_distinct i) bn_nos) ~~ alpha_ts_bn))
   405     ((map (fn i => nth rel_distinct i) bn_nos) ~~ alpha_ts_bn))
   380   
   406   
   381   (* definition of raw_alpha_eq_iff  lemmas *)
   407   (* definition of raw_alpha_eq_iff  lemmas *)
   382   val alpha_eq_iff = build_rel_inj alpha_intros (inject @ distincts) alpha_cases lthy4
   408   val alpha_eq_iff = build_rel_inj alpha_intros (inject @ distincts) alpha_cases lthy4
   383   
   409   val alpha_eq_iff_simp = map remove_loop alpha_eq_iff;
       
   410   val _ = map tracing (map PolyML.makestring alpha_eq_iff_simp);
       
   411 
   384   (* proving equivariance lemmas *)
   412   (* proving equivariance lemmas *)
   385   val _ = warning "Proving equivariance";
   413   val _ = warning "Proving equivariance";
   386   val (bv_eqvt, lthy5) = prove_eqvt raw_tys induct (raw_bn_eqs @ raw_perm_def) (map fst bns) lthy4
   414   val (bv_eqvt, lthy5) = prove_eqvt raw_tys induct (raw_bn_eqs @ raw_perm_def) (map fst bns) lthy4
   387   val (fv_eqvt, lthy6) = prove_eqvt raw_tys induct (fv_def @ raw_perm_def) (fv @ fvbn) lthy5
   415   val (fv_eqvt, lthy6) = prove_eqvt raw_tys induct (fv_def @ raw_perm_def) (fv @ fvbn) lthy5
   388   fun alpha_eqvt_tac' _ = Skip_Proof.cheat_tac thy
   416   fun alpha_eqvt_tac' _ =
       
   417     if !cheat_alpha_eqvt then Skip_Proof.cheat_tac thy
       
   418     else alpha_eqvt_tac alpha_induct (raw_perm_def @ alpha_eq_iff_simp) lthy6 1
   389   val alpha_eqvt = build_alpha_eqvts alpha_ts alpha_eqvt_tac' lthy6;
   419   val alpha_eqvt = build_alpha_eqvts alpha_ts alpha_eqvt_tac' lthy6;
   390 
   420 
   391   (* provind alpha equivalence *)
   421   (* provind alpha equivalence *)
   392   val _ = warning "Proving equivalence";
   422   val _ = warning "Proving equivalence";
   393   val fv_alpha_all = combine_fv_alpha_bns (fv, fvbn) (alpha_ts_nobn, alpha_ts_bn) bn_nos;
   423   val fv_alpha_all = combine_fv_alpha_bns (fv, fvbn) (alpha_ts_nobn, alpha_ts_bn) bn_nos;
   394   val reflps = build_alpha_refl fv_alpha_all alpha_ts induct alpha_eq_iff lthy6;
   424   val reflps = build_alpha_refl fv_alpha_all alpha_ts induct alpha_eq_iff_simp lthy6;
   395   val alpha_equivp =
   425   val alpha_equivp =
   396     build_equivps alpha_ts reflps alpha_induct
   426     if !cheat_equivp then map (equivp_hack lthy6) alpha_ts
   397       inject alpha_eq_iff distincts alpha_cases alpha_eqvt lthy6;
   427     else build_equivps alpha_ts reflps alpha_induct
       
   428       inject alpha_eq_iff_simp distincts alpha_cases alpha_eqvt lthy6;
   398   val qty_binds = map (fn (_, b, _, _) => b) dts;
   429   val qty_binds = map (fn (_, b, _, _) => b) dts;
   399   val qty_names = map Name.of_binding qty_binds;
   430   val qty_names = map Name.of_binding qty_binds;
   400   val qty_full_names = map (Long_Name.qualify thy_name) qty_names
   431   val qty_full_names = map (Long_Name.qualify thy_name) qty_names
   401   val (qtys, lthy7) = define_quotient_types qty_binds all_typs alpha_ts_nobn alpha_equivp lthy6;
   432   val (qtys, lthy7) = define_quotient_types qty_binds all_typs alpha_ts_nobn alpha_equivp lthy6;
   402   val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts));
   433   val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts));
   403   val raw_consts =
   434   val raw_consts =
   404     flat (map (fn (i, (_, _, l)) =>
   435     flat (map (fn (i, (_, _, l)) =>
   405       map (fn (cname, dts) =>
   436       map (fn (cname, dts) =>
   406         Const (cname, map (typ_of_dtyp descr sorts) dts --->
   437         Const (cname, map (Datatype_Aux.typ_of_dtyp descr sorts) dts --->
   407           typ_of_dtyp descr sorts (DtRec i))) l) descr);
   438           Datatype_Aux.typ_of_dtyp descr sorts (Datatype_Aux.DtRec i))) l) descr);
   408   val (consts, const_defs, lthy8) = quotient_lift_consts_export qtys (const_names ~~ raw_consts) lthy7;
   439   val (consts, const_defs, lthy8) = quotient_lift_consts_export qtys (const_names ~~ raw_consts) lthy7;
   409   val _ = warning "Proving respects";
   440   val _ = warning "Proving respects";
   410   val bns_rsp_pre' = build_fvbv_rsps alpha_ts alpha_induct raw_bn_eqs (map fst bns) lthy8;
   441   val bns_rsp_pre' = build_fvbv_rsps alpha_ts alpha_induct raw_bn_eqs (map fst bns) lthy8;
   411   val (bns_rsp_pre, lthy9) = fold_map (
   442   val (bns_rsp_pre, lthy9) = fold_map (
   412     fn (bn_t, _) => prove_const_rsp qtys Binding.empty [bn_t] (fn _ =>
   443     fn (bn_t, _) => prove_const_rsp qtys Binding.empty [bn_t] (fn _ =>
   413        resolve_tac bns_rsp_pre' 1)) bns lthy8;
   444        resolve_tac bns_rsp_pre' 1)) bns lthy8;
   414   val bns_rsp = flat (map snd bns_rsp_pre);
   445   val bns_rsp = flat (map snd bns_rsp_pre);
   415   (*val _ = map tracing (map PolyML.makestring fv_alpha_all);*)
   446 
   416   fun fv_rsp_tac _ = Skip_Proof.cheat_tac thy
   447   fun fv_rsp_tac _ = if !cheat_fv_rsp then Skip_Proof.cheat_tac thy
       
   448     else fvbv_rsp_tac alpha_induct fv_def lthy8 1;
   417   val fv_rsps = prove_fv_rsp fv_alpha_all alpha_ts fv_rsp_tac lthy9;
   449   val fv_rsps = prove_fv_rsp fv_alpha_all alpha_ts fv_rsp_tac lthy9;
   418   val (fv_rsp_pre, lthy10) = fold_map
   450   val (fv_rsp_pre, lthy10) = fold_map
   419     (fn fv => fn ctxt => prove_const_rsp qtys Binding.empty [fv]
   451     (fn fv => fn ctxt => prove_const_rsp qtys Binding.empty [fv]
   420     (fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) (fv @ fvbn) lthy9;
   452     (fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) (fv @ fvbn) lthy9;
   421   val fv_rsp = flat (map snd fv_rsp_pre);
   453   val fv_rsp = flat (map snd fv_rsp_pre);
   422   val (perms_rsp, lthy11) = prove_const_rsp qtys Binding.empty perms
   454   val (perms_rsp, lthy11) = prove_const_rsp qtys Binding.empty perms
   423     (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
   455     (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
       
   456   fun alpha_bn_rsp_tac _ = if !cheat_alpha_bn_rsp then Skip_Proof.cheat_tac thy
       
   457     else
       
   458       let val alpha_bn_rsp_pre = prove_alpha_bn_rsp alpha_ts alpha_induct (alpha_eq_iff_simp @ rel_dists @ rel_dists_bn) alpha_equivp exhausts alpha_ts_bn lthy11 in asm_simp_tac (HOL_ss addsimps alpha_bn_rsp_pre) 1 end;
   424   val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
   459   val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
   425         (fn _ => Skip_Proof.cheat_tac thy)) alpha_ts_bn lthy11
   460     alpha_bn_rsp_tac) alpha_ts_bn lthy11
   426   fun const_rsp_tac _ =
   461   fun const_rsp_tac _ =
   427     let val alpha_alphabn = prove_alpha_alphabn alpha_ts alpha_induct alpha_eq_iff alpha_ts_bn lthy11a
   462     let val alpha_alphabn = prove_alpha_alphabn alpha_ts alpha_induct alpha_eq_iff_simp alpha_ts_bn lthy11a
   428       in constr_rsp_tac alpha_eq_iff (fv_rsp @ bns_rsp @ reflps @ alpha_alphabn) 1 end
   463       in constr_rsp_tac alpha_eq_iff_simp (fv_rsp @ bns_rsp @ reflps @ alpha_alphabn) 1 end
   429   val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
   464   val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
   430     const_rsp_tac) raw_consts lthy11a
   465     const_rsp_tac) raw_consts lthy11a
   431     val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) (fv @ fvbn)
   466     val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) (fv @ fvbn)
   432   val (qfv_ts, qfv_defs, lthy12a) = quotient_lift_consts_export qtys (qfv_names ~~ (fv @ fvbn)) lthy12;
   467   val (qfv_ts, qfv_defs, lthy12a) = quotient_lift_consts_export qtys (qfv_names ~~ (fv @ fvbn)) lthy12;
   433   val (qfv_ts_nobn, qfv_ts_bn) = chop (length perms) qfv_ts;
   468   val (qfv_ts_nobn, qfv_ts_bn) = chop (length perms) qfv_ts;
   461   val lthy16 = note_simp_suffix "fv" q_fv lthy15;
   496   val lthy16 = note_simp_suffix "fv" q_fv lthy15;
   462   val q_bn = map (lift_thm qtys lthy16) raw_bn_eqs;
   497   val q_bn = map (lift_thm qtys lthy16) raw_bn_eqs;
   463   val lthy17 = note_simp_suffix "bn" q_bn lthy16;
   498   val lthy17 = note_simp_suffix "bn" q_bn lthy16;
   464   val _ = warning "Lifting eq-iff";
   499   val _ = warning "Lifting eq-iff";
   465   (*val _ = map tracing (map PolyML.makestring alpha_eq_iff);*)
   500   (*val _ = map tracing (map PolyML.makestring alpha_eq_iff);*)
   466   val eq_iff_unfolded0 = map (Local_Defs.unfold lthy17 @{thms alphas3}) alpha_eq_iff
   501   val eq_iff_unfolded0 = map (Local_Defs.unfold lthy17 @{thms alphas3}) alpha_eq_iff_simp
   467   val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms alphas2}) eq_iff_unfolded0
   502   val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms alphas2}) eq_iff_unfolded0
   468   val eq_iff_unfolded2 = map (Local_Defs.unfold lthy17 @{thms alphas} ) eq_iff_unfolded1
   503   val eq_iff_unfolded2 = map (Local_Defs.unfold lthy17 @{thms alphas} ) eq_iff_unfolded1
   469   val q_eq_iff_pre0 = map (lift_thm qtys lthy17) eq_iff_unfolded2;
   504   val q_eq_iff_pre0 = map (lift_thm qtys lthy17) eq_iff_unfolded2;
   470   val q_eq_iff_pre1 = map (Local_Defs.fold lthy17 @{thms alphas3}) q_eq_iff_pre0
   505   val q_eq_iff_pre1 = map (Local_Defs.fold lthy17 @{thms alphas3}) q_eq_iff_pre0
   471   val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas2}) q_eq_iff_pre1
   506   val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas2}) q_eq_iff_pre1
   485   fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp))
   520   fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp))
   486   val lthy22 = Class.prove_instantiation_instance tac lthy21
   521   val lthy22 = Class.prove_instantiation_instance tac lthy21
   487   val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_ts_nobn, qalpha_ts_bn) bn_nos;
   522   val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_ts_nobn, qalpha_ts_bn) bn_nos;
   488   val (names, supp_eq_t) = supp_eq fv_alpha_all;
   523   val (names, supp_eq_t) = supp_eq fv_alpha_all;
   489   val _ = warning "Support Equations";
   524   val _ = warning "Support Equations";
   490   val q_supp = HOLogic.conj_elims (Goal.prove lthy22 names [] supp_eq_t (fn _ => supp_eq_tac q_induct q_fv q_perm q_eq_iff lthy22 1)) handle _ => [];
   525   fun supp_eq_tac' _ = if !cheat_supp_eq then Skip_Proof.cheat_tac thy else
       
   526     supp_eq_tac q_induct q_fv q_perm q_eq_iff lthy22 1;
       
   527   val q_supp = HOLogic.conj_elims (Goal.prove lthy22 names [] supp_eq_t supp_eq_tac') handle e =>
       
   528     let val _ = warning ("Support eqs failed") in [] end;
   491   val lthy23 = note_suffix "supp" q_supp lthy22;
   529   val lthy23 = note_suffix "supp" q_supp lthy22;
   492 in
   530 in
   493   (0, lthy23)
   531   (0, lthy23)
   494 end
   532 end
   495 *}
   533 *}
   658 
   696 
   659 val _ = OuterSyntax.local_theory "nominal_datatype" "test" OuterKeyword.thy_decl
   697 val _ = OuterSyntax.local_theory "nominal_datatype" "test" OuterKeyword.thy_decl
   660   (main_parser >> nominal_datatype2_cmd)
   698   (main_parser >> nominal_datatype2_cmd)
   661 *}
   699 *}
   662 
   700 
   663 atom_decl name
   701 (*atom_decl name
   664 
   702 
   665 nominal_datatype lam =
   703 nominal_datatype lam =
   666   Var name
   704   Var name
   667 | App lam lam
   705 | App lam lam
   668 | Lam x::name t::lam  bind_set x in t
   706 | Lam x::name t::lam  bind_set x in t
   738   Al xs::"name fset" t::"ty" bind_res xs in t
   776   Al xs::"name fset" t::"ty" bind_res xs in t
   739 
   777 
   740 thm ty_tys.fv[simplified ty_tys.supp]
   778 thm ty_tys.fv[simplified ty_tys.supp]
   741 thm ty_tys.eq_iff
   779 thm ty_tys.eq_iff
   742 
   780 
   743 
   781 *)
   744 
   782 
   745 (* some further tests *)
   783 (* some further tests *)
   746 
   784 
   747 (*
   785 (*
   748 nominal_datatype ty2 =
   786 nominal_datatype ty2 =