src/HOL/Nominal/nominal_package.ML
author urbanc
Fri Oct 28 18:53:26 2005 +0200 (2005-10-28)
changeset 18017 f6abeac6dcb5
parent 18016 8f3a80033ba4
child 18045 6d69a4190eb2
permissions -rw-r--r--
fixed case names in the weak induction principle and
changed name from "induct" to "induct_weak"
berghofe@17870
     1
(* $Id$ *)
berghofe@17870
     2
berghofe@17870
     3
signature NOMINAL_PACKAGE =
berghofe@17870
     4
sig
berghofe@17870
     5
  val create_nom_typedecls : string list -> theory -> theory
berghofe@17870
     6
  val add_nominal_datatype : bool -> string list -> (string list * bstring * mixfix *
berghofe@17870
     7
    (bstring * string list * mixfix) list) list -> theory -> theory *
berghofe@17870
     8
      {distinct : thm list list,
berghofe@17870
     9
       inject : thm list list,
berghofe@17870
    10
       exhaustion : thm list,
berghofe@17870
    11
       rec_thms : thm list,
berghofe@17870
    12
       case_thms : thm list list,
berghofe@17870
    13
       split_thms : (thm * thm) list,
berghofe@17870
    14
       induction : thm,
berghofe@17870
    15
       size : thm list,
berghofe@17870
    16
       simps : thm list}
berghofe@17870
    17
  val setup : (theory -> theory) list
berghofe@17870
    18
end
berghofe@17870
    19
berghofe@17870
    20
structure NominalPackage (*: NOMINAL_PACKAGE *) =
berghofe@17870
    21
struct
berghofe@17870
    22
berghofe@17870
    23
open DatatypeAux;
berghofe@17870
    24
berghofe@17870
    25
(* data kind 'HOL/nominal' *)
berghofe@17870
    26
berghofe@17870
    27
structure NominalArgs =
berghofe@17870
    28
struct
berghofe@17870
    29
  val name = "HOL/nominal";
berghofe@17870
    30
  type T = unit Symtab.table;
berghofe@17870
    31
berghofe@17870
    32
  val empty = Symtab.empty;
berghofe@17870
    33
  val copy = I;
berghofe@17870
    34
  val extend = I;
berghofe@17870
    35
  fun merge _ x = Symtab.merge (K true) x;
berghofe@17870
    36
berghofe@17870
    37
  fun print sg tab = ();
berghofe@17870
    38
end;
berghofe@17870
    39
berghofe@17870
    40
structure NominalData = TheoryDataFun(NominalArgs);
berghofe@17870
    41
berghofe@17870
    42
fun atoms_of thy = map fst (Symtab.dest (NominalData.get thy));
berghofe@17870
    43
berghofe@17870
    44
(* FIXME: add to hologic.ML ? *)
berghofe@17870
    45
fun mk_listT T = Type ("List.list", [T]);
berghofe@17870
    46
fun mk_permT T = mk_listT (HOLogic.mk_prodT (T, T));
berghofe@17870
    47
berghofe@17870
    48
fun mk_Cons x xs =
berghofe@17870
    49
  let val T = fastype_of x
berghofe@17870
    50
  in Const ("List.list.Cons", T --> mk_listT T --> mk_listT T) $ x $ xs end;
berghofe@17870
    51
berghofe@17870
    52
berghofe@17870
    53
(* this function sets up all matters related to atom-  *)
berghofe@17870
    54
(* kinds; the user specifies a list of atom-kind names *)
berghofe@17870
    55
(* atom_decl <ak1> ... <akn>                           *)
berghofe@17870
    56
fun create_nom_typedecls ak_names thy =
berghofe@17870
    57
  let
berghofe@17870
    58
    (* declares a type-decl for every atom-kind: *) 
berghofe@17870
    59
    (* that is typedecl <ak>                     *)
berghofe@17870
    60
    val thy1 = TypedefPackage.add_typedecls (map (fn x => (x,[],NoSyn)) ak_names) thy;
berghofe@17870
    61
    
berghofe@17870
    62
    (* produces a list consisting of pairs:         *)
berghofe@17870
    63
    (*  fst component is the atom-kind name         *)
berghofe@17870
    64
    (*  snd component is its type                   *)
berghofe@17870
    65
    val full_ak_names = map (Sign.intern_type (sign_of thy1)) ak_names;
berghofe@17870
    66
    val ak_names_types = ak_names ~~ map (Type o rpair []) full_ak_names;
berghofe@17870
    67
     
berghofe@17870
    68
    (* adds for every atom-kind an axiom             *)
berghofe@17870
    69
    (* <ak>_infinite: infinite (UNIV::<ak_type> set) *)
berghofe@17870
    70
    val (thy2,inf_axs) = PureThy.add_axioms_i (map (fn (ak_name, T) =>
berghofe@17870
    71
      let 
berghofe@17870
    72
	val name = ak_name ^ "_infinite"
berghofe@17870
    73
        val axiom = HOLogic.mk_Trueprop (HOLogic.mk_not
berghofe@17870
    74
                    (HOLogic.mk_mem (HOLogic.mk_UNIV T,
berghofe@17870
    75
                     Const ("Finite_Set.Finites", HOLogic.mk_setT (HOLogic.mk_setT T)))))
berghofe@17870
    76
      in
berghofe@17870
    77
	((name, axiom), []) 
berghofe@17870
    78
      end) ak_names_types) thy1;
berghofe@17870
    79
    
berghofe@17870
    80
    (* declares a swapping function for every atom-kind, it is         *)
berghofe@17870
    81
    (* const swap_<ak> :: <akT> * <akT> => <akT> => <akT>              *)
berghofe@17870
    82
    (* swap_<ak> (a,b) c = (if a=c then b (else if b=c then a else c)) *)
berghofe@17870
    83
    (* overloades then the general swap-function                       *) 
berghofe@17870
    84
    val (thy3, swap_eqs) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
    85
      let
berghofe@17870
    86
        val swapT = HOLogic.mk_prodT (T, T) --> T --> T;
berghofe@17870
    87
        val swap_name = Sign.full_name (sign_of thy) ("swap_" ^ ak_name);
berghofe@17870
    88
        val a = Free ("a", T);
berghofe@17870
    89
        val b = Free ("b", T);
berghofe@17870
    90
        val c = Free ("c", T);
berghofe@17870
    91
        val ab = Free ("ab", HOLogic.mk_prodT (T, T))
berghofe@17870
    92
        val cif = Const ("HOL.If", HOLogic.boolT --> T --> T --> T);
berghofe@17870
    93
        val cswap_akname = Const (swap_name, swapT);
berghofe@17870
    94
        val cswap = Const ("nominal.swap", swapT)
berghofe@17870
    95
berghofe@17870
    96
        val name = "swap_"^ak_name^"_def";
berghofe@17870
    97
        val def1 = HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
    98
		   (cswap_akname $ HOLogic.mk_prod (a,b) $ c,
berghofe@17870
    99
                    cif $ HOLogic.mk_eq (a,c) $ b $ (cif $ HOLogic.mk_eq (b,c) $ a $ c)))
berghofe@17870
   100
        val def2 = Logic.mk_equals (cswap $ ab $ c, cswap_akname $ ab $ c)
berghofe@17870
   101
      in
berghofe@17870
   102
        thy |> Theory.add_consts_i [("swap_" ^ ak_name, swapT, NoSyn)] 
berghofe@17870
   103
            |> (#1 o PureThy.add_defs_i true [((name, def2),[])])
berghofe@17870
   104
            |> PrimrecPackage.add_primrec_i "" [(("", def1),[])]            
berghofe@17870
   105
      end) (thy2, ak_names_types);
berghofe@17870
   106
    
berghofe@17870
   107
    (* declares a permutation function for every atom-kind acting  *)
berghofe@17870
   108
    (* on such atoms                                               *)
berghofe@17870
   109
    (* const <ak>_prm_<ak> :: (<akT> * <akT>)list => akT => akT    *)
berghofe@17870
   110
    (* <ak>_prm_<ak> []     a = a                                  *)
berghofe@17870
   111
    (* <ak>_prm_<ak> (x#xs) a = swap_<ak> x (perm xs a)            *)
berghofe@17870
   112
    val (thy4, prm_eqs) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   113
      let
berghofe@17870
   114
        val swapT = HOLogic.mk_prodT (T, T) --> T --> T;
berghofe@17870
   115
        val swap_name = Sign.full_name (sign_of thy) ("swap_" ^ ak_name)
berghofe@17870
   116
        val prmT = mk_permT T --> T --> T;
berghofe@17870
   117
        val prm_name = ak_name ^ "_prm_" ^ ak_name;
berghofe@17870
   118
        val qu_prm_name = Sign.full_name (sign_of thy) prm_name;
berghofe@17870
   119
        val x  = Free ("x", HOLogic.mk_prodT (T, T));
berghofe@17870
   120
        val xs = Free ("xs", mk_permT T);
berghofe@17870
   121
        val a  = Free ("a", T) ;
berghofe@17870
   122
berghofe@17870
   123
        val cnil  = Const ("List.list.Nil", mk_permT T);
berghofe@17870
   124
        
berghofe@17870
   125
        val def1 = HOLogic.mk_Trueprop (HOLogic.mk_eq (Const (qu_prm_name, prmT) $ cnil $ a, a));
berghofe@17870
   126
berghofe@17870
   127
        val def2 = HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
   128
                   (Const (qu_prm_name, prmT) $ mk_Cons x xs $ a,
berghofe@17870
   129
                    Const (swap_name, swapT) $ x $ (Const (qu_prm_name, prmT) $ xs $ a)));
berghofe@17870
   130
      in
berghofe@17870
   131
        thy |> Theory.add_consts_i [(prm_name, mk_permT T --> T --> T, NoSyn)] 
berghofe@17870
   132
            |> PrimrecPackage.add_primrec_i "" [(("", def1), []),(("", def2), [])]
berghofe@17870
   133
      end) (thy3, ak_names_types);
berghofe@17870
   134
    
berghofe@17870
   135
    (* defines permutation functions for all combinations of atom-kinds; *)
berghofe@17870
   136
    (* there are a trivial cases and non-trivial cases                   *)
berghofe@17870
   137
    (* non-trivial case:                                                 *)
berghofe@17870
   138
    (* <ak>_prm_<ak>_def:  perm pi a == <ak>_prm_<ak> pi a               *)
berghofe@17870
   139
    (* trivial case with <ak> != <ak'>                                   *)
berghofe@17870
   140
    (* <ak>_prm<ak'>_def[simp]:  perm pi a == a                          *)
berghofe@17870
   141
    (*                                                                   *)
berghofe@17870
   142
    (* the trivial cases are added to the simplifier, while the non-     *)
berghofe@17870
   143
    (* have their own rules proved below                                 *)  
berghofe@17870
   144
    val (thy5, perm_defs) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   145
      foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   146
        let
berghofe@17870
   147
          val perm_def_name = ak_name ^ "_prm_" ^ ak_name';
berghofe@17870
   148
          val pi = Free ("pi", mk_permT T);
berghofe@17870
   149
          val a  = Free ("a", T');
berghofe@17870
   150
          val cperm = Const ("nominal.perm", mk_permT T --> T' --> T');
berghofe@17870
   151
          val cperm_def = Const (Sign.full_name (sign_of thy') perm_def_name, mk_permT T --> T' --> T');
berghofe@17870
   152
berghofe@17870
   153
          val name = ak_name ^ "_prm_" ^ ak_name' ^ "_def";
berghofe@17870
   154
          val def = Logic.mk_equals
berghofe@17870
   155
                    (cperm $ pi $ a, if ak_name = ak_name' then cperm_def $ pi $ a else a)
berghofe@17870
   156
        in
berghofe@17870
   157
          thy' |> PureThy.add_defs_i true [((name, def),[])] 
berghofe@17870
   158
        end) (thy, ak_names_types)) (thy4, ak_names_types);
berghofe@17870
   159
    
berghofe@17870
   160
    (* proves that every atom-kind is an instance of at *)
berghofe@17870
   161
    (* lemma at_<ak>_inst:                              *)
berghofe@17870
   162
    (* at TYPE(<ak>)                                    *)
berghofe@17870
   163
    val (thy6, prm_cons_thms) = 
berghofe@17870
   164
      thy5 |> PureThy.add_thms (map (fn (ak_name, T) =>
berghofe@17870
   165
      let
berghofe@17870
   166
        val ak_name_qu = Sign.full_name (sign_of thy5) (ak_name);
berghofe@17870
   167
        val i_type = Type(ak_name_qu,[]);
berghofe@17870
   168
	val cat = Const ("nominal.at",(Term.itselfT i_type)  --> HOLogic.boolT);
berghofe@17870
   169
        val at_type = Logic.mk_type i_type;
berghofe@17870
   170
        val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy5
berghofe@17870
   171
                                  [Name "at_def",
berghofe@17870
   172
                                   Name (ak_name ^ "_prm_" ^ ak_name ^ "_def"),
berghofe@17870
   173
                                   Name (ak_name ^ "_prm_" ^ ak_name ^ ".simps"),
berghofe@17870
   174
                                   Name ("swap_" ^ ak_name ^ "_def"),
berghofe@17870
   175
                                   Name ("swap_" ^ ak_name ^ ".simps"),
berghofe@17870
   176
                                   Name (ak_name ^ "_infinite")]
berghofe@17870
   177
	    
berghofe@17870
   178
	val name = "at_"^ak_name^ "_inst";
berghofe@17870
   179
        val statement = HOLogic.mk_Trueprop (cat $ at_type);
berghofe@17870
   180
berghofe@18010
   181
        val proof = fn _ => auto_tac (claset(),simp_s);
berghofe@17870
   182
berghofe@17870
   183
      in 
berghofe@18010
   184
        ((name, standard (Goal.prove thy5 [] [] statement proof)), []) 
berghofe@17870
   185
      end) ak_names_types);
berghofe@17870
   186
berghofe@17870
   187
    (* declares a perm-axclass for every atom-kind               *)
berghofe@17870
   188
    (* axclass pt_<ak>                                           *)
berghofe@17870
   189
    (* pt_<ak>1[simp]: perm [] x = x                             *)
berghofe@17870
   190
    (* pt_<ak>2:       perm (pi1@pi2) x = perm pi1 (perm pi2 x)  *)
berghofe@17870
   191
    (* pt_<ak>3:       pi1 ~ pi2 ==> perm pi1 x = perm pi2 x     *)
berghofe@17870
   192
     val (thy7, pt_ax_classes) =  foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   193
      let 
berghofe@17870
   194
	  val cl_name = "pt_"^ak_name;
berghofe@17870
   195
          val ty = TFree("'a",["HOL.type"]);
berghofe@17870
   196
          val x   = Free ("x", ty);
berghofe@17870
   197
          val pi1 = Free ("pi1", mk_permT T);
berghofe@17870
   198
          val pi2 = Free ("pi2", mk_permT T);
berghofe@17870
   199
          val cperm = Const ("nominal.perm", mk_permT T --> ty --> ty);
berghofe@17870
   200
          val cnil  = Const ("List.list.Nil", mk_permT T);
berghofe@17870
   201
          val cappend = Const ("List.op @",mk_permT T --> mk_permT T --> mk_permT T);
berghofe@17870
   202
          val cprm_eq = Const ("nominal.prm_eq",mk_permT T --> mk_permT T --> HOLogic.boolT);
berghofe@17870
   203
          (* nil axiom *)
berghofe@17870
   204
          val axiom1 = HOLogic.mk_Trueprop (HOLogic.mk_eq 
berghofe@17870
   205
                       (cperm $ cnil $ x, x));
berghofe@17870
   206
          (* append axiom *)
berghofe@17870
   207
          val axiom2 = HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
   208
                       (cperm $ (cappend $ pi1 $ pi2) $ x, cperm $ pi1 $ (cperm $ pi2 $ x)));
berghofe@17870
   209
          (* perm-eq axiom *)
berghofe@17870
   210
          val axiom3 = Logic.mk_implies
berghofe@17870
   211
                       (HOLogic.mk_Trueprop (cprm_eq $ pi1 $ pi2),
berghofe@17870
   212
                        HOLogic.mk_Trueprop (HOLogic.mk_eq (cperm $ pi1 $ x, cperm $ pi2 $ x)));
berghofe@17870
   213
      in
berghofe@17870
   214
        thy |> AxClass.add_axclass_i (cl_name, ["HOL.type"])
berghofe@17870
   215
                [((cl_name^"1", axiom1),[Simplifier.simp_add_global]), 
berghofe@17870
   216
                 ((cl_name^"2", axiom2),[]),                           
berghofe@17870
   217
                 ((cl_name^"3", axiom3),[])]                          
berghofe@17870
   218
      end) (thy6,ak_names_types);
berghofe@17870
   219
berghofe@17870
   220
    (* proves that every pt_<ak>-type together with <ak>-type *)
berghofe@17870
   221
    (* instance of pt                                         *)
berghofe@17870
   222
    (* lemma pt_<ak>_inst:                                    *)
berghofe@17870
   223
    (* pt TYPE('x::pt_<ak>) TYPE(<ak>)                        *)
berghofe@17870
   224
    val (thy8, prm_inst_thms) = 
berghofe@17870
   225
      thy7 |> PureThy.add_thms (map (fn (ak_name, T) =>
berghofe@17870
   226
      let
berghofe@17870
   227
        val ak_name_qu = Sign.full_name (sign_of thy7) (ak_name);
berghofe@17870
   228
        val pt_name_qu = Sign.full_name (sign_of thy7) ("pt_"^ak_name);
berghofe@17870
   229
        val i_type1 = TFree("'x",[pt_name_qu]);
berghofe@17870
   230
        val i_type2 = Type(ak_name_qu,[]);
berghofe@17870
   231
	val cpt = Const ("nominal.pt",(Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT);
berghofe@17870
   232
        val pt_type = Logic.mk_type i_type1;
berghofe@17870
   233
        val at_type = Logic.mk_type i_type2;
berghofe@17870
   234
        val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy7
berghofe@17870
   235
                                  [Name "pt_def",
berghofe@17870
   236
                                   Name ("pt_" ^ ak_name ^ "1"),
berghofe@17870
   237
                                   Name ("pt_" ^ ak_name ^ "2"),
berghofe@17870
   238
                                   Name ("pt_" ^ ak_name ^ "3")];
berghofe@17870
   239
berghofe@17870
   240
	val name = "pt_"^ak_name^ "_inst";
berghofe@17870
   241
        val statement = HOLogic.mk_Trueprop (cpt $ pt_type $ at_type);
berghofe@17870
   242
berghofe@18010
   243
        val proof = fn _ => auto_tac (claset(),simp_s);
berghofe@17870
   244
      in 
berghofe@18010
   245
        ((name, standard (Goal.prove thy7 [] [] statement proof)), []) 
berghofe@17870
   246
      end) ak_names_types);
berghofe@17870
   247
berghofe@17870
   248
     (* declares an fs-axclass for every atom-kind       *)
berghofe@17870
   249
     (* axclass fs_<ak>                                  *)
berghofe@17870
   250
     (* fs_<ak>1: finite ((supp x)::<ak> set)            *)
berghofe@17870
   251
     val (thy11, fs_ax_classes) =  foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   252
       let 
berghofe@17870
   253
	  val cl_name = "fs_"^ak_name;
berghofe@17870
   254
	  val pt_name = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   255
          val ty = TFree("'a",["HOL.type"]);
berghofe@17870
   256
          val x   = Free ("x", ty);
berghofe@17870
   257
          val csupp    = Const ("nominal.supp", ty --> HOLogic.mk_setT T);
berghofe@17870
   258
          val cfinites = Const ("Finite_Set.Finites", HOLogic.mk_setT (HOLogic.mk_setT T))
berghofe@17870
   259
          
berghofe@17870
   260
          val axiom1   = HOLogic.mk_Trueprop (HOLogic.mk_mem (csupp $ x, cfinites));
berghofe@17870
   261
berghofe@17870
   262
       in  
berghofe@17870
   263
        thy |> AxClass.add_axclass_i (cl_name, [pt_name]) [((cl_name^"1", axiom1),[])]            
berghofe@17870
   264
       end) (thy8,ak_names_types); 
berghofe@17870
   265
berghofe@17870
   266
     (* proves that every fs_<ak>-type together with <ak>-type   *)
berghofe@17870
   267
     (* instance of fs-type                                      *)
berghofe@17870
   268
     (* lemma abst_<ak>_inst:                                    *)
berghofe@17870
   269
     (* fs TYPE('x::pt_<ak>) TYPE (<ak>)                         *)
berghofe@17870
   270
     val (thy12, fs_inst_thms) = 
berghofe@17870
   271
       thy11 |> PureThy.add_thms (map (fn (ak_name, T) =>
berghofe@17870
   272
       let
berghofe@17870
   273
         val ak_name_qu = Sign.full_name (sign_of thy11) (ak_name);
berghofe@17870
   274
         val fs_name_qu = Sign.full_name (sign_of thy11) ("fs_"^ak_name);
berghofe@17870
   275
         val i_type1 = TFree("'x",[fs_name_qu]);
berghofe@17870
   276
         val i_type2 = Type(ak_name_qu,[]);
berghofe@17870
   277
 	 val cfs = Const ("nominal.fs", 
berghofe@17870
   278
                                 (Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT);
berghofe@17870
   279
         val fs_type = Logic.mk_type i_type1;
berghofe@17870
   280
         val at_type = Logic.mk_type i_type2;
berghofe@17870
   281
	 val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy11
berghofe@17870
   282
                                   [Name "fs_def",
berghofe@17870
   283
                                    Name ("fs_" ^ ak_name ^ "1")];
berghofe@17870
   284
    
berghofe@17870
   285
	 val name = "fs_"^ak_name^ "_inst";
berghofe@17870
   286
         val statement = HOLogic.mk_Trueprop (cfs $ fs_type $ at_type);
berghofe@17870
   287
berghofe@18010
   288
         val proof = fn _ => auto_tac (claset(),simp_s);
berghofe@17870
   289
       in 
berghofe@18010
   290
         ((name, standard (Goal.prove thy11 [] [] statement proof)), []) 
berghofe@17870
   291
       end) ak_names_types);
berghofe@17870
   292
berghofe@17870
   293
       (* declares for every atom-kind combination an axclass            *)
berghofe@17870
   294
       (* cp_<ak1>_<ak2> giving a composition property                   *)
berghofe@17870
   295
       (* cp_<ak1>_<ak2>1: pi1 o pi2 o x = (pi1 o pi2) o (pi1 o x)       *)
berghofe@17870
   296
        val (thy12b,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   297
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   298
	     let
berghofe@17870
   299
	       val cl_name = "cp_"^ak_name^"_"^ak_name';
berghofe@17870
   300
	       val ty = TFree("'a",["HOL.type"]);
berghofe@17870
   301
               val x   = Free ("x", ty);
berghofe@17870
   302
               val pi1 = Free ("pi1", mk_permT T);
berghofe@17870
   303
	       val pi2 = Free ("pi2", mk_permT T');                  
berghofe@17870
   304
	       val cperm1 = Const ("nominal.perm", mk_permT T  --> ty --> ty);
berghofe@17870
   305
               val cperm2 = Const ("nominal.perm", mk_permT T' --> ty --> ty);
berghofe@17870
   306
               val cperm3 = Const ("nominal.perm", mk_permT T  --> mk_permT T' --> mk_permT T');
berghofe@17870
   307
berghofe@17870
   308
               val ax1   = HOLogic.mk_Trueprop 
berghofe@17870
   309
			   (HOLogic.mk_eq (cperm1 $ pi1 $ (cperm2 $ pi2 $ x), 
berghofe@17870
   310
                                           cperm2 $ (cperm3 $ pi1 $ pi2) $ (cperm1 $ pi1 $ x)));
berghofe@17870
   311
	       in  
berghofe@17870
   312
	       (fst (AxClass.add_axclass_i (cl_name, ["HOL.type"]) [((cl_name^"1", ax1),[])] thy'),())  
berghofe@17870
   313
	       end) 
berghofe@17870
   314
	   (thy, ak_names_types)) (thy12, ak_names_types)
berghofe@17870
   315
berghofe@17870
   316
        (* proves for every <ak>-combination a cp_<ak1>_<ak2>_inst theorem;     *)
berghofe@17870
   317
        (* lemma cp_<ak1>_<ak2>_inst:                                           *)
berghofe@17870
   318
        (* cp TYPE('a::cp_<ak1>_<ak2>) TYPE(<ak1>) TYPE(<ak2>)                  *)
berghofe@17870
   319
        val (thy12c, cp_thms) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   320
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   321
           let
berghofe@17870
   322
             val ak_name_qu  = Sign.full_name (sign_of thy') (ak_name);
berghofe@17870
   323
	     val ak_name_qu' = Sign.full_name (sign_of thy') (ak_name');
berghofe@17870
   324
             val cp_name_qu  = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   325
             val i_type0 = TFree("'a",[cp_name_qu]);
berghofe@17870
   326
             val i_type1 = Type(ak_name_qu,[]);
berghofe@17870
   327
             val i_type2 = Type(ak_name_qu',[]);
berghofe@17870
   328
	     val ccp = Const ("nominal.cp",
berghofe@17870
   329
                             (Term.itselfT i_type0)-->(Term.itselfT i_type1)-->
berghofe@17870
   330
                                                      (Term.itselfT i_type2)-->HOLogic.boolT);
berghofe@17870
   331
             val at_type  = Logic.mk_type i_type1;
berghofe@17870
   332
             val at_type' = Logic.mk_type i_type2;
berghofe@17870
   333
	     val cp_type  = Logic.mk_type i_type0;
berghofe@17870
   334
             val simp_s   = HOL_basic_ss addsimps PureThy.get_thmss thy' [(Name "cp_def")];
berghofe@17870
   335
	     val cp1      = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"1"));
berghofe@17870
   336
berghofe@17870
   337
	     val name = "cp_"^ak_name^ "_"^ak_name'^"_inst";
berghofe@17870
   338
             val statement = HOLogic.mk_Trueprop (ccp $ cp_type $ at_type $ at_type');
berghofe@17870
   339
berghofe@18010
   340
             val proof = fn _ => EVERY [auto_tac (claset(),simp_s), rtac cp1 1];
berghofe@17870
   341
	   in
berghofe@17870
   342
	     thy' |> PureThy.add_thms 
berghofe@18010
   343
                    [((name, standard (Goal.prove thy' [] [] statement proof)), [])]
berghofe@17870
   344
	   end) 
berghofe@17870
   345
	   (thy, ak_names_types)) (thy12b, ak_names_types);
berghofe@17870
   346
       
berghofe@17870
   347
        (* proves for every non-trivial <ak>-combination a disjointness   *)
berghofe@17870
   348
        (* theorem; i.e. <ak1> != <ak2>                                   *)
berghofe@17870
   349
        (* lemma ds_<ak1>_<ak2>:                                          *)
berghofe@17870
   350
        (* dj TYPE(<ak1>) TYPE(<ak2>)                                     *)
berghofe@17870
   351
        val (thy12d, dj_thms) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   352
	  foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   353
          (if not (ak_name = ak_name') 
berghofe@17870
   354
           then 
berghofe@17870
   355
	       let
berghofe@17870
   356
		 val ak_name_qu  = Sign.full_name (sign_of thy') (ak_name);
berghofe@17870
   357
	         val ak_name_qu' = Sign.full_name (sign_of thy') (ak_name');
berghofe@17870
   358
                 val i_type1 = Type(ak_name_qu,[]);
berghofe@17870
   359
                 val i_type2 = Type(ak_name_qu',[]);
berghofe@17870
   360
	         val cdj = Const ("nominal.disjoint",
berghofe@17870
   361
                           (Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT);
berghofe@17870
   362
                 val at_type  = Logic.mk_type i_type1;
berghofe@17870
   363
                 val at_type' = Logic.mk_type i_type2;
berghofe@17870
   364
                 val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy' 
berghofe@17870
   365
					   [Name "disjoint_def",
berghofe@17870
   366
                                            Name (ak_name^"_prm_"^ak_name'^"_def"),
berghofe@17870
   367
                                            Name (ak_name'^"_prm_"^ak_name^"_def")];
berghofe@17870
   368
berghofe@17870
   369
	         val name = "dj_"^ak_name^"_"^ak_name';
berghofe@17870
   370
                 val statement = HOLogic.mk_Trueprop (cdj $ at_type $ at_type');
berghofe@17870
   371
berghofe@18010
   372
                 val proof = fn _ => auto_tac (claset(),simp_s);
berghofe@17870
   373
	       in
berghofe@17870
   374
		   thy' |> PureThy.add_thms 
berghofe@18010
   375
                        [((name, standard (Goal.prove thy' [] [] statement proof)), []) ]
berghofe@17870
   376
	       end
berghofe@17870
   377
           else 
berghofe@17870
   378
            (thy',[])))  (* do nothing branch, if ak_name = ak_name' *) 
berghofe@17870
   379
	   (thy, ak_names_types)) (thy12c, ak_names_types);
berghofe@17870
   380
berghofe@17870
   381
     (*<<<<<<<  pt_<ak> class instances  >>>>>>>*)
berghofe@17870
   382
     (*=========================================*)
berghofe@17870
   383
     
berghofe@17870
   384
     (* some frequently used theorems *)
berghofe@17870
   385
      val pt1 = PureThy.get_thm thy12c (Name "pt1");
berghofe@17870
   386
      val pt2 = PureThy.get_thm thy12c (Name "pt2");
berghofe@17870
   387
      val pt3 = PureThy.get_thm thy12c (Name "pt3");
berghofe@17870
   388
      val at_pt_inst    = PureThy.get_thm thy12c (Name "at_pt_inst");
berghofe@17870
   389
      val pt_bool_inst  = PureThy.get_thm thy12c (Name "pt_bool_inst");
berghofe@17870
   390
      val pt_set_inst   = PureThy.get_thm thy12c (Name "pt_set_inst"); 
berghofe@17870
   391
      val pt_unit_inst  = PureThy.get_thm thy12c (Name "pt_unit_inst");
berghofe@17870
   392
      val pt_prod_inst  = PureThy.get_thm thy12c (Name "pt_prod_inst"); 
berghofe@17870
   393
      val pt_list_inst  = PureThy.get_thm thy12c (Name "pt_list_inst");   
berghofe@17870
   394
      val pt_optn_inst  = PureThy.get_thm thy12c (Name "pt_option_inst");   
berghofe@17870
   395
      val pt_noptn_inst = PureThy.get_thm thy12c (Name "pt_noption_inst");   
berghofe@17870
   396
      val pt_fun_inst   = PureThy.get_thm thy12c (Name "pt_fun_inst");     
berghofe@17870
   397
berghofe@17870
   398
     (* for all atom-kind combination shows that         *)
berghofe@17870
   399
     (* every <ak> is an instance of pt_<ai>             *)
berghofe@17870
   400
     val (thy13,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   401
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   402
          (if ak_name = ak_name'
berghofe@17870
   403
	   then
berghofe@17870
   404
	     let
berghofe@17870
   405
	      val qu_name =  Sign.full_name (sign_of thy') ak_name;
berghofe@17870
   406
              val qu_class = Sign.full_name (sign_of thy') ("pt_"^ak_name);
berghofe@17870
   407
              val at_inst  = PureThy.get_thm thy' (Name ("at_"^ak_name ^"_inst"));
berghofe@17870
   408
              val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   409
                                 rtac ((at_inst RS at_pt_inst) RS pt1) 1,
berghofe@17870
   410
                                 rtac ((at_inst RS at_pt_inst) RS pt2) 1,
berghofe@17870
   411
                                 rtac ((at_inst RS at_pt_inst) RS pt3) 1,
berghofe@17870
   412
                                 atac 1];
berghofe@17870
   413
             in 
berghofe@17870
   414
	      (AxClass.add_inst_arity_i (qu_name,[],[qu_class]) proof thy',()) 
berghofe@17870
   415
             end
berghofe@17870
   416
           else 
berghofe@17870
   417
             let
berghofe@17870
   418
	      val qu_name' = Sign.full_name (sign_of thy') ak_name';
berghofe@17870
   419
              val qu_class = Sign.full_name (sign_of thy') ("pt_"^ak_name);
berghofe@17870
   420
              val simp_s = HOL_basic_ss addsimps 
berghofe@17870
   421
                           PureThy.get_thmss thy' [Name (ak_name^"_prm_"^ak_name'^"_def")];  
berghofe@17870
   422
              val proof = EVERY [AxClass.intro_classes_tac [], auto_tac (claset(),simp_s)];
berghofe@17870
   423
             in 
berghofe@17870
   424
	      (AxClass.add_inst_arity_i (qu_name',[],[qu_class]) proof thy',()) 
berghofe@17870
   425
             end)) 
berghofe@17870
   426
	     (thy, ak_names_types)) (thy12c, ak_names_types);
berghofe@17870
   427
berghofe@17870
   428
     (* shows that bool is an instance of pt_<ak>     *)
berghofe@17870
   429
     (* uses the theorem pt_bool_inst                 *)
berghofe@17870
   430
     val (thy14,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   431
       let
berghofe@17870
   432
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   433
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   434
                             rtac (pt_bool_inst RS pt1) 1,
berghofe@17870
   435
                             rtac (pt_bool_inst RS pt2) 1,
berghofe@17870
   436
                             rtac (pt_bool_inst RS pt3) 1,
berghofe@17870
   437
                             atac 1];
berghofe@17870
   438
       in 
berghofe@17870
   439
	 (AxClass.add_inst_arity_i ("bool",[],[qu_class]) proof thy,()) 
berghofe@17870
   440
       end) (thy13,ak_names_types); 
berghofe@17870
   441
berghofe@17870
   442
     (* shows that set(pt_<ak>) is an instance of pt_<ak>          *)
berghofe@17870
   443
     (* unfolds the permutation definition and applies pt_<ak>i    *)
berghofe@17870
   444
     val (thy15,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   445
       let
berghofe@17870
   446
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   447
          val pt_inst  = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst"));  
berghofe@17870
   448
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   449
                             rtac ((pt_inst RS pt_set_inst) RS pt1) 1,
berghofe@17870
   450
                             rtac ((pt_inst RS pt_set_inst) RS pt2) 1,
berghofe@17870
   451
                             rtac ((pt_inst RS pt_set_inst) RS pt3) 1,
berghofe@17870
   452
                             atac 1];
berghofe@17870
   453
       in 
berghofe@17870
   454
	 (AxClass.add_inst_arity_i ("set",[[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   455
       end) (thy14,ak_names_types); 
berghofe@17870
   456
berghofe@17870
   457
     (* shows that unit is an instance of pt_<ak>          *)
berghofe@17870
   458
     val (thy16,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   459
       let
berghofe@17870
   460
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   461
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   462
                             rtac (pt_unit_inst RS pt1) 1,
berghofe@17870
   463
                             rtac (pt_unit_inst RS pt2) 1,
berghofe@17870
   464
                             rtac (pt_unit_inst RS pt3) 1,
berghofe@17870
   465
                             atac 1];
berghofe@17870
   466
       in 
berghofe@17870
   467
	 (AxClass.add_inst_arity_i ("Product_Type.unit",[],[qu_class]) proof thy,()) 
berghofe@17870
   468
       end) (thy15,ak_names_types); 
berghofe@17870
   469
berghofe@17870
   470
     (* shows that *(pt_<ak>,pt_<ak>) is an instance of pt_<ak> *)
berghofe@17870
   471
     (* uses the theorem pt_prod_inst and pt_<ak>_inst          *)
berghofe@17870
   472
     val (thy17,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   473
       let
berghofe@17870
   474
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   475
          val pt_inst  = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst"));  
berghofe@17870
   476
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   477
                             rtac ((pt_inst RS (pt_inst RS pt_prod_inst)) RS pt1) 1,
berghofe@17870
   478
                             rtac ((pt_inst RS (pt_inst RS pt_prod_inst)) RS pt2) 1,
berghofe@17870
   479
                             rtac ((pt_inst RS (pt_inst RS pt_prod_inst)) RS pt3) 1,
berghofe@17870
   480
                             atac 1];
berghofe@17870
   481
       in 
berghofe@17870
   482
          (AxClass.add_inst_arity_i ("*",[[qu_class],[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   483
       end) (thy16,ak_names_types); 
berghofe@17870
   484
berghofe@17870
   485
     (* shows that list(pt_<ak>) is an instance of pt_<ak>     *)
berghofe@17870
   486
     (* uses the theorem pt_list_inst and pt_<ak>_inst         *)
berghofe@17870
   487
     val (thy18,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   488
       let
berghofe@17870
   489
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   490
          val pt_inst  = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst"));
berghofe@17870
   491
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   492
                             rtac ((pt_inst RS pt_list_inst) RS pt1) 1,
berghofe@17870
   493
                             rtac ((pt_inst RS pt_list_inst) RS pt2) 1,
berghofe@17870
   494
                             rtac ((pt_inst RS pt_list_inst) RS pt3) 1,
berghofe@17870
   495
                             atac 1];      
berghofe@17870
   496
       in 
berghofe@17870
   497
	 (AxClass.add_inst_arity_i ("List.list",[[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   498
       end) (thy17,ak_names_types); 
berghofe@17870
   499
berghofe@17870
   500
     (* shows that option(pt_<ak>) is an instance of pt_<ak>   *)
berghofe@17870
   501
     (* uses the theorem pt_option_inst and pt_<ak>_inst       *)
berghofe@17870
   502
     val (thy18a,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   503
       let
berghofe@17870
   504
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   505
          val pt_inst  = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst"));
berghofe@17870
   506
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   507
                             rtac ((pt_inst RS pt_optn_inst) RS pt1) 1,
berghofe@17870
   508
                             rtac ((pt_inst RS pt_optn_inst) RS pt2) 1,
berghofe@17870
   509
                             rtac ((pt_inst RS pt_optn_inst) RS pt3) 1,
berghofe@17870
   510
                             atac 1];      
berghofe@17870
   511
       in 
berghofe@17870
   512
	 (AxClass.add_inst_arity_i ("Datatype.option",[[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   513
       end) (thy18,ak_names_types); 
berghofe@17870
   514
berghofe@17870
   515
     (* shows that nOption(pt_<ak>) is an instance of pt_<ak>   *)
berghofe@17870
   516
     (* uses the theorem pt_option_inst and pt_<ak>_inst       *)
berghofe@17870
   517
     val (thy18b,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   518
       let
berghofe@17870
   519
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   520
          val pt_inst  = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst"));
berghofe@17870
   521
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   522
                             rtac ((pt_inst RS pt_noptn_inst) RS pt1) 1,
berghofe@17870
   523
                             rtac ((pt_inst RS pt_noptn_inst) RS pt2) 1,
berghofe@17870
   524
                             rtac ((pt_inst RS pt_noptn_inst) RS pt3) 1,
berghofe@17870
   525
                             atac 1];      
berghofe@17870
   526
       in 
berghofe@17870
   527
	 (AxClass.add_inst_arity_i ("nominal.nOption",[[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   528
       end) (thy18a,ak_names_types); 
berghofe@17870
   529
berghofe@17870
   530
berghofe@17870
   531
     (* shows that fun(pt_<ak>,pt_<ak>) is an instance of pt_<ak>     *)
berghofe@17870
   532
     (* uses the theorem pt_list_inst and pt_<ak>_inst                *)
berghofe@17870
   533
     val (thy19,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   534
       let
berghofe@17870
   535
          val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name);
berghofe@17870
   536
          val at_thm   = PureThy.get_thm thy (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   537
          val pt_inst  = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst"));
berghofe@17870
   538
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   539
                             rtac ((at_thm RS (pt_inst RS (pt_inst RS pt_fun_inst))) RS pt1) 1,
berghofe@17870
   540
                             rtac ((at_thm RS (pt_inst RS (pt_inst RS pt_fun_inst))) RS pt2) 1,
berghofe@17870
   541
                             rtac ((at_thm RS (pt_inst RS (pt_inst RS pt_fun_inst))) RS pt3) 1,
berghofe@17870
   542
                             atac 1];      
berghofe@17870
   543
       in 
berghofe@17870
   544
	 (AxClass.add_inst_arity_i ("fun",[[qu_class],[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   545
       end) (thy18b,ak_names_types);
berghofe@17870
   546
berghofe@17870
   547
       (*<<<<<<<  fs_<ak> class instances  >>>>>>>*)
berghofe@17870
   548
       (*=========================================*)
berghofe@17870
   549
       val fs1          = PureThy.get_thm thy19 (Name "fs1");
berghofe@17870
   550
       val fs_at_inst   = PureThy.get_thm thy19 (Name "fs_at_inst");
berghofe@17870
   551
       val fs_unit_inst = PureThy.get_thm thy19 (Name "fs_unit_inst");
berghofe@17870
   552
       val fs_bool_inst = PureThy.get_thm thy19 (Name "fs_bool_inst");
berghofe@17870
   553
       val fs_prod_inst = PureThy.get_thm thy19 (Name "fs_prod_inst");
berghofe@17870
   554
       val fs_list_inst = PureThy.get_thm thy19 (Name "fs_list_inst");
berghofe@17870
   555
berghofe@17870
   556
       (* shows that <ak> is an instance of fs_<ak>     *)
berghofe@17870
   557
       (* uses the theorem at_<ak>_inst                 *)
berghofe@17870
   558
       val (thy20,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   559
       let
berghofe@17870
   560
          val qu_name =  Sign.full_name (sign_of thy) ak_name;
berghofe@17870
   561
          val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name);
berghofe@17870
   562
          val at_thm   = PureThy.get_thm thy (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   563
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   564
                             rtac ((at_thm RS fs_at_inst) RS fs1) 1];      
berghofe@17870
   565
       in 
berghofe@17870
   566
	 (AxClass.add_inst_arity_i (qu_name,[],[qu_class]) proof thy,()) 
berghofe@17870
   567
       end) (thy19,ak_names_types);  
berghofe@17870
   568
berghofe@17870
   569
       (* shows that unit is an instance of fs_<ak>     *)
berghofe@17870
   570
       (* uses the theorem fs_unit_inst                 *)
berghofe@17870
   571
       val (thy21,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   572
       let
berghofe@17870
   573
          val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name);
berghofe@17870
   574
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   575
                             rtac (fs_unit_inst RS fs1) 1];      
berghofe@17870
   576
       in 
berghofe@17870
   577
	 (AxClass.add_inst_arity_i ("Product_Type.unit",[],[qu_class]) proof thy,()) 
berghofe@17870
   578
       end) (thy20,ak_names_types);  
berghofe@17870
   579
berghofe@17870
   580
       (* shows that bool is an instance of fs_<ak>     *)
berghofe@17870
   581
       (* uses the theorem fs_bool_inst                 *)
berghofe@17870
   582
       val (thy22,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   583
       let
berghofe@17870
   584
          val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name);
berghofe@17870
   585
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   586
                             rtac (fs_bool_inst RS fs1) 1];      
berghofe@17870
   587
       in 
berghofe@17870
   588
	 (AxClass.add_inst_arity_i ("bool",[],[qu_class]) proof thy,()) 
berghofe@17870
   589
       end) (thy21,ak_names_types);  
berghofe@17870
   590
berghofe@17870
   591
       (* shows that *(fs_<ak>,fs_<ak>) is an instance of fs_<ak>     *)
berghofe@17870
   592
       (* uses the theorem fs_prod_inst                               *)
berghofe@17870
   593
       val (thy23,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   594
       let
berghofe@17870
   595
          val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name);
berghofe@17870
   596
          val fs_inst  = PureThy.get_thm thy (Name ("fs_"^ak_name^"_inst"));
berghofe@17870
   597
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   598
                             rtac ((fs_inst RS (fs_inst RS fs_prod_inst)) RS fs1) 1];      
berghofe@17870
   599
       in 
berghofe@17870
   600
	 (AxClass.add_inst_arity_i ("*",[[qu_class],[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   601
       end) (thy22,ak_names_types);  
berghofe@17870
   602
berghofe@17870
   603
       (* shows that list(fs_<ak>) is an instance of fs_<ak>     *)
berghofe@17870
   604
       (* uses the theorem fs_list_inst                          *)
berghofe@17870
   605
       val (thy24,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   606
       let
berghofe@17870
   607
          val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name);
berghofe@17870
   608
          val fs_inst  = PureThy.get_thm thy (Name ("fs_"^ak_name^"_inst"));
berghofe@17870
   609
          val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   610
                              rtac ((fs_inst RS fs_list_inst) RS fs1) 1];      
berghofe@17870
   611
       in 
berghofe@17870
   612
	 (AxClass.add_inst_arity_i ("List.list",[[qu_class]],[qu_class]) proof thy,()) 
berghofe@17870
   613
       end) (thy23,ak_names_types);  
berghofe@17870
   614
	   
berghofe@17870
   615
       (*<<<<<<<  cp_<ak>_<ai> class instances  >>>>>>>*)
berghofe@17870
   616
       (*==============================================*)
berghofe@17870
   617
       val cp1             = PureThy.get_thm thy24 (Name "cp1");
berghofe@17870
   618
       val cp_unit_inst    = PureThy.get_thm thy24 (Name "cp_unit_inst");
berghofe@17870
   619
       val cp_bool_inst    = PureThy.get_thm thy24 (Name "cp_bool_inst");
berghofe@17870
   620
       val cp_prod_inst    = PureThy.get_thm thy24 (Name "cp_prod_inst");
berghofe@17870
   621
       val cp_list_inst    = PureThy.get_thm thy24 (Name "cp_list_inst");
berghofe@17870
   622
       val cp_fun_inst     = PureThy.get_thm thy24 (Name "cp_fun_inst");
berghofe@17870
   623
       val cp_option_inst  = PureThy.get_thm thy24 (Name "cp_option_inst");
berghofe@17870
   624
       val cp_noption_inst = PureThy.get_thm thy24 (Name "cp_noption_inst");
berghofe@17870
   625
       val pt_perm_compose = PureThy.get_thm thy24 (Name "pt_perm_compose");
berghofe@17870
   626
       val dj_pp_forget    = PureThy.get_thm thy24 (Name "dj_perm_perm_forget");
berghofe@17870
   627
berghofe@17870
   628
       (* shows that <aj> is an instance of cp_<ak>_<ai>  *)
berghofe@17870
   629
       (* that needs a three-nested loop *)
berghofe@17870
   630
       val (thy25,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   631
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   632
          foldl_map (fn (thy'', (ak_name'', T'')) =>
berghofe@17870
   633
            let
berghofe@17870
   634
              val qu_name =  Sign.full_name (sign_of thy'') ak_name;
berghofe@17870
   635
              val qu_class = Sign.full_name (sign_of thy'') ("cp_"^ak_name'^"_"^ak_name'');
berghofe@17870
   636
              val proof =
berghofe@17870
   637
                (if (ak_name'=ak_name'') then 
berghofe@17870
   638
		  (let
berghofe@17870
   639
                    val pt_inst  = PureThy.get_thm thy'' (Name ("pt_"^ak_name''^"_inst"));
berghofe@17870
   640
		    val at_inst  = PureThy.get_thm thy'' (Name ("at_"^ak_name''^"_inst"));
berghofe@17870
   641
                  in 
berghofe@17870
   642
		   EVERY [AxClass.intro_classes_tac [], 
berghofe@17870
   643
                          rtac (at_inst RS (pt_inst RS pt_perm_compose)) 1]
berghofe@17870
   644
                  end)
berghofe@17870
   645
		else
berghofe@17870
   646
		  (let 
berghofe@17870
   647
                     val dj_inst  = PureThy.get_thm thy'' (Name ("dj_"^ak_name''^"_"^ak_name'));
berghofe@17870
   648
		     val simp_s = HOL_basic_ss addsimps 
berghofe@17870
   649
                                        ((dj_inst RS dj_pp_forget)::
berghofe@17870
   650
                                         (PureThy.get_thmss thy'' 
berghofe@17870
   651
					   [Name (ak_name' ^"_prm_"^ak_name^"_def"),
berghofe@17870
   652
                                            Name (ak_name''^"_prm_"^ak_name^"_def")]));  
berghofe@17870
   653
		  in 
berghofe@17870
   654
                    EVERY [AxClass.intro_classes_tac [], simp_tac simp_s 1]
berghofe@17870
   655
                  end))
berghofe@17870
   656
	      in
berghofe@17870
   657
                (AxClass.add_inst_arity_i (qu_name,[],[qu_class]) proof thy'',())
berghofe@17870
   658
	      end)
berghofe@17870
   659
	   (thy', ak_names_types)) (thy, ak_names_types)) (thy24, ak_names_types);
berghofe@17870
   660
      
berghofe@17870
   661
       (* shows that unit is an instance of cp_<ak>_<ai>     *)
berghofe@17870
   662
       (* for every <ak>-combination                         *)
berghofe@17870
   663
       val (thy26,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   664
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   665
          let
berghofe@17870
   666
            val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   667
            val proof = EVERY [AxClass.intro_classes_tac [],rtac (cp_unit_inst RS cp1) 1];     
berghofe@17870
   668
	  in
berghofe@17870
   669
            (AxClass.add_inst_arity_i ("Product_Type.unit",[],[qu_class]) proof thy',())
berghofe@17870
   670
	  end) 
berghofe@17870
   671
	   (thy, ak_names_types)) (thy25, ak_names_types);
berghofe@17870
   672
       
berghofe@17870
   673
       (* shows that bool is an instance of cp_<ak>_<ai>     *)
berghofe@17870
   674
       (* for every <ak>-combination                         *)
berghofe@17870
   675
       val (thy27,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   676
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   677
           let
berghofe@17870
   678
	     val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   679
             val proof = EVERY [AxClass.intro_classes_tac [], rtac (cp_bool_inst RS cp1) 1];     
berghofe@17870
   680
	   in
berghofe@17870
   681
             (AxClass.add_inst_arity_i ("bool",[],[qu_class]) proof thy',())
berghofe@17870
   682
	   end) 
berghofe@17870
   683
	   (thy, ak_names_types)) (thy26, ak_names_types);
berghofe@17870
   684
berghofe@17870
   685
       (* shows that prod is an instance of cp_<ak>_<ai>     *)
berghofe@17870
   686
       (* for every <ak>-combination                         *)
berghofe@17870
   687
       val (thy28,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   688
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   689
          let
berghofe@17870
   690
	    val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   691
            val cp_inst  = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   692
            val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   693
                               rtac ((cp_inst RS (cp_inst RS cp_prod_inst)) RS cp1) 1];     
berghofe@17870
   694
	  in
berghofe@17870
   695
            (AxClass.add_inst_arity_i ("*",[[qu_class],[qu_class]],[qu_class]) proof thy',())
berghofe@17870
   696
	  end)  
berghofe@17870
   697
	  (thy, ak_names_types)) (thy27, ak_names_types);
berghofe@17870
   698
berghofe@17870
   699
       (* shows that list is an instance of cp_<ak>_<ai>     *)
berghofe@17870
   700
       (* for every <ak>-combination                         *)
berghofe@17870
   701
       val (thy29,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   702
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   703
           let
berghofe@17870
   704
	     val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   705
             val cp_inst  = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   706
             val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   707
                                rtac ((cp_inst RS cp_list_inst) RS cp1) 1];     
berghofe@17870
   708
	   in
berghofe@17870
   709
            (AxClass.add_inst_arity_i ("List.list",[[qu_class]],[qu_class]) proof thy',())
berghofe@17870
   710
	   end) 
berghofe@17870
   711
	   (thy, ak_names_types)) (thy28, ak_names_types);
berghofe@17870
   712
berghofe@17870
   713
       (* shows that function is an instance of cp_<ak>_<ai>     *)
berghofe@17870
   714
       (* for every <ak>-combination                             *)
berghofe@17870
   715
       val (thy30,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   716
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   717
          let
berghofe@17870
   718
	    val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   719
            val cp_inst  = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   720
            val pt_inst  = PureThy.get_thm thy' (Name ("pt_"^ak_name^"_inst"));
berghofe@17870
   721
            val at_inst  = PureThy.get_thm thy' (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   722
            val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   723
                    rtac ((at_inst RS (pt_inst RS (cp_inst RS (cp_inst RS cp_fun_inst)))) RS cp1) 1];  
berghofe@17870
   724
	  in
berghofe@17870
   725
            (AxClass.add_inst_arity_i ("fun",[[qu_class],[qu_class]],[qu_class]) proof thy',())
berghofe@17870
   726
	  end) 
berghofe@17870
   727
	  (thy, ak_names_types)) (thy29, ak_names_types);
berghofe@17870
   728
berghofe@17870
   729
       (* shows that option is an instance of cp_<ak>_<ai>     *)
berghofe@17870
   730
       (* for every <ak>-combination                           *)
berghofe@17870
   731
       val (thy31,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   732
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   733
          let
berghofe@17870
   734
	    val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   735
            val cp_inst  = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   736
            val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   737
                               rtac ((cp_inst RS cp_option_inst) RS cp1) 1];     
berghofe@17870
   738
	  in
berghofe@17870
   739
            (AxClass.add_inst_arity_i ("Datatype.option",[[qu_class]],[qu_class]) proof thy',())
berghofe@17870
   740
	  end) 
berghofe@17870
   741
	  (thy, ak_names_types)) (thy30, ak_names_types);
berghofe@17870
   742
berghofe@17870
   743
       (* shows that nOption is an instance of cp_<ak>_<ai>     *)
berghofe@17870
   744
       (* for every <ak>-combination                            *)
berghofe@17870
   745
       val (thy32,_) = foldl_map (fn (thy, (ak_name, T)) =>
berghofe@17870
   746
	 foldl_map (fn (thy', (ak_name', T')) =>
berghofe@17870
   747
          let
berghofe@17870
   748
	    val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name');
berghofe@17870
   749
            val cp_inst  = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   750
            val proof = EVERY [AxClass.intro_classes_tac [],
berghofe@17870
   751
                               rtac ((cp_inst RS cp_noption_inst) RS cp1) 1];     
berghofe@17870
   752
	  in
berghofe@17870
   753
           (AxClass.add_inst_arity_i ("nominal.nOption",[[qu_class]],[qu_class]) proof thy',())
berghofe@17870
   754
	  end) 
berghofe@17870
   755
	  (thy, ak_names_types)) (thy31, ak_names_types);
berghofe@17870
   756
berghofe@17870
   757
       (* abbreviations for some collection of rules *)
berghofe@17870
   758
       (*============================================*)
berghofe@17870
   759
       val abs_fun_pi        = PureThy.get_thm thy32 (Name ("nominal.abs_fun_pi"));
berghofe@17870
   760
       val abs_fun_pi_ineq   = PureThy.get_thm thy32 (Name ("nominal.abs_fun_pi_ineq"));
berghofe@17870
   761
       val abs_fun_eq        = PureThy.get_thm thy32 (Name ("nominal.abs_fun_eq"));
berghofe@17870
   762
       val dj_perm_forget    = PureThy.get_thm thy32 (Name ("nominal.dj_perm_forget"));
berghofe@17870
   763
       val dj_pp_forget      = PureThy.get_thm thy32 (Name ("nominal.dj_perm_perm_forget"));
berghofe@17870
   764
       val fresh_iff         = PureThy.get_thm thy32 (Name ("nominal.fresh_abs_fun_iff"));
berghofe@17870
   765
       val fresh_iff_ineq    = PureThy.get_thm thy32 (Name ("nominal.fresh_abs_fun_iff_ineq"));
berghofe@17870
   766
       val abs_fun_supp      = PureThy.get_thm thy32 (Name ("nominal.abs_fun_supp"));
berghofe@17870
   767
       val abs_fun_supp_ineq = PureThy.get_thm thy32 (Name ("nominal.abs_fun_supp_ineq"));
berghofe@17870
   768
       val pt_swap_bij       = PureThy.get_thm thy32 (Name ("nominal.pt_swap_bij"));
berghofe@17870
   769
       val pt_fresh_fresh    = PureThy.get_thm thy32 (Name ("nominal.pt_fresh_fresh"));
berghofe@17870
   770
       val pt_bij            = PureThy.get_thm thy32 (Name ("nominal.pt_bij"));
berghofe@17870
   771
       val pt_perm_compose   = PureThy.get_thm thy32 (Name ("nominal.pt_perm_compose"));
berghofe@17870
   772
       val perm_eq_app       = PureThy.get_thm thy32 (Name ("nominal.perm_eq_app"));
berghofe@17870
   773
berghofe@17870
   774
       (* abs_perm collects all lemmas for simplifying a permutation *)
berghofe@17870
   775
       (* in front of an abs_fun                                     *)
berghofe@17870
   776
       val (thy33,_) = 
berghofe@17870
   777
	   let 
berghofe@17870
   778
	     val name = "abs_perm"
berghofe@17870
   779
             val thm_list = Library.flat (map (fn (ak_name, T) =>
berghofe@17870
   780
	        let	
berghofe@17870
   781
		  val at_inst = PureThy.get_thm thy32 (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   782
		  val pt_inst = PureThy.get_thm thy32 (Name ("pt_"^ak_name^"_inst"));	      
berghofe@17870
   783
	          val thm = [pt_inst, at_inst] MRS abs_fun_pi
berghofe@17870
   784
                  val thm_list = map (fn (ak_name', T') =>
berghofe@17870
   785
                     let
berghofe@17870
   786
                      val cp_inst = PureThy.get_thm thy32 (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   787
	             in
berghofe@17870
   788
                     [pt_inst, pt_inst, at_inst, cp_inst] MRS abs_fun_pi_ineq
berghofe@17870
   789
	             end) ak_names_types;
berghofe@17870
   790
                 in thm::thm_list end) (ak_names_types))
berghofe@17870
   791
           in
berghofe@17870
   792
             (PureThy.add_thmss [((name, thm_list),[])] thy32)
berghofe@17870
   793
           end;
berghofe@17870
   794
berghofe@17870
   795
        (* alpha collects all lemmas analysing an equation *)
berghofe@17870
   796
        (* between abs_funs                                *)
berghofe@17870
   797
        (*val (thy34,_) = 
berghofe@17870
   798
	   let 
berghofe@17870
   799
	     val name = "alpha"
berghofe@17870
   800
             val thm_list = map (fn (ak_name, T) =>
berghofe@17870
   801
	        let	
berghofe@17870
   802
		  val at_inst = PureThy.get_thm thy33 (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   803
		  val pt_inst = PureThy.get_thm thy33 (Name ("pt_"^ak_name^"_inst"));	      
berghofe@17870
   804
	        in
berghofe@17870
   805
                  [pt_inst, at_inst] MRS abs_fun_eq
berghofe@17870
   806
	        end) ak_names_types
berghofe@17870
   807
           in
berghofe@17870
   808
             (PureThy.add_thmss [((name, thm_list),[])] thy33)
berghofe@17870
   809
           end;*)
berghofe@17870
   810
 
berghofe@17870
   811
          val (thy34,_) = 
berghofe@17870
   812
	   let 
berghofe@17870
   813
	     fun inst_pt_at thm ak_name =
berghofe@17870
   814
		 let	
berghofe@17870
   815
		   val at_inst = PureThy.get_thm thy33 (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   816
		   val pt_inst = PureThy.get_thm thy33 (Name ("pt_"^ak_name^"_inst"));	      
berghofe@17870
   817
	         in
berghofe@17870
   818
                     [pt_inst, at_inst] MRS thm
berghofe@17870
   819
	         end	 
berghofe@17870
   820
berghofe@17870
   821
           in
berghofe@17870
   822
            thy33 
berghofe@17870
   823
	    |> PureThy.add_thmss   [(("alpha", map (inst_pt_at abs_fun_eq) ak_names),[])]
berghofe@17870
   824
            |>>> PureThy.add_thmss [(("perm_swap", map (inst_pt_at pt_swap_bij) ak_names),[])]
berghofe@17870
   825
            |>>> PureThy.add_thmss [(("perm_fresh_fresh", map (inst_pt_at pt_fresh_fresh) ak_names),[])]
berghofe@17870
   826
            |>>> PureThy.add_thmss [(("perm_bij", map (inst_pt_at pt_bij) ak_names),[])]
berghofe@17870
   827
            |>>> PureThy.add_thmss [(("perm_compose", map (inst_pt_at pt_perm_compose) ak_names),[])]
berghofe@17870
   828
            |>>> PureThy.add_thmss [(("perm_app_eq", map (inst_pt_at perm_eq_app) ak_names),[])]
berghofe@17870
   829
	   end;
berghofe@17870
   830
berghofe@17870
   831
         (* perm_dj collects all lemmas that forget an permutation *)
berghofe@17870
   832
         (* when it acts on an atom of different type              *)
berghofe@17870
   833
         val (thy35,_) = 
berghofe@17870
   834
	   let 
berghofe@17870
   835
	     val name = "perm_dj"
berghofe@17870
   836
             val thm_list = Library.flat (map (fn (ak_name, T) =>
berghofe@17870
   837
	        Library.flat (map (fn (ak_name', T') => 
berghofe@17870
   838
                 if not (ak_name = ak_name') 
berghofe@17870
   839
                 then 
berghofe@17870
   840
		    let
berghofe@17870
   841
                      val dj_inst = PureThy.get_thm thy34 (Name ("dj_"^ak_name^"_"^ak_name'));
berghofe@17870
   842
                    in
berghofe@17870
   843
                      [dj_inst RS dj_perm_forget, dj_inst RS dj_pp_forget]
berghofe@17870
   844
                    end 
berghofe@17870
   845
                 else []) ak_names_types)) ak_names_types)
berghofe@17870
   846
           in
berghofe@17870
   847
             (PureThy.add_thmss [((name, thm_list),[])] thy34)
berghofe@17870
   848
           end;
berghofe@17870
   849
berghofe@17870
   850
         (* abs_fresh collects all lemmas for simplifying a freshness *)
berghofe@17870
   851
         (* proposition involving an abs_fun                          *)
berghofe@17870
   852
berghofe@17870
   853
         val (thy36,_) = 
berghofe@17870
   854
	   let 
berghofe@17870
   855
	     val name = "abs_fresh"
berghofe@17870
   856
             val thm_list = Library.flat (map (fn (ak_name, T) =>
berghofe@17870
   857
	        let	
berghofe@17870
   858
		  val at_inst = PureThy.get_thm thy35 (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   859
		  val pt_inst = PureThy.get_thm thy35 (Name ("pt_"^ak_name^"_inst"));
berghofe@17870
   860
                  val fs_inst = PureThy.get_thm thy35 (Name ("fs_"^ak_name^"_inst"));	      
berghofe@17870
   861
	          val thm = [pt_inst, at_inst, (fs_inst RS fs1)] MRS fresh_iff
berghofe@17870
   862
                  val thm_list = Library.flat (map (fn (ak_name', T') =>
berghofe@17870
   863
                     (if (not (ak_name = ak_name')) 
berghofe@17870
   864
                     then
berghofe@17870
   865
                       let
berghofe@17870
   866
                        val cp_inst = PureThy.get_thm thy35 (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   867
	                val dj_inst = PureThy.get_thm thy35 (Name ("dj_"^ak_name'^"_"^ak_name));
berghofe@17870
   868
                       in
berghofe@17870
   869
                        [[pt_inst, pt_inst, at_inst, cp_inst, dj_inst] MRS fresh_iff_ineq]
berghofe@17870
   870
	               end
berghofe@17870
   871
                     else [])) ak_names_types);
berghofe@17870
   872
                 in thm::thm_list end) (ak_names_types))
berghofe@17870
   873
           in
berghofe@17870
   874
             (PureThy.add_thmss [((name, thm_list),[])] thy35)
berghofe@17870
   875
           end;
berghofe@17870
   876
berghofe@17870
   877
         (* abs_supp collects all lemmas for simplifying  *)
berghofe@17870
   878
         (* support proposition involving an abs_fun      *)
berghofe@17870
   879
berghofe@17870
   880
         val (thy37,_) = 
berghofe@17870
   881
	   let 
berghofe@17870
   882
	     val name = "abs_supp"
berghofe@17870
   883
             val thm_list = Library.flat (map (fn (ak_name, T) =>
berghofe@17870
   884
	        let	
berghofe@17870
   885
		  val at_inst = PureThy.get_thm thy36 (Name ("at_"^ak_name^"_inst"));
berghofe@17870
   886
		  val pt_inst = PureThy.get_thm thy36 (Name ("pt_"^ak_name^"_inst"));
berghofe@17870
   887
                  val fs_inst = PureThy.get_thm thy36 (Name ("fs_"^ak_name^"_inst"));	      
berghofe@17870
   888
	          val thm1 = [pt_inst, at_inst, (fs_inst RS fs1)] MRS abs_fun_supp
berghofe@17870
   889
                  val thm2 = [pt_inst, at_inst] MRS abs_fun_supp
berghofe@17870
   890
                  val thm_list = Library.flat (map (fn (ak_name', T') =>
berghofe@17870
   891
                     (if (not (ak_name = ak_name')) 
berghofe@17870
   892
                     then
berghofe@17870
   893
                       let
berghofe@17870
   894
                        val cp_inst = PureThy.get_thm thy36 (Name ("cp_"^ak_name^"_"^ak_name'^"_inst"));
berghofe@17870
   895
	                val dj_inst = PureThy.get_thm thy36 (Name ("dj_"^ak_name'^"_"^ak_name));
berghofe@17870
   896
                       in
berghofe@17870
   897
                        [[pt_inst, pt_inst, at_inst, cp_inst, dj_inst] MRS abs_fun_supp_ineq]
berghofe@17870
   898
	               end
berghofe@17870
   899
                     else [])) ak_names_types);
berghofe@17870
   900
                 in thm1::thm2::thm_list end) (ak_names_types))
berghofe@17870
   901
           in
berghofe@17870
   902
             (PureThy.add_thmss [((name, thm_list),[])] thy36)
berghofe@17870
   903
           end;
berghofe@17870
   904
berghofe@17870
   905
    in NominalData.put (fold Symtab.update (map (rpair ()) full_ak_names)
berghofe@17870
   906
      (NominalData.get thy11)) thy37
berghofe@17870
   907
    end;
berghofe@17870
   908
berghofe@17870
   909
berghofe@17870
   910
(* syntax und parsing *)
berghofe@17870
   911
structure P = OuterParse and K = OuterKeyword;
berghofe@17870
   912
berghofe@17870
   913
val atom_declP =
berghofe@17870
   914
  OuterSyntax.command "atom_decl" "Declare new kinds of atoms" K.thy_decl
berghofe@17870
   915
    (Scan.repeat1 P.name >> (Toplevel.theory o create_nom_typedecls));
berghofe@17870
   916
berghofe@17870
   917
val _ = OuterSyntax.add_parsers [atom_declP];
berghofe@17870
   918
berghofe@17870
   919
val setup = [NominalData.init];
berghofe@17870
   920
berghofe@17870
   921
(*=======================================================================*)
berghofe@17870
   922
berghofe@18016
   923
(** FIXME: DatatypePackage should export this function **)
berghofe@18016
   924
berghofe@18016
   925
local
berghofe@18016
   926
berghofe@18016
   927
fun dt_recs (DtTFree _) = []
berghofe@18016
   928
  | dt_recs (DtType (_, dts)) = List.concat (map dt_recs dts)
berghofe@18016
   929
  | dt_recs (DtRec i) = [i];
berghofe@18016
   930
berghofe@18016
   931
fun dt_cases (descr: descr) (_, args, constrs) =
berghofe@18016
   932
  let
berghofe@18016
   933
    fun the_bname i = Sign.base_name (#1 (valOf (AList.lookup (op =) descr i)));
berghofe@18016
   934
    val bnames = map the_bname (distinct (List.concat (map dt_recs args)));
berghofe@18016
   935
  in map (fn (c, _) => space_implode "_" (Sign.base_name c :: bnames)) constrs end;
berghofe@18016
   936
berghofe@18016
   937
berghofe@18016
   938
fun induct_cases descr =
berghofe@18016
   939
  DatatypeProp.indexify_names (List.concat (map (dt_cases descr) (map #2 descr)));
berghofe@18016
   940
berghofe@18016
   941
fun exhaust_cases descr i = dt_cases descr (valOf (AList.lookup (op =) descr i));
berghofe@18016
   942
berghofe@18016
   943
in
berghofe@18016
   944
berghofe@18016
   945
fun mk_case_names_induct descr = RuleCases.case_names (induct_cases descr);
berghofe@18016
   946
berghofe@18016
   947
fun mk_case_names_exhausts descr new =
berghofe@18016
   948
  map (RuleCases.case_names o exhaust_cases descr o #1)
berghofe@18016
   949
    (List.filter (fn ((_, (name, _, _))) => name mem_string new) descr);
berghofe@18016
   950
berghofe@18016
   951
end;
berghofe@18016
   952
berghofe@18016
   953
(*******************************)
berghofe@18016
   954
berghofe@17870
   955
val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma);
berghofe@17870
   956
berghofe@17870
   957
fun read_typ sign ((Ts, sorts), str) =
berghofe@17870
   958
  let
berghofe@17870
   959
    val T = Type.no_tvars (Sign.read_typ (sign, (AList.lookup op =)
berghofe@17870
   960
      (map (apfst (rpair ~1)) sorts)) str) handle TYPE (msg, _, _) => error msg
berghofe@17870
   961
  in (Ts @ [T], add_typ_tfrees (T, sorts)) end;
berghofe@17870
   962
berghofe@17870
   963
(** taken from HOL/Tools/datatype_aux.ML **)
berghofe@17870
   964
berghofe@17870
   965
fun indtac indrule indnames i st =
berghofe@17870
   966
  let
berghofe@17870
   967
    val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule));
berghofe@17870
   968
    val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop
berghofe@17870
   969
      (Logic.strip_imp_concl (List.nth (prems_of st, i - 1))));
berghofe@17870
   970
    val getP = if can HOLogic.dest_imp (hd ts) then
berghofe@17870
   971
      (apfst SOME) o HOLogic.dest_imp else pair NONE;
berghofe@17870
   972
    fun abstr (t1, t2) = (case t1 of
berghofe@17870
   973
        NONE => (case filter (fn Free (s, _) => s mem indnames | _ => false)
berghofe@17870
   974
              (term_frees t2) of
berghofe@17870
   975
            [Free (s, T)] => absfree (s, T, t2)
berghofe@17870
   976
          | _ => sys_error "indtac")
berghofe@17870
   977
      | SOME (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2)))
berghofe@17870
   978
    val cert = cterm_of (Thm.sign_of_thm st);
berghofe@17870
   979
    val Ps = map (cert o head_of o snd o getP) ts;
berghofe@17870
   980
    val indrule' = cterm_instantiate (Ps ~~
berghofe@17870
   981
      (map (cert o abstr o getP) ts')) indrule
berghofe@17870
   982
  in
berghofe@17870
   983
    rtac indrule' i st
berghofe@17870
   984
  end;
berghofe@17870
   985
berghofe@17870
   986
fun gen_add_nominal_datatype prep_typ err flat_names new_type_names dts thy =
berghofe@17870
   987
  let
berghofe@17870
   988
    (* this theory is used just for parsing *)
berghofe@17870
   989
  
berghofe@17870
   990
    val tmp_thy = thy |>
berghofe@17870
   991
      Theory.copy |>
berghofe@17870
   992
      Theory.add_types (map (fn (tvs, tname, mx, _) =>
berghofe@17870
   993
        (tname, length tvs, mx)) dts);
berghofe@17870
   994
berghofe@17870
   995
    val sign = Theory.sign_of tmp_thy;
berghofe@17870
   996
berghofe@17870
   997
    val atoms = atoms_of thy;
berghofe@17870
   998
    val classes = map (NameSpace.map_base (fn s => "pt_" ^ s)) atoms;
berghofe@17870
   999
    val cp_classes = List.concat (map (fn atom1 => map (fn atom2 =>
berghofe@17870
  1000
      Sign.intern_class thy ("cp_" ^ Sign.base_name atom1 ^ "_" ^
berghofe@17870
  1001
        Sign.base_name atom2)) atoms) atoms);
berghofe@17870
  1002
    fun augment_sort S = S union classes;
berghofe@17870
  1003
    val augment_sort_typ = map_type_tfree (fn (s, S) => TFree (s, augment_sort S));
berghofe@17870
  1004
berghofe@17870
  1005
    fun prep_constr ((constrs, sorts), (cname, cargs, mx)) =
berghofe@17870
  1006
      let val (cargs', sorts') = Library.foldl (prep_typ sign) (([], sorts), cargs)
berghofe@17870
  1007
      in (constrs @ [(cname, cargs', mx)], sorts') end
berghofe@17870
  1008
berghofe@17870
  1009
    fun prep_dt_spec ((dts, sorts), (tvs, tname, mx, constrs)) =
berghofe@17870
  1010
      let val (constrs', sorts') = Library.foldl prep_constr (([], sorts), constrs)
berghofe@17870
  1011
      in (dts @ [(tvs, tname, mx, constrs')], sorts') end
berghofe@17870
  1012
berghofe@17870
  1013
    val (dts', sorts) = Library.foldl prep_dt_spec (([], []), dts);
berghofe@17870
  1014
    val sorts' = map (apsnd augment_sort) sorts;
berghofe@17870
  1015
    val tyvars = map #1 dts';
berghofe@17870
  1016
berghofe@17870
  1017
    val types_syntax = map (fn (tvs, tname, mx, constrs) => (tname, mx)) dts';
berghofe@17870
  1018
    val constr_syntax = map (fn (tvs, tname, mx, constrs) =>
berghofe@17870
  1019
      map (fn (cname, cargs, mx) => (cname, mx)) constrs) dts';
berghofe@17870
  1020
berghofe@17870
  1021
    val ps = map (fn (_, n, _, _) =>
berghofe@17870
  1022
      (Sign.full_name sign n, Sign.full_name sign (n ^ "_Rep"))) dts;
berghofe@17870
  1023
    val rps = map Library.swap ps;
berghofe@17870
  1024
berghofe@17870
  1025
    fun replace_types (Type ("nominal.ABS", [T, U])) = 
berghofe@17870
  1026
          Type ("fun", [T, Type ("nominal.nOption", [replace_types U])])
berghofe@17870
  1027
      | replace_types (Type (s, Ts)) =
berghofe@17870
  1028
          Type (getOpt (AList.lookup op = ps s, s), map replace_types Ts)
berghofe@17870
  1029
      | replace_types T = T;
berghofe@17870
  1030
berghofe@17870
  1031
    fun replace_types' (Type (s, Ts)) =
berghofe@17870
  1032
          Type (getOpt (AList.lookup op = rps s, s), map replace_types' Ts)
berghofe@17870
  1033
      | replace_types' T = T;
berghofe@17870
  1034
berghofe@17870
  1035
    val dts'' = map (fn (tvs, tname, mx, constrs) => (tvs, tname ^ "_Rep", NoSyn,
berghofe@17870
  1036
      map (fn (cname, cargs, mx) => (cname,
berghofe@17870
  1037
        map (augment_sort_typ o replace_types) cargs, NoSyn)) constrs)) dts';
berghofe@17870
  1038
berghofe@17870
  1039
    val new_type_names' = map (fn n => n ^ "_Rep") new_type_names;
berghofe@17870
  1040
    val full_new_type_names' = map (Sign.full_name (sign_of thy)) new_type_names';
berghofe@17870
  1041
berghofe@17870
  1042
    val (thy1, {induction, ...}) =
berghofe@17870
  1043
      DatatypePackage.add_datatype_i err flat_names new_type_names' dts'' thy;
berghofe@17870
  1044
berghofe@17870
  1045
    val SOME {descr, ...} = Symtab.lookup
berghofe@17870
  1046
      (DatatypePackage.get_datatypes thy1) (hd full_new_type_names');
berghofe@17870
  1047
    val typ_of_dtyp = typ_of_dtyp descr sorts';
berghofe@17870
  1048
    fun nth_dtyp i = typ_of_dtyp (DtRec i);
berghofe@17870
  1049
berghofe@17870
  1050
    (**** define permutation functions ****)
berghofe@17870
  1051
berghofe@17870
  1052
    val permT = mk_permT (TFree ("'x", HOLogic.typeS));
berghofe@17870
  1053
    val pi = Free ("pi", permT);
berghofe@17870
  1054
    val perm_types = map (fn (i, _) =>
berghofe@17870
  1055
      let val T = nth_dtyp i
berghofe@17870
  1056
      in permT --> T --> T end) descr;
berghofe@17870
  1057
    val perm_names = replicate (length new_type_names) "nominal.perm" @
berghofe@17870
  1058
      DatatypeProp.indexify_names (map (fn i => Sign.full_name (sign_of thy1)
berghofe@17870
  1059
        ("perm_" ^ name_of_typ (nth_dtyp i)))
berghofe@17870
  1060
          (length new_type_names upto length descr - 1));
berghofe@17870
  1061
    val perm_names_types = perm_names ~~ perm_types;
berghofe@17870
  1062
berghofe@17870
  1063
    val perm_eqs = List.concat (map (fn (i, (_, _, constrs)) =>
berghofe@17870
  1064
      let val T = nth_dtyp i
berghofe@17870
  1065
      in map (fn (cname, dts) => 
berghofe@17870
  1066
        let
berghofe@17870
  1067
          val Ts = map typ_of_dtyp dts;
berghofe@17870
  1068
          val names = DatatypeProp.make_tnames Ts;
berghofe@17870
  1069
          val args = map Free (names ~~ Ts);
berghofe@17870
  1070
          val c = Const (cname, Ts ---> T);
berghofe@17870
  1071
          fun perm_arg (dt, x) =
berghofe@17870
  1072
            let val T = type_of x
berghofe@17870
  1073
            in if is_rec_type dt then
berghofe@17870
  1074
                let val (Us, _) = strip_type T
berghofe@17870
  1075
                in list_abs (map (pair "x") Us,
berghofe@17870
  1076
                  Const (List.nth (perm_names_types, body_index dt)) $ pi $
berghofe@17870
  1077
                    list_comb (x, map (fn (i, U) =>
berghofe@17870
  1078
                      Const ("nominal.perm", permT --> U --> U) $
berghofe@17870
  1079
                        (Const ("List.rev", permT --> permT) $ pi) $
berghofe@17870
  1080
                        Bound i) ((length Us - 1 downto 0) ~~ Us)))
berghofe@17870
  1081
                end
berghofe@17870
  1082
              else Const ("nominal.perm", permT --> T --> T) $ pi $ x
berghofe@17870
  1083
            end;  
berghofe@17870
  1084
        in
berghofe@17870
  1085
          (("", HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
  1086
            (Const (List.nth (perm_names_types, i)) $
berghofe@17870
  1087
               Free ("pi", mk_permT (TFree ("'x", HOLogic.typeS))) $
berghofe@17870
  1088
               list_comb (c, args),
berghofe@17870
  1089
             list_comb (c, map perm_arg (dts ~~ args))))), [])
berghofe@17870
  1090
        end) constrs
berghofe@17870
  1091
      end) descr);
berghofe@17870
  1092
berghofe@17870
  1093
    val (thy2, perm_simps) = thy1 |>
berghofe@17870
  1094
      Theory.add_consts_i (map (fn (s, T) => (Sign.base_name s, T, NoSyn))
berghofe@17870
  1095
        (List.drop (perm_names_types, length new_type_names))) |>
berghofe@17870
  1096
      PrimrecPackage.add_primrec_i "" perm_eqs;
berghofe@17870
  1097
berghofe@17870
  1098
    (**** prove that permutation functions introduced by unfolding are ****)
berghofe@17870
  1099
    (**** equivalent to already existing permutation functions         ****)
berghofe@17870
  1100
berghofe@17870
  1101
    val _ = warning ("length descr: " ^ string_of_int (length descr));
berghofe@17870
  1102
    val _ = warning ("length new_type_names: " ^ string_of_int (length new_type_names));
berghofe@17870
  1103
berghofe@17870
  1104
    val perm_indnames = DatatypeProp.make_tnames (map body_type perm_types);
berghofe@17870
  1105
    val perm_fun_def = PureThy.get_thm thy2 (Name "perm_fun_def");
berghofe@17870
  1106
berghofe@17870
  1107
    val unfolded_perm_eq_thms =
berghofe@17870
  1108
      if length descr = length new_type_names then []
berghofe@17870
  1109
      else map standard (List.drop (split_conj_thm
berghofe@18010
  1110
        (Goal.prove thy2 [] []
berghofe@17870
  1111
          (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
  1112
            (map (fn (c as (s, T), x) =>
berghofe@17870
  1113
               let val [T1, T2] = binder_types T
berghofe@17870
  1114
               in HOLogic.mk_eq (Const c $ pi $ Free (x, T2),
berghofe@17870
  1115
                 Const ("nominal.perm", T) $ pi $ Free (x, T2))
berghofe@17870
  1116
               end)
berghofe@18010
  1117
             (perm_names_types ~~ perm_indnames))))
berghofe@18010
  1118
          (fn _ => EVERY [indtac induction perm_indnames 1,
berghofe@17870
  1119
            ALLGOALS (asm_full_simp_tac
berghofe@17870
  1120
              (simpset_of thy2 addsimps [perm_fun_def]))])),
berghofe@17870
  1121
        length new_type_names));
berghofe@17870
  1122
berghofe@17870
  1123
    (**** prove [] \<bullet> t = t ****)
berghofe@17870
  1124
berghofe@17870
  1125
    val _ = warning "perm_empty_thms";
berghofe@17870
  1126
berghofe@17870
  1127
    val perm_empty_thms = List.concat (map (fn a =>
berghofe@17870
  1128
      let val permT = mk_permT (Type (a, []))
berghofe@17870
  1129
      in map standard (List.take (split_conj_thm
berghofe@18010
  1130
        (Goal.prove thy2 [] []
berghofe@17870
  1131
          (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
  1132
            (map (fn ((s, T), x) => HOLogic.mk_eq
berghofe@17870
  1133
                (Const (s, permT --> T --> T) $
berghofe@17870
  1134
                   Const ("List.list.Nil", permT) $ Free (x, T),
berghofe@17870
  1135
                 Free (x, T)))
berghofe@17870
  1136
             (perm_names ~~
berghofe@18010
  1137
              map body_type perm_types ~~ perm_indnames))))
berghofe@18010
  1138
          (fn _ => EVERY [indtac induction perm_indnames 1,
berghofe@17870
  1139
            ALLGOALS (asm_full_simp_tac (simpset_of thy2))])),
berghofe@17870
  1140
        length new_type_names))
berghofe@17870
  1141
      end)
berghofe@17870
  1142
      atoms);
berghofe@17870
  1143
berghofe@17870
  1144
    (**** prove (pi1 @ pi2) \<bullet> t = pi1 \<bullet> (pi2 \<bullet> t) ****)
berghofe@17870
  1145
berghofe@17870
  1146
    val _ = warning "perm_append_thms";
berghofe@17870
  1147
berghofe@17870
  1148
    (*FIXME: these should be looked up statically*)
berghofe@17870
  1149
    val at_pt_inst = PureThy.get_thm thy2 (Name "at_pt_inst");
berghofe@17870
  1150
    val pt2 = PureThy.get_thm thy2 (Name "pt2");
berghofe@17870
  1151
berghofe@17870
  1152
    val perm_append_thms = List.concat (map (fn a =>
berghofe@17870
  1153
      let
berghofe@17870
  1154
        val permT = mk_permT (Type (a, []));
berghofe@17870
  1155
        val pi1 = Free ("pi1", permT);
berghofe@17870
  1156
        val pi2 = Free ("pi2", permT);
berghofe@17870
  1157
        val pt_inst = PureThy.get_thm thy2 (Name ("pt_" ^ Sign.base_name a ^ "_inst"));
berghofe@17870
  1158
        val pt2' = pt_inst RS pt2;
berghofe@17870
  1159
        val pt2_ax = PureThy.get_thm thy2
berghofe@17870
  1160
          (Name (NameSpace.map_base (fn s => "pt_" ^ s ^ "2") a));
berghofe@17870
  1161
      in List.take (map standard (split_conj_thm
berghofe@18010
  1162
        (Goal.prove thy2 [] []
berghofe@17870
  1163
             (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
  1164
                (map (fn ((s, T), x) =>
berghofe@17870
  1165
                    let val perm = Const (s, permT --> T --> T)
berghofe@17870
  1166
                    in HOLogic.mk_eq
berghofe@17870
  1167
                      (perm $ (Const ("List.op @", permT --> permT --> permT) $
berghofe@17870
  1168
                         pi1 $ pi2) $ Free (x, T),
berghofe@17870
  1169
                       perm $ pi1 $ (perm $ pi2 $ Free (x, T)))
berghofe@17870
  1170
                    end)
berghofe@17870
  1171
                  (perm_names ~~
berghofe@18010
  1172
                   map body_type perm_types ~~ perm_indnames))))
berghofe@18010
  1173
           (fn _ => EVERY [indtac induction perm_indnames 1,
berghofe@17870
  1174
              ALLGOALS (asm_full_simp_tac (simpset_of thy2 addsimps [pt2', pt2_ax]))]))),
berghofe@17870
  1175
         length new_type_names)
berghofe@17870
  1176
      end) atoms);
berghofe@17870
  1177
berghofe@17870
  1178
    (**** prove pi1 ~ pi2 ==> pi1 \<bullet> t = pi2 \<bullet> t ****)
berghofe@17870
  1179
berghofe@17870
  1180
    val _ = warning "perm_eq_thms";
berghofe@17870
  1181
berghofe@17870
  1182
    val pt3 = PureThy.get_thm thy2 (Name "pt3");
berghofe@17870
  1183
    val pt3_rev = PureThy.get_thm thy2 (Name "pt3_rev");
berghofe@17870
  1184
berghofe@17870
  1185
    val perm_eq_thms = List.concat (map (fn a =>
berghofe@17870
  1186
      let
berghofe@17870
  1187
        val permT = mk_permT (Type (a, []));
berghofe@17870
  1188
        val pi1 = Free ("pi1", permT);
berghofe@17870
  1189
        val pi2 = Free ("pi2", permT);
berghofe@17870
  1190
        (*FIXME: not robust - better access these theorems using NominalData?*)
berghofe@17870
  1191
        val at_inst = PureThy.get_thm thy2 (Name ("at_" ^ Sign.base_name a ^ "_inst"));
berghofe@17870
  1192
        val pt_inst = PureThy.get_thm thy2 (Name ("pt_" ^ Sign.base_name a ^ "_inst"));
berghofe@17870
  1193
        val pt3' = pt_inst RS pt3;
berghofe@17870
  1194
        val pt3_rev' = at_inst RS (pt_inst RS pt3_rev);
berghofe@17870
  1195
        val pt3_ax = PureThy.get_thm thy2
berghofe@17870
  1196
          (Name (NameSpace.map_base (fn s => "pt_" ^ s ^ "3") a));
berghofe@17870
  1197
      in List.take (map standard (split_conj_thm
berghofe@18010
  1198
        (Goal.prove thy2 [] [] (Logic.mk_implies
berghofe@17870
  1199
             (HOLogic.mk_Trueprop (Const ("nominal.prm_eq",
berghofe@17870
  1200
                permT --> permT --> HOLogic.boolT) $ pi1 $ pi2),
berghofe@17870
  1201
              HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
  1202
                (map (fn ((s, T), x) =>
berghofe@17870
  1203
                    let val perm = Const (s, permT --> T --> T)
berghofe@17870
  1204
                    in HOLogic.mk_eq
berghofe@17870
  1205
                      (perm $ pi1 $ Free (x, T),
berghofe@17870
  1206
                       perm $ pi2 $ Free (x, T))
berghofe@17870
  1207
                    end)
berghofe@17870
  1208
                  (perm_names ~~
berghofe@18010
  1209
                   map body_type perm_types ~~ perm_indnames)))))
berghofe@18010
  1210
           (fn _ => EVERY [indtac induction perm_indnames 1,
berghofe@17870
  1211
              ALLGOALS (asm_full_simp_tac (simpset_of thy2 addsimps [pt3', pt3_rev', pt3_ax]))]))),
berghofe@17870
  1212
         length new_type_names)
berghofe@17870
  1213
      end) atoms);
berghofe@17870
  1214
berghofe@17870
  1215
    (**** prove pi1 \<bullet> (pi2 \<bullet> t) = (pi1 \<bullet> pi2) \<bullet> (pi1 \<bullet> t) ****)
berghofe@17870
  1216
berghofe@17870
  1217
    val cp1 = PureThy.get_thm thy2 (Name "cp1");
berghofe@17870
  1218
    val dj_cp = PureThy.get_thm thy2 (Name "dj_cp");
berghofe@17870
  1219
    val pt_perm_compose = PureThy.get_thm thy2 (Name "pt_perm_compose");
berghofe@17870
  1220
    val pt_perm_compose_rev = PureThy.get_thm thy2 (Name "pt_perm_compose_rev");
berghofe@17870
  1221
    val dj_perm_perm_forget = PureThy.get_thm thy2 (Name "dj_perm_perm_forget");
berghofe@17870
  1222
berghofe@17870
  1223
    fun composition_instance name1 name2 thy =
berghofe@17870
  1224
      let
berghofe@17870
  1225
        val name1' = Sign.base_name name1;
berghofe@17870
  1226
        val name2' = Sign.base_name name2;
berghofe@17870
  1227
        val cp_class = Sign.intern_class thy ("cp_" ^ name1' ^ "_" ^ name2');
berghofe@17870
  1228
        val permT1 = mk_permT (Type (name1, []));
berghofe@17870
  1229
        val permT2 = mk_permT (Type (name2, []));
berghofe@17870
  1230
        val augment = map_type_tfree
berghofe@17870
  1231
          (fn (x, S) => TFree (x, cp_class :: S));
berghofe@17870
  1232
        val Ts = map (augment o body_type) perm_types;
berghofe@17870
  1233
        val cp_inst = PureThy.get_thm thy
berghofe@17870
  1234
          (Name ("cp_" ^ name1' ^ "_" ^ name2' ^ "_inst"));
berghofe@17870
  1235
        val simps = simpset_of thy addsimps (perm_fun_def ::
berghofe@17870
  1236
          (if name1 <> name2 then
berghofe@17870
  1237
             let val dj = PureThy.get_thm thy (Name ("dj_" ^ name2' ^ "_" ^ name1'))
berghofe@17870
  1238
             in [dj RS (cp_inst RS dj_cp), dj RS dj_perm_perm_forget] end
berghofe@17870
  1239
           else
berghofe@17870
  1240
             let
berghofe@17870
  1241
               val at_inst = PureThy.get_thm thy (Name ("at_" ^ name1' ^ "_inst"));
berghofe@17870
  1242
               val pt_inst = PureThy.get_thm thy (Name ("pt_" ^ name1' ^ "_inst"))
berghofe@17870
  1243
             in
berghofe@17870
  1244
               [cp_inst RS cp1 RS sym,
berghofe@17870
  1245
                at_inst RS (pt_inst RS pt_perm_compose) RS sym,
berghofe@17870
  1246
                at_inst RS (pt_inst RS pt_perm_compose_rev) RS sym]
berghofe@17870
  1247
            end))
berghofe@18010
  1248
        val thms = split_conj_thm (standard (Goal.prove thy [] []
berghofe@17870
  1249
            (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
  1250
              (map (fn ((s, T), x) =>
berghofe@17870
  1251
                  let
berghofe@17870
  1252
                    val pi1 = Free ("pi1", permT1);
berghofe@17870
  1253
                    val pi2 = Free ("pi2", permT2);
berghofe@17870
  1254
                    val perm1 = Const (s, permT1 --> T --> T);
berghofe@17870
  1255
                    val perm2 = Const (s, permT2 --> T --> T);
berghofe@17870
  1256
                    val perm3 = Const ("nominal.perm", permT1 --> permT2 --> permT2)
berghofe@17870
  1257
                  in HOLogic.mk_eq
berghofe@17870
  1258
                    (perm1 $ pi1 $ (perm2 $ pi2 $ Free (x, T)),
berghofe@17870
  1259
                     perm2 $ (perm3 $ pi1 $ pi2) $ (perm1 $ pi1 $ Free (x, T)))
berghofe@17870
  1260
                  end)
berghofe@18010
  1261
                (perm_names ~~ Ts ~~ perm_indnames))))
berghofe@18010
  1262
          (fn _ => EVERY [indtac induction perm_indnames 1,
berghofe@18010
  1263
             ALLGOALS (asm_full_simp_tac simps)])))
berghofe@17870
  1264
      in
berghofe@17870
  1265
        foldl (fn ((s, tvs), thy) => AxClass.add_inst_arity_i
berghofe@17870
  1266
            (s, replicate (length tvs) (cp_class :: classes), [cp_class])
berghofe@17870
  1267
            (AxClass.intro_classes_tac [] THEN ALLGOALS (resolve_tac thms)) thy)
berghofe@17870
  1268
          thy (full_new_type_names' ~~ tyvars)
berghofe@17870
  1269
      end;
berghofe@17870
  1270
berghofe@17870
  1271
    val (thy3, perm_thmss) = thy2 |>
berghofe@17870
  1272
      fold (fn name1 => fold (composition_instance name1) atoms) atoms |>
berghofe@17870
  1273
      curry (Library.foldr (fn ((i, (tyname, args, _)), thy) =>
berghofe@17870
  1274
        AxClass.add_inst_arity_i (tyname, replicate (length args) classes, classes)
berghofe@17870
  1275
        (AxClass.intro_classes_tac [] THEN REPEAT (EVERY
berghofe@17870
  1276
           [resolve_tac perm_empty_thms 1,
berghofe@17870
  1277
            resolve_tac perm_append_thms 1,
berghofe@17870
  1278
            resolve_tac perm_eq_thms 1, assume_tac 1])) thy))
berghofe@17870
  1279
        (List.take (descr, length new_type_names)) |>
berghofe@17870
  1280
      PureThy.add_thmss
berghofe@17870
  1281
        [((space_implode "_" new_type_names ^ "_unfolded_perm_eq",
berghofe@17870
  1282
          unfolded_perm_eq_thms), [Simplifier.simp_add_global]),
berghofe@17870
  1283
         ((space_implode "_" new_type_names ^ "_perm_empty",
berghofe@17870
  1284
          perm_empty_thms), [Simplifier.simp_add_global]),
berghofe@17870
  1285
         ((space_implode "_" new_type_names ^ "_perm_append",
berghofe@17870
  1286
          perm_append_thms), [Simplifier.simp_add_global]),
berghofe@17870
  1287
         ((space_implode "_" new_type_names ^ "_perm_eq",
berghofe@17870
  1288
          perm_eq_thms), [Simplifier.simp_add_global])];
berghofe@17870
  1289
  
berghofe@17870
  1290
    (**** Define representing sets ****)
berghofe@17870
  1291
berghofe@17870
  1292
    val _ = warning "representing sets";
berghofe@17870
  1293
berghofe@17870
  1294
    val rep_set_names = map (Sign.full_name thy3) (DatatypeProp.indexify_names
berghofe@17870
  1295
      (map (fn (i, _) => name_of_typ (nth_dtyp i) ^ "_set") descr));
berghofe@17870
  1296
    val big_rep_name =
berghofe@17870
  1297
      space_implode "_" (DatatypeProp.indexify_names (List.mapPartial
berghofe@17870
  1298
        (fn (i, ("nominal.nOption", _, _)) => NONE
berghofe@17870
  1299
          | (i, _) => SOME (name_of_typ (nth_dtyp i))) descr)) ^ "_set";
berghofe@17870
  1300
    val _ = warning ("big_rep_name: " ^ big_rep_name);
berghofe@17870
  1301
berghofe@17870
  1302
    fun strip_option (dtf as DtType ("fun", [dt, DtRec i])) =
berghofe@17870
  1303
          (case AList.lookup op = descr i of
berghofe@17870
  1304
             SOME ("nominal.nOption", _, [(_, [dt']), _]) =>
berghofe@17870
  1305
               apfst (cons dt) (strip_option dt')
berghofe@17870
  1306
           | _ => ([], dtf))
berghofe@17870
  1307
      | strip_option dt = ([], dt);
berghofe@17870
  1308
berghofe@17870
  1309
    fun make_intr s T (cname, cargs) =
berghofe@17870
  1310
      let
berghofe@17870
  1311
        fun mk_prem (dt, (j, j', prems, ts)) = 
berghofe@17870
  1312
          let
berghofe@17870
  1313
            val (dts, dt') = strip_option dt;
berghofe@17870
  1314
            val (dts', dt'') = strip_dtyp dt';
berghofe@17870
  1315
            val Ts = map typ_of_dtyp dts;
berghofe@17870
  1316
            val Us = map typ_of_dtyp dts';
berghofe@17870
  1317
            val T = typ_of_dtyp dt'';
berghofe@17870
  1318
            val free = mk_Free "x" (Us ---> T) j;
berghofe@17870
  1319
            val free' = app_bnds free (length Us);
berghofe@17870
  1320
            fun mk_abs_fun (T, (i, t)) =
berghofe@17870
  1321
              let val U = fastype_of t
berghofe@17870
  1322
              in (i + 1, Const ("nominal.abs_fun", [T, U, T] --->
berghofe@17870
  1323
                Type ("nominal.nOption", [U])) $ mk_Free "y" T i $ t)
berghofe@17870
  1324
              end
berghofe@17870
  1325
          in (j + 1, j' + length Ts,
berghofe@17870
  1326
            case dt'' of
berghofe@17870
  1327
                DtRec k => list_all (map (pair "x") Us,
berghofe@17870
  1328
                  HOLogic.mk_Trueprop (HOLogic.mk_mem (free',
berghofe@17870
  1329
                    Const (List.nth (rep_set_names, k),
berghofe@17870
  1330
                      HOLogic.mk_setT T)))) :: prems
berghofe@17870
  1331
              | _ => prems,
berghofe@17870
  1332
            snd (foldr mk_abs_fun (j', free) Ts) :: ts)
berghofe@17870
  1333
          end;
berghofe@17870
  1334
berghofe@17870
  1335
        val (_, _, prems, ts) = foldr mk_prem (1, 1, [], []) cargs;
berghofe@17870
  1336
        val concl = HOLogic.mk_Trueprop (HOLogic.mk_mem
berghofe@17870
  1337
          (list_comb (Const (cname, map fastype_of ts ---> T), ts),
berghofe@17870
  1338
           Const (s, HOLogic.mk_setT T)))
berghofe@17870
  1339
      in Logic.list_implies (prems, concl)
berghofe@17870
  1340
      end;
berghofe@17870
  1341
berghofe@17870
  1342
    val (intr_ts, ind_consts) =
berghofe@17870
  1343
      apfst List.concat (ListPair.unzip (List.mapPartial
berghofe@17870
  1344
        (fn ((_, ("nominal.nOption", _, _)), _) => NONE
berghofe@17870
  1345
          | ((i, (_, _, constrs)), rep_set_name) =>
berghofe@17870
  1346
              let val T = nth_dtyp i
berghofe@17870
  1347
              in SOME (map (make_intr rep_set_name T) constrs,
berghofe@17870
  1348
                Const (rep_set_name, HOLogic.mk_setT T))
berghofe@17870
  1349
              end)
berghofe@17870
  1350
                (descr ~~ rep_set_names)));
berghofe@17870
  1351
berghofe@17870
  1352
    val (thy4, {raw_induct = rep_induct, intrs = rep_intrs, ...}) =
berghofe@17870
  1353
      setmp InductivePackage.quiet_mode false
berghofe@17870
  1354
        (InductivePackage.add_inductive_i false true big_rep_name false true false
berghofe@17870
  1355
           ind_consts (map (fn x => (("", x), [])) intr_ts) []) thy3;
berghofe@17870
  1356
berghofe@17870
  1357
    (**** Prove that representing set is closed under permutation ****)
berghofe@17870
  1358
berghofe@17870
  1359
    val _ = warning "proving closure under permutation...";
berghofe@17870
  1360
berghofe@17870
  1361
    val perm_indnames' = List.mapPartial
berghofe@17870
  1362
      (fn (x, (_, ("nominal.nOption", _, _))) => NONE | (x, _) => SOME x)
berghofe@17870
  1363
      (perm_indnames ~~ descr);
berghofe@17870
  1364
berghofe@17870
  1365
    fun mk_perm_closed name = map (fn th => standard (th RS mp))
berghofe@18010
  1366
      (List.take (split_conj_thm (Goal.prove thy4 [] []
berghofe@17870
  1367
        (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map
berghofe@17870
  1368
           (fn (S, x) =>
berghofe@17870
  1369
              let
berghofe@17870
  1370
                val S = map_term_types (map_type_tfree
berghofe@17870
  1371
                  (fn (s, cs) => TFree (s, cs union cp_classes))) S;
berghofe@17870
  1372
                val T = HOLogic.dest_setT (fastype_of S);
berghofe@17870
  1373
                val permT = mk_permT (Type (name, []))
berghofe@17870
  1374
              in HOLogic.mk_imp (HOLogic.mk_mem (Free (x, T), S),
berghofe@17870
  1375
                HOLogic.mk_mem (Const ("nominal.perm", permT --> T --> T) $
berghofe@17870
  1376
                  Free ("pi", permT) $ Free (x, T), S))
berghofe@18010
  1377
              end) (ind_consts ~~ perm_indnames'))))
berghofe@18010
  1378
        (fn _ => EVERY (* CU: added perm_fun_def in the final tactic in order to deal with funs *)
berghofe@17870
  1379
           [indtac rep_induct [] 1,
berghofe@17870
  1380
            ALLGOALS (simp_tac (simpset_of thy4 addsimps
berghofe@17870
  1381
              (symmetric perm_fun_def :: PureThy.get_thms thy4 (Name ("abs_perm"))))),
berghofe@17870
  1382
            ALLGOALS (resolve_tac rep_intrs 
berghofe@17870
  1383
               THEN_ALL_NEW (asm_full_simp_tac (simpset_of thy4 addsimps [perm_fun_def])))])),
berghofe@17870
  1384
        length new_type_names));
berghofe@17870
  1385
berghofe@17870
  1386
    (* FIXME: theorems are stored in database for testing only *)
berghofe@17870
  1387
    val perm_closed_thmss = map mk_perm_closed atoms;
berghofe@17870
  1388
    val (thy5, _) = PureThy.add_thmss [(("perm_closed",
berghofe@17870
  1389
      List.concat perm_closed_thmss), [])] thy4;
berghofe@17870
  1390
berghofe@17870
  1391
    (**** typedef ****)
berghofe@17870
  1392
berghofe@17870
  1393
    val _ = warning "defining type...";
berghofe@17870
  1394
berghofe@17870
  1395
    val (thy6, typedefs) =
berghofe@17870
  1396
      foldl_map (fn (thy, ((((name, mx), tvs), c), name')) =>
berghofe@17870
  1397
        setmp TypedefPackage.quiet_mode true
berghofe@17870
  1398
          (TypedefPackage.add_typedef_i false (SOME name') (name, tvs, mx) c NONE
berghofe@17870
  1399
            (rtac exI 1 THEN
berghofe@17870
  1400
              QUIET_BREADTH_FIRST (has_fewer_prems 1)
berghofe@17870
  1401
              (resolve_tac rep_intrs 1))) thy |> (fn (thy, r) =>
berghofe@17870
  1402
        let
berghofe@17870
  1403
          val permT = mk_permT (TFree (variant tvs "'a", HOLogic.typeS));
berghofe@17870
  1404
          val pi = Free ("pi", permT);
berghofe@17870
  1405
          val tvs' = map (fn s => TFree (s, the (AList.lookup op = sorts' s))) tvs;
berghofe@17870
  1406
          val T = Type (Sign.intern_type thy name, tvs');
berghofe@17870
  1407
          val Const (_, Type (_, [U])) = c
berghofe@17870
  1408
        in apsnd (pair r o hd)
berghofe@17870
  1409
          (PureThy.add_defs_i true [(("prm_" ^ name ^ "_def", Logic.mk_equals
berghofe@17870
  1410
            (Const ("nominal.perm", permT --> T --> T) $ pi $ Free ("x", T),
berghofe@17870
  1411
             Const (Sign.intern_const thy ("Abs_" ^ name), U --> T) $
berghofe@17870
  1412
               (Const ("nominal.perm", permT --> U --> U) $ pi $
berghofe@17870
  1413
                 (Const (Sign.intern_const thy ("Rep_" ^ name), T --> U) $
berghofe@17870
  1414
                   Free ("x", T))))), [])] thy)
berghofe@17870
  1415
        end))
berghofe@17870
  1416
          (thy5, types_syntax ~~ tyvars ~~
berghofe@17870
  1417
            (List.take (ind_consts, length new_type_names)) ~~ new_type_names);
berghofe@17870
  1418
berghofe@17870
  1419
    val perm_defs = map snd typedefs;
berghofe@17870
  1420
    val Abs_inverse_thms = map (#Abs_inverse o fst) typedefs;
berghofe@18016
  1421
    val Rep_inverse_thms = map (#Rep_inverse o fst) typedefs;
berghofe@17870
  1422
    val Rep_thms = map (#Rep o fst) typedefs;
berghofe@17870
  1423
berghofe@18016
  1424
    val big_name = space_implode "_" new_type_names;
berghofe@18016
  1425
berghofe@18016
  1426
berghofe@17870
  1427
    (** prove that new types are in class pt_<name> **)
berghofe@17870
  1428
berghofe@17870
  1429
    val _ = warning "prove that new types are in class pt_<name> ...";
berghofe@17870
  1430
berghofe@17870
  1431
    fun pt_instance ((class, atom), perm_closed_thms) =
berghofe@17870
  1432
      fold (fn (((({Abs_inverse, Rep_inverse, Rep, ...},
berghofe@17870
  1433
        perm_def), name), tvs), perm_closed) => fn thy =>
berghofe@17870
  1434
          AxClass.add_inst_arity_i
berghofe@17870
  1435
            (Sign.intern_type thy name,
berghofe@17870
  1436
              replicate (length tvs) (classes @ cp_classes), [class])
berghofe@17870
  1437
            (EVERY [AxClass.intro_classes_tac [],
berghofe@17870
  1438
              rewrite_goals_tac [perm_def],
berghofe@17870
  1439
              asm_full_simp_tac (simpset_of thy addsimps [Rep_inverse]) 1,
berghofe@17870
  1440
              asm_full_simp_tac (simpset_of thy addsimps
berghofe@17870
  1441
                [Rep RS perm_closed RS Abs_inverse]) 1,
berghofe@17870
  1442
              asm_full_simp_tac (HOL_basic_ss addsimps [PureThy.get_thm thy
berghofe@17870
  1443
                (Name ("pt_" ^ Sign.base_name atom ^ "3"))]) 1]) thy)
berghofe@17870
  1444
        (typedefs ~~ new_type_names ~~ tyvars ~~ perm_closed_thms);
berghofe@17870
  1445
berghofe@17870
  1446
berghofe@17870
  1447
    (** prove that new types are in class cp_<name1>_<name2> **)
berghofe@17870
  1448
berghofe@17870
  1449
    val _ = warning "prove that new types are in class cp_<name1>_<name2> ...";
berghofe@17870
  1450
berghofe@17870
  1451
    fun cp_instance (atom1, perm_closed_thms1) (atom2, perm_closed_thms2) thy =
berghofe@17870
  1452
      let
berghofe@17870
  1453
        val name = "cp_" ^ Sign.base_name atom1 ^ "_" ^ Sign.base_name atom2;
berghofe@17870
  1454
        val class = Sign.intern_class thy name;
berghofe@17870
  1455
        val cp1' = PureThy.get_thm thy (Name (name ^ "_inst")) RS cp1
berghofe@17870
  1456
      in fold (fn ((((({Abs_inverse, Rep_inverse, Rep, ...},
berghofe@17870
  1457
        perm_def), name), tvs), perm_closed1), perm_closed2) => fn thy =>
berghofe@17870
  1458
          AxClass.add_inst_arity_i
berghofe@17870
  1459
            (Sign.intern_type thy name,
berghofe@17870
  1460
              replicate (length tvs) (classes @ cp_classes), [class])
berghofe@17870
  1461
            (EVERY [AxClass.intro_classes_tac [],
berghofe@17870
  1462
              rewrite_goals_tac [perm_def],
berghofe@17870
  1463
              asm_full_simp_tac (simpset_of thy addsimps
berghofe@17870
  1464
                ((Rep RS perm_closed1 RS Abs_inverse) ::
berghofe@17870
  1465
                 (if atom1 = atom2 then []
berghofe@17870
  1466
                  else [Rep RS perm_closed2 RS Abs_inverse]))) 1,
berghofe@18016
  1467
              cong_tac 1,
berghofe@17870
  1468
              rtac refl 1,
berghofe@17870
  1469
              rtac cp1' 1]) thy)
berghofe@17870
  1470
        (typedefs ~~ new_type_names ~~ tyvars ~~ perm_closed_thms1 ~~
berghofe@17870
  1471
          perm_closed_thms2) thy
berghofe@17870
  1472
      end;
berghofe@17870
  1473
berghofe@17870
  1474
    val thy7 = fold (fn x => fn thy => thy |>
berghofe@17870
  1475
      pt_instance x |>
berghofe@17870
  1476
      fold (cp_instance (apfst snd x)) (atoms ~~ perm_closed_thmss))
berghofe@17870
  1477
        (classes ~~ atoms ~~ perm_closed_thmss) thy6;
berghofe@17870
  1478
berghofe@17870
  1479
    (**** constructors ****)
berghofe@17870
  1480
berghofe@17870
  1481
    fun mk_abs_fun (x, t) =
berghofe@17870
  1482
      let
berghofe@17870
  1483
        val T = fastype_of x;
berghofe@17870
  1484
        val U = fastype_of t
berghofe@17870
  1485
      in
berghofe@17870
  1486
        Const ("nominal.abs_fun", T --> U --> T -->
berghofe@17870
  1487
          Type ("nominal.nOption", [U])) $ x $ t
berghofe@17870
  1488
      end;
berghofe@17870
  1489
berghofe@18016
  1490
    val (ty_idxs, _) = foldl
berghofe@18016
  1491
      (fn ((i, ("nominal.nOption", _, _)), p) => p
berghofe@18016
  1492
        | ((i, _), (ty_idxs, j)) => (ty_idxs @ [(i, j)], j + 1)) ([], 0) descr;
berghofe@18016
  1493
berghofe@18016
  1494
    fun reindex (DtType (s, dts)) = DtType (s, map reindex dts)
berghofe@18016
  1495
      | reindex (DtRec i) = DtRec (the (AList.lookup op = ty_idxs i))
berghofe@18016
  1496
      | reindex dt = dt;
berghofe@18016
  1497
berghofe@18016
  1498
    fun strip_suffix i s = implode (List.take (explode s, size s - i));
berghofe@18016
  1499
berghofe@18016
  1500
    (** strips the "_Rep" in type names *)
berghofe@18016
  1501
    fun strip_nth_name i s =
berghofe@18016
  1502
      let val xs = NameSpace.unpack s
berghofe@18016
  1503
      in NameSpace.pack (map_nth_elem (length xs - i) (strip_suffix 4) xs) end;
berghofe@18016
  1504
berghofe@18016
  1505
    val descr'' = List.mapPartial
berghofe@18016
  1506
      (fn (i, ("nominal.nOption", _, _)) => NONE
berghofe@18016
  1507
        | (i, (s, dts, constrs)) => SOME (the (AList.lookup op = ty_idxs i),
berghofe@18016
  1508
            (strip_nth_name 1 s,  map reindex dts,
berghofe@18016
  1509
             map (fn (cname, cargs) =>
berghofe@18016
  1510
               (strip_nth_name 2 cname,
berghofe@18016
  1511
                foldl (fn (dt, dts) =>
berghofe@18016
  1512
                  let val (dts', dt') = strip_option dt
berghofe@18016
  1513
                  in (dts @ dts' @ [reindex dt']) end) [] cargs)) constrs))) descr;
berghofe@18016
  1514
    val (descr1, descr2) = splitAt (length new_type_names, descr'');
berghofe@18016
  1515
    val descr' = [descr1, descr2];
berghofe@18016
  1516
berghofe@17870
  1517
    val typ_of_dtyp' = replace_types' o typ_of_dtyp;
berghofe@17870
  1518
berghofe@17870
  1519
    val rep_names = map (fn s =>
berghofe@17870
  1520
      Sign.intern_const thy7 ("Rep_" ^ s)) new_type_names;
berghofe@17870
  1521
    val abs_names = map (fn s =>
berghofe@17870
  1522
      Sign.intern_const thy7 ("Abs_" ^ s)) new_type_names;
berghofe@17870
  1523
berghofe@18016
  1524
    val recTs' = List.mapPartial
berghofe@18016
  1525
      (fn ((_, ("nominal.nOption", _, _)), T) => NONE
berghofe@18016
  1526
        | (_, T) => SOME T) (descr ~~ get_rec_types descr sorts');
berghofe@18016
  1527
    val recTs = get_rec_types (List.concat descr') sorts';
berghofe@18016
  1528
    val newTs' = Library.take (length new_type_names, recTs');
berghofe@18016
  1529
    val newTs = Library.take (length new_type_names, recTs);
berghofe@17870
  1530
berghofe@17870
  1531
    val full_new_type_names = map (Sign.full_name (sign_of thy)) new_type_names;
berghofe@17870
  1532
berghofe@17870
  1533
    fun make_constr_def tname T T' ((thy, defs, eqns), ((cname, cargs), (cname', mx))) =
berghofe@17870
  1534
      let
berghofe@17870
  1535
        fun constr_arg (dt, (j, l_args, r_args)) =
berghofe@17870
  1536
          let
berghofe@17870
  1537
            val x' = mk_Free "x" (typ_of_dtyp' dt) j;
berghofe@17870
  1538
            val (dts, dt') = strip_option dt;
berghofe@17870
  1539
            val xs = map (fn (dt, i) => mk_Free "x" (typ_of_dtyp' dt) i)
berghofe@17870
  1540
              (dts ~~ (j upto j + length dts - 1))
berghofe@17870
  1541
            val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts)
berghofe@17870
  1542
            val (dts', dt'') = strip_dtyp dt'
berghofe@17870
  1543
          in case dt'' of
berghofe@17870
  1544
              DtRec k => if k < length new_type_names then
berghofe@17870
  1545
                  (j + length dts + 1,
berghofe@17870
  1546
                   xs @ x :: l_args,
berghofe@17870
  1547
                   foldr mk_abs_fun
berghofe@17870
  1548
                     (list_abs (map (pair "z" o typ_of_dtyp') dts',
berghofe@17870
  1549
                       Const (List.nth (rep_names, k), typ_of_dtyp' dt'' -->
berghofe@17870
  1550
                         typ_of_dtyp dt'') $ app_bnds x (length dts')))
berghofe@17870
  1551
                     xs :: r_args)
berghofe@17870
  1552
                else error "nested recursion not (yet) supported"
berghofe@17870
  1553
            | _ => (j + 1, x' :: l_args, x' :: r_args)
berghofe@17870
  1554
          end
berghofe@17870
  1555
berghofe@17870
  1556
        val (_, l_args, r_args) = foldr constr_arg (1, [], []) cargs;
berghofe@17870
  1557
        val abs_name = Sign.intern_const (Theory.sign_of thy) ("Abs_" ^ tname);
berghofe@17870
  1558
        val rep_name = Sign.intern_const (Theory.sign_of thy) ("Rep_" ^ tname);
berghofe@17870
  1559
        val constrT = map fastype_of l_args ---> T;
berghofe@17870
  1560
        val lhs = list_comb (Const (Sign.full_name thy (Sign.base_name cname),
berghofe@17870
  1561
          constrT), l_args);
berghofe@17870
  1562
        val rhs = list_comb (Const (cname, map fastype_of r_args ---> T'), r_args);
berghofe@17870
  1563
        val def = Logic.mk_equals (lhs, Const (abs_name, T' --> T) $ rhs);
berghofe@17870
  1564
        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
  1565
          (Const (rep_name, T --> T') $ lhs, rhs));
berghofe@17870
  1566
        val def_name = (Sign.base_name cname) ^ "_def";
berghofe@17870
  1567
        val (thy', [def_thm]) = thy |>
berghofe@17870
  1568
          Theory.add_consts_i [(cname', constrT, mx)] |>
berghofe@17870
  1569
          (PureThy.add_defs_i false o map Thm.no_attributes) [(def_name, def)]
berghofe@17870
  1570
      in (thy', defs @ [def_thm], eqns @ [eqn]) end;
berghofe@17870
  1571
berghofe@17870
  1572
    fun dt_constr_defs ((thy, defs, eqns, dist_lemmas),
berghofe@17870
  1573
        (((((_, (_, _, constrs)), tname), T), T'), constr_syntax)) =
berghofe@17870
  1574
      let
berghofe@17870
  1575
        val rep_const = cterm_of thy
berghofe@17870
  1576
          (Const (Sign.intern_const thy ("Rep_" ^ tname), T --> T'));
berghofe@17870
  1577
        val dist = standard (cterm_instantiate [(cterm_of thy distinct_f, rep_const)] distinct_lemma);
berghofe@17870
  1578
        val (thy', defs', eqns') = Library.foldl (make_constr_def tname T T')
berghofe@17870
  1579
          ((Theory.add_path tname thy, defs, []), constrs ~~ constr_syntax)
berghofe@17870
  1580
      in
berghofe@17870
  1581
        (parent_path flat_names thy', defs', eqns @ [eqns'], dist_lemmas @ [dist])
berghofe@17870
  1582
      end;
berghofe@17870
  1583
berghofe@17870
  1584
    val (thy8, constr_defs, constr_rep_eqns, dist_lemmas) = Library.foldl dt_constr_defs
berghofe@17870
  1585
      ((thy7, [], [], []), List.take (descr, length new_type_names) ~~
berghofe@17870
  1586
        new_type_names ~~ newTs ~~ newTs' ~~ constr_syntax);
berghofe@17870
  1587
berghofe@17870
  1588
    val abs_inject_thms = map (fn tname =>
berghofe@17870
  1589
      PureThy.get_thm thy8 (Name ("Abs_" ^ tname ^ "_inject"))) new_type_names;
berghofe@17870
  1590
berghofe@17870
  1591
    val rep_inject_thms = map (fn tname =>
berghofe@17870
  1592
      PureThy.get_thm thy8 (Name ("Rep_" ^ tname ^ "_inject"))) new_type_names;
berghofe@17870
  1593
berghofe@17870
  1594
    val rep_thms = map (fn tname =>
berghofe@17870
  1595
      PureThy.get_thm thy8 (Name ("Rep_" ^ tname))) new_type_names;
berghofe@17870
  1596
berghofe@17870
  1597
    val rep_inverse_thms = map (fn tname =>
berghofe@17870
  1598
      PureThy.get_thm thy8 (Name ("Rep_" ^ tname ^ "_inverse"))) new_type_names;
berghofe@17870
  1599
berghofe@17870
  1600
    (* prove theorem  Rep_i (Constr_j ...) = Constr'_j ...  *)
berghofe@17870
  1601
    
berghofe@17870
  1602
    fun prove_constr_rep_thm eqn =
berghofe@17870
  1603
      let
berghofe@17870
  1604
        val inj_thms = map (fn r => r RS iffD1) abs_inject_thms;
berghofe@17870
  1605
        val rewrites = constr_defs @ map mk_meta_eq rep_inverse_thms
berghofe@18010
  1606
      in standard (Goal.prove thy8 [] [] eqn (fn _ => EVERY
berghofe@17870
  1607
        [resolve_tac inj_thms 1,
berghofe@17870
  1608
         rewrite_goals_tac rewrites,
berghofe@17870
  1609
         rtac refl 3,
berghofe@17870
  1610
         resolve_tac rep_intrs 2,
berghofe@18010
  1611
         REPEAT (resolve_tac rep_thms 1)]))
berghofe@17870
  1612
      end;
berghofe@17870
  1613
berghofe@17870
  1614
    val constr_rep_thmss = map (map prove_constr_rep_thm) constr_rep_eqns;
berghofe@17870
  1615
berghofe@17870
  1616
    (* prove theorem  pi \<bullet> Rep_i x = Rep_i (pi \<bullet> x) *)
berghofe@17870
  1617
berghofe@17870
  1618
    fun prove_perm_rep_perm (atom, perm_closed_thms) = map (fn th =>
berghofe@17870
  1619
      let
berghofe@17870
  1620
        val _ $ (_ $ (Rep $ x) $ _) = Logic.unvarify (prop_of th);
berghofe@17870
  1621
        val Type ("fun", [T, U]) = fastype_of Rep;
berghofe@17870
  1622
        val permT = mk_permT (Type (atom, []));
berghofe@17870
  1623
        val pi = Free ("pi", permT);
berghofe@17870
  1624
      in
berghofe@18010
  1625
        standard (Goal.prove thy8 [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
  1626
            (Const ("nominal.perm", permT --> U --> U) $ pi $ (Rep $ x),
berghofe@18010
  1627
             Rep $ (Const ("nominal.perm", permT --> T --> T) $ pi $ x))))
berghofe@18010
  1628
          (fn _ => simp_tac (HOL_basic_ss addsimps (perm_defs @ Abs_inverse_thms @
berghofe@18010
  1629
            perm_closed_thms @ Rep_thms)) 1))
berghofe@17870
  1630
      end) Rep_thms;
berghofe@17870
  1631
berghofe@17870
  1632
    val perm_rep_perm_thms = List.concat (map prove_perm_rep_perm
berghofe@17870
  1633
      (atoms ~~ perm_closed_thmss));
berghofe@17870
  1634
berghofe@17870
  1635
    (* prove distinctness theorems *)
berghofe@17870
  1636
berghofe@18016
  1637
    val distinct_props = setmp DatatypeProp.dtK 1000
berghofe@18016
  1638
      (DatatypeProp.make_distincts new_type_names descr' sorts') thy8;
berghofe@17870
  1639
berghofe@17870
  1640
    val dist_rewrites = map (fn (rep_thms, dist_lemma) =>
berghofe@17870
  1641
      dist_lemma::(rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0]))
berghofe@17870
  1642
        (constr_rep_thmss ~~ dist_lemmas);
berghofe@17870
  1643
berghofe@17870
  1644
    fun prove_distinct_thms (_, []) = []
berghofe@17870
  1645
      | prove_distinct_thms (p as (rep_thms, dist_lemma), t::ts) =
berghofe@17870
  1646
          let
berghofe@18010
  1647
            val dist_thm = standard (Goal.prove thy8 [] [] t (fn _ =>
berghofe@18010
  1648
              simp_tac (simpset_of thy8 addsimps (dist_lemma :: rep_thms)) 1))
berghofe@17870
  1649
          in dist_thm::(standard (dist_thm RS not_sym))::
berghofe@17870
  1650
            (prove_distinct_thms (p, ts))
berghofe@17870
  1651
          end;
berghofe@17870
  1652
berghofe@17870
  1653
    val distinct_thms = map prove_distinct_thms
berghofe@17870
  1654
      (constr_rep_thmss ~~ dist_lemmas ~~ distinct_props);
berghofe@17870
  1655
berghofe@17870
  1656
    (** prove equations for permutation functions **)
berghofe@17870
  1657
berghofe@17870
  1658
    val abs_perm = PureThy.get_thms thy8 (Name "abs_perm"); (* FIXME *)
berghofe@17870
  1659
berghofe@17870
  1660
    val perm_simps' = map (fn (((i, (_, _, constrs)), tname), constr_rep_thms) =>
berghofe@17870
  1661
      let val T = replace_types' (nth_dtyp i)
berghofe@17870
  1662
      in List.concat (map (fn (atom, perm_closed_thms) =>
berghofe@17870
  1663
          map (fn ((cname, dts), constr_rep_thm) => 
berghofe@17870
  1664
        let
berghofe@17870
  1665
          val cname = Sign.intern_const thy8
berghofe@17870
  1666
            (NameSpace.append tname (Sign.base_name cname));
berghofe@17870
  1667
          val permT = mk_permT (Type (atom, []));
berghofe@17870
  1668
          val pi = Free ("pi", permT);
berghofe@17870
  1669
berghofe@17870
  1670
          fun perm t =
berghofe@17870
  1671
            let val T = fastype_of t
berghofe@17870
  1672
            in Const ("nominal.perm", permT --> T --> T) $ pi $ t end;
berghofe@17870
  1673
berghofe@17870
  1674
          fun constr_arg (dt, (j, l_args, r_args)) =
berghofe@17870
  1675
            let
berghofe@17870
  1676
              val x' = mk_Free "x" (typ_of_dtyp' dt) j;
berghofe@17870
  1677
              val (dts, dt') = strip_option dt;
berghofe@17870
  1678
              val Ts = map typ_of_dtyp' dts;
berghofe@17870
  1679
              val xs = map (fn (T, i) => mk_Free "x" T i)
berghofe@17870
  1680
                (Ts ~~ (j upto j + length dts - 1))
berghofe@17870
  1681
              val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts);
berghofe@17870
  1682
              val (dts', dt'') = strip_dtyp dt';
berghofe@17870
  1683
            in case dt'' of
berghofe@17870
  1684
                DtRec k => if k < length new_type_names then
berghofe@17870
  1685
                    (j + length dts + 1,
berghofe@17870
  1686
                     xs @ x :: l_args,
berghofe@17870
  1687
                     map perm (xs @ [x]) @ r_args)
berghofe@17870
  1688
                  else error "nested recursion not (yet) supported"
berghofe@17870
  1689
              | _ => (j + 1, x' :: l_args, perm x' :: r_args)
berghofe@17870
  1690
            end
berghofe@17870
  1691
berghofe@17870
  1692
          val (_, l_args, r_args) = foldr constr_arg (1, [], []) dts;
berghofe@17870
  1693
          val c = Const (cname, map fastype_of l_args ---> T)
berghofe@17870
  1694
        in
berghofe@18010
  1695
          standard (Goal.prove thy8 [] []
berghofe@17870
  1696
            (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@18010
  1697
              (perm (list_comb (c, l_args)), list_comb (c, r_args))))
berghofe@18010
  1698
            (fn _ => EVERY
berghofe@17870
  1699
              [simp_tac (simpset_of thy8 addsimps (constr_rep_thm :: perm_defs)) 1,
berghofe@17870
  1700
               simp_tac (HOL_basic_ss addsimps (Rep_thms @ Abs_inverse_thms @
berghofe@17870
  1701
                 constr_defs @ perm_closed_thms)) 1,
berghofe@17870
  1702
               TRY (simp_tac (HOL_basic_ss addsimps
berghofe@17870
  1703
                 (symmetric perm_fun_def :: abs_perm)) 1),
berghofe@17870
  1704
               TRY (simp_tac (HOL_basic_ss addsimps
berghofe@17870
  1705
                 (perm_fun_def :: perm_defs @ Rep_thms @ Abs_inverse_thms @
berghofe@18010
  1706
                    perm_closed_thms)) 1)]))
berghofe@17870
  1707
        end) (constrs ~~ constr_rep_thms)) (atoms ~~ perm_closed_thmss))
berghofe@17870
  1708
      end) (List.take (descr, length new_type_names) ~~ new_type_names ~~ constr_rep_thmss);
berghofe@17870
  1709
berghofe@17870
  1710
    (** prove injectivity of constructors **)
berghofe@17870
  1711
berghofe@17870
  1712
    val rep_inject_thms' = map (fn th => th RS sym) rep_inject_thms;
berghofe@17870
  1713
    val alpha = PureThy.get_thms thy8 (Name "alpha");
berghofe@17870
  1714
    val abs_fresh = PureThy.get_thms thy8 (Name "abs_fresh");
berghofe@17870
  1715
    val fresh_def = PureThy.get_thm thy8 (Name "fresh_def");
berghofe@17870
  1716
    val supp_def = PureThy.get_thm thy8 (Name "supp_def");
berghofe@17870
  1717
berghofe@17870
  1718
    val inject_thms = map (fn (((i, (_, _, constrs)), tname), constr_rep_thms) =>
berghofe@17870
  1719
      let val T = replace_types' (nth_dtyp i)
berghofe@17870
  1720
      in List.mapPartial (fn ((cname, dts), constr_rep_thm) =>
berghofe@17870
  1721
        if null dts then NONE else SOME
berghofe@17870
  1722
        let
berghofe@17870
  1723
          val cname = Sign.intern_const thy8
berghofe@17870
  1724
            (NameSpace.append tname (Sign.base_name cname));
berghofe@17870
  1725
berghofe@17870
  1726
          fun make_inj (dt, (j, args1, args2, eqs)) =
berghofe@17870
  1727
            let
berghofe@17870
  1728
              val x' = mk_Free "x" (typ_of_dtyp' dt) j;
berghofe@17870
  1729
              val y' = mk_Free "y" (typ_of_dtyp' dt) j;
berghofe@17870
  1730
              val (dts, dt') = strip_option dt;
berghofe@17870
  1731
              val Ts_idx = map typ_of_dtyp' dts ~~ (j upto j + length dts - 1);
berghofe@17870
  1732
              val xs = map (fn (T, i) => mk_Free "x" T i) Ts_idx;
berghofe@17870
  1733
              val ys = map (fn (T, i) => mk_Free "y" T i) Ts_idx;
berghofe@17870
  1734
              val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts);
berghofe@17870
  1735
              val y = mk_Free "y" (typ_of_dtyp' dt') (j + length dts);
berghofe@17870
  1736
              val (dts', dt'') = strip_dtyp dt';
berghofe@17870
  1737
            in case dt'' of
berghofe@17870
  1738
                DtRec k => if k < length new_type_names then
berghofe@17870
  1739
                    (j + length dts + 1,
berghofe@17870
  1740
                     xs @ (x :: args1), ys @ (y :: args2),
berghofe@17870
  1741
                     HOLogic.mk_eq
berghofe@17870
  1742
                       (foldr mk_abs_fun x xs, foldr mk_abs_fun y ys) :: eqs)
berghofe@17870
  1743
                  else error "nested recursion not (yet) supported"
berghofe@17870
  1744
              | _ => (j + 1, x' :: args1, y' :: args2, HOLogic.mk_eq (x', y') :: eqs)
berghofe@17870
  1745
            end;
berghofe@17870
  1746
berghofe@17870
  1747
          val (_, args1, args2, eqs) = foldr make_inj (1, [], [], []) dts;
berghofe@17870
  1748
          val Ts = map fastype_of args1;
berghofe@17870
  1749
          val c = Const (cname, Ts ---> T)
berghofe@17870
  1750
        in
berghofe@18010
  1751
          standard (Goal.prove thy8 [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
  1752
              (HOLogic.mk_eq (list_comb (c, args1), list_comb (c, args2)),
berghofe@18010
  1753
               foldr1 HOLogic.mk_conj eqs)))
berghofe@18010
  1754
            (fn _ => EVERY
berghofe@17870
  1755
               [asm_full_simp_tac (simpset_of thy8 addsimps (constr_rep_thm ::
berghofe@17870
  1756
                  rep_inject_thms')) 1,
berghofe@17870
  1757
                TRY (asm_full_simp_tac (HOL_basic_ss addsimps (fresh_def :: supp_def ::
berghofe@17870
  1758
                  alpha @ abs_perm @ abs_fresh @ rep_inject_thms @
berghofe@17874
  1759
                  perm_rep_perm_thms)) 1),
berghofe@17874
  1760
                TRY (asm_full_simp_tac (HOL_basic_ss addsimps (perm_fun_def ::
berghofe@18010
  1761
                  expand_fun_eq :: rep_inject_thms @ perm_rep_perm_thms)) 1)]))
berghofe@17870
  1762
        end) (constrs ~~ constr_rep_thms)
berghofe@17870
  1763
      end) (List.take (descr, length new_type_names) ~~ new_type_names ~~ constr_rep_thmss);
berghofe@17870
  1764
berghofe@17872
  1765
    (** equations for support and freshness **)
berghofe@17872
  1766
berghofe@17872
  1767
    val Un_assoc = PureThy.get_thm thy8 (Name "Un_assoc");
berghofe@17872
  1768
    val de_Morgan_conj = PureThy.get_thm thy8 (Name "de_Morgan_conj");
berghofe@17872
  1769
    val Collect_disj_eq = PureThy.get_thm thy8 (Name "Collect_disj_eq");
berghofe@17872
  1770
    val finite_Un = PureThy.get_thm thy8 (Name "finite_Un");
berghofe@17872
  1771
berghofe@17872
  1772
    val (supp_thms, fresh_thms) = ListPair.unzip (map ListPair.unzip
berghofe@17872
  1773
      (map (fn ((((i, (_, _, constrs)), tname), inject_thms'), perm_thms') =>
berghofe@17872
  1774
      let val T = replace_types' (nth_dtyp i)
berghofe@17872
  1775
      in List.concat (map (fn (cname, dts) => map (fn atom =>
berghofe@17872
  1776
        let
berghofe@17872
  1777
          val cname = Sign.intern_const thy8
berghofe@17872
  1778
            (NameSpace.append tname (Sign.base_name cname));
berghofe@17872
  1779
          val atomT = Type (atom, []);
berghofe@17872
  1780
berghofe@17872
  1781
          fun process_constr (dt, (j, args1, args2)) =
berghofe@17872
  1782
            let
berghofe@17872
  1783
              val x' = mk_Free "x" (typ_of_dtyp' dt) j;
berghofe@17872
  1784
              val (dts, dt') = strip_option dt;
berghofe@17872
  1785
              val Ts_idx = map typ_of_dtyp' dts ~~ (j upto j + length dts - 1);
berghofe@17872
  1786
              val xs = map (fn (T, i) => mk_Free "x" T i) Ts_idx;
berghofe@17872
  1787
              val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts);
berghofe@17872
  1788
              val (dts', dt'') = strip_dtyp dt';
berghofe@17872
  1789
            in case dt'' of
berghofe@17872
  1790
                DtRec k => if k < length new_type_names then
berghofe@17872
  1791
                    (j + length dts + 1,
berghofe@17872
  1792
                     xs @ (x :: args1), foldr mk_abs_fun x xs :: args2)
berghofe@17872
  1793
                  else error "nested recursion not (yet) supported"
berghofe@17872
  1794
              | _ => (j + 1, x' :: args1, x' :: args2)
berghofe@17872
  1795
            end;
berghofe@17872
  1796
berghofe@17872
  1797
          val (_, args1, args2) = foldr process_constr (1, [], []) dts;
berghofe@17872
  1798
          val Ts = map fastype_of args1;
berghofe@17872
  1799
          val c = list_comb (Const (cname, Ts ---> T), args1);
berghofe@17872
  1800
          fun supp t =
berghofe@17872
  1801
            Const ("nominal.supp", fastype_of t --> HOLogic.mk_setT atomT) $ t;
berghofe@17872
  1802
          fun fresh t =
berghofe@17872
  1803
            Const ("nominal.fresh", atomT --> fastype_of t --> HOLogic.boolT) $
berghofe@17872
  1804
              Free ("a", atomT) $ t;
berghofe@18010
  1805
          val supp_thm = standard (Goal.prove thy8 [] []
berghofe@17872
  1806
              (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17872
  1807
                (supp c,
berghofe@17872
  1808
                 if null dts then Const ("{}", HOLogic.mk_setT atomT)
berghofe@18010
  1809
                 else foldr1 (HOLogic.mk_binop "op Un") (map supp args2))))
berghofe@17872
  1810
            (fn _ =>
berghofe@18010
  1811
              simp_tac (HOL_basic_ss addsimps (supp_def ::
berghofe@17872
  1812
                 Un_assoc :: de_Morgan_conj :: Collect_disj_eq :: finite_Un ::
berghofe@17874
  1813
                 symmetric empty_def :: Finites.emptyI :: simp_thms @
berghofe@18010
  1814
                 abs_perm @ abs_fresh @ inject_thms' @ perm_thms')) 1))
berghofe@17872
  1815
        in
berghofe@17872
  1816
          (supp_thm,
berghofe@18010
  1817
           standard (Goal.prove thy8 [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17872
  1818
              (fresh c,
berghofe@17872
  1819
               if null dts then HOLogic.true_const
berghofe@18010
  1820
               else foldr1 HOLogic.mk_conj (map fresh args2))))
berghofe@17872
  1821
             (fn _ =>
berghofe@18010
  1822
               simp_tac (simpset_of thy8 addsimps [fresh_def, supp_thm]) 1)))
berghofe@17872
  1823
        end) atoms) constrs)
berghofe@17872
  1824
      end) (List.take (descr, length new_type_names) ~~ new_type_names ~~ inject_thms ~~ perm_simps')));
berghofe@17872
  1825
berghofe@18016
  1826
    (**** Induction theorem ****)
berghofe@18016
  1827
berghofe@18016
  1828
    val arities = get_arities (List.concat descr');
berghofe@18016
  1829
berghofe@18016
  1830
    fun mk_funs_inv thm =
berghofe@18016
  1831
      let
berghofe@18016
  1832
        val {sign, prop, ...} = rep_thm thm;
berghofe@18016
  1833
        val _ $ (_ $ (Const (_, Type (_, [U, _])) $ _ $ S)) $
berghofe@18016
  1834
          (_ $ (_ $ (r $ (a $ _)) $ _)) = Type.freeze prop;
berghofe@18016
  1835
        val used = add_term_tfree_names (a, []);
berghofe@18016
  1836
berghofe@18016
  1837
        fun mk_thm i =
berghofe@18016
  1838
          let
berghofe@18016
  1839
            val Ts = map (TFree o rpair HOLogic.typeS)
berghofe@18016
  1840
              (variantlist (replicate i "'t", used));
berghofe@18016
  1841
            val f = Free ("f", Ts ---> U)
berghofe@18016
  1842
          in standard (Goal.prove sign [] [] (Logic.mk_implies
berghofe@18016
  1843
            (HOLogic.mk_Trueprop (HOLogic.list_all
berghofe@18016
  1844
               (map (pair "x") Ts, HOLogic.mk_mem (app_bnds f i, S))),
berghofe@18016
  1845
             HOLogic.mk_Trueprop (HOLogic.mk_eq (list_abs (map (pair "x") Ts,
berghofe@18016
  1846
               r $ (a $ app_bnds f i)), f))))
berghofe@18016
  1847
            (fn _ => EVERY [REPEAT (rtac ext 1), REPEAT (etac allE 1), rtac thm 1, atac 1]))
berghofe@18016
  1848
          end
berghofe@18016
  1849
      in map (fn r => r RS subst) (thm :: map mk_thm arities) end;
berghofe@18016
  1850
berghofe@18016
  1851
    fun mk_indrule_lemma ((prems, concls), (((i, _), T), U)) =
berghofe@18016
  1852
      let
berghofe@18016
  1853
        val Rep_t = Const (List.nth (rep_names, i), T --> U) $
berghofe@18016
  1854
          mk_Free "x" T i;
berghofe@18016
  1855
berghofe@18016
  1856
        val Abs_t =  Const (List.nth (abs_names, i), U --> T)
berghofe@18016
  1857
berghofe@18016
  1858
      in (prems @ [HOLogic.imp $ HOLogic.mk_mem (Rep_t,
berghofe@18016
  1859
            Const (List.nth (rep_set_names, i), HOLogic.mk_setT U)) $
berghofe@18016
  1860
              (mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))],
berghofe@18016
  1861
          concls @ [mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ mk_Free "x" T i])
berghofe@18016
  1862
      end;
berghofe@18016
  1863
berghofe@18016
  1864
    val (indrule_lemma_prems, indrule_lemma_concls) =
berghofe@18016
  1865
      Library.foldl mk_indrule_lemma (([], []), (List.concat descr' ~~ recTs ~~ recTs'));
berghofe@18016
  1866
berghofe@18016
  1867
    val indrule_lemma = standard (Goal.prove thy8 [] []
berghofe@18016
  1868
      (Logic.mk_implies
berghofe@18016
  1869
        (HOLogic.mk_Trueprop (mk_conj indrule_lemma_prems),
berghofe@18016
  1870
         HOLogic.mk_Trueprop (mk_conj indrule_lemma_concls))) (fn _ => EVERY
berghofe@18016
  1871
           [REPEAT (etac conjE 1),
berghofe@18016
  1872
            REPEAT (EVERY
berghofe@18016
  1873
              [TRY (rtac conjI 1), full_simp_tac (HOL_basic_ss addsimps Rep_inverse_thms) 1,
berghofe@18016
  1874
               etac mp 1, resolve_tac Rep_thms 1])]));
berghofe@18016
  1875
berghofe@18016
  1876
    val Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule_lemma)));
berghofe@18016
  1877
    val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else
berghofe@18016
  1878
      map (Free o apfst fst o dest_Var) Ps;
berghofe@18016
  1879
    val indrule_lemma' = cterm_instantiate
berghofe@18016
  1880
      (map (cterm_of thy8) Ps ~~ map (cterm_of thy8) frees) indrule_lemma;
berghofe@18016
  1881
berghofe@18016
  1882
    val Abs_inverse_thms' = List.concat (map mk_funs_inv Abs_inverse_thms);
berghofe@18016
  1883
berghofe@18016
  1884
    val dt_induct_prop = DatatypeProp.make_ind descr' sorts';
berghofe@18016
  1885
    val dt_induct = standard (Goal.prove thy8 []
berghofe@18016
  1886
      (Logic.strip_imp_prems dt_induct_prop) (Logic.strip_imp_concl dt_induct_prop)
berghofe@18016
  1887
      (fn prems => EVERY
berghofe@18016
  1888
        [rtac indrule_lemma' 1,
berghofe@18016
  1889
         (DatatypeAux.indtac rep_induct THEN_ALL_NEW ObjectLogic.atomize_tac) 1,
berghofe@18016
  1890
         EVERY (map (fn (prem, r) => (EVERY
berghofe@18016
  1891
           [REPEAT (eresolve_tac Abs_inverse_thms' 1),
berghofe@18016
  1892
            simp_tac (HOL_basic_ss addsimps [symmetric r]) 1,
berghofe@18016
  1893
            DEPTH_SOLVE_1 (ares_tac [prem] 1 ORELSE etac allE 1)]))
berghofe@18016
  1894
                (prems ~~ constr_defs))]));
berghofe@18016
  1895
urbanc@18017
  1896
    val case_names_induct = mk_case_names_induct (List.concat descr');
berghofe@18016
  1897
berghofe@17870
  1898
    val (thy9, _) = thy8 |>
berghofe@18016
  1899
      Theory.add_path big_name |>
urbanc@18017
  1900
      PureThy.add_thms [(("induct_weak", dt_induct), [case_names_induct])] |>>
berghofe@18016
  1901
      Theory.parent_path |>>>
berghofe@17870
  1902
      DatatypeAux.store_thmss "distinct" new_type_names distinct_thms |>>>
berghofe@17870
  1903
      DatatypeAux.store_thmss "constr_rep" new_type_names constr_rep_thmss |>>>
berghofe@17870
  1904
      DatatypeAux.store_thmss "perm" new_type_names perm_simps' |>>>
berghofe@17872
  1905
      DatatypeAux.store_thmss "inject" new_type_names inject_thms |>>>
berghofe@17872
  1906
      DatatypeAux.store_thmss "supp" new_type_names supp_thms |>>>
berghofe@17872
  1907
      DatatypeAux.store_thmss "fresh" new_type_names fresh_thms;
berghofe@17870
  1908
berghofe@17870
  1909
  in
berghofe@17870
  1910
    (thy9, perm_eq_thms)
berghofe@17870
  1911
  end;
berghofe@17870
  1912
berghofe@17870
  1913
val add_nominal_datatype = gen_add_nominal_datatype read_typ true;
berghofe@17870
  1914
berghofe@17870
  1915
berghofe@17870
  1916
(* FIXME: The following stuff should be exported by DatatypePackage *)
berghofe@17870
  1917
berghofe@17870
  1918
local structure P = OuterParse and K = OuterKeyword in
berghofe@17870
  1919
berghofe@17870
  1920
val datatype_decl =
berghofe@17870
  1921
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.type_args -- P.name -- P.opt_infix --
berghofe@17870
  1922
    (P.$$$ "=" |-- P.enum1 "|" (P.name -- Scan.repeat P.typ -- P.opt_mixfix));
berghofe@17870
  1923
berghofe@17870
  1924
fun mk_datatype args =
berghofe@17870
  1925
  let
berghofe@17870
  1926
    val names = map (fn ((((NONE, _), t), _), _) => t | ((((SOME t, _), _), _), _) => t) args;
berghofe@17870
  1927
    val specs = map (fn ((((_, vs), t), mx), cons) =>
berghofe@17870
  1928
      (vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args;
berghofe@17870
  1929
  in #1 o add_nominal_datatype false names specs end;
berghofe@17870
  1930
berghofe@17870
  1931
val nominal_datatypeP =
berghofe@17870
  1932
  OuterSyntax.command "nominal_datatype" "define inductive datatypes" K.thy_decl
berghofe@17870
  1933
    (P.and_list1 datatype_decl >> (Toplevel.theory o mk_datatype));
berghofe@17870
  1934
berghofe@17870
  1935
val _ = OuterSyntax.add_parsers [nominal_datatypeP];
berghofe@17870
  1936
berghofe@17870
  1937
end;
berghofe@17870
  1938
berghofe@17870
  1939
end