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