src/HOL/Nominal/nominal_atoms.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (23 months ago)
changeset 66695 91500c024c7f
parent 63352 4eaf35781b23
child 69597 ff784d5a5bfb
permissions -rw-r--r--
tuned;
     1 (*  Title:      HOL/Nominal/nominal_atoms.ML
     2     Author:     Christian Urban and Stefan Berghofer, TU Muenchen
     3 
     4 Declaration of atom types to be used in nominal datatypes.
     5 *)
     6 
     7 signature NOMINAL_ATOMS =
     8 sig
     9   val create_nom_typedecls : string list -> theory -> theory
    10   type atom_info
    11   val get_atom_infos : theory -> atom_info Symtab.table
    12   val get_atom_info : theory -> string -> atom_info option
    13   val the_atom_info : theory -> string -> atom_info
    14   val fs_class_of : theory -> string -> string
    15   val pt_class_of : theory -> string -> string
    16   val cp_class_of : theory -> string -> string -> string
    17   val at_inst_of : theory -> string -> thm
    18   val pt_inst_of : theory -> string -> thm
    19   val cp_inst_of : theory -> string -> string -> thm
    20   val dj_thm_of : theory -> string -> string -> thm
    21   val atoms_of : theory -> string list
    22   val mk_permT : typ -> typ
    23 end
    24 
    25 structure NominalAtoms : NOMINAL_ATOMS =
    26 struct
    27 
    28 val finite_emptyI = @{thm "finite.emptyI"};
    29 val Collect_const = @{thm "Collect_const"};
    30 
    31 val inductive_forall_def = @{thm HOL.induct_forall_def};
    32 
    33 
    34 (* theory data *)
    35 
    36 type atom_info =
    37   {pt_class : string,
    38    fs_class : string,
    39    cp_classes : string Symtab.table,
    40    at_inst : thm,
    41    pt_inst : thm,
    42    cp_inst : thm Symtab.table,
    43    dj_thms : thm Symtab.table};
    44 
    45 structure NominalData = Theory_Data
    46 (
    47   type T = atom_info Symtab.table;
    48   val empty = Symtab.empty;
    49   val extend = I;
    50   fun merge data = Symtab.merge (K true) data;
    51 );
    52 
    53 fun make_atom_info ((((((pt_class, fs_class), cp_classes), at_inst), pt_inst), cp_inst), dj_thms) =
    54   {pt_class = pt_class,
    55    fs_class = fs_class,
    56    cp_classes = cp_classes,
    57    at_inst = at_inst,
    58    pt_inst = pt_inst,
    59    cp_inst = cp_inst,
    60    dj_thms = dj_thms};
    61 
    62 val get_atom_infos = NominalData.get;
    63 val get_atom_info = Symtab.lookup o NominalData.get;
    64 
    65 fun gen_lookup lookup name = case lookup name of
    66     SOME info => info
    67   | NONE => error ("Unknown atom type " ^ quote name);
    68 
    69 fun the_atom_info thy = gen_lookup (get_atom_info thy);
    70 
    71 fun gen_lookup' f thy = the_atom_info thy #> f;
    72 fun gen_lookup'' f thy =
    73   gen_lookup' (f #> Symtab.lookup #> gen_lookup) thy;
    74 
    75 val fs_class_of = gen_lookup' #fs_class;
    76 val pt_class_of = gen_lookup' #pt_class;
    77 val at_inst_of = gen_lookup' #at_inst;
    78 val pt_inst_of = gen_lookup' #pt_inst;
    79 val cp_class_of = gen_lookup'' #cp_classes;
    80 val cp_inst_of = gen_lookup'' #cp_inst;
    81 val dj_thm_of = gen_lookup'' #dj_thms;
    82 
    83 fun atoms_of thy = map fst (Symtab.dest (NominalData.get thy));
    84 
    85 fun mk_permT T = HOLogic.listT (HOLogic.mk_prodT (T, T));
    86 
    87 fun mk_Cons x xs =
    88   let val T = fastype_of x
    89   in Const (@{const_name Cons}, T --> HOLogic.listT T --> HOLogic.listT T) $ x $ xs end;
    90 
    91 fun add_thms_string args = Global_Theory.add_thms ((map o apfst o apfst) Binding.name args);
    92 fun add_thmss_string args = Global_Theory.add_thmss ((map o apfst o apfst) Binding.name args);
    93 
    94 (* this function sets up all matters related to atom-  *)
    95 (* kinds; the user specifies a list of atom-kind names *)
    96 (* atom_decl <ak1> ... <akn>                           *)
    97 fun create_nom_typedecls ak_names thy =
    98   let
    99     
   100     val (_,thy1) = 
   101     fold_map (fn ak => fn thy => 
   102           let val dt = ((Binding.name ak, [], NoSyn), [(Binding.name ak, [@{typ nat}], NoSyn)])
   103               val (dt_names, thy1) = BNF_LFP_Compat.add_datatype [BNF_LFP_Compat.Kill_Type_Args] [dt] thy;
   104             
   105               val injects = maps (#inject o BNF_LFP_Compat.the_info thy1 []) dt_names;
   106               val ak_type = Type (Sign.intern_type thy1 ak,[])
   107               val ak_sign = Sign.intern_const thy1 ak 
   108               
   109               val inj_type = @{typ nat} --> ak_type
   110               val inj_on_type = inj_type --> @{typ "nat set"} --> @{typ bool}
   111 
   112               (* first statement *)
   113               val stmnt1 = HOLogic.mk_Trueprop 
   114                   (Const (@{const_name "inj_on"},inj_on_type) $ 
   115                          Const (ak_sign,inj_type) $ HOLogic.mk_UNIV @{typ nat})
   116 
   117               val simp1 = @{thm inj_on_def} :: injects;
   118               
   119               fun proof1 ctxt = EVERY [simp_tac (put_simpset HOL_basic_ss ctxt addsimps simp1) 1,
   120                 resolve_tac ctxt @{thms ballI} 1,
   121                 resolve_tac ctxt @{thms ballI} 1,
   122                 resolve_tac ctxt @{thms impI} 1,
   123                 assume_tac ctxt 1]
   124              
   125               val (inj_thm,thy2) = 
   126                    add_thms_string [((ak^"_inj",Goal.prove_global thy1 [] [] stmnt1 (proof1 o #context)), [])] thy1
   127               
   128               (* second statement *)
   129               val y = Free ("y",ak_type)  
   130               val stmnt2 = HOLogic.mk_Trueprop
   131                   (HOLogic.mk_exists ("x",@{typ nat},HOLogic.mk_eq (y,Const (ak_sign,inj_type) $ Bound 0)))
   132 
   133               val proof2 = fn {prems, context = ctxt} =>
   134                 Induct_Tacs.case_tac ctxt "y" [] NONE 1 THEN
   135                 asm_simp_tac (put_simpset HOL_basic_ss ctxt addsimps simp1) 1 THEN
   136                 resolve_tac ctxt @{thms exI} 1 THEN
   137                 resolve_tac ctxt @{thms refl} 1
   138 
   139               (* third statement *)
   140               val (inject_thm,thy3) =
   141                   add_thms_string [((ak^"_injection",Goal.prove_global thy2 [] [] stmnt2 proof2), [])] thy2
   142   
   143               val stmnt3 = HOLogic.mk_Trueprop
   144                            (HOLogic.mk_not
   145                               (Const (@{const_name finite}, HOLogic.mk_setT ak_type --> HOLogic.boolT) $
   146                                   HOLogic.mk_UNIV ak_type))
   147              
   148               val simp2 = [@{thm image_def},@{thm bex_UNIV}]@inject_thm
   149               val simp3 = [@{thm UNIV_def}]
   150 
   151               fun proof3 ctxt = EVERY [cut_facts_tac inj_thm 1,
   152                 dresolve_tac ctxt @{thms range_inj_infinite} 1,
   153                 asm_full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps simp2) 1,
   154                 simp_tac (put_simpset HOL_basic_ss ctxt addsimps simp3) 1]
   155            
   156               val (inf_thm,thy4) =  
   157                     add_thms_string [((ak^"_infinite",Goal.prove_global thy3 [] [] stmnt3 (proof3 o #context)), [])] thy3
   158           in 
   159             ((inj_thm,inject_thm,inf_thm),thy4)
   160           end) ak_names thy
   161 
   162     (* produces a list consisting of pairs:         *)
   163     (*  fst component is the atom-kind name         *)
   164     (*  snd component is its type                   *)
   165     val full_ak_names = map (Sign.intern_type thy1) ak_names;
   166     val ak_names_types = ak_names ~~ map (Type o rpair []) full_ak_names;
   167      
   168     (* declares a swapping function for every atom-kind, it is         *)
   169     (* const swap_<ak> :: <akT> * <akT> => <akT> => <akT>              *)
   170     (* swap_<ak> (a,b) c = (if a=c then b (else if b=c then a else c)) *)
   171     (* overloades then the general swap-function                       *) 
   172     val (swap_eqs, thy3) = fold_map (fn (ak_name, T) => fn thy =>
   173       let
   174         val thy' = Sign.add_path "rec" thy;
   175         val swapT = HOLogic.mk_prodT (T, T) --> T --> T;
   176         val swap_name = "swap_" ^ ak_name;
   177         val full_swap_name = Sign.full_bname thy' swap_name;
   178         val a = Free ("a", T);
   179         val b = Free ("b", T);
   180         val c = Free ("c", T);
   181         val ab = Free ("ab", HOLogic.mk_prodT (T, T))
   182         val cif = Const (@{const_name If}, HOLogic.boolT --> T --> T --> T);
   183         val cswap_akname = Const (full_swap_name, swapT);
   184         val cswap = Const (@{const_name Nominal.swap}, swapT)
   185 
   186         val name = swap_name ^ "_def";
   187         val def1 = HOLogic.mk_Trueprop (HOLogic.mk_eq
   188                 (Free (swap_name, swapT) $ HOLogic.mk_prod (a,b) $ c,
   189                     cif $ HOLogic.mk_eq (a,c) $ b $ (cif $ HOLogic.mk_eq (b,c) $ a $ c)))
   190         val def2 = Logic.mk_equals (cswap $ ab $ c, cswap_akname $ ab $ c)
   191       in
   192         thy' |>
   193         BNF_LFP_Compat.primrec_global
   194           [(Binding.name swap_name, SOME swapT, NoSyn)]
   195           [((Binding.empty_atts, def1), [], [])] ||>
   196         Sign.parent_path ||>>
   197         Global_Theory.add_defs_unchecked true
   198           [((Binding.name name, def2), [])] |>> (snd o fst)
   199       end) ak_names_types thy1;
   200     
   201     (* declares a permutation function for every atom-kind acting  *)
   202     (* on such atoms                                               *)
   203     (* const <ak>_prm_<ak> :: (<akT> * <akT>)list => akT => akT    *)
   204     (* <ak>_prm_<ak> []     a = a                                  *)
   205     (* <ak>_prm_<ak> (x#xs) a = swap_<ak> x (perm xs a)            *)
   206     val (prm_eqs, thy4) = fold_map (fn (ak_name, T) => fn thy =>
   207       let
   208         val thy' = Sign.add_path "rec" thy;
   209         val swapT = HOLogic.mk_prodT (T, T) --> T --> T;
   210         val swap_name = Sign.full_bname thy' ("swap_" ^ ak_name)
   211         val prmT = mk_permT T --> T --> T;
   212         val prm_name = ak_name ^ "_prm_" ^ ak_name;
   213         val prm = Free (prm_name, prmT);
   214         val x  = Free ("x", HOLogic.mk_prodT (T, T));
   215         val xs = Free ("xs", mk_permT T);
   216         val a  = Free ("a", T) ;
   217 
   218         val cnil  = Const (@{const_name Nil}, mk_permT T);
   219         
   220         val def1 = HOLogic.mk_Trueprop (HOLogic.mk_eq (prm $ cnil $ a, a));
   221 
   222         val def2 = HOLogic.mk_Trueprop (HOLogic.mk_eq
   223                    (prm $ mk_Cons x xs $ a,
   224                     Const (swap_name, swapT) $ x $ (prm $ xs $ a)));
   225       in
   226         thy' |>
   227         BNF_LFP_Compat.primrec_global
   228           [(Binding.name prm_name, SOME prmT, NoSyn)]
   229           (map (fn def => ((Binding.empty_atts, def), [], [])) [def1, def2]) ||>
   230         Sign.parent_path
   231       end) ak_names_types thy3;
   232     
   233     (* defines permutation functions for all combinations of atom-kinds; *)
   234     (* there are a trivial cases and non-trivial cases                   *)
   235     (* non-trivial case:                                                 *)
   236     (* <ak>_prm_<ak>_def:  perm pi a == <ak>_prm_<ak> pi a               *)
   237     (* trivial case with <ak> != <ak'>                                   *)
   238     (* <ak>_prm<ak'>_def[simp]:  perm pi a == a                          *)
   239     (*                                                                   *)
   240     (* the trivial cases are added to the simplifier, while the non-     *)
   241     (* have their own rules proved below                                 *)  
   242     val (perm_defs, thy5) = fold_map (fn (ak_name, T) => fn thy =>
   243       fold_map (fn (ak_name', T') => fn thy' =>
   244         let
   245           val perm_def_name = ak_name ^ "_prm_" ^ ak_name';
   246           val pi = Free ("pi", mk_permT T);
   247           val a  = Free ("a", T');
   248           val cperm = Const (@{const_name Nominal.perm}, mk_permT T --> T' --> T');
   249           val thy'' = Sign.add_path "rec" thy'
   250           val cperm_def = Const (Sign.full_bname thy'' perm_def_name, mk_permT T --> T' --> T');
   251           val thy''' = Sign.parent_path thy'';
   252 
   253           val name = ak_name ^ "_prm_" ^ ak_name' ^ "_def";
   254           val def = Logic.mk_equals
   255                     (cperm $ pi $ a, if ak_name = ak_name' then cperm_def $ pi $ a else a)
   256         in
   257           Global_Theory.add_defs_unchecked true [((Binding.name name, def), [])] thy'''
   258         end) ak_names_types thy) ak_names_types thy4;
   259     
   260     (* proves that every atom-kind is an instance of at *)
   261     (* lemma at_<ak>_inst:                              *)
   262     (* at TYPE(<ak>)                                    *)
   263     val (prm_cons_thms,thy6) = 
   264       thy5 |> add_thms_string (map (fn (ak_name, T) =>
   265       let
   266         val ak_name_qu = Sign.full_bname thy5 (ak_name);
   267         val i_type = Type(ak_name_qu,[]);
   268         val cat = Const (@{const_name Nominal.at}, Term.itselfT i_type --> HOLogic.boolT);
   269         val at_type = Logic.mk_type i_type;
   270         fun proof ctxt =
   271           simp_tac (put_simpset HOL_ss ctxt
   272             addsimps maps (Global_Theory.get_thms thy5)
   273                                   ["at_def",
   274                                    ak_name ^ "_prm_" ^ ak_name ^ "_def",
   275                                    ak_name ^ "_prm_" ^ ak_name ^ ".simps",
   276                                    "swap_" ^ ak_name ^ "_def",
   277                                    "swap_" ^ ak_name ^ ".simps",
   278                                    ak_name ^ "_infinite"]) 1;            
   279         val name = "at_"^ak_name^ "_inst";
   280         val statement = HOLogic.mk_Trueprop (cat $ at_type);
   281       in 
   282         ((name, Goal.prove_global thy5 [] [] statement (proof o #context)), [])
   283       end) ak_names_types);
   284 
   285     (* declares a perm-axclass for every atom-kind               *)
   286     (* axclass pt_<ak>                                           *)
   287     (* pt_<ak>1[simp]: perm [] x = x                             *)
   288     (* pt_<ak>2:       perm (pi1@pi2) x = perm pi1 (perm pi2 x)  *)
   289     (* pt_<ak>3:       pi1 ~ pi2 ==> perm pi1 x = perm pi2 x     *)
   290      val (pt_ax_classes,thy7) =  fold_map (fn (ak_name, T) => fn thy =>
   291       let 
   292           val cl_name = "pt_"^ak_name;
   293           val ty = TFree("'a", @{sort type});
   294           val x   = Free ("x", ty);
   295           val pi1 = Free ("pi1", mk_permT T);
   296           val pi2 = Free ("pi2", mk_permT T);
   297           val cperm = Const (@{const_name Nominal.perm}, mk_permT T --> ty --> ty);
   298           val cnil  = Const (@{const_name Nil}, mk_permT T);
   299           val cappend = Const (@{const_name append}, mk_permT T --> mk_permT T --> mk_permT T);
   300           val cprm_eq = Const (@{const_name Nominal.prm_eq}, mk_permT T --> mk_permT T --> HOLogic.boolT);
   301           (* nil axiom *)
   302           val axiom1 = HOLogic.mk_Trueprop (HOLogic.mk_eq 
   303                        (cperm $ cnil $ x, x));
   304           (* append axiom *)
   305           val axiom2 = HOLogic.mk_Trueprop (HOLogic.mk_eq
   306                        (cperm $ (cappend $ pi1 $ pi2) $ x, cperm $ pi1 $ (cperm $ pi2 $ x)));
   307           (* perm-eq axiom *)
   308           val axiom3 = Logic.mk_implies
   309                        (HOLogic.mk_Trueprop (cprm_eq $ pi1 $ pi2),
   310                         HOLogic.mk_Trueprop (HOLogic.mk_eq (cperm $ pi1 $ x, cperm $ pi2 $ x)));
   311       in
   312           Axclass.define_class (Binding.name cl_name, @{sort type}) []
   313                 [((Binding.name (cl_name ^ "1"), [Simplifier.simp_add]), [axiom1]),
   314                  ((Binding.name (cl_name ^ "2"), []), [axiom2]),                           
   315                  ((Binding.name (cl_name ^ "3"), []), [axiom3])] thy
   316       end) ak_names_types thy6;
   317 
   318     (* proves that every pt_<ak>-type together with <ak>-type *)
   319     (* instance of pt                                         *)
   320     (* lemma pt_<ak>_inst:                                    *)
   321     (* pt TYPE('x::pt_<ak>) TYPE(<ak>)                        *)
   322     val (prm_inst_thms,thy8) = 
   323       thy7 |> add_thms_string (map (fn (ak_name, T) =>
   324       let
   325         val ak_name_qu = Sign.full_bname thy7 ak_name;
   326         val pt_name_qu = Sign.full_bname thy7 ("pt_"^ak_name);
   327         val i_type1 = TFree("'x",[pt_name_qu]);
   328         val i_type2 = Type(ak_name_qu,[]);
   329         val cpt =
   330           Const (@{const_name Nominal.pt}, (Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT);
   331         val pt_type = Logic.mk_type i_type1;
   332         val at_type = Logic.mk_type i_type2;
   333         fun proof ctxt =
   334           simp_tac (put_simpset HOL_ss ctxt addsimps maps (Global_Theory.get_thms thy7)
   335                                   ["pt_def",
   336                                    "pt_" ^ ak_name ^ "1",
   337                                    "pt_" ^ ak_name ^ "2",
   338                                    "pt_" ^ ak_name ^ "3"]) 1;
   339 
   340         val name = "pt_"^ak_name^ "_inst";
   341         val statement = HOLogic.mk_Trueprop (cpt $ pt_type $ at_type);
   342       in 
   343         ((name, Goal.prove_global thy7 [] [] statement (proof o #context)), []) 
   344       end) ak_names_types);
   345 
   346      (* declares an fs-axclass for every atom-kind       *)
   347      (* axclass fs_<ak>                                  *)
   348      (* fs_<ak>1: finite ((supp x)::<ak> set)            *)
   349      val (fs_ax_classes,thy11) =  fold_map (fn (ak_name, T) => fn thy =>
   350        let 
   351           val cl_name = "fs_"^ak_name;
   352           val pt_name = Sign.full_bname thy ("pt_"^ak_name);
   353           val ty = TFree("'a",@{sort type});
   354           val x   = Free ("x", ty);
   355           val csupp    = Const (@{const_name Nominal.supp}, ty --> HOLogic.mk_setT T);
   356           val cfinite  = Const (@{const_name finite}, HOLogic.mk_setT T --> HOLogic.boolT)
   357           
   358           val axiom1   = HOLogic.mk_Trueprop (cfinite $ (csupp $ x));
   359 
   360        in  
   361         Axclass.define_class (Binding.name cl_name, [pt_name]) []
   362           [((Binding.name (cl_name ^ "1"), []), [axiom1])] thy
   363        end) ak_names_types thy8; 
   364          
   365      (* proves that every fs_<ak>-type together with <ak>-type   *)
   366      (* instance of fs-type                                      *)
   367      (* lemma abst_<ak>_inst:                                    *)
   368      (* fs TYPE('x::pt_<ak>) TYPE (<ak>)                         *)
   369      val (fs_inst_thms,thy12) = 
   370        thy11 |> add_thms_string (map (fn (ak_name, T) =>
   371        let
   372          val ak_name_qu = Sign.full_bname thy11 ak_name;
   373          val fs_name_qu = Sign.full_bname thy11 ("fs_"^ak_name);
   374          val i_type1 = TFree("'x",[fs_name_qu]);
   375          val i_type2 = Type(ak_name_qu,[]);
   376          val cfs = Const (@{const_name Nominal.fs},
   377                                  (Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT);
   378          val fs_type = Logic.mk_type i_type1;
   379          val at_type = Logic.mk_type i_type2;
   380          fun proof ctxt =
   381           simp_tac (put_simpset HOL_ss ctxt addsimps maps (Global_Theory.get_thms thy11)
   382                                    ["fs_def",
   383                                     "fs_" ^ ak_name ^ "1"]) 1;
   384     
   385          val name = "fs_"^ak_name^ "_inst";
   386          val statement = HOLogic.mk_Trueprop (cfs $ fs_type $ at_type);
   387        in 
   388          ((name, Goal.prove_global thy11 [] [] statement (proof o #context)), []) 
   389        end) ak_names_types);
   390 
   391        (* declares for every atom-kind combination an axclass            *)
   392        (* cp_<ak1>_<ak2> giving a composition property                   *)
   393        (* cp_<ak1>_<ak2>1: pi1 o pi2 o x = (pi1 o pi2) o (pi1 o x)       *)
   394         val (cp_ax_classes,thy12b) = fold_map (fn (ak_name, T) => fn thy =>
   395          fold_map (fn (ak_name', T') => fn thy' =>
   396              let
   397                val cl_name = "cp_"^ak_name^"_"^ak_name';
   398                val ty = TFree("'a",@{sort type});
   399                val x   = Free ("x", ty);
   400                val pi1 = Free ("pi1", mk_permT T);
   401                val pi2 = Free ("pi2", mk_permT T');                  
   402                val cperm1 = Const (@{const_name Nominal.perm}, mk_permT T  --> ty --> ty);
   403                val cperm2 = Const (@{const_name Nominal.perm}, mk_permT T' --> ty --> ty);
   404                val cperm3 = Const (@{const_name Nominal.perm}, mk_permT T  --> mk_permT T' --> mk_permT T');
   405 
   406                val ax1   = HOLogic.mk_Trueprop 
   407                            (HOLogic.mk_eq (cperm1 $ pi1 $ (cperm2 $ pi2 $ x), 
   408                                            cperm2 $ (cperm3 $ pi1 $ pi2) $ (cperm1 $ pi1 $ x)));
   409                in  
   410                  Axclass.define_class (Binding.name cl_name, @{sort type}) []
   411                    [((Binding.name (cl_name ^ "1"), []), [ax1])] thy'  
   412                end) ak_names_types thy) ak_names_types thy12;
   413 
   414         (* proves for every <ak>-combination a cp_<ak1>_<ak2>_inst theorem;     *)
   415         (* lemma cp_<ak1>_<ak2>_inst:                                           *)
   416         (* cp TYPE('a::cp_<ak1>_<ak2>) TYPE(<ak1>) TYPE(<ak2>)                  *)
   417         val (cp_thms,thy12c) = fold_map (fn (ak_name, T) => fn thy =>
   418          fold_map (fn (ak_name', T') => fn thy' =>
   419            let
   420              val ak_name_qu  = Sign.full_bname thy' (ak_name);
   421              val ak_name_qu' = Sign.full_bname thy' (ak_name');
   422              val cp_name_qu  = Sign.full_bname thy' ("cp_"^ak_name^"_"^ak_name');
   423              val i_type0 = TFree("'a",[cp_name_qu]);
   424              val i_type1 = Type(ak_name_qu,[]);
   425              val i_type2 = Type(ak_name_qu',[]);
   426              val ccp = Const (@{const_name Nominal.cp},
   427                              (Term.itselfT i_type0)-->(Term.itselfT i_type1)-->
   428                                                       (Term.itselfT i_type2)-->HOLogic.boolT);
   429              val at_type  = Logic.mk_type i_type1;
   430              val at_type' = Logic.mk_type i_type2;
   431              val cp_type  = Logic.mk_type i_type0;
   432              val cp1      = Global_Theory.get_thm thy' ("cp_" ^ ak_name ^ "_" ^ ak_name' ^ "1");
   433 
   434              val name = "cp_"^ak_name^ "_"^ak_name'^"_inst";
   435              val statement = HOLogic.mk_Trueprop (ccp $ cp_type $ at_type $ at_type');
   436 
   437              fun proof ctxt =
   438               simp_tac (put_simpset HOL_basic_ss ctxt
   439                   addsimps maps (Global_Theory.get_thms thy') ["cp_def"]) 1
   440                 THEN EVERY [resolve_tac ctxt [allI] 1,
   441                   resolve_tac ctxt [allI] 1,
   442                   resolve_tac ctxt [allI] 1,
   443                   resolve_tac ctxt [cp1] 1];
   444            in
   445              yield_singleton add_thms_string ((name,
   446                Goal.prove_global thy' [] [] statement (proof o #context)), []) thy'
   447            end) 
   448            ak_names_types thy) ak_names_types thy12b;
   449        
   450         (* proves for every non-trivial <ak>-combination a disjointness   *)
   451         (* theorem; i.e. <ak1> != <ak2>                                   *)
   452         (* lemma ds_<ak1>_<ak2>:                                          *)
   453         (* dj TYPE(<ak1>) TYPE(<ak2>)                                     *)
   454         val (dj_thms, thy12d) = fold_map (fn (ak_name,T) => fn thy =>
   455           fold_map (fn (ak_name',T') => fn thy' =>
   456           (if not (ak_name = ak_name') 
   457            then 
   458                let
   459                  val ak_name_qu  = Sign.full_bname thy' ak_name;
   460                  val ak_name_qu' = Sign.full_bname thy' ak_name';
   461                  val i_type1 = Type(ak_name_qu,[]);
   462                  val i_type2 = Type(ak_name_qu',[]);
   463                  val cdj = Const (@{const_name Nominal.disjoint},
   464                            (Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT);
   465                  val at_type  = Logic.mk_type i_type1;
   466                  val at_type' = Logic.mk_type i_type2;
   467                  fun proof ctxt =
   468                   simp_tac (put_simpset HOL_ss ctxt
   469                     addsimps maps (Global_Theory.get_thms thy')
   470                                            ["disjoint_def",
   471                                             ak_name ^ "_prm_" ^ ak_name' ^ "_def",
   472                                             ak_name' ^ "_prm_" ^ ak_name ^ "_def"]) 1;
   473 
   474                  val name = "dj_"^ak_name^"_"^ak_name';
   475                  val statement = HOLogic.mk_Trueprop (cdj $ at_type $ at_type');
   476                in
   477                 add_thms_string [((name, Goal.prove_global thy' [] [] statement (proof o #context)), [])] thy'
   478                end
   479            else 
   480             ([],thy')))  (* do nothing branch, if ak_name = ak_name' *) 
   481             ak_names_types thy) ak_names_types thy12c;
   482 
   483      (********  pt_<ak> class instances  ********)
   484      (*=========================================*)
   485      (* some abbreviations for theorems *)
   486       val pt1           = @{thm "pt1"};
   487       val pt2           = @{thm "pt2"};
   488       val pt3           = @{thm "pt3"};
   489       val at_pt_inst    = @{thm "at_pt_inst"};
   490       val pt_unit_inst  = @{thm "pt_unit_inst"};
   491       val pt_prod_inst  = @{thm "pt_prod_inst"}; 
   492       val pt_nprod_inst = @{thm "pt_nprod_inst"}; 
   493       val pt_list_inst  = @{thm "pt_list_inst"};
   494       val pt_optn_inst  = @{thm "pt_option_inst"};
   495       val pt_noptn_inst = @{thm "pt_noption_inst"};
   496       val pt_fun_inst   = @{thm "pt_fun_inst"};
   497       val pt_set_inst   = @{thm "pt_set_inst"};
   498 
   499      (* for all atom-kind combinations <ak>/<ak'> show that        *)
   500      (* every <ak> is an instance of pt_<ak'>; the proof for       *)
   501      (* ak!=ak' is by definition; the case ak=ak' uses at_pt_inst. *)
   502      val thy13 = fold (fn ak_name => fn thy =>
   503         fold (fn ak_name' => fn thy' =>
   504          let
   505            val qu_name =  Sign.full_bname thy' ak_name';
   506            val cls_name = Sign.full_bname thy' ("pt_"^ak_name);
   507            val at_inst  = Global_Theory.get_thm thy' ("at_" ^ ak_name' ^ "_inst");
   508 
   509            fun proof1 ctxt = EVERY [Class.intro_classes_tac ctxt [],
   510              resolve_tac ctxt [(at_inst RS at_pt_inst) RS pt1] 1,
   511              resolve_tac ctxt [(at_inst RS at_pt_inst) RS pt2] 1,
   512              resolve_tac ctxt [(at_inst RS at_pt_inst) RS pt3] 1,
   513              assume_tac ctxt 1];
   514            fun proof2 ctxt =
   515              Class.intro_classes_tac ctxt [] THEN
   516              REPEAT (asm_simp_tac
   517               (put_simpset HOL_basic_ss ctxt addsimps
   518                 maps (Global_Theory.get_thms thy') [ak_name ^ "_prm_" ^ ak_name' ^ "_def"]) 1);
   519          in
   520            thy'
   521            |> Axclass.prove_arity (qu_name,[],[cls_name])
   522               (fn ctxt => if ak_name = ak_name' then proof1 ctxt else proof2 ctxt)
   523          end) ak_names thy) ak_names thy12d;
   524 
   525      (* show that                       *)
   526      (*      fun(pt_<ak>,pt_<ak>)       *)
   527      (*      noption(pt_<ak>)           *)
   528      (*      option(pt_<ak>)            *)
   529      (*      list(pt_<ak>)              *)
   530      (*      *(pt_<ak>,pt_<ak>)         *)
   531      (*      nprod(pt_<ak>,pt_<ak>)     *)
   532      (*      unit                       *)
   533      (*      set(pt_<ak>)               *)
   534      (* are instances of pt_<ak>        *)
   535      val thy18 = fold (fn ak_name => fn thy =>
   536        let
   537           val cls_name = Sign.full_bname thy ("pt_"^ak_name);
   538           val at_thm   = Global_Theory.get_thm thy ("at_"^ak_name^"_inst");
   539           val pt_inst  = Global_Theory.get_thm thy ("pt_"^ak_name^"_inst");
   540 
   541           fun pt_proof thm ctxt =
   542               EVERY [Class.intro_classes_tac ctxt [],
   543                 resolve_tac ctxt [thm RS pt1] 1,
   544                 resolve_tac ctxt [thm RS pt2] 1,
   545                 resolve_tac ctxt [thm RS pt3] 1,
   546                 assume_tac ctxt 1];
   547 
   548           val pt_thm_fun   = at_thm RS (pt_inst RS (pt_inst RS pt_fun_inst));
   549           val pt_thm_set   = pt_inst RS pt_set_inst;
   550           val pt_thm_noptn = pt_inst RS pt_noptn_inst; 
   551           val pt_thm_optn  = pt_inst RS pt_optn_inst; 
   552           val pt_thm_list  = pt_inst RS pt_list_inst;
   553           val pt_thm_prod  = pt_inst RS (pt_inst RS pt_prod_inst);
   554           val pt_thm_nprod = pt_inst RS (pt_inst RS pt_nprod_inst);
   555           val pt_thm_unit  = pt_unit_inst;
   556        in
   557         thy
   558         |> Axclass.prove_arity (@{type_name fun},[[cls_name],[cls_name]],[cls_name]) (pt_proof pt_thm_fun)
   559         |> Axclass.prove_arity (@{type_name set},[[cls_name]],[cls_name]) (pt_proof pt_thm_set)
   560         |> Axclass.prove_arity (@{type_name noption},[[cls_name]],[cls_name]) (pt_proof pt_thm_noptn) 
   561         |> Axclass.prove_arity (@{type_name option},[[cls_name]],[cls_name]) (pt_proof pt_thm_optn)
   562         |> Axclass.prove_arity (@{type_name list},[[cls_name]],[cls_name]) (pt_proof pt_thm_list)
   563         |> Axclass.prove_arity (@{type_name prod},[[cls_name],[cls_name]],[cls_name]) (pt_proof pt_thm_prod)
   564         |> Axclass.prove_arity (@{type_name nprod},[[cls_name],[cls_name]],[cls_name]) (pt_proof pt_thm_nprod)
   565         |> Axclass.prove_arity (@{type_name unit},[],[cls_name]) (pt_proof pt_thm_unit)
   566      end) ak_names thy13; 
   567 
   568        (********  fs_<ak> class instances  ********)
   569        (*=========================================*)
   570        (* abbreviations for some lemmas *)
   571        val fs1            = @{thm "fs1"};
   572        val fs_at_inst     = @{thm "fs_at_inst"};
   573        val fs_unit_inst   = @{thm "fs_unit_inst"};
   574        val fs_prod_inst   = @{thm "fs_prod_inst"};
   575        val fs_nprod_inst  = @{thm "fs_nprod_inst"};
   576        val fs_list_inst   = @{thm "fs_list_inst"};
   577        val fs_option_inst = @{thm "fs_option_inst"};
   578        val dj_supp        = @{thm "dj_supp"};
   579 
   580        (* shows that <ak> is an instance of fs_<ak>     *)
   581        (* uses the theorem at_<ak>_inst                 *)
   582        val thy20 = fold (fn ak_name => fn thy =>
   583         fold (fn ak_name' => fn thy' =>
   584         let
   585            val qu_name =  Sign.full_bname thy' ak_name';
   586            val qu_class = Sign.full_bname thy' ("fs_"^ak_name);
   587            fun proof ctxt =
   588                (if ak_name = ak_name'
   589                 then
   590                   let val at_thm = Global_Theory.get_thm thy' ("at_"^ak_name^"_inst") in
   591                     EVERY [Class.intro_classes_tac ctxt [],
   592                       resolve_tac ctxt [(at_thm RS fs_at_inst) RS fs1] 1]
   593                   end
   594                 else
   595                   let val dj_inst = Global_Theory.get_thm thy' ("dj_"^ak_name'^"_"^ak_name);
   596                       val simp_s =
   597                         put_simpset HOL_basic_ss ctxt addsimps [dj_inst RS dj_supp, finite_emptyI];
   598                   in EVERY [Class.intro_classes_tac ctxt [], asm_simp_tac simp_s 1] end)
   599         in
   600          Axclass.prove_arity (qu_name,[],[qu_class]) proof thy'
   601         end) ak_names thy) ak_names thy18;
   602 
   603        (* shows that                  *)
   604        (*    unit                     *)
   605        (*    *(fs_<ak>,fs_<ak>)       *)
   606        (*    nprod(fs_<ak>,fs_<ak>)   *)
   607        (*    list(fs_<ak>)            *)
   608        (*    option(fs_<ak>)          *) 
   609        (* are instances of fs_<ak>    *)
   610 
   611        val thy24 = fold (fn ak_name => fn thy => 
   612         let
   613           val cls_name = Sign.full_bname thy ("fs_"^ak_name);
   614           val fs_inst  = Global_Theory.get_thm thy ("fs_"^ak_name^"_inst");
   615           fun fs_proof thm ctxt =
   616             EVERY [Class.intro_classes_tac ctxt [], resolve_tac ctxt [thm RS fs1] 1];
   617 
   618           val fs_thm_unit  = fs_unit_inst;
   619           val fs_thm_prod  = fs_inst RS (fs_inst RS fs_prod_inst);
   620           val fs_thm_nprod = fs_inst RS (fs_inst RS fs_nprod_inst);
   621           val fs_thm_list  = fs_inst RS fs_list_inst;
   622           val fs_thm_optn  = fs_inst RS fs_option_inst;
   623         in 
   624          thy
   625          |> Axclass.prove_arity (@{type_name unit},[],[cls_name]) (fs_proof fs_thm_unit) 
   626          |> Axclass.prove_arity (@{type_name prod},[[cls_name],[cls_name]],[cls_name]) (fs_proof fs_thm_prod) 
   627          |> Axclass.prove_arity (@{type_name nprod},[[cls_name],[cls_name]],[cls_name]) (fs_proof fs_thm_nprod)
   628          |> Axclass.prove_arity (@{type_name list},[[cls_name]],[cls_name]) (fs_proof fs_thm_list)
   629          |> Axclass.prove_arity (@{type_name option},[[cls_name]],[cls_name]) (fs_proof fs_thm_optn)
   630         end) ak_names thy20;
   631 
   632        (********  cp_<ak>_<ai> class instances  ********)
   633        (*==============================================*)
   634        (* abbreviations for some lemmas *)
   635        val cp1             = @{thm "cp1"};
   636        val cp_unit_inst    = @{thm "cp_unit_inst"};
   637        val cp_bool_inst    = @{thm "cp_bool_inst"};
   638        val cp_prod_inst    = @{thm "cp_prod_inst"};
   639        val cp_list_inst    = @{thm "cp_list_inst"};
   640        val cp_fun_inst     = @{thm "cp_fun_inst"};
   641        val cp_option_inst  = @{thm "cp_option_inst"};
   642        val cp_noption_inst = @{thm "cp_noption_inst"};
   643        val cp_set_inst     = @{thm "cp_set_inst"};
   644        val pt_perm_compose = @{thm "pt_perm_compose"};
   645 
   646        val dj_pp_forget    = @{thm "dj_perm_perm_forget"};
   647 
   648        (* shows that <aj> is an instance of cp_<ak>_<ai>  *)
   649        (* for every  <ak>/<ai>-combination                *)
   650        val thy25 = fold (fn ak_name => fn thy =>
   651          fold (fn ak_name' => fn thy' =>
   652           fold (fn ak_name'' => fn thy'' =>
   653             let
   654               val name =  Sign.full_bname thy'' ak_name;
   655               val cls_name = Sign.full_bname thy'' ("cp_"^ak_name'^"_"^ak_name'');
   656               fun proof ctxt =
   657                 (if (ak_name'=ak_name'') then 
   658                   (let
   659                     val pt_inst  = Global_Theory.get_thm thy'' ("pt_"^ak_name''^"_inst");
   660                     val at_inst  = Global_Theory.get_thm thy'' ("at_"^ak_name''^"_inst");
   661                   in
   662                    EVERY [Class.intro_classes_tac ctxt [],
   663                      resolve_tac ctxt [at_inst RS (pt_inst RS pt_perm_compose)] 1]
   664                   end)
   665                 else
   666                   (let
   667                      val dj_inst  = Global_Theory.get_thm thy'' ("dj_"^ak_name''^"_"^ak_name');
   668                      val simp_s = put_simpset HOL_basic_ss ctxt addsimps
   669                                         ((dj_inst RS dj_pp_forget)::
   670                                          (maps (Global_Theory.get_thms thy'')
   671                                            [ak_name' ^"_prm_"^ak_name^"_def",
   672                                             ak_name''^"_prm_"^ak_name^"_def"]));
   673                   in
   674                     EVERY [Class.intro_classes_tac ctxt [], simp_tac simp_s 1]
   675                   end))
   676               in
   677                 Axclass.prove_arity (name,[],[cls_name]) proof thy''
   678               end) ak_names thy') ak_names thy) ak_names thy24;
   679 
   680        (* shows that                                                    *) 
   681        (*      units                                                    *) 
   682        (*      products                                                 *)
   683        (*      lists                                                    *)
   684        (*      functions                                                *)
   685        (*      options                                                  *)
   686        (*      noptions                                                 *)
   687        (*      sets                                                     *)
   688        (* are instances of cp_<ak>_<ai> for every <ak>/<ai>-combination *)
   689        val thy26 = fold (fn ak_name => fn thy =>
   690         fold (fn ak_name' => fn thy' =>
   691         let
   692             val cls_name = Sign.full_bname thy' ("cp_"^ak_name^"_"^ak_name');
   693             val cp_inst  = Global_Theory.get_thm thy' ("cp_"^ak_name^"_"^ak_name'^"_inst");
   694             val pt_inst  = Global_Theory.get_thm thy' ("pt_"^ak_name^"_inst");
   695             val at_inst  = Global_Theory.get_thm thy' ("at_"^ak_name^"_inst");
   696 
   697             fun cp_proof thm ctxt =
   698               EVERY [Class.intro_classes_tac ctxt [], resolve_tac ctxt [thm RS cp1] 1];
   699           
   700             val cp_thm_unit = cp_unit_inst;
   701             val cp_thm_prod = cp_inst RS (cp_inst RS cp_prod_inst);
   702             val cp_thm_list = cp_inst RS cp_list_inst;
   703             val cp_thm_fun  = at_inst RS (pt_inst RS (cp_inst RS (cp_inst RS cp_fun_inst)));
   704             val cp_thm_optn = cp_inst RS cp_option_inst;
   705             val cp_thm_noptn = cp_inst RS cp_noption_inst;
   706             val cp_thm_set = cp_inst RS cp_set_inst;
   707         in
   708          thy'
   709          |> Axclass.prove_arity (@{type_name unit},[],[cls_name]) (cp_proof cp_thm_unit)
   710          |> Axclass.prove_arity (@{type_name Product_Type.prod}, [[cls_name],[cls_name]],[cls_name]) (cp_proof cp_thm_prod)
   711          |> Axclass.prove_arity (@{type_name list},[[cls_name]],[cls_name]) (cp_proof cp_thm_list)
   712          |> Axclass.prove_arity (@{type_name fun},[[cls_name],[cls_name]],[cls_name]) (cp_proof cp_thm_fun)
   713          |> Axclass.prove_arity (@{type_name option},[[cls_name]],[cls_name]) (cp_proof cp_thm_optn)
   714          |> Axclass.prove_arity (@{type_name noption},[[cls_name]],[cls_name]) (cp_proof cp_thm_noptn)
   715          |> Axclass.prove_arity (@{type_name set},[[cls_name]],[cls_name]) (cp_proof cp_thm_set)
   716         end) ak_names thy) ak_names thy25;
   717 
   718      (* show that discrete nominal types are permutation types, finitely     *)
   719      (* supported and have the commutation property                          *)
   720      (* discrete types have a permutation operation defined as pi o x = x;   *)
   721      (* which renders the proofs to be simple "simp_all"-proofs.             *)
   722      val thy32 =
   723         let
   724           fun discrete_pt_inst discrete_ty defn =
   725              fold (fn ak_name => fn thy =>
   726              let
   727                val qu_class = Sign.full_bname thy ("pt_"^ak_name);
   728                fun proof ctxt =
   729                 Class.intro_classes_tac ctxt [] THEN
   730                 REPEAT (asm_simp_tac (put_simpset HOL_basic_ss ctxt addsimps [Simpdata.mk_eq defn]) 1);
   731              in 
   732                Axclass.prove_arity (discrete_ty, [], [qu_class]) proof thy
   733              end) ak_names;
   734 
   735           fun discrete_fs_inst discrete_ty defn = 
   736              fold (fn ak_name => fn thy =>
   737              let
   738                val qu_class = Sign.full_bname thy ("fs_"^ak_name);
   739                val supp_def = Simpdata.mk_eq @{thm "Nominal.supp_def"};
   740                fun proof ctxt =
   741                 Class.intro_classes_tac ctxt [] THEN
   742                 asm_simp_tac (put_simpset HOL_ss ctxt
   743                   addsimps [supp_def, Collect_const, finite_emptyI, Simpdata.mk_eq defn]) 1;
   744              in 
   745                Axclass.prove_arity (discrete_ty, [], [qu_class]) proof thy
   746              end) ak_names;
   747 
   748           fun discrete_cp_inst discrete_ty defn = 
   749              fold (fn ak_name' => (fold (fn ak_name => fn thy =>
   750              let
   751                val qu_class = Sign.full_bname thy ("cp_"^ak_name^"_"^ak_name');
   752                val supp_def = Simpdata.mk_eq @{thm "Nominal.supp_def"};
   753                fun proof ctxt =
   754                 Class.intro_classes_tac ctxt [] THEN
   755                 asm_simp_tac (put_simpset HOL_ss ctxt addsimps [Simpdata.mk_eq defn]) 1;
   756              in
   757                Axclass.prove_arity (discrete_ty, [], [qu_class]) proof thy
   758              end) ak_names)) ak_names;
   759 
   760         in
   761          thy26
   762          |> discrete_pt_inst @{type_name nat} @{thm perm_nat_def}
   763          |> discrete_fs_inst @{type_name nat} @{thm perm_nat_def}
   764          |> discrete_cp_inst @{type_name nat} @{thm perm_nat_def}
   765          |> discrete_pt_inst @{type_name bool} @{thm perm_bool_def}
   766          |> discrete_fs_inst @{type_name bool} @{thm perm_bool_def}
   767          |> discrete_cp_inst @{type_name bool} @{thm perm_bool_def}
   768          |> discrete_pt_inst @{type_name int} @{thm perm_int_def}
   769          |> discrete_fs_inst @{type_name int} @{thm perm_int_def}
   770          |> discrete_cp_inst @{type_name int} @{thm perm_int_def}
   771          |> discrete_pt_inst @{type_name char} @{thm perm_char_def}
   772          |> discrete_fs_inst @{type_name char} @{thm perm_char_def}
   773          |> discrete_cp_inst @{type_name char} @{thm perm_char_def}
   774         end;
   775 
   776 
   777        (* abbreviations for some lemmas *)
   778        (*===============================*)
   779        val abs_fun_pi          = @{thm "Nominal.abs_fun_pi"};
   780        val abs_fun_pi_ineq     = @{thm "Nominal.abs_fun_pi_ineq"};
   781        val abs_fun_eq          = @{thm "Nominal.abs_fun_eq"};
   782        val abs_fun_eq'         = @{thm "Nominal.abs_fun_eq'"};
   783        val abs_fun_fresh       = @{thm "Nominal.abs_fun_fresh"};
   784        val abs_fun_fresh'      = @{thm "Nominal.abs_fun_fresh'"};
   785        val dj_perm_forget      = @{thm "Nominal.dj_perm_forget"};
   786        val dj_pp_forget        = @{thm "Nominal.dj_perm_perm_forget"};
   787        val fresh_iff           = @{thm "Nominal.fresh_abs_fun_iff"};
   788        val fresh_iff_ineq      = @{thm "Nominal.fresh_abs_fun_iff_ineq"};
   789        val abs_fun_supp        = @{thm "Nominal.abs_fun_supp"};
   790        val abs_fun_supp_ineq   = @{thm "Nominal.abs_fun_supp_ineq"};
   791        val pt_swap_bij         = @{thm "Nominal.pt_swap_bij"};
   792        val pt_swap_bij'        = @{thm "Nominal.pt_swap_bij'"};
   793        val pt_fresh_fresh      = @{thm "Nominal.pt_fresh_fresh"};
   794        val pt_bij              = @{thm "Nominal.pt_bij"};
   795        val pt_perm_compose     = @{thm "Nominal.pt_perm_compose"};
   796        val pt_perm_compose'    = @{thm "Nominal.pt_perm_compose'"};
   797        val perm_app            = @{thm "Nominal.pt_fun_app_eq"};
   798        val at_fresh            = @{thm "Nominal.at_fresh"};
   799        val at_fresh_ineq       = @{thm "Nominal.at_fresh_ineq"};
   800        val at_calc             = @{thms "Nominal.at_calc"};
   801        val at_swap_simps       = @{thms "Nominal.at_swap_simps"};
   802        val at_supp             = @{thm "Nominal.at_supp"};
   803        val dj_supp             = @{thm "Nominal.dj_supp"};
   804        val fresh_left_ineq     = @{thm "Nominal.pt_fresh_left_ineq"};
   805        val fresh_left          = @{thm "Nominal.pt_fresh_left"};
   806        val fresh_right_ineq    = @{thm "Nominal.pt_fresh_right_ineq"};
   807        val fresh_right         = @{thm "Nominal.pt_fresh_right"};
   808        val fresh_bij_ineq      = @{thm "Nominal.pt_fresh_bij_ineq"};
   809        val fresh_bij           = @{thm "Nominal.pt_fresh_bij"};
   810        val fresh_star_bij_ineq = @{thms "Nominal.pt_fresh_star_bij_ineq"};
   811        val fresh_star_bij      = @{thms "Nominal.pt_fresh_star_bij"};
   812        val fresh_eqvt          = @{thm "Nominal.pt_fresh_eqvt"};
   813        val fresh_eqvt_ineq     = @{thm "Nominal.pt_fresh_eqvt_ineq"};
   814        val fresh_star_eqvt     = @{thms "Nominal.pt_fresh_star_eqvt"};
   815        val fresh_star_eqvt_ineq= @{thms "Nominal.pt_fresh_star_eqvt_ineq"};
   816        val set_diff_eqvt       = @{thm "Nominal.pt_set_diff_eqvt"};
   817        val in_eqvt             = @{thm "Nominal.pt_in_eqvt"};
   818        val eq_eqvt             = @{thm "Nominal.pt_eq_eqvt"};
   819        val all_eqvt            = @{thm "Nominal.pt_all_eqvt"};
   820        val ex_eqvt             = @{thm "Nominal.pt_ex_eqvt"};
   821        val ex1_eqvt            = @{thm "Nominal.pt_ex1_eqvt"};
   822        val the_eqvt            = @{thm "Nominal.pt_the_eqvt"};
   823        val pt_pi_rev           = @{thm "Nominal.pt_pi_rev"};
   824        val pt_rev_pi           = @{thm "Nominal.pt_rev_pi"};
   825        val at_exists_fresh     = @{thm "Nominal.at_exists_fresh"};
   826        val at_exists_fresh'    = @{thm "Nominal.at_exists_fresh'"};
   827        val fresh_perm_app_ineq = @{thm "Nominal.pt_fresh_perm_app_ineq"};
   828        val fresh_perm_app      = @{thm "Nominal.pt_fresh_perm_app"};    
   829        val fresh_aux           = @{thm "Nominal.pt_fresh_aux"};  
   830        val pt_perm_supp_ineq   = @{thm "Nominal.pt_perm_supp_ineq"};
   831        val pt_perm_supp        = @{thm "Nominal.pt_perm_supp"};
   832        val subseteq_eqvt       = @{thm "Nominal.pt_subseteq_eqvt"};
   833 
   834        (* Now we collect and instantiate some lemmas w.r.t. all atom      *)
   835        (* types; this allows for example to use abs_perm (which is a      *)
   836        (* collection of theorems) instead of thm abs_fun_pi with explicit *)
   837        (* instantiations.                                                 *)
   838        val (_, thy33) =
   839          let
   840              val ctxt32 = Proof_Context.init_global thy32;
   841 
   842              (* takes a theorem thm and a list of theorems [t1,..,tn]            *)
   843              (* produces a list of theorems of the form [t1 RS thm,..,tn RS thm] *) 
   844              fun instR thm thms = map (fn ti => ti RS thm) thms;
   845 
   846              (* takes a theorem thm and a list of theorems [(t1,m1),..,(tn,mn)]  *)
   847              (* produces a list of theorems of the form [[t1,m1] MRS thm,..,[tn,mn] MRS thm] *) 
   848              fun instRR thm thms = map (fn (ti,mi) => [ti,mi] MRS thm) thms;
   849 
   850              (* takes two theorem lists (hopefully of the same length ;o)                *)
   851              (* produces a list of theorems of the form                                  *)
   852              (* [t1 RS m1,..,tn RS mn] where [t1,..,tn] is thms1 and [m1,..,mn] is thms2 *) 
   853              fun inst_zip thms1 thms2 = map (fn (t1,t2) => t1 RS t2) (thms1 ~~ thms2);
   854 
   855              (* takes a theorem list of the form [l1,...,ln]              *)
   856              (* and a list of theorem lists of the form                   *)
   857              (* [[h11,...,h1m],....,[hk1,....,hkm]                        *)
   858              (* produces the list of theorem lists                        *)
   859              (* [[l1 RS h11,...,l1 RS h1m],...,[ln RS hk1,...,ln RS hkm]] *)
   860              fun inst_mult thms thmss = map (fn (t,ts) => instR t ts) (thms ~~ thmss);
   861 
   862              (* FIXME: these lists do not need to be created dynamically again *)
   863 
   864              
   865              (* list of all at_inst-theorems *)
   866              val ats = map (fn ak => Global_Theory.get_thm thy32 ("at_"^ak^"_inst")) ak_names
   867              (* list of all pt_inst-theorems *)
   868              val pts = map (fn ak => Global_Theory.get_thm thy32 ("pt_"^ak^"_inst")) ak_names
   869              (* list of all cp_inst-theorems as a collection of lists*)
   870              val cps = 
   871                  let fun cps_fun ak1 ak2 =  Global_Theory.get_thm thy32 ("cp_"^ak1^"_"^ak2^"_inst")
   872                  in map (fn aki => (map (cps_fun aki) ak_names)) ak_names end; 
   873              (* list of all cp_inst-theorems that have different atom types *)
   874              val cps' = 
   875                 let fun cps'_fun ak1 ak2 = 
   876                 if ak1=ak2 then NONE else SOME (Global_Theory.get_thm thy32 ("cp_"^ak1^"_"^ak2^"_inst"))
   877                 in map (fn aki => (map_filter I (map (cps'_fun aki) ak_names))) ak_names end;
   878              (* list of all dj_inst-theorems *)
   879              val djs = 
   880                let fun djs_fun ak1 ak2 = 
   881                      if ak1=ak2 then NONE else SOME(Global_Theory.get_thm thy32 ("dj_"^ak2^"_"^ak1))
   882                in map_filter I (map_product djs_fun ak_names ak_names) end;
   883              (* list of all fs_inst-theorems *)
   884              val fss = map (fn ak => Global_Theory.get_thm thy32 ("fs_"^ak^"_inst")) ak_names
   885              (* list of all at_inst-theorems *)
   886              val fs_axs = map (fn ak => Global_Theory.get_thm thy32 ("fs_"^ak^"1")) ak_names
   887 
   888              fun inst_pt thms = maps (fn ti => instR ti pts) thms;
   889              fun inst_at thms = maps (fn ti => instR ti ats) thms;
   890              fun inst_fs thms = maps (fn ti => instR ti fss) thms;
   891              fun inst_cp thms cps = flat (inst_mult thms cps);
   892              fun inst_pt_at thms = maps (fn ti => instRR ti (pts ~~ ats)) thms;
   893              fun inst_dj thms = maps (fn ti => instR ti djs) thms;
   894              fun inst_pt_pt_at_cp thms = inst_cp (inst_zip ats (inst_zip pts (inst_pt thms))) cps;
   895              fun inst_pt_at_fs thms = inst_zip (inst_fs [fs1]) (inst_zip ats (inst_pt thms));
   896              fun inst_pt_pt_at_cp thms =
   897                  let val i_pt_pt_at = inst_zip ats (inst_zip pts (inst_pt thms));
   898                      val i_pt_pt_at_cp = inst_cp i_pt_pt_at cps';
   899                  in i_pt_pt_at_cp end;
   900              fun inst_pt_pt_at_cp_dj thms = inst_zip djs (inst_pt_pt_at_cp thms);
   901            in
   902             thy32 
   903             |>   add_thmss_string [(("alpha", inst_pt_at [abs_fun_eq]),[])]
   904             ||>> add_thmss_string [(("alpha'", inst_pt_at [abs_fun_eq']),[])]
   905             ||>> add_thmss_string [(("alpha_fresh", inst_pt_at [abs_fun_fresh]),[])]
   906             ||>> add_thmss_string [(("alpha_fresh'", inst_pt_at [abs_fun_fresh']),[])]
   907             ||>> add_thmss_string [(("perm_swap", inst_pt_at [pt_swap_bij] @ inst_pt_at [pt_swap_bij']),[])]
   908             ||>> add_thmss_string 
   909               let val thms1 = inst_at at_swap_simps
   910                   and thms2 = inst_dj [dj_perm_forget]
   911               in [(("swap_simps", thms1 @ thms2),[])] end 
   912             ||>> add_thmss_string 
   913               let val thms1 = inst_pt_at [pt_pi_rev];
   914                   val thms2 = inst_pt_at [pt_rev_pi];
   915               in [(("perm_pi_simp",thms1 @ thms2),[])] end
   916             ||>> add_thmss_string [(("perm_fresh_fresh", inst_pt_at [pt_fresh_fresh]),[])]
   917             ||>> add_thmss_string [(("perm_bij", inst_pt_at [pt_bij]),[])]
   918             ||>> add_thmss_string 
   919               let val thms1 = inst_pt_at [pt_perm_compose];
   920                   val thms2 = instR cp1 (Library.flat cps');
   921               in [(("perm_compose",thms1 @ thms2),[])] end
   922             ||>> add_thmss_string [(("perm_compose'",inst_pt_at [pt_perm_compose']),[])] 
   923             ||>> add_thmss_string [(("perm_app", inst_pt_at [perm_app]),[])]
   924             ||>> add_thmss_string [(("supp_atm", (inst_at [at_supp]) @ (inst_dj [dj_supp])),[])]
   925             ||>> add_thmss_string [(("exists_fresh", inst_at [at_exists_fresh]),[])]
   926             ||>> add_thmss_string [(("exists_fresh'", inst_at [at_exists_fresh']),[])]
   927             ||>> add_thmss_string
   928               let
   929                 val thms1 = inst_pt_at [all_eqvt];
   930                 val thms2 = map (fold_rule ctxt32 [inductive_forall_def]) thms1
   931               in
   932                 [(("all_eqvt", thms1 @ thms2), [NominalThmDecls.eqvt_force_add])]
   933               end
   934             ||>> add_thmss_string [(("ex_eqvt", inst_pt_at [ex_eqvt]),[NominalThmDecls.eqvt_force_add])]
   935             ||>> add_thmss_string [(("ex1_eqvt", inst_pt_at [ex1_eqvt]),[NominalThmDecls.eqvt_force_add])]
   936             ||>> add_thmss_string [(("the_eqvt", inst_pt_at [the_eqvt]),[NominalThmDecls.eqvt_force_add])]
   937             ||>> add_thmss_string 
   938               let val thms1 = inst_at [at_fresh]
   939                   val thms2 = inst_dj [at_fresh_ineq]
   940               in [(("fresh_atm", thms1 @ thms2),[])] end
   941             ||>> add_thmss_string
   942               let val thms1 = inst_at at_calc
   943                   and thms2 = inst_dj [dj_perm_forget]
   944               in [(("calc_atm", thms1 @ thms2),[])] end
   945             ||>> add_thmss_string
   946               let val thms1 = inst_pt_at [abs_fun_pi]
   947                   and thms2 = inst_pt_pt_at_cp [abs_fun_pi_ineq]
   948               in [(("abs_perm", thms1 @ thms2),[NominalThmDecls.eqvt_add])] end
   949             ||>> add_thmss_string
   950               let val thms1 = inst_dj [dj_perm_forget]
   951                   and thms2 = inst_dj [dj_pp_forget]
   952               in [(("perm_dj", thms1 @ thms2),[])] end
   953             ||>> add_thmss_string
   954               let val thms1 = inst_pt_at_fs [fresh_iff]
   955                   and thms2 = inst_pt_at [fresh_iff]
   956                   and thms3 = inst_pt_pt_at_cp_dj [fresh_iff_ineq]
   957             in [(("abs_fresh", thms1 @ thms2 @ thms3),[])] end
   958             ||>> add_thmss_string
   959               let val thms1 = inst_pt_at [abs_fun_supp]
   960                   and thms2 = inst_pt_at_fs [abs_fun_supp]
   961                   and thms3 = inst_pt_pt_at_cp_dj [abs_fun_supp_ineq]
   962               in [(("abs_supp", thms1 @ thms2 @ thms3),[])] end
   963             ||>> add_thmss_string
   964               let val thms1 = inst_pt_at [fresh_left]
   965                   and thms2 = inst_pt_pt_at_cp [fresh_left_ineq]
   966               in [(("fresh_left", thms1 @ thms2),[])] end
   967             ||>> add_thmss_string
   968               let val thms1 = inst_pt_at [fresh_right]
   969                   and thms2 = inst_pt_pt_at_cp [fresh_right_ineq]
   970               in [(("fresh_right", thms1 @ thms2),[])] end
   971             ||>> add_thmss_string
   972               let val thms1 = inst_pt_at [fresh_bij]
   973                   and thms2 = inst_pt_pt_at_cp [fresh_bij_ineq]
   974               in [(("fresh_bij", thms1 @ thms2),[])] end
   975             ||>> add_thmss_string
   976               let val thms1 = inst_pt_at fresh_star_bij
   977                   and thms2 = maps (fn ti => inst_pt_pt_at_cp [ti]) fresh_star_bij_ineq
   978               in [(("fresh_star_bij", thms1 @ thms2),[])] end
   979             ||>> add_thmss_string
   980               let val thms1 = inst_pt_at [fresh_eqvt]
   981                   and thms2 = inst_pt_pt_at_cp_dj [fresh_eqvt_ineq]
   982               in [(("fresh_eqvt", thms1 @ thms2),[NominalThmDecls.eqvt_add])] end
   983             ||>> add_thmss_string
   984               let val thms1 = inst_pt_at fresh_star_eqvt
   985                   and thms2 = maps (fn ti => inst_pt_pt_at_cp_dj [ti]) fresh_star_eqvt_ineq
   986               in [(("fresh_star_eqvt", thms1 @ thms2),[NominalThmDecls.eqvt_add])] end
   987             ||>> add_thmss_string
   988               let val thms1 = inst_pt_at [in_eqvt]
   989               in [(("in_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   990             ||>> add_thmss_string
   991               let val thms1 = inst_pt_at [eq_eqvt]
   992               in [(("eq_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   993             ||>> add_thmss_string
   994               let val thms1 = inst_pt_at [set_diff_eqvt]
   995               in [(("set_diff_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   996             ||>> add_thmss_string
   997               let val thms1 = inst_pt_at [subseteq_eqvt]
   998               in [(("subseteq_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   999             ||>> add_thmss_string
  1000               let val thms1 = inst_pt_at [fresh_aux]
  1001                   and thms2 = inst_pt_pt_at_cp_dj [fresh_perm_app_ineq] 
  1002               in  [(("fresh_aux", thms1 @ thms2),[])] end  
  1003             ||>> add_thmss_string
  1004               let val thms1 = inst_pt_at [fresh_perm_app]
  1005                   and thms2 = inst_pt_pt_at_cp_dj [fresh_perm_app_ineq] 
  1006               in  [(("fresh_perm_app", thms1 @ thms2),[])] end 
  1007             ||>> add_thmss_string
  1008               let val thms1 = inst_pt_at [pt_perm_supp]
  1009                   and thms2 = inst_pt_pt_at_cp [pt_perm_supp_ineq] 
  1010               in  [(("supp_eqvt", thms1 @ thms2),[NominalThmDecls.eqvt_add])] end  
  1011             ||>> add_thmss_string [(("fin_supp",fs_axs),[])]
  1012            end;
  1013 
  1014     in 
  1015       NominalData.map (fold Symtab.update (full_ak_names ~~ map make_atom_info
  1016         (pt_ax_classes ~~
  1017          fs_ax_classes ~~
  1018          map (fn cls => Symtab.make (full_ak_names ~~ cls)) cp_ax_classes ~~
  1019          prm_cons_thms ~~ prm_inst_thms ~~
  1020          map (fn ths => Symtab.make (full_ak_names ~~ ths)) cp_thms ~~
  1021          map (fn thss => Symtab.make
  1022            (map_filter (fn (s, [th]) => SOME (s, th) | _ => NONE)
  1023               (full_ak_names ~~ thss))) dj_thms))) thy33
  1024     end;
  1025 
  1026 
  1027 (* syntax und parsing *)
  1028 
  1029 val _ =
  1030   Outer_Syntax.command @{command_keyword atom_decl} "declare new kinds of atoms"
  1031     (Scan.repeat1 Parse.name >> (Toplevel.theory o create_nom_typedecls));
  1032 
  1033 end;