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