src/HOL/Nominal/nominal_atoms.ML
author haftmann
Wed Jan 21 18:27:43 2009 +0100 (2009-01-21 ago)
changeset 29585 c23295521af5
parent 29128 4c243e6a71b2
child 30086 2023fb9fbf31
permissions -rw-r--r--
binding replaces bstring
     1 (*  title:      HOL/Nominal/nominal_atoms.ML
     2     ID:         $Id$
     3     Author:     Christian Urban and Stefan Berghofer, TU Muenchen
     4 
     5 Declaration of atom types to be used in nominal datatypes.
     6 *)
     7 
     8 signature NOMINAL_ATOMS =
     9 sig
    10   val create_nom_typedecls : string list -> theory -> theory
    11   type atom_info
    12   val get_atom_infos : theory -> atom_info Symtab.table
    13   val get_atom_info : theory -> string -> atom_info option
    14   val the_atom_info : theory -> string -> atom_info
    15   val fs_class_of : theory -> string -> string
    16   val pt_class_of : theory -> string -> string
    17   val cp_class_of : theory -> string -> string -> string
    18   val at_inst_of : theory -> string -> thm
    19   val pt_inst_of : theory -> string -> thm
    20   val cp_inst_of : theory -> string -> string -> thm
    21   val dj_thm_of : theory -> string -> string -> thm
    22   val atoms_of : theory -> string list
    23   val mk_permT : typ -> typ
    24 end
    25 
    26 structure NominalAtoms : NOMINAL_ATOMS =
    27 struct
    28 
    29 val finite_emptyI = @{thm "finite.emptyI"};
    30 val Collect_const = @{thm "Collect_const"};
    31 
    32 val inductive_forall_def = @{thm "induct_forall_def"};
    33 
    34 
    35 (* theory data *)
    36 
    37 type atom_info =
    38   {pt_class : string,
    39    fs_class : string,
    40    cp_classes : string Symtab.table,
    41    at_inst : thm,
    42    pt_inst : thm,
    43    cp_inst : thm Symtab.table,
    44    dj_thms : thm Symtab.table};
    45 
    46 structure NominalData = TheoryDataFun
    47 (
    48   type T = atom_info Symtab.table;
    49   val empty = Symtab.empty;
    50   val copy = I;
    51   val extend = I;
    52   fun merge _ x = Symtab.merge (K true) x;
    53 );
    54 
    55 fun make_atom_info ((((((pt_class, fs_class), cp_classes), at_inst), pt_inst), cp_inst), dj_thms) =
    56   {pt_class = pt_class,
    57    fs_class = fs_class,
    58    cp_classes = cp_classes,
    59    at_inst = at_inst,
    60    pt_inst = pt_inst,
    61    cp_inst = cp_inst,
    62    dj_thms = dj_thms};
    63 
    64 val get_atom_infos = NominalData.get;
    65 val get_atom_info = Symtab.lookup o NominalData.get;
    66 
    67 fun gen_lookup lookup name = case lookup name of
    68     SOME info => info
    69   | NONE => error ("Unknown atom type " ^ quote name);
    70 
    71 fun the_atom_info thy = gen_lookup (get_atom_info thy);
    72 
    73 fun gen_lookup' f thy = the_atom_info thy #> f;
    74 fun gen_lookup'' f thy =
    75   gen_lookup' (f #> Symtab.lookup #> gen_lookup) thy;
    76 
    77 val fs_class_of = gen_lookup' #fs_class;
    78 val pt_class_of = gen_lookup' #pt_class;
    79 val at_inst_of = gen_lookup' #at_inst;
    80 val pt_inst_of = gen_lookup' #pt_inst;
    81 val cp_class_of = gen_lookup'' #cp_classes;
    82 val cp_inst_of = gen_lookup'' #cp_inst;
    83 val dj_thm_of = gen_lookup'' #dj_thms;
    84 
    85 fun atoms_of thy = map fst (Symtab.dest (NominalData.get thy));
    86 
    87 fun mk_permT T = HOLogic.listT (HOLogic.mk_prodT (T, T));
    88 
    89 fun mk_Cons x xs =
    90   let val T = fastype_of x
    91   in Const ("List.list.Cons", T --> HOLogic.listT T --> HOLogic.listT T) $ x $ xs end;
    92 
    93 fun add_thms_string args = PureThy.add_thms ((map o apfst o apfst) Binding.name args);
    94 fun add_thmss_string args = PureThy.add_thmss ((map o apfst o apfst) Binding.name args);
    95 
    96 (* this function sets up all matters related to atom-  *)
    97 (* kinds; the user specifies a list of atom-kind names *)
    98 (* atom_decl <ak1> ... <akn>                           *)
    99 fun create_nom_typedecls ak_names thy =
   100   let
   101     
   102     val (_,thy1) = 
   103     fold_map (fn ak => fn thy => 
   104           let val dt = ([],ak,NoSyn,[(ak,[@{typ nat}],NoSyn)]) 
   105               val ({inject,case_thms,...},thy1) = DatatypePackage.add_datatype true false [ak] [dt] thy
   106               val inject_flat = Library.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 [("swap_" ^ ak_name, swapT, NoSyn)] 
   192             |> PureThy.add_defs_unchecked true [((Binding.name name, def2),[])]
   193             |> snd
   194             |> OldPrimrecPackage.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 [(prm_name, mk_permT T --> T --> T, NoSyn)] 
   222             |> OldPrimrecPackage.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 (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 (cl_name, [pt_name]) [] [((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 (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 ("Datatype.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 ("Datatype.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 ("Datatype.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 set_diff_eqvt       = @{thm "Nominal.pt_set_diff_eqvt"};
   788        val in_eqvt             = @{thm "Nominal.pt_in_eqvt"};
   789        val eq_eqvt             = @{thm "Nominal.pt_eq_eqvt"};
   790        val all_eqvt            = @{thm "Nominal.pt_all_eqvt"};
   791        val ex_eqvt             = @{thm "Nominal.pt_ex_eqvt"};
   792        val ex1_eqvt            = @{thm "Nominal.pt_ex1_eqvt"};
   793        val the_eqvt            = @{thm "Nominal.pt_the_eqvt"};
   794        val pt_pi_rev           = @{thm "Nominal.pt_pi_rev"};
   795        val pt_rev_pi           = @{thm "Nominal.pt_rev_pi"};
   796        val at_exists_fresh     = @{thm "Nominal.at_exists_fresh"};
   797        val at_exists_fresh'    = @{thm "Nominal.at_exists_fresh'"};
   798        val fresh_perm_app_ineq = @{thm "Nominal.pt_fresh_perm_app_ineq"};
   799        val fresh_perm_app      = @{thm "Nominal.pt_fresh_perm_app"};    
   800        val fresh_aux           = @{thm "Nominal.pt_fresh_aux"};  
   801        val pt_perm_supp_ineq   = @{thm "Nominal.pt_perm_supp_ineq"};
   802        val pt_perm_supp        = @{thm "Nominal.pt_perm_supp"};
   803        val subseteq_eqvt       = @{thm "Nominal.pt_subseteq_eqvt"};
   804        val insert_eqvt         = @{thm "Nominal.pt_insert_eqvt"};
   805        val set_eqvt            = @{thm "Nominal.pt_set_eqvt"};
   806        val perm_set_eq         = @{thm "Nominal.perm_set_eq"};
   807 
   808        (* Now we collect and instantiate some lemmas w.r.t. all atom      *)
   809        (* types; this allows for example to use abs_perm (which is a      *)
   810        (* collection of theorems) instead of thm abs_fun_pi with explicit *)
   811        (* instantiations.                                                 *)
   812        val (_, thy33) =
   813          let
   814 
   815              (* takes a theorem thm and a list of theorems [t1,..,tn]            *)
   816              (* produces a list of theorems of the form [t1 RS thm,..,tn RS thm] *) 
   817              fun instR thm thms = map (fn ti => ti RS thm) thms;
   818 
   819 	     (* takes a theorem thm and a list of theorems [(t1,m1),..,(tn,mn)]  *)
   820              (* produces a list of theorems of the form [[t1,m1] MRS thm,..,[tn,mn] MRS thm] *) 
   821              fun instRR thm thms = map (fn (ti,mi) => [ti,mi] MRS thm) thms;
   822 
   823              (* takes two theorem lists (hopefully of the same length ;o)                *)
   824              (* produces a list of theorems of the form                                  *)
   825              (* [t1 RS m1,..,tn RS mn] where [t1,..,tn] is thms1 and [m1,..,mn] is thms2 *) 
   826              fun inst_zip thms1 thms2 = map (fn (t1,t2) => t1 RS t2) (thms1 ~~ thms2);
   827 
   828              (* takes a theorem list of the form [l1,...,ln]              *)
   829              (* and a list of theorem lists of the form                   *)
   830              (* [[h11,...,h1m],....,[hk1,....,hkm]                        *)
   831              (* produces the list of theorem lists                        *)
   832              (* [[l1 RS h11,...,l1 RS h1m],...,[ln RS hk1,...,ln RS hkm]] *)
   833              fun inst_mult thms thmss = map (fn (t,ts) => instR t ts) (thms ~~ thmss);
   834 
   835              (* FIXME: these lists do not need to be created dynamically again *)
   836 
   837              
   838              (* list of all at_inst-theorems *)
   839              val ats = map (fn ak => PureThy.get_thm thy32 ("at_"^ak^"_inst")) ak_names
   840              (* list of all pt_inst-theorems *)
   841              val pts = map (fn ak => PureThy.get_thm thy32 ("pt_"^ak^"_inst")) ak_names
   842              (* list of all cp_inst-theorems as a collection of lists*)
   843              val cps = 
   844                  let fun cps_fun ak1 ak2 =  PureThy.get_thm thy32 ("cp_"^ak1^"_"^ak2^"_inst")
   845                  in map (fn aki => (map (cps_fun aki) ak_names)) ak_names end; 
   846              (* list of all cp_inst-theorems that have different atom types *)
   847              val cps' = 
   848                 let fun cps'_fun ak1 ak2 = 
   849                 if ak1=ak2 then NONE else SOME (PureThy.get_thm thy32 ("cp_"^ak1^"_"^ak2^"_inst"))
   850                 in map (fn aki => (List.mapPartial I (map (cps'_fun aki) ak_names))) ak_names end;
   851              (* list of all dj_inst-theorems *)
   852              val djs = 
   853                let fun djs_fun ak1 ak2 = 
   854                      if ak1=ak2 then NONE else SOME(PureThy.get_thm thy32 ("dj_"^ak2^"_"^ak1))
   855                in map_filter I (map_product djs_fun ak_names ak_names) end;
   856              (* list of all fs_inst-theorems *)
   857              val fss = map (fn ak => PureThy.get_thm thy32 ("fs_"^ak^"_inst")) ak_names
   858              (* list of all at_inst-theorems *)
   859              val fs_axs = map (fn ak => PureThy.get_thm thy32 ("fs_"^ak^"1")) ak_names
   860 
   861              fun inst_pt thms = maps (fn ti => instR ti pts) thms;
   862              fun inst_at thms = maps (fn ti => instR ti ats) thms;
   863              fun inst_fs thms = maps (fn ti => instR ti fss) thms;
   864              fun inst_cp thms cps = flat (inst_mult thms cps);
   865              fun inst_pt_at thms = maps (fn ti => instRR ti (pts ~~ ats)) thms;
   866              fun inst_dj thms = maps (fn ti => instR ti djs) thms;
   867              fun inst_pt_pt_at_cp thms = inst_cp (inst_zip ats (inst_zip pts (inst_pt thms))) cps;
   868              fun inst_pt_at_fs thms = inst_zip (inst_fs [fs1]) (inst_zip ats (inst_pt thms));
   869              fun inst_pt_pt_at_cp thms =
   870                  let val i_pt_pt_at = inst_zip ats (inst_zip pts (inst_pt thms));
   871                      val i_pt_pt_at_cp = inst_cp i_pt_pt_at cps';
   872                  in i_pt_pt_at_cp end;
   873              fun inst_pt_pt_at_cp_dj thms = inst_zip djs (inst_pt_pt_at_cp thms);
   874            in
   875             thy32 
   876             |>   add_thmss_string [(("alpha", inst_pt_at [abs_fun_eq]),[])]
   877             ||>> add_thmss_string [(("alpha'", inst_pt_at [abs_fun_eq']),[])]
   878             ||>> add_thmss_string [(("alpha_fresh", inst_pt_at [abs_fun_fresh]),[])]
   879             ||>> add_thmss_string [(("alpha_fresh'", inst_pt_at [abs_fun_fresh']),[])]
   880             ||>> add_thmss_string [(("perm_swap", inst_pt_at [pt_swap_bij] @ inst_pt_at [pt_swap_bij']),[])]
   881             ||>> add_thmss_string 
   882 	      let val thms1 = inst_at at_swap_simps
   883                   and thms2 = inst_dj [dj_perm_forget]
   884               in [(("swap_simps", thms1 @ thms2),[])] end 
   885             ||>> add_thmss_string 
   886               let val thms1 = inst_pt_at [pt_pi_rev];
   887                   val thms2 = inst_pt_at [pt_rev_pi];
   888               in [(("perm_pi_simp",thms1 @ thms2),[])] end
   889             ||>> add_thmss_string [(("perm_fresh_fresh", inst_pt_at [pt_fresh_fresh]),[])]
   890             ||>> add_thmss_string [(("perm_bij", inst_pt_at [pt_bij]),[])]
   891             ||>> add_thmss_string 
   892               let val thms1 = inst_pt_at [pt_perm_compose];
   893                   val thms2 = instR cp1 (Library.flat cps');
   894               in [(("perm_compose",thms1 @ thms2),[])] end
   895             ||>> add_thmss_string [(("perm_compose'",inst_pt_at [pt_perm_compose']),[])] 
   896             ||>> add_thmss_string [(("perm_app", inst_pt_at [perm_app]),[])]
   897             ||>> add_thmss_string [(("supp_atm", (inst_at [at_supp]) @ (inst_dj [dj_supp])),[])]
   898             ||>> add_thmss_string [(("exists_fresh", inst_at [at_exists_fresh]),[])]
   899             ||>> add_thmss_string [(("exists_fresh'", inst_at [at_exists_fresh']),[])]
   900             ||>> add_thmss_string
   901               let
   902                 val thms1 = inst_pt_at [all_eqvt];
   903                 val thms2 = map (fold_rule [inductive_forall_def]) thms1
   904               in
   905                 [(("all_eqvt", thms1 @ thms2), [NominalThmDecls.eqvt_force_add])]
   906               end
   907             ||>> add_thmss_string [(("ex_eqvt", inst_pt_at [ex_eqvt]),[NominalThmDecls.eqvt_force_add])]
   908             ||>> add_thmss_string [(("ex1_eqvt", inst_pt_at [ex1_eqvt]),[NominalThmDecls.eqvt_force_add])]
   909             ||>> add_thmss_string [(("the_eqvt", inst_pt_at [the_eqvt]),[NominalThmDecls.eqvt_force_add])]
   910             ||>> add_thmss_string 
   911               let val thms1 = inst_at [at_fresh]
   912                   val thms2 = inst_dj [at_fresh_ineq]
   913               in [(("fresh_atm", thms1 @ thms2),[])] end
   914             ||>> add_thmss_string
   915               let val thms1 = inst_at at_calc
   916                   and thms2 = inst_dj [dj_perm_forget]
   917               in [(("calc_atm", thms1 @ thms2),[])] end
   918             ||>> add_thmss_string
   919               let val thms1 = inst_pt_at [abs_fun_pi]
   920                   and thms2 = inst_pt_pt_at_cp [abs_fun_pi_ineq]
   921               in [(("abs_perm", thms1 @ thms2),[NominalThmDecls.eqvt_add])] end
   922             ||>> add_thmss_string
   923               let val thms1 = inst_dj [dj_perm_forget]
   924                   and thms2 = inst_dj [dj_pp_forget]
   925               in [(("perm_dj", thms1 @ thms2),[])] end
   926             ||>> add_thmss_string
   927               let val thms1 = inst_pt_at_fs [fresh_iff]
   928                   and thms2 = inst_pt_at [fresh_iff]
   929                   and thms3 = inst_pt_pt_at_cp_dj [fresh_iff_ineq]
   930             in [(("abs_fresh", thms1 @ thms2 @ thms3),[])] end
   931             ||>> add_thmss_string
   932               let val thms1 = inst_pt_at [abs_fun_supp]
   933                   and thms2 = inst_pt_at_fs [abs_fun_supp]
   934                   and thms3 = inst_pt_pt_at_cp_dj [abs_fun_supp_ineq]
   935               in [(("abs_supp", thms1 @ thms2 @ thms3),[])] end
   936             ||>> add_thmss_string
   937               let val thms1 = inst_pt_at [fresh_left]
   938                   and thms2 = inst_pt_pt_at_cp [fresh_left_ineq]
   939               in [(("fresh_left", thms1 @ thms2),[])] end
   940             ||>> add_thmss_string
   941               let val thms1 = inst_pt_at [fresh_right]
   942                   and thms2 = inst_pt_pt_at_cp [fresh_right_ineq]
   943               in [(("fresh_right", thms1 @ thms2),[])] end
   944             ||>> add_thmss_string
   945               let val thms1 = inst_pt_at [fresh_bij]
   946                   and thms2 = inst_pt_pt_at_cp [fresh_bij_ineq]
   947               in [(("fresh_bij", thms1 @ thms2),[])] end
   948             ||>> add_thmss_string
   949               let val thms1 = inst_pt_at fresh_star_bij
   950                   and thms2 = flat (map (fn ti => inst_pt_pt_at_cp [ti]) fresh_star_bij_ineq);
   951               in [(("fresh_star_bij", thms1 @ thms2),[])] end
   952             ||>> add_thmss_string
   953               let val thms1 = inst_pt_at [fresh_eqvt]
   954                   and thms2 = inst_pt_pt_at_cp_dj [fresh_eqvt_ineq]
   955               in [(("fresh_eqvt", thms1 @ thms2),[NominalThmDecls.eqvt_add])] end
   956             ||>> add_thmss_string
   957               let val thms1 = inst_pt_at [in_eqvt]
   958               in [(("in_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   959             ||>> add_thmss_string
   960               let val thms1 = inst_pt_at [eq_eqvt]
   961               in [(("eq_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   962             ||>> add_thmss_string
   963               let val thms1 = inst_pt_at [set_diff_eqvt]
   964               in [(("set_diff_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   965             ||>> add_thmss_string
   966               let val thms1 = inst_pt_at [subseteq_eqvt]
   967               in [(("subseteq_eqvt", thms1),[NominalThmDecls.eqvt_add])] end
   968             ||>> add_thmss_string [(("insert_eqvt", inst_pt_at [insert_eqvt]), [NominalThmDecls.eqvt_add])]
   969             ||>> add_thmss_string [(("set_eqvt", inst_pt_at [set_eqvt]), [NominalThmDecls.eqvt_add])]
   970             ||>> add_thmss_string [(("perm_set_eq", inst_pt_at [perm_set_eq]), [])]
   971             ||>> add_thmss_string
   972               let val thms1 = inst_pt_at [fresh_aux]
   973                   and thms2 = inst_pt_pt_at_cp_dj [fresh_perm_app_ineq] 
   974               in  [(("fresh_aux", thms1 @ thms2),[])] end  
   975             ||>> add_thmss_string
   976               let val thms1 = inst_pt_at [fresh_perm_app]
   977                   and thms2 = inst_pt_pt_at_cp_dj [fresh_perm_app_ineq] 
   978               in  [(("fresh_perm_app", thms1 @ thms2),[])] end 
   979             ||>> add_thmss_string
   980               let val thms1 = inst_pt_at [pt_perm_supp]
   981                   and thms2 = inst_pt_pt_at_cp [pt_perm_supp_ineq] 
   982               in  [(("supp_eqvt", thms1 @ thms2),[NominalThmDecls.eqvt_add])] end  
   983             ||>> add_thmss_string [(("fin_supp",fs_axs),[])]
   984            end;
   985 
   986     in 
   987       NominalData.map (fold Symtab.update (full_ak_names ~~ map make_atom_info
   988         (pt_ax_classes ~~
   989          fs_ax_classes ~~
   990          map (fn cls => Symtab.make (full_ak_names ~~ cls)) cp_ax_classes ~~
   991          prm_cons_thms ~~ prm_inst_thms ~~
   992          map (fn ths => Symtab.make (full_ak_names ~~ ths)) cp_thms ~~
   993          map (fn thss => Symtab.make
   994            (List.mapPartial (fn (s, [th]) => SOME (s, th) | _ => NONE)
   995               (full_ak_names ~~ thss))) dj_thms))) thy33
   996     end;
   997 
   998 
   999 (* syntax und parsing *)
  1000 structure P = OuterParse and K = OuterKeyword;
  1001 
  1002 val _ =
  1003   OuterSyntax.command "atom_decl" "Declare new kinds of atoms" K.thy_decl
  1004     (Scan.repeat1 P.name >> (Toplevel.theory o create_nom_typedecls));
  1005 
  1006 end;