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