Nominal/NewParser.thy
changeset 2398 1e6160690546
parent 2397 c670a849af65
child 2399 107c06267f33
equal deleted inserted replaced
2397:c670a849af65 2398:1e6160690546
    10   we need to also export a cases-rule for nominal datatypes
    10   we need to also export a cases-rule for nominal datatypes
    11   size function
    11   size function
    12 *)
    12 *)
    13 
    13 
    14 section{* Interface for nominal_datatype *}
    14 section{* Interface for nominal_datatype *}
       
    15 
       
    16 ML {*
       
    17 (* attributes *)
       
    18 val eqvt_attrib = Attrib.internal (K Nominal_ThmDecls.eqvt_add)
       
    19 val rsp_attrib = Attrib.internal (K Quotient_Info.rsp_rules_add)
       
    20 
       
    21 *}
    15 
    22 
    16 
    23 
    17 ML {* 
    24 ML {* 
    18 (* nominal datatype parser *)
    25 (* nominal datatype parser *)
    19 local
    26 local
   303     then raw_nominal_decls dts bn_funs bn_eqs bclauses lthy
   310     then raw_nominal_decls dts bn_funs bn_eqs bclauses lthy
   304     else raise TEST lthy
   311     else raise TEST lthy
   305 
   312 
   306   val dtinfo = Datatype.the_info (ProofContext.theory_of lthy0) (hd raw_dt_names)
   313   val dtinfo = Datatype.the_info (ProofContext.theory_of lthy0) (hd raw_dt_names)
   307   val {descr, sorts, ...} = dtinfo
   314   val {descr, sorts, ...} = dtinfo
   308   val all_raw_tys = all_dtyps descr sorts
       
   309   val all_raw_ty_names = map (fn (_, (n, _, _)) => n) descr
       
   310   val all_raw_constrs = 
   315   val all_raw_constrs = 
   311     flat (map (map (fn (c, _, _, _) => c)) (all_dtyp_constrs_types descr sorts))
   316     flat (map (map (fn (c, _, _, _) => c)) (all_dtyp_constrs_types descr sorts))
   312 
   317   val all_raw_tys = all_dtyps descr sorts
       
   318   val all_raw_ty_names = map (fst o dest_Type) all_raw_tys
       
   319   
   313   val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy0)) all_raw_ty_names  
   320   val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy0)) all_raw_ty_names  
   314   val inject_thms = flat (map #inject dtinfos);
   321   val inject_thms = flat (map #inject dtinfos);
   315   val distinct_thms = flat (map #distinct dtinfos);
   322   val distinct_thms = flat (map #distinct dtinfos);
   316   val constr_thms = inject_thms @ distinct_thms
   323   val constr_thms = inject_thms @ distinct_thms
   317   val rel_dtinfos = List.take (dtinfos, (length dts)); 
   324   val rel_dtinfos = List.take (dtinfos, (length dts)); 
   327 
   334 
   328   (* definitions of raw permutations *)
   335   (* definitions of raw permutations *)
   329   val _ = warning "Definition of raw permutations";
   336   val _ = warning "Definition of raw permutations";
   330   val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2) =
   337   val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2) =
   331     if get_STEPS lthy0 > 1 
   338     if get_STEPS lthy0 > 1 
   332     then Local_Theory.theory_result (define_raw_perms descr sorts raw_induct_thm (length dts)) lthy0
   339     then Local_Theory.theory_result 
       
   340       (define_raw_perms all_raw_ty_names all_raw_tys all_raw_constrs raw_induct_thm) lthy0
   333     else raise TEST lthy0
   341     else raise TEST lthy0
       
   342   val lthy2a = Named_Target.reinit lthy2 lthy2
   334  
   343  
   335   (* noting the raw permutations as eqvt theorems *)
   344   (* noting the raw permutations as eqvt theorems *)
   336   val eqvt_attrib = Attrib.internal (K Nominal_ThmDecls.eqvt_add)
   345   val (_, lthy3) = Local_Theory.note ((Binding.empty, [eqvt_attrib]), raw_perm_simps) lthy2a
   337   val (_, lthy2a) = Local_Theory.note ((Binding.empty, [eqvt_attrib]), raw_perm_simps) lthy2
       
   338 
       
   339   val thy = Local_Theory.exit_global lthy2a;
       
   340   val thy_name = Context.theory_name thy
       
   341 
   346 
   342   (* definition of raw fv_functions *)
   347   (* definition of raw fv_functions *)
   343   val _ = warning "Definition of raw fv-functions";
   348   val _ = warning "Definition of raw fv-functions";
   344   val lthy3 = Named_Target.init NONE thy;
       
   345 
       
   346   val (raw_bns, raw_bn_eqs, raw_bn_info, raw_bn_induct, lthy3a) =
   349   val (raw_bns, raw_bn_eqs, raw_bn_info, raw_bn_induct, lthy3a) =
   347     if get_STEPS lthy3 > 2 
   350     if get_STEPS lthy3 > 2 
   348     then raw_bn_decls all_raw_ty_names raw_dts raw_bn_funs raw_bn_eqs constr_thms lthy3
   351     then raw_bn_decls all_raw_ty_names raw_dts raw_bn_funs raw_bn_eqs constr_thms lthy3
   349     else raise TEST lthy3
   352     else raise TEST lthy3
   350 
   353 
   380     if get_STEPS lthy > 6
   383     if get_STEPS lthy > 6
   381     then raw_prove_eqvt raw_bns raw_bn_induct (raw_bn_eqs @ raw_perm_simps) lthy4
   384     then raw_prove_eqvt raw_bns raw_bn_induct (raw_bn_eqs @ raw_perm_simps) lthy4
   382     else raise TEST lthy4
   385     else raise TEST lthy4
   383 
   386 
   384   (* noting the bn_eqvt lemmas in a temprorary theory *)
   387   (* noting the bn_eqvt lemmas in a temprorary theory *)
   385   val add_eqvt = Attrib.internal (K Nominal_ThmDecls.eqvt_add)
   388   val lthy_tmp = snd (Local_Theory.note ((Binding.empty, [eqvt_attrib]), bn_eqvt) lthy4)
   386   val lthy_tmp = snd (Local_Theory.note ((Binding.empty, [add_eqvt]), bn_eqvt) lthy4)
       
   387 
   389 
   388   val fv_eqvt = 
   390   val fv_eqvt = 
   389     if get_STEPS lthy > 7
   391     if get_STEPS lthy > 7
   390     then raw_prove_eqvt (raw_fvs @ raw_fv_bns) raw_fv_bns_induct (raw_fv_defs @ raw_perm_simps) 
   392     then raw_prove_eqvt (raw_fvs @ raw_fv_bns) raw_fv_bns_induct (raw_fv_defs @ raw_perm_simps) 
   391       (Local_Theory.restore lthy_tmp)
   393       (Local_Theory.restore lthy_tmp)
   397       (Local_Theory.restore lthy_tmp)
   399       (Local_Theory.restore lthy_tmp)
   398       |> map (rewrite_rule @{thms permute_nat_def[THEN eq_reflection]})
   400       |> map (rewrite_rule @{thms permute_nat_def[THEN eq_reflection]})
   399       |> map (fn thm => thm RS @{thm sym})
   401       |> map (fn thm => thm RS @{thm sym})
   400     else raise TEST lthy4
   402     else raise TEST lthy4
   401  
   403  
   402   val lthy5 = snd (Local_Theory.note ((Binding.empty, [add_eqvt]), fv_eqvt) lthy_tmp)
   404   val lthy5 = snd (Local_Theory.note ((Binding.empty, [eqvt_attrib]), fv_eqvt) lthy_tmp)
   403 
   405 
   404   val (alpha_eqvt, lthy6) =
   406   val (alpha_eqvt, lthy6) =
   405     if get_STEPS lthy > 9
   407     if get_STEPS lthy > 9
   406     then Nominal_Eqvt.equivariance true (alpha_trms @ alpha_bn_trms) alpha_induct alpha_intros lthy5
   408     then Nominal_Eqvt.equivariance true (alpha_trms @ alpha_bn_trms) alpha_induct alpha_intros lthy5
   407     else raise TEST lthy4
   409     else raise TEST lthy4
   450   val raw_constrs_rsp = raw_constrs_rsp all_raw_constrs alpha_trms alpha_intros
   452   val raw_constrs_rsp = raw_constrs_rsp all_raw_constrs alpha_trms alpha_intros
   451     (alpha_bn_imp_thms @ raw_funs_rsp_aux) lthy6 
   453     (alpha_bn_imp_thms @ raw_funs_rsp_aux) lthy6 
   452 
   454 
   453   val alpha_permute_rsp = map mk_alpha_permute_rsp alpha_eqvt
   455   val alpha_permute_rsp = map mk_alpha_permute_rsp alpha_eqvt
   454 
   456 
       
   457   (* noting the quot_respects lemmas *)
       
   458   val (_, lthy6a) = 
       
   459     if get_STEPS lthy > 15
       
   460     then Local_Theory.note ((Binding.empty, [rsp_attrib]),
       
   461       raw_constrs_rsp @ raw_funs_rsp @ raw_size_rsp @ alpha_permute_rsp) lthy6
       
   462     else raise TEST lthy6
       
   463 
   455   (* defining the quotient type *)
   464   (* defining the quotient type *)
   456   val _ = warning "Declaring the quotient types"
   465   val _ = warning "Declaring the quotient types"
   457   val qty_descr = map (fn (vs, bind, mx, _) => (vs, bind, mx)) dts
   466   val qty_descr = map (fn (vs, bind, mx, _) => (vs, bind, mx)) dts
   458   val qty_binds = map (fn (_, bind, _, _) => bind) dts        
   467   val qty_binds = map (fn (_, bind, _, _) => bind) dts        
   459   val qty_names = map Name.of_binding qty_binds;              
   468   val qty_names = map Name.of_binding qty_binds;              
   460   val qty_full_names = map (Long_Name.qualify thy_name) qty_names (* not used *)
       
   461   
   469   
   462   val (qty_infos, lthy7) = 
   470   val (qty_infos, lthy7) = 
   463     if get_STEPS lthy > 15
   471     if get_STEPS lthy > 16
   464     then qtype_defs qty_descr alpha_tys alpha_trms alpha_equivp_thms lthy6
   472     then qtype_defs qty_descr alpha_tys alpha_trms alpha_equivp_thms lthy6a
   465     else raise TEST lthy6
   473     else raise TEST lthy6a
   466 
   474 
   467   val qtys = map #qtyp qty_infos
   475   val qtys = map #qtyp qty_infos
   468   
   476   
   469   (* defining of quotient term-constructors, binding functions, free vars functions *)
   477   (* defining of quotient term-constructors, binding functions, free vars functions *)
   470   val _ = warning "Defining the quotient constants"
   478   val _ = warning "Defining the quotient constants"
   477 
   485 
   478   val qfvs_descr = 
   486   val qfvs_descr = 
   479     map2 (fn n => fn t => ("fv_" ^ n, t, NoSyn)) qty_names raw_fvs
   487     map2 (fn n => fn t => ("fv_" ^ n, t, NoSyn)) qty_names raw_fvs
   480 
   488 
   481   val qfv_bns_descr = 
   489   val qfv_bns_descr = 
   482     map2 (fn (b, _, _) => fn t => ("fv_" ^ Name.of_binding b, t, NoSyn)) bn_funs  raw_fv_bns
   490     map2 (fn (b, _, _) => fn t => ("fv_" ^ Name.of_binding b, t, NoSyn)) bn_funs raw_fv_bns
   483 
   491 
   484   val qalpha_bns_descr = 
   492   val qalpha_bns_descr = 
   485     map2 (fn (b, _, _) => fn t => ("alpha_" ^ Name.of_binding b, t, NoSyn)) bn_funs  alpha_bn_trms
   493     map2 (fn (b, _, _) => fn t => ("alpha_" ^ Name.of_binding b, t, NoSyn)) bn_funs  alpha_bn_trms
   486 
   494 
       
   495   val qperm_descr =
       
   496     map2 (fn n => fn t => ("permute_" ^ n, t, NoSyn)) qty_names raw_perm_funs
       
   497 
   487   val (((((qconstrs_info, qbns_info), qfvs_info), qfv_bns_info), qalpha_bns_info), lthy8) = 
   498   val (((((qconstrs_info, qbns_info), qfvs_info), qfv_bns_info), qalpha_bns_info), lthy8) = 
   488     if get_STEPS lthy > 16
   499     if get_STEPS lthy > 17
   489     then 
   500     then 
   490       lthy7
   501       lthy7
   491       |> qconst_defs qtys qconstrs_descr 
   502       |> qconst_defs qtys qconstrs_descr 
   492       ||>> qconst_defs qtys qbns_descr 
   503       ||>> qconst_defs qtys qbns_descr 
   493       ||>> qconst_defs qtys qfvs_descr
   504       ||>> qconst_defs qtys qfvs_descr
   494       ||>> qconst_defs qtys qfv_bns_descr
   505       ||>> qconst_defs qtys qfv_bns_descr
   495       ||>> qconst_defs qtys qalpha_bns_descr
   506       ||>> qconst_defs qtys qalpha_bns_descr
   496     else raise TEST lthy7
   507     else raise TEST lthy7
   497 
   508 
       
   509   (*val _ = Local_Theory.theory_result 
       
   510     (qperm_defs qtys qty_full_names qperm_descr raw_perm_laws) lthy8*) 
       
   511 
       
   512 
   498   val qconstrs = map #qconst qconstrs_info
   513   val qconstrs = map #qconst qconstrs_info
   499   val qbns = map #qconst qbns_info
   514   val qbns = map #qconst qbns_info
   500   val qfvs = map #qconst qfvs_info
   515   val qfvs = map #qconst qfvs_info
   501   val qfv_bns = map #qconst qfv_bns_info
   516   val qfv_bns = map #qconst qfv_bns_info
   502   val qalpha_bns = map #qconst qalpha_bns_info
   517   val qalpha_bns = map #qconst qalpha_bns_info
   503 
   518 
       
   519   
   504   (* temporary theorem bindings *)
   520   (* temporary theorem bindings *)
   505 
   521 
   506   val (_, lthy8') = lthy8
   522   val (_, lthy8') = lthy8
   507      |> Local_Theory.note ((@{binding "distinct"}, []), alpha_distincts) 
   523      |> Local_Theory.note ((@{binding "distinct"}, []), alpha_distincts) 
   508      ||>> Local_Theory.note ((@{binding "eq_iff"}, []), alpha_eq_iff)
   524      ||>> Local_Theory.note ((@{binding "eq_iff"}, []), alpha_eq_iff)
   509      ||>> Local_Theory.note ((@{binding "eq_iff_simps"}, []), alpha_eq_iff_simps) 
   525      ||>> Local_Theory.note ((@{binding "eq_iff_simps"}, []), alpha_eq_iff_simps) 
   510      ||>> Local_Theory.note ((@{binding "fv_defs"}, []), raw_fv_defs) 
   526      ||>> Local_Theory.note ((@{binding "fv_defs"}, []), raw_fv_defs) 
   511      ||>> Local_Theory.note ((@{binding "perm_simps"}, []), raw_perm_simps) 
   527      ||>> Local_Theory.note ((@{binding "perm_simps"}, []), raw_perm_simps) 
   512      ||>> Local_Theory.note ((@{binding "perm_laws"}, []), raw_perm_laws) 
   528      ||>> Local_Theory.note ((@{binding "perm_laws"}, []), raw_perm_laws) 
   513      ||>> Local_Theory.note ((@{binding "alpha_bn_imps"}, []), alpha_bn_imp_thms)
   529      ||>> Local_Theory.note ((@{binding "alpha_bn_imps"}, []), alpha_bn_imp_thms)
   514      ||>> Local_Theory.note ((@{binding "funs_rsp"}, []), raw_funs_rsp)
       
   515      ||>> Local_Theory.note ((@{binding "size_rsp"}, []), raw_size_rsp)
       
   516      ||>> Local_Theory.note ((@{binding "constrs_rsp"}, []), raw_constrs_rsp)
       
   517      ||>> Local_Theory.note ((@{binding "permute_rsp"}, []), alpha_permute_rsp)
       
   518 
   530 
   519   val _ = 
   531   val _ = 
   520     if get_STEPS lthy > 17
   532     if get_STEPS lthy > 18
   521     then true else raise TEST lthy8'
   533     then true else raise TEST lthy8'
   522   
   534   
   523   (* old stuff *)
   535   (* old stuff *)
       
   536 
       
   537   val thy = ProofContext.theory_of lthy8'
       
   538   val thy_name = Context.theory_name thy  
       
   539   val qty_full_names = map (Long_Name.qualify thy_name) qty_names 
   524 
   540 
   525   val _ = warning "Proving respects";
   541   val _ = warning "Proving respects";
   526 
   542 
   527   val bn_nos = map (fn (_, i, _) => i) raw_bn_info;
   543   val bn_nos = map (fn (_, i, _) => i) raw_bn_info;
   528   val bns = raw_bns ~~ bn_nos;
   544   val bns = raw_bns ~~ bn_nos;
   576   val thy = Local_Theory.exit_global lthy12c;
   592   val thy = Local_Theory.exit_global lthy12c;
   577   val perm_names = map (fn x => "permute_" ^ x) qty_names
   593   val perm_names = map (fn x => "permute_" ^ x) qty_names
   578   val dd = map2 (fn x => fn y => (x, y, NoSyn)) perm_names raw_perm_funs
   594   val dd = map2 (fn x => fn y => (x, y, NoSyn)) perm_names raw_perm_funs
   579   (* use Local_Theory.theory_result *)
   595   (* use Local_Theory.theory_result *)
   580   val thy' = qperm_defs qtys qty_full_names dd raw_perm_laws thy;
   596   val thy' = qperm_defs qtys qty_full_names dd raw_perm_laws thy;
   581   val lthy13 = Named_Target.init NONE thy';
   597   val lthy13 = Named_Target.init "" thy';
   582   
   598   
   583   val q_name = space_implode "_" qty_names;
   599   val q_name = space_implode "_" qty_names;
   584   fun suffix_bind s = Binding.qualify true q_name (Binding.name s);
   600   fun suffix_bind s = Binding.qualify true q_name (Binding.name s);
   585   val _ = warning "Lifting induction";
   601   val _ = warning "Lifting induction";
   586   val constr_names = map (Long_Name.base_name o fst o dest_Const) qconstrs;
   602   val constr_names = map (Long_Name.base_name o fst o dest_Const) qconstrs;