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