Nominal/nominal_dt_rawfuns.ML
changeset 2598 b136721eedb2
parent 2571 f0252365936c
child 2601 89c55d36980f
equal deleted inserted replaced
2597:0f289a52edbe 2598:b136721eedb2
     1 (*  Title:      nominal_dt_rawfuns.ML
     1 (*  Title:      nominal_dt_rawfuns.ML
     2     Author:     Cezary Kaliszyk
     2     Author:     Cezary Kaliszyk
     3     Author:     Christian Urban
     3     Author:     Christian Urban
     4 
     4 
     5   Definitions of the raw fv and fv_bn functions
     5   Definitions of the raw fv, fv_bn and permute functions.
     6 *)
     6 *)
     7 
     7 
     8 signature NOMINAL_DT_RAWFUNS =
     8 signature NOMINAL_DT_RAWFUNS =
     9 sig
     9 sig
    10   (* info of raw datatypes *)
    10   (* info of raw datatypes *)
    39 
    39 
    40   val define_raw_bn_perms: typ list -> bn_info list -> cns_info list -> thm list -> thm list -> 
    40   val define_raw_bn_perms: typ list -> bn_info list -> cns_info list -> thm list -> thm list -> 
    41     local_theory -> (term list * thm list * local_theory)
    41     local_theory -> (term list * thm list * local_theory)
    42  
    42  
    43   val raw_prove_eqvt: term list -> thm list -> thm list -> Proof.context -> thm list
    43   val raw_prove_eqvt: term list -> thm list -> thm list -> Proof.context -> thm list
       
    44 
       
    45   val define_raw_perms: string list -> typ list -> (string * sort) list -> term list -> thm -> 
       
    46     local_theory -> (term list * thm list * thm list) * local_theory
    44 end
    47 end
    45 
    48 
    46 
    49 
    47 structure Nominal_Dt_RawFuns: NOMINAL_DT_RAWFUNS =
    50 structure Nominal_Dt_RawFuns: NOMINAL_DT_RAWFUNS =
    48 struct
    51 struct
   365     val lhs = mk_perm pi (const $ arg)
   368     val lhs = mk_perm pi (const $ arg)
   366     val rhs = const $ (mk_perm pi arg)  
   369     val rhs = const $ (mk_perm pi arg)  
   367   in
   370   in
   368     HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   371     HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   369   end
   372   end
       
   373 
   370 
   374 
   371 fun raw_prove_eqvt consts ind_thms simps ctxt =
   375 fun raw_prove_eqvt consts ind_thms simps ctxt =
   372   if null consts then []
   376   if null consts then []
   373   else
   377   else
   374     let 
   378     let 
   388       Goal.prove_multi ctxt'' [] [] goals (fn {context, ...} => 
   392       Goal.prove_multi ctxt'' [] [] goals (fn {context, ...} => 
   389         prove_eqvt_tac insts ind_thms const_names simps context)
   393         prove_eqvt_tac insts ind_thms const_names simps context)
   390       |> ProofContext.export ctxt'' ctxt
   394       |> ProofContext.export ctxt'' ctxt
   391     end
   395     end
   392 
   396 
       
   397 
       
   398 
       
   399 (*** raw permutation functions ***)
       
   400 
       
   401 (** proves the two pt-type class properties **)
       
   402 
       
   403 fun prove_permute_zero induct perm_defs perm_fns lthy =
       
   404   let
       
   405     val perm_types = map (body_type o fastype_of) perm_fns
       
   406     val perm_indnames = Datatype_Prop.make_tnames perm_types
       
   407   
       
   408     fun single_goal ((perm_fn, T), x) =
       
   409       HOLogic.mk_eq (perm_fn $ @{term "0::perm"} $ Free (x, T), Free (x, T))
       
   410 
       
   411     val goals =
       
   412       HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
       
   413         (map single_goal (perm_fns ~~ perm_types ~~ perm_indnames)))
       
   414 
       
   415     val simps = HOL_basic_ss addsimps (@{thm permute_zero} :: perm_defs)
       
   416 
       
   417     val tac = (Datatype_Aux.indtac induct perm_indnames 
       
   418                THEN_ALL_NEW asm_simp_tac simps) 1
       
   419   in
       
   420     Goal.prove lthy perm_indnames [] goals (K tac)
       
   421     |> Datatype_Aux.split_conj_thm
       
   422   end
       
   423 
       
   424 
       
   425 fun prove_permute_plus induct perm_defs perm_fns lthy =
       
   426   let
       
   427     val p = Free ("p", @{typ perm})
       
   428     val q = Free ("q", @{typ perm})
       
   429     val perm_types = map (body_type o fastype_of) perm_fns
       
   430     val perm_indnames = Datatype_Prop.make_tnames perm_types
       
   431   
       
   432     fun single_goal ((perm_fn, T), x) = HOLogic.mk_eq 
       
   433       (perm_fn $ (mk_plus p q) $ Free (x, T), perm_fn $ p $ (perm_fn $ q $ Free (x, T)))
       
   434 
       
   435     val goals =
       
   436       HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
       
   437         (map single_goal (perm_fns ~~ perm_types ~~ perm_indnames)))
       
   438 
       
   439     val simps = HOL_basic_ss addsimps (@{thm permute_plus} :: perm_defs)
       
   440 
       
   441     val tac = (Datatype_Aux.indtac induct perm_indnames
       
   442                THEN_ALL_NEW asm_simp_tac simps) 1
       
   443   in
       
   444     Goal.prove lthy ("p" :: "q" :: perm_indnames) [] goals (K tac)
       
   445     |> Datatype_Aux.split_conj_thm 
       
   446   end
       
   447 
       
   448 
       
   449 fun mk_perm_eq ty_perm_assoc cnstr = 
       
   450   let
       
   451     fun lookup_perm p (ty, arg) = 
       
   452       case (AList.lookup (op=) ty_perm_assoc ty) of
       
   453         SOME perm => perm $ p $ arg
       
   454       | NONE => Const (@{const_name permute}, perm_ty ty) $ p $ arg
       
   455 
       
   456     val p = Free ("p", @{typ perm})
       
   457     val (arg_tys, ty) =
       
   458       fastype_of cnstr
       
   459       |> strip_type
       
   460 
       
   461     val arg_names = Name.variant_list ["p"] (Datatype_Prop.make_tnames arg_tys)
       
   462     val args = map Free (arg_names ~~ arg_tys)
       
   463 
       
   464     val lhs = lookup_perm p (ty, list_comb (cnstr, args))
       
   465     val rhs = list_comb (cnstr, map (lookup_perm p) (arg_tys ~~ args))
       
   466   
       
   467     val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))  
       
   468   in
       
   469     (Attrib.empty_binding, eq)
       
   470   end
       
   471 
       
   472 
       
   473 fun define_raw_perms full_ty_names tys tvs constrs induct_thm lthy =
       
   474   let
       
   475     val perm_fn_names = full_ty_names
       
   476       |> map Long_Name.base_name
       
   477       |> map (prefix "permute_")
       
   478 
       
   479     val perm_fn_types = map perm_ty tys
       
   480     val perm_fn_frees = map Free (perm_fn_names ~~ perm_fn_types)
       
   481     val perm_fn_binds = map (fn s => (Binding.name s, NONE, NoSyn)) perm_fn_names
       
   482 
       
   483     val perm_eqs = map (mk_perm_eq (tys ~~ perm_fn_frees)) constrs
       
   484 
       
   485     fun tac _ (_, _, simps) =
       
   486       Class.intro_classes_tac [] THEN ALLGOALS (resolve_tac simps)
       
   487   
       
   488     fun morphism phi (fvs, dfs, simps) =
       
   489       (map (Morphism.term phi) fvs, 
       
   490        map (Morphism.thm phi) dfs, 
       
   491        map (Morphism.thm phi) simps);
       
   492 
       
   493     val ((perm_funs, perm_eq_thms), lthy') =
       
   494       lthy
       
   495       |> Local_Theory.exit_global
       
   496       |> Class.instantiation (full_ty_names, tvs, @{sort pt}) 
       
   497       |> Primrec.add_primrec perm_fn_binds perm_eqs
       
   498     
       
   499     val perm_zero_thms = prove_permute_zero induct_thm perm_eq_thms perm_funs lthy'
       
   500     val perm_plus_thms = prove_permute_plus induct_thm perm_eq_thms perm_funs lthy'  
       
   501   in
       
   502     lthy'
       
   503     |> Class.prove_instantiation_exit_result morphism tac 
       
   504          (perm_funs, perm_eq_thms, perm_zero_thms @ perm_plus_thms)
       
   505     ||> Named_Target.theory_init
       
   506   end
       
   507 
       
   508 
   393 end (* structure *)
   509 end (* structure *)
   394 
   510