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