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