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