src/HOL/Tools/datatype_rep_proofs.ML
changeset 5177 0d3a168e4d44
child 5215 3224d1a9a8f1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/datatype_rep_proofs.ML	Fri Jul 24 12:50:06 1998 +0200
     1.3 @@ -0,0 +1,542 @@
     1.4 +(*  Title:      HOL/Tools/datatype_rep_proofs.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Stefan Berghofer
     1.7 +    Copyright   1998  TU Muenchen
     1.8 +
     1.9 +Definitional introduction of datatypes
    1.10 +Proof of characteristic theorems:
    1.11 +
    1.12 + - injectivity of constructors
    1.13 + - distinctness of constructors (internal version)
    1.14 + - induction theorem
    1.15 +
    1.16 +*)
    1.17 +
    1.18 +signature DATATYPE_REP_PROOFS =
    1.19 +sig
    1.20 +  val representation_proofs : DatatypeAux.datatype_info Symtab.table ->
    1.21 +    string list -> (int * (string * DatatypeAux.dtyp list *
    1.22 +      (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    1.23 +        (string * mixfix) list -> (string * mixfix) list list -> theory ->
    1.24 +          theory * thm list list * thm list list * thm
    1.25 +end;
    1.26 +
    1.27 +structure DatatypeRepProofs : DATATYPE_REP_PROOFS =
    1.28 +struct
    1.29 +
    1.30 +open DatatypeAux;
    1.31 +
    1.32 +val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma);
    1.33 +
    1.34 +(* figure out internal names *)
    1.35 +
    1.36 +val image_name = Sign.intern_const (sign_of Set.thy) "op ``";
    1.37 +val UNIV_name = Sign.intern_const (sign_of Set.thy) "UNIV";
    1.38 +val inj_name = Sign.intern_const (sign_of Fun.thy) "inj";
    1.39 +val inj_on_name = Sign.intern_const (sign_of Fun.thy) "inj_on";
    1.40 +val inv_name = Sign.intern_const (sign_of Fun.thy) "inv";
    1.41 +
    1.42 +fun exh_thm_of (dt_info : datatype_info Symtab.table) tname =
    1.43 +  #exhaustion (the (Symtab.lookup (dt_info, tname)));
    1.44 +
    1.45 +(******************************************************************************)
    1.46 +
    1.47 +(*----------------------------------------------------------*)
    1.48 +(* Proofs dependent on concrete representation of datatypes *)
    1.49 +(*                                                          *)
    1.50 +(* - injectivity of constructors                            *)
    1.51 +(* - distinctness of constructors (internal version)        *)
    1.52 +(* - induction theorem                                      *)
    1.53 +(*----------------------------------------------------------*)
    1.54 +
    1.55 +fun representation_proofs (dt_info : datatype_info Symtab.table)
    1.56 +      new_type_names descr sorts types_syntax constr_syntax thy =
    1.57 +  let
    1.58 +    val Univ_thy = the (get_thy "Univ" thy);
    1.59 +    val node_name = Sign.intern_tycon (sign_of Univ_thy) "node";
    1.60 +    val [In0_name, In1_name, Scons_name, Leaf_name, Numb_name] =
    1.61 +      map (Sign.intern_const (sign_of Univ_thy))
    1.62 +        ["In0", "In1", "Scons", "Leaf", "Numb"];
    1.63 +    val [In0_inject, In1_inject, Scons_inject, Leaf_inject, In0_eq, In1_eq,
    1.64 +      In0_not_In1, In1_not_In0] = map (get_thm Univ_thy)
    1.65 +        ["In0_inject", "In1_inject", "Scons_inject", "Leaf_inject", "In0_eq",
    1.66 +         "In1_eq", "In0_not_In1", "In1_not_In0"];
    1.67 +
    1.68 +    val descr' = flat descr;
    1.69 +
    1.70 +    val big_rec_name = (space_implode "_" new_type_names) ^ "_rep_set";
    1.71 +    val rep_set_names = map (Sign.full_name (sign_of thy))
    1.72 +      (if length descr' = 1 then [big_rec_name] else
    1.73 +        (map ((curry (op ^) (big_rec_name ^ "_")) o string_of_int)
    1.74 +          (1 upto (length descr'))));
    1.75 +
    1.76 +    val leafTs = get_nonrec_types descr' sorts;
    1.77 +    val recTs = get_rec_types descr' sorts;
    1.78 +    val newTs = take (length (hd descr), recTs);
    1.79 +    val oldTs = drop (length (hd descr), recTs);
    1.80 +    val sumT = if null leafTs then HOLogic.unitT
    1.81 +      else fold_bal (fn (T, U) => Type ("+", [T, U])) leafTs;
    1.82 +    val Univ_elT = HOLogic.mk_setT (Type (node_name, [sumT]));
    1.83 +    val UnivT = HOLogic.mk_setT Univ_elT;
    1.84 +
    1.85 +    val In0 = Const (In0_name, Univ_elT --> Univ_elT);
    1.86 +    val In1 = Const (In1_name, Univ_elT --> Univ_elT);
    1.87 +    val Leaf = Const (Leaf_name, sumT --> Univ_elT);
    1.88 +
    1.89 +    (* make injections needed for embedding types in leaves *)
    1.90 +
    1.91 +    fun mk_inj T' x =
    1.92 +      let
    1.93 +        fun mk_inj' T n i =
    1.94 +          if n = 1 then x else
    1.95 +          let val n2 = n div 2;
    1.96 +              val Type (_, [T1, T2]) = T
    1.97 +          in
    1.98 +            if i <= n2 then
    1.99 +              Const ("Inl", T1 --> T) $ (mk_inj' T1 n2 i)
   1.100 +            else
   1.101 +              Const ("Inr", T2 --> T) $ (mk_inj' T2 (n - n2) (i - n2))
   1.102 +          end
   1.103 +      in mk_inj' sumT (length leafTs) (1 + find_index_eq T' leafTs)
   1.104 +      end;
   1.105 +
   1.106 +    (* make injections for constructors *)
   1.107 +
   1.108 +    fun mk_univ_inj ts = access_bal (ap In0, ap In1, if ts = [] then
   1.109 +        Const ("arbitrary", Univ_elT)
   1.110 +      else
   1.111 +        foldr1 (HOLogic.mk_binop Scons_name) ts);
   1.112 +
   1.113 +    (************** generate introduction rules for representing set **********)
   1.114 +
   1.115 +    val _ = writeln "Constructing representing sets...";
   1.116 +
   1.117 +    (* make introduction rule for a single constructor *)
   1.118 +
   1.119 +    fun make_intr s n (i, (_, cargs)) =
   1.120 +      let
   1.121 +        fun mk_prem (DtRec k, (j, prems, ts)) =
   1.122 +              let val free_t = mk_Free "x" Univ_elT j
   1.123 +              in (j + 1, (HOLogic.mk_mem (free_t,
   1.124 +                Const (nth_elem (k, rep_set_names), UnivT)))::prems, free_t::ts)
   1.125 +              end
   1.126 +          | mk_prem (dt, (j, prems, ts)) =
   1.127 +              let val T = typ_of_dtyp descr' sorts dt
   1.128 +              in (j + 1, prems, (Leaf $ mk_inj T (mk_Free "x" T j))::ts)
   1.129 +              end;
   1.130 +
   1.131 +        val (_, prems, ts) = foldr mk_prem (cargs, (1, [], []));
   1.132 +        val concl = HOLogic.mk_Trueprop (HOLogic.mk_mem
   1.133 +          (mk_univ_inj ts n i, Const (s, UnivT)))
   1.134 +      in Logic.list_implies (map HOLogic.mk_Trueprop prems, concl)
   1.135 +      end;
   1.136 +
   1.137 +    val consts = map (fn s => Const (s, UnivT)) rep_set_names;
   1.138 +
   1.139 +    val intr_ts = flat (map (fn ((_, (_, _, constrs)), rep_set_name) =>
   1.140 +      map (make_intr rep_set_name (length constrs))
   1.141 +        ((1 upto (length constrs)) ~~ constrs)) (descr' ~~ rep_set_names));
   1.142 +
   1.143 +    val (thy2, {raw_induct = rep_induct, intrs = rep_intrs, ...}) =
   1.144 +      InductivePackage.add_inductive_i false true big_rec_name false true false
   1.145 +        consts intr_ts [] [] thy;
   1.146 +
   1.147 +    (********************************* typedef ********************************)
   1.148 +
   1.149 +    val tyvars = map (fn (_, (_, Ts, _)) => map dest_DtTFree Ts) (hd descr);
   1.150 +
   1.151 +    val thy3 = foldl (fn (thy, ((((name, mx), tvs), c), name')) =>
   1.152 +      TypedefPackage.add_typedef_i_no_def name' (name, tvs, mx) c [] []
   1.153 +        (Some (BREADTH_FIRST (has_fewer_prems 1) (resolve_tac rep_intrs 1))) thy)
   1.154 +          (thy2, types_syntax ~~ tyvars ~~ (take (length newTs, consts)) ~~
   1.155 +            new_type_names);
   1.156 +
   1.157 +    (*********************** definition of constructors ***********************)
   1.158 +
   1.159 +    val big_rep_name = (space_implode "_" new_type_names) ^ "_Rep_";
   1.160 +    val rep_names = map (curry op ^ "Rep_") new_type_names;
   1.161 +    val rep_names' = map (fn i => big_rep_name ^ (string_of_int i))
   1.162 +      (1 upto (length (flat (tl descr))));
   1.163 +    val all_rep_names = map (Sign.full_name (sign_of thy3)) (rep_names @ rep_names');
   1.164 +
   1.165 +    (* isomorphism declarations *)
   1.166 +
   1.167 +    val iso_decls = map (fn (T, s) => (s, T --> Univ_elT, NoSyn))
   1.168 +      (oldTs ~~ rep_names');
   1.169 +
   1.170 +    (* constructor definitions *)
   1.171 +
   1.172 +    fun make_constr_def tname T n ((thy, defs, eqns, i), ((cname, cargs), (cname', mx))) =
   1.173 +      let
   1.174 +        fun constr_arg (dt, (j, l_args, r_args)) =
   1.175 +          let val T = typ_of_dtyp descr' sorts dt;
   1.176 +              val free_t = mk_Free "x" T j
   1.177 +          in (case dt of
   1.178 +              DtRec m => (j + 1, free_t::l_args, (Const (nth_elem (m, all_rep_names),
   1.179 +                T --> Univ_elT) $ free_t)::r_args)
   1.180 +            | _ => (j + 1, free_t::l_args, (Leaf $ mk_inj T free_t)::r_args))
   1.181 +          end;
   1.182 +
   1.183 +        val (_, l_args, r_args) = foldr constr_arg (cargs, (1, [], []));
   1.184 +        val constrT = (map (typ_of_dtyp descr' sorts) cargs) ---> T;
   1.185 +        val abs_name = Sign.intern_const (sign_of thy) ("Abs_" ^ tname);
   1.186 +        val rep_name = Sign.intern_const (sign_of thy) ("Rep_" ^ tname);
   1.187 +        val lhs = list_comb (Const (cname, constrT), l_args);
   1.188 +        val rhs = mk_univ_inj r_args n i;
   1.189 +        val def = equals T $ lhs $ (Const (abs_name, Univ_elT --> T) $ rhs);
   1.190 +        val def_name = (Sign.base_name cname) ^ "_def";
   1.191 +        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
   1.192 +          (Const (rep_name, T --> Univ_elT) $ lhs, rhs));
   1.193 +        val thy' = thy |>
   1.194 +          Theory.add_consts_i [(cname', constrT, mx)] |>
   1.195 +          Theory.add_defs_i [(def_name, def)];
   1.196 +
   1.197 +      in (thy', defs @ [get_axiom thy' def_name], eqns @ [eqn], i + 1)
   1.198 +      end;
   1.199 +
   1.200 +    (* constructor definitions for datatype *)
   1.201 +
   1.202 +    fun dt_constr_defs ((thy, defs, eqns, rep_congs, dist_lemmas),
   1.203 +        ((((_, (_, _, constrs)), tname), T), constr_syntax)) =
   1.204 +      let
   1.205 +        val _ $ (_ $ (cong_f $ _) $ _) = concl_of arg_cong;
   1.206 +        val sg = sign_of thy;
   1.207 +        val rep_const = cterm_of sg
   1.208 +          (Const (Sign.intern_const sg ("Rep_" ^ tname), T --> Univ_elT));
   1.209 +        val cong' = cterm_instantiate [(cterm_of sg cong_f, rep_const)] arg_cong;
   1.210 +        val dist = cterm_instantiate [(cterm_of sg distinct_f, rep_const)] distinct_lemma;
   1.211 +        val (thy', defs', eqns', _) = foldl ((make_constr_def tname T) (length constrs))
   1.212 +          ((if length newTs = 1 then thy else Theory.add_path tname thy, defs, [], 1),
   1.213 +            constrs ~~ constr_syntax)
   1.214 +      in
   1.215 +        (if length newTs = 1 then thy' else Theory.parent_path thy', defs', eqns @ [eqns'],
   1.216 +          rep_congs @ [cong'], dist_lemmas @ [dist])
   1.217 +      end;
   1.218 +
   1.219 +    val (thy4, constr_defs, constr_rep_eqns, rep_congs, dist_lemmas) = foldl dt_constr_defs
   1.220 +      ((Theory.add_consts_i iso_decls thy3, [], [], [], []),
   1.221 +        hd descr ~~ new_type_names ~~ newTs ~~ constr_syntax);
   1.222 +
   1.223 +    (*********** isomorphisms for new types (introduced by typedef) ***********)
   1.224 +
   1.225 +    val _ = writeln "Proving isomorphism properties...";
   1.226 +
   1.227 +    (* get axioms from theory *)
   1.228 +
   1.229 +    val newT_iso_axms = map (fn s =>
   1.230 +      (get_axiom thy4 ("Abs_" ^ s ^ "_inverse"),
   1.231 +       get_axiom thy4 ("Rep_" ^ s ^ "_inverse"),
   1.232 +       get_axiom thy4 ("Rep_" ^ s))) new_type_names;
   1.233 +
   1.234 +    (*------------------------------------------------*)
   1.235 +    (* prove additional theorems:                     *)
   1.236 +    (*  inj_on dt_Abs_i rep_set_i  and  inj dt_Rep_i  *)
   1.237 +    (*------------------------------------------------*)
   1.238 +
   1.239 +    fun prove_newT_iso_inj_thm (((s, (thm1, thm2, _)), T), rep_set_name) =
   1.240 +      let
   1.241 +        val sg = sign_of thy4;
   1.242 +        val RepT = T --> Univ_elT;
   1.243 +        val Rep_name = Sign.intern_const sg ("Rep_" ^ s);
   1.244 +        val AbsT = Univ_elT --> T;
   1.245 +        val Abs_name = Sign.intern_const sg ("Abs_" ^ s);
   1.246 +
   1.247 +        val inj_on_Abs_thm = prove_goalw_cterm [] (cterm_of sg
   1.248 +          (HOLogic.mk_Trueprop (Const (inj_on_name, [AbsT, UnivT] ---> HOLogic.boolT) $
   1.249 +            Const (Abs_name, AbsT) $ Const (rep_set_name, UnivT))))
   1.250 +              (fn _ => [rtac inj_on_inverseI 1, etac thm1 1]);
   1.251 +
   1.252 +        val inj_Rep_thm = prove_goalw_cterm [] (cterm_of sg
   1.253 +          (HOLogic.mk_Trueprop (Const (inj_name, RepT --> HOLogic.boolT) $
   1.254 +            Const (Rep_name, RepT))))
   1.255 +              (fn _ => [rtac inj_inverseI 1, rtac thm2 1])
   1.256 +
   1.257 +      in (inj_on_Abs_thm, inj_Rep_thm) end;
   1.258 +
   1.259 +    val newT_iso_inj_thms = map prove_newT_iso_inj_thm
   1.260 +      (new_type_names ~~ newT_iso_axms ~~ newTs ~~
   1.261 +        take (length newTs, rep_set_names));
   1.262 +
   1.263 +    (********* isomorphisms between existing types and "unfolded" types *******)
   1.264 +
   1.265 +    (*---------------------------------------------------------------------*)
   1.266 +    (* isomorphisms are defined using primrec-combinators:                 *)
   1.267 +    (* generate appropriate functions for instantiating primrec-combinator *)
   1.268 +    (*                                                                     *)
   1.269 +    (*   e.g.  dt_Rep_i = list_rec ... (%h t y. In1 ((Leaf h) $ y))        *)
   1.270 +    (*                                                                     *)
   1.271 +    (* also generate characteristic equations for isomorphisms             *)
   1.272 +    (*                                                                     *)
   1.273 +    (*   e.g.  dt_Rep_i (cons h t) = In1 ((dt_Rep_j h) $ (dt_Rep_i t))     *)
   1.274 +    (*---------------------------------------------------------------------*)
   1.275 +
   1.276 +    fun make_iso_def k ks n ((fs, eqns, i), (cname, cargs)) =
   1.277 +      let
   1.278 +        val argTs = map (typ_of_dtyp descr' sorts) cargs;
   1.279 +        val T = nth_elem (k, recTs);
   1.280 +        val rep_name = nth_elem (k, all_rep_names);
   1.281 +        val rep_const = Const (rep_name, T --> Univ_elT);
   1.282 +        val constr = Const (cname, argTs ---> T);
   1.283 +
   1.284 +        fun process_arg ks' ((i2, i2', ts), dt) =
   1.285 +          let val T' = typ_of_dtyp descr' sorts dt
   1.286 +          in (case dt of
   1.287 +              DtRec j => if j mem ks' then
   1.288 +                  (i2 + 1, i2' + 1, ts @ [mk_Free "y" Univ_elT i2'])
   1.289 +                else
   1.290 +                  (i2 + 1, i2', ts @ [Const (nth_elem (j, all_rep_names),
   1.291 +                    T' --> Univ_elT) $ mk_Free "x" T' i2])
   1.292 +            | _ => (i2 + 1, i2', ts @ [Leaf $ mk_inj T' (mk_Free "x" T' i2)]))
   1.293 +          end;
   1.294 +
   1.295 +        val (i2, i2', ts) = foldl (process_arg ks) ((1, 1, []), cargs);
   1.296 +        val xs = map (uncurry (mk_Free "x")) (argTs ~~ (1 upto (i2 - 1)));
   1.297 +        val ys = map (mk_Free "y" Univ_elT) (1 upto (i2' - 1));
   1.298 +        val f = list_abs_free (map dest_Free (xs @ ys), mk_univ_inj ts n i);
   1.299 +
   1.300 +        val (_, _, ts') = foldl (process_arg []) ((1, 1, []), cargs);
   1.301 +        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
   1.302 +          (rep_const $ list_comb (constr, xs), mk_univ_inj ts' n i))
   1.303 +
   1.304 +      in (fs @ [f], eqns @ [eqn], i + 1) end;
   1.305 +
   1.306 +    (* define isomorphisms for all mutually recursive datatypes in list ds *)
   1.307 +
   1.308 +    fun make_iso_defs (ds, (thy, char_thms)) =
   1.309 +      let
   1.310 +        val ks = map fst ds;
   1.311 +        val (_, (tname, _, _)) = hd ds;
   1.312 +        val {rec_rewrites, rec_names, ...} = the (Symtab.lookup (dt_info, tname));
   1.313 +
   1.314 +        fun process_dt ((fs, eqns, isos), (k, (tname, _, constrs))) =
   1.315 +          let
   1.316 +            val (fs', eqns', _) = foldl (make_iso_def k ks (length constrs))
   1.317 +              ((fs, eqns, 1), constrs);
   1.318 +            val iso = (nth_elem (k, recTs), nth_elem (k, all_rep_names))
   1.319 +          in (fs', eqns', isos @ [iso]) end;
   1.320 +        
   1.321 +        val (fs, eqns, isos) = foldl process_dt (([], [], []), ds);
   1.322 +        val fTs = map fastype_of fs;
   1.323 +        val defs = map (fn (rec_name, (T, iso_name)) => ((Sign.base_name iso_name) ^ "_def",
   1.324 +          equals (T --> Univ_elT) $ Const (iso_name, T --> Univ_elT) $
   1.325 +            list_comb (Const (rec_name, fTs @ [T] ---> Univ_elT), fs))) (rec_names ~~ isos);
   1.326 +        val thy' = Theory.add_defs_i defs thy;
   1.327 +        val def_thms = map (get_axiom thy') (map fst defs);
   1.328 +
   1.329 +        (* prove characteristic equations *)
   1.330 +
   1.331 +        val rewrites = def_thms @ (map mk_meta_eq rec_rewrites);
   1.332 +        val char_thms' = map (fn eqn => prove_goalw_cterm rewrites
   1.333 +          (cterm_of (sign_of thy') eqn) (fn _ => [rtac refl 1])) eqns;
   1.334 +
   1.335 +      in (thy', char_thms' @ char_thms) end;
   1.336 +
   1.337 +    val (thy5, iso_char_thms) = foldr make_iso_defs (tl descr, (thy4, []));
   1.338 +
   1.339 +    (* prove isomorphism properties *)
   1.340 +
   1.341 +    (* prove  x : dt_rep_set_i --> x : range dt_Rep_i *)
   1.342 +
   1.343 +    fun mk_iso_t (((set_name, iso_name), i), T) =
   1.344 +      let val isoT = T --> Univ_elT
   1.345 +      in HOLogic.imp $ 
   1.346 +        HOLogic.mk_mem (mk_Free "x" Univ_elT i, Const (set_name, UnivT)) $
   1.347 +          (if i < length newTs then Const ("True", HOLogic.boolT)
   1.348 +           else HOLogic.mk_mem (mk_Free "x" Univ_elT i,
   1.349 +             Const (image_name, [isoT, HOLogic.mk_setT T] ---> UnivT) $
   1.350 +               Const (iso_name, isoT) $ Const (UNIV_name, HOLogic.mk_setT T)))
   1.351 +      end;
   1.352 +
   1.353 +    val iso_t = HOLogic.mk_Trueprop (mk_conj (map mk_iso_t
   1.354 +      (rep_set_names ~~ all_rep_names ~~ (0 upto (length descr' - 1)) ~~ recTs)));
   1.355 +
   1.356 +    val newT_Abs_inverse_thms = map (fn (iso, _, _) => iso RS subst) newT_iso_axms;
   1.357 +
   1.358 +    (* all the theorems are proved by one single simultaneous induction *)
   1.359 +
   1.360 +    val iso_thms = if length descr = 1 then [] else
   1.361 +      drop (length newTs, split_conj_thm
   1.362 +        (prove_goalw_cterm [] (cterm_of (sign_of thy5) iso_t) (fn _ =>
   1.363 +           [indtac rep_induct 1,
   1.364 +            REPEAT (rtac TrueI 1),
   1.365 +            REPEAT (EVERY
   1.366 +              [REPEAT (etac rangeE 1),
   1.367 +               REPEAT (eresolve_tac newT_Abs_inverse_thms 1),
   1.368 +               TRY (hyp_subst_tac 1),
   1.369 +               rtac (sym RS range_eqI) 1,
   1.370 +               resolve_tac iso_char_thms 1])])));
   1.371 +
   1.372 +    val Abs_inverse_thms = newT_Abs_inverse_thms @ (map (fn r =>
   1.373 +      r RS mp RS f_inv_f RS subst) iso_thms);
   1.374 +
   1.375 +    (* prove  inj dt_Rep_i  and  dt_Rep_i x : dt_rep_set_i *)
   1.376 +
   1.377 +    fun prove_iso_thms (ds, (inj_thms, elem_thms)) =
   1.378 +      let
   1.379 +        val (_, (tname, _, _)) = hd ds;
   1.380 +        val {induction, ...} = the (Symtab.lookup (dt_info, tname));
   1.381 +
   1.382 +        fun mk_ind_concl (i, _) =
   1.383 +          let
   1.384 +            val T = nth_elem (i, recTs);
   1.385 +            val Rep_t = Const (nth_elem (i, all_rep_names), T --> Univ_elT);
   1.386 +            val rep_set_name = nth_elem (i, rep_set_names)
   1.387 +          in (HOLogic.all_const T $ Abs ("y", T, HOLogic.imp $
   1.388 +                HOLogic.mk_eq (Rep_t $ mk_Free "x" T i, Rep_t $ Bound 0) $
   1.389 +                  HOLogic.mk_eq (mk_Free "x" T i, Bound 0)),
   1.390 +              HOLogic.mk_mem (Rep_t $ mk_Free "x" T i, Const (rep_set_name, UnivT)))
   1.391 +          end;
   1.392 +
   1.393 +        val (ind_concl1, ind_concl2) = ListPair.unzip (map mk_ind_concl ds);
   1.394 +
   1.395 +        val rewrites = map mk_meta_eq iso_char_thms;
   1.396 +        val inj_thms' = map (fn r => r RS injD) inj_thms;
   1.397 +
   1.398 +        val inj_thm = prove_goalw_cterm [] (cterm_of (sign_of thy5)
   1.399 +          (HOLogic.mk_Trueprop (mk_conj ind_concl1))) (fn _ =>
   1.400 +            [indtac induction 1,
   1.401 +             REPEAT (EVERY
   1.402 +               [rtac allI 1, rtac impI 1,
   1.403 +                exh_tac (exh_thm_of dt_info) 1,
   1.404 +                REPEAT (EVERY
   1.405 +                  [hyp_subst_tac 1,
   1.406 +                   rewrite_goals_tac rewrites,
   1.407 +                   REPEAT (dresolve_tac [In0_inject, In1_inject] 1),
   1.408 +                   (eresolve_tac [In0_not_In1 RS notE, In1_not_In0 RS notE] 1)
   1.409 +                   ORELSE (EVERY
   1.410 +                     [REPEAT (etac Scons_inject 1),
   1.411 +                      REPEAT (dresolve_tac
   1.412 +                        (inj_thms' @ [Leaf_inject, Inl_inject, Inr_inject]) 1),
   1.413 +                      REPEAT (EVERY [etac allE 1, dtac mp 1, atac 1]),
   1.414 +                      TRY (hyp_subst_tac 1),
   1.415 +                      rtac refl 1])])])]);
   1.416 +
   1.417 +        val inj_thms'' = map (fn r =>
   1.418 +          r RS (allI RS (inj_def RS meta_eq_to_obj_eq RS iffD2)))
   1.419 +            (split_conj_thm inj_thm);
   1.420 +
   1.421 +        val elem_thm = prove_goalw_cterm [] (cterm_of (sign_of thy5)
   1.422 +          (HOLogic.mk_Trueprop (mk_conj ind_concl2))) (fn _ =>
   1.423 +            [indtac induction 1,
   1.424 +             rewrite_goals_tac rewrites,
   1.425 +             REPEAT (EVERY
   1.426 +               [resolve_tac rep_intrs 1,
   1.427 +                REPEAT ((atac 1) ORELSE (resolve_tac elem_thms 1))])]);
   1.428 +
   1.429 +      in (inj_thms @ inj_thms'', elem_thms @ (split_conj_thm elem_thm))
   1.430 +      end;
   1.431 +
   1.432 +    val (iso_inj_thms, iso_elem_thms) = foldr prove_iso_thms
   1.433 +      (tl descr, (map snd newT_iso_inj_thms, map #3 newT_iso_axms));
   1.434 +
   1.435 +    (******************* freeness theorems for constructors *******************)
   1.436 +
   1.437 +    val _ = writeln "Proving freeness of constructors...";
   1.438 +
   1.439 +    (* prove theorem  Rep_i (Constr_j ...) = Inj_j ...  *)
   1.440 +    
   1.441 +    fun prove_constr_rep_thm eqn =
   1.442 +      let
   1.443 +        val inj_thms = map (fn (r, _) => r RS inj_onD) newT_iso_inj_thms;
   1.444 +        val rewrites = constr_defs @ (map (mk_meta_eq o #2) newT_iso_axms)
   1.445 +      in prove_goalw_cterm [] (cterm_of (sign_of thy5) eqn) (fn _ =>
   1.446 +        [resolve_tac inj_thms 1,
   1.447 +         rewrite_goals_tac rewrites,
   1.448 +         rtac refl 1,
   1.449 +         resolve_tac rep_intrs 2,
   1.450 +         REPEAT (resolve_tac iso_elem_thms 1)])
   1.451 +      end;
   1.452 +
   1.453 +    (*--------------------------------------------------------------*)
   1.454 +    (* constr_rep_thms and rep_congs are used to prove distinctness *)
   1.455 +    (* of constructors internally.                                  *)
   1.456 +    (* the external version uses dt_case which is not defined yet   *)
   1.457 +    (*--------------------------------------------------------------*)
   1.458 +
   1.459 +    val constr_rep_thms = map (map prove_constr_rep_thm) constr_rep_eqns;
   1.460 +
   1.461 +    val dist_rewrites = map (fn (rep_thms, dist_lemma) =>
   1.462 +      dist_lemma::(rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0]))
   1.463 +        (constr_rep_thms ~~ dist_lemmas);
   1.464 +
   1.465 +    (* prove injectivity of constructors *)
   1.466 +
   1.467 +    fun prove_constr_inj_thm rep_thms t =
   1.468 +      let val inj_thms = Scons_inject::(map make_elim
   1.469 +        ((map (fn r => r RS injD) iso_inj_thms) @
   1.470 +          [In0_inject, In1_inject, Leaf_inject, Inl_inject, Inr_inject]))
   1.471 +      in prove_goalw_cterm [] (cterm_of (sign_of thy5) t) (fn _ =>
   1.472 +        [rtac iffI 1,
   1.473 +         REPEAT (etac conjE 2), hyp_subst_tac 2, rtac refl 2,
   1.474 +         dresolve_tac rep_congs 1, dtac box_equals 1,
   1.475 +         REPEAT (resolve_tac rep_thms 1),
   1.476 +         REPEAT (eresolve_tac inj_thms 1),
   1.477 +         hyp_subst_tac 1,
   1.478 +         REPEAT (resolve_tac [conjI, refl] 1)])
   1.479 +      end;
   1.480 +
   1.481 +    val constr_inject = map (fn (ts, thms) => map (prove_constr_inj_thm thms) ts)
   1.482 +      ((DatatypeProp.make_injs descr sorts) ~~ constr_rep_thms);
   1.483 +
   1.484 +    val thy6 = store_thmss "inject" new_type_names constr_inject thy5;
   1.485 +
   1.486 +    (*************************** induction theorem ****************************)
   1.487 +
   1.488 +    val _ = writeln "Proving induction rule for datatypes...";
   1.489 +
   1.490 +    val Rep_inverse_thms = (map (fn (_, iso, _) => iso RS subst) newT_iso_axms) @
   1.491 +      (map (fn r => r RS inv_f_f RS subst) (drop (length newTs, iso_inj_thms)));
   1.492 +    val Rep_inverse_thms' = map (fn r => r RS inv_f_f)
   1.493 +      (drop (length newTs, iso_inj_thms));
   1.494 +
   1.495 +    fun mk_indrule_lemma ((prems, concls), ((i, _), T)) =
   1.496 +      let
   1.497 +        val Rep_t = Const (nth_elem (i, all_rep_names), T --> Univ_elT) $
   1.498 +          mk_Free "x" T i;
   1.499 +
   1.500 +        val Abs_t = if i < length newTs then
   1.501 +            Const (Sign.intern_const (sign_of thy6)
   1.502 +              ("Abs_" ^ (nth_elem (i, new_type_names))), Univ_elT --> T)
   1.503 +          else Const (inv_name, [T --> Univ_elT, Univ_elT] ---> T) $
   1.504 +            Const (nth_elem (i, all_rep_names), T --> Univ_elT)
   1.505 +
   1.506 +      in (prems @ [HOLogic.imp $ HOLogic.mk_mem (Rep_t,
   1.507 +            Const (nth_elem (i, rep_set_names), UnivT)) $
   1.508 +              (mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))],
   1.509 +          concls @ [mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ mk_Free "x" T i])
   1.510 +      end;
   1.511 +
   1.512 +    val (indrule_lemma_prems, indrule_lemma_concls) =
   1.513 +      foldl mk_indrule_lemma (([], []), (descr' ~~ recTs));
   1.514 +
   1.515 +    val cert = cterm_of (sign_of thy6);
   1.516 +
   1.517 +    val indrule_lemma = prove_goalw_cterm [] (cert
   1.518 +      (Logic.mk_implies
   1.519 +        (HOLogic.mk_Trueprop (mk_conj indrule_lemma_prems),
   1.520 +         HOLogic.mk_Trueprop (mk_conj indrule_lemma_concls)))) (fn prems =>
   1.521 +           [cut_facts_tac prems 1, REPEAT (etac conjE 1),
   1.522 +            REPEAT (EVERY
   1.523 +              [TRY (rtac conjI 1), resolve_tac Rep_inverse_thms 1,
   1.524 +               etac mp 1, resolve_tac iso_elem_thms 1])]);
   1.525 +
   1.526 +    val Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of indrule_lemma)));
   1.527 +    val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else
   1.528 +      map (Free o apfst fst o dest_Var) Ps;
   1.529 +    val indrule_lemma' = cterm_instantiate (map cert Ps ~~ map cert frees) indrule_lemma;
   1.530 +
   1.531 +    val dt_induct = prove_goalw_cterm [] (cert
   1.532 +      (DatatypeProp.make_ind descr sorts)) (fn prems =>
   1.533 +        [rtac indrule_lemma' 1, indtac rep_induct 1,
   1.534 +         EVERY (map (fn (prem, r) => (EVERY
   1.535 +           [REPEAT (eresolve_tac Abs_inverse_thms 1),
   1.536 +            simp_tac (HOL_basic_ss addsimps ((symmetric r)::Rep_inverse_thms')) 1,
   1.537 +            DEPTH_SOLVE_1 (ares_tac [prem] 1)]))
   1.538 +              (prems ~~ (constr_defs @ (map mk_meta_eq iso_char_thms))))]);
   1.539 +
   1.540 +    val thy7 = PureThy.add_tthms [(("induct", Attribute.tthm_of dt_induct), [])] thy6;
   1.541 +
   1.542 +  in (thy7, constr_inject, dist_rewrites, dt_induct)
   1.543 +  end;
   1.544 +
   1.545 +end;