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