src/HOL/Tools/datatype_rep_proofs.ML
author berghofe
Thu Jul 30 15:54:03 1998 +0200 (1998-07-30)
changeset 5215 3224d1a9a8f1
parent 5177 0d3a168e4d44
child 5303 22029546d109
permissions -rw-r--r--
Deleted obsolete comments.
     1 (*  Title:      HOL/Tools/datatype_rep_proofs.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer
     4     Copyright   1998  TU Muenchen
     5 
     6 Definitional introduction of datatypes
     7 Proof of characteristic theorems:
     8 
     9  - injectivity of constructors
    10  - distinctness of constructors (internal version)
    11  - induction theorem
    12 
    13 *)
    14 
    15 signature DATATYPE_REP_PROOFS =
    16 sig
    17   val representation_proofs : DatatypeAux.datatype_info Symtab.table ->
    18     string list -> (int * (string * DatatypeAux.dtyp list *
    19       (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    20         (string * mixfix) list -> (string * mixfix) list list -> theory ->
    21           theory * thm list list * thm list list * thm
    22 end;
    23 
    24 structure DatatypeRepProofs : DATATYPE_REP_PROOFS =
    25 struct
    26 
    27 open DatatypeAux;
    28 
    29 val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma);
    30 
    31 (* figure out internal names *)
    32 
    33 val image_name = Sign.intern_const (sign_of Set.thy) "op ``";
    34 val UNIV_name = Sign.intern_const (sign_of Set.thy) "UNIV";
    35 val inj_name = Sign.intern_const (sign_of Fun.thy) "inj";
    36 val inj_on_name = Sign.intern_const (sign_of Fun.thy) "inj_on";
    37 val inv_name = Sign.intern_const (sign_of Fun.thy) "inv";
    38 
    39 fun exh_thm_of (dt_info : datatype_info Symtab.table) tname =
    40   #exhaustion (the (Symtab.lookup (dt_info, tname)));
    41 
    42 (******************************************************************************)
    43 
    44 fun representation_proofs (dt_info : datatype_info Symtab.table)
    45       new_type_names descr sorts types_syntax constr_syntax thy =
    46   let
    47     val Univ_thy = the (get_thy "Univ" thy);
    48     val node_name = Sign.intern_tycon (sign_of Univ_thy) "node";
    49     val [In0_name, In1_name, Scons_name, Leaf_name, Numb_name] =
    50       map (Sign.intern_const (sign_of Univ_thy))
    51         ["In0", "In1", "Scons", "Leaf", "Numb"];
    52     val [In0_inject, In1_inject, Scons_inject, Leaf_inject, In0_eq, In1_eq,
    53       In0_not_In1, In1_not_In0] = map (get_thm Univ_thy)
    54         ["In0_inject", "In1_inject", "Scons_inject", "Leaf_inject", "In0_eq",
    55          "In1_eq", "In0_not_In1", "In1_not_In0"];
    56 
    57     val descr' = flat descr;
    58 
    59     val big_rec_name = (space_implode "_" new_type_names) ^ "_rep_set";
    60     val rep_set_names = map (Sign.full_name (sign_of thy))
    61       (if length descr' = 1 then [big_rec_name] else
    62         (map ((curry (op ^) (big_rec_name ^ "_")) o string_of_int)
    63           (1 upto (length descr'))));
    64 
    65     val leafTs = get_nonrec_types descr' sorts;
    66     val recTs = get_rec_types descr' sorts;
    67     val newTs = take (length (hd descr), recTs);
    68     val oldTs = drop (length (hd descr), recTs);
    69     val sumT = if null leafTs then HOLogic.unitT
    70       else fold_bal (fn (T, U) => Type ("+", [T, U])) leafTs;
    71     val Univ_elT = HOLogic.mk_setT (Type (node_name, [sumT]));
    72     val UnivT = HOLogic.mk_setT Univ_elT;
    73 
    74     val In0 = Const (In0_name, Univ_elT --> Univ_elT);
    75     val In1 = Const (In1_name, Univ_elT --> Univ_elT);
    76     val Leaf = Const (Leaf_name, sumT --> Univ_elT);
    77 
    78     (* make injections needed for embedding types in leaves *)
    79 
    80     fun mk_inj T' x =
    81       let
    82         fun mk_inj' T n i =
    83           if n = 1 then x else
    84           let val n2 = n div 2;
    85               val Type (_, [T1, T2]) = T
    86           in
    87             if i <= n2 then
    88               Const ("Inl", T1 --> T) $ (mk_inj' T1 n2 i)
    89             else
    90               Const ("Inr", T2 --> T) $ (mk_inj' T2 (n - n2) (i - n2))
    91           end
    92       in mk_inj' sumT (length leafTs) (1 + find_index_eq T' leafTs)
    93       end;
    94 
    95     (* make injections for constructors *)
    96 
    97     fun mk_univ_inj ts = access_bal (ap In0, ap In1, if ts = [] then
    98         Const ("arbitrary", Univ_elT)
    99       else
   100         foldr1 (HOLogic.mk_binop Scons_name) ts);
   101 
   102     (************** generate introduction rules for representing set **********)
   103 
   104     val _ = writeln "Constructing representing sets...";
   105 
   106     (* make introduction rule for a single constructor *)
   107 
   108     fun make_intr s n (i, (_, cargs)) =
   109       let
   110         fun mk_prem (DtRec k, (j, prems, ts)) =
   111               let val free_t = mk_Free "x" Univ_elT j
   112               in (j + 1, (HOLogic.mk_mem (free_t,
   113                 Const (nth_elem (k, rep_set_names), UnivT)))::prems, free_t::ts)
   114               end
   115           | mk_prem (dt, (j, prems, ts)) =
   116               let val T = typ_of_dtyp descr' sorts dt
   117               in (j + 1, prems, (Leaf $ mk_inj T (mk_Free "x" T j))::ts)
   118               end;
   119 
   120         val (_, prems, ts) = foldr mk_prem (cargs, (1, [], []));
   121         val concl = HOLogic.mk_Trueprop (HOLogic.mk_mem
   122           (mk_univ_inj ts n i, Const (s, UnivT)))
   123       in Logic.list_implies (map HOLogic.mk_Trueprop prems, concl)
   124       end;
   125 
   126     val consts = map (fn s => Const (s, UnivT)) rep_set_names;
   127 
   128     val intr_ts = flat (map (fn ((_, (_, _, constrs)), rep_set_name) =>
   129       map (make_intr rep_set_name (length constrs))
   130         ((1 upto (length constrs)) ~~ constrs)) (descr' ~~ rep_set_names));
   131 
   132     val (thy2, {raw_induct = rep_induct, intrs = rep_intrs, ...}) =
   133       InductivePackage.add_inductive_i false true big_rec_name false true false
   134         consts intr_ts [] [] thy;
   135 
   136     (********************************* typedef ********************************)
   137 
   138     val tyvars = map (fn (_, (_, Ts, _)) => map dest_DtTFree Ts) (hd descr);
   139 
   140     val thy3 = foldl (fn (thy, ((((name, mx), tvs), c), name')) =>
   141       TypedefPackage.add_typedef_i_no_def name' (name, tvs, mx) c [] []
   142         (Some (BREADTH_FIRST (has_fewer_prems 1) (resolve_tac rep_intrs 1))) thy)
   143           (thy2, types_syntax ~~ tyvars ~~ (take (length newTs, consts)) ~~
   144             new_type_names);
   145 
   146     (*********************** definition of constructors ***********************)
   147 
   148     val big_rep_name = (space_implode "_" new_type_names) ^ "_Rep_";
   149     val rep_names = map (curry op ^ "Rep_") new_type_names;
   150     val rep_names' = map (fn i => big_rep_name ^ (string_of_int i))
   151       (1 upto (length (flat (tl descr))));
   152     val all_rep_names = map (Sign.full_name (sign_of thy3)) (rep_names @ rep_names');
   153 
   154     (* isomorphism declarations *)
   155 
   156     val iso_decls = map (fn (T, s) => (s, T --> Univ_elT, NoSyn))
   157       (oldTs ~~ rep_names');
   158 
   159     (* constructor definitions *)
   160 
   161     fun make_constr_def tname T n ((thy, defs, eqns, i), ((cname, cargs), (cname', mx))) =
   162       let
   163         fun constr_arg (dt, (j, l_args, r_args)) =
   164           let val T = typ_of_dtyp descr' sorts dt;
   165               val free_t = mk_Free "x" T j
   166           in (case dt of
   167               DtRec m => (j + 1, free_t::l_args, (Const (nth_elem (m, all_rep_names),
   168                 T --> Univ_elT) $ free_t)::r_args)
   169             | _ => (j + 1, free_t::l_args, (Leaf $ mk_inj T free_t)::r_args))
   170           end;
   171 
   172         val (_, l_args, r_args) = foldr constr_arg (cargs, (1, [], []));
   173         val constrT = (map (typ_of_dtyp descr' sorts) cargs) ---> T;
   174         val abs_name = Sign.intern_const (sign_of thy) ("Abs_" ^ tname);
   175         val rep_name = Sign.intern_const (sign_of thy) ("Rep_" ^ tname);
   176         val lhs = list_comb (Const (cname, constrT), l_args);
   177         val rhs = mk_univ_inj r_args n i;
   178         val def = equals T $ lhs $ (Const (abs_name, Univ_elT --> T) $ rhs);
   179         val def_name = (Sign.base_name cname) ^ "_def";
   180         val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
   181           (Const (rep_name, T --> Univ_elT) $ lhs, rhs));
   182         val thy' = thy |>
   183           Theory.add_consts_i [(cname', constrT, mx)] |>
   184           Theory.add_defs_i [(def_name, def)];
   185 
   186       in (thy', defs @ [get_axiom thy' def_name], eqns @ [eqn], i + 1)
   187       end;
   188 
   189     (* constructor definitions for datatype *)
   190 
   191     fun dt_constr_defs ((thy, defs, eqns, rep_congs, dist_lemmas),
   192         ((((_, (_, _, constrs)), tname), T), constr_syntax)) =
   193       let
   194         val _ $ (_ $ (cong_f $ _) $ _) = concl_of arg_cong;
   195         val sg = sign_of thy;
   196         val rep_const = cterm_of sg
   197           (Const (Sign.intern_const sg ("Rep_" ^ tname), T --> Univ_elT));
   198         val cong' = cterm_instantiate [(cterm_of sg cong_f, rep_const)] arg_cong;
   199         val dist = cterm_instantiate [(cterm_of sg distinct_f, rep_const)] distinct_lemma;
   200         val (thy', defs', eqns', _) = foldl ((make_constr_def tname T) (length constrs))
   201           ((if length newTs = 1 then thy else Theory.add_path tname thy, defs, [], 1),
   202             constrs ~~ constr_syntax)
   203       in
   204         (if length newTs = 1 then thy' else Theory.parent_path thy', defs', eqns @ [eqns'],
   205           rep_congs @ [cong'], dist_lemmas @ [dist])
   206       end;
   207 
   208     val (thy4, constr_defs, constr_rep_eqns, rep_congs, dist_lemmas) = foldl dt_constr_defs
   209       ((Theory.add_consts_i iso_decls thy3, [], [], [], []),
   210         hd descr ~~ new_type_names ~~ newTs ~~ constr_syntax);
   211 
   212     (*********** isomorphisms for new types (introduced by typedef) ***********)
   213 
   214     val _ = writeln "Proving isomorphism properties...";
   215 
   216     (* get axioms from theory *)
   217 
   218     val newT_iso_axms = map (fn s =>
   219       (get_axiom thy4 ("Abs_" ^ s ^ "_inverse"),
   220        get_axiom thy4 ("Rep_" ^ s ^ "_inverse"),
   221        get_axiom thy4 ("Rep_" ^ s))) new_type_names;
   222 
   223     (*------------------------------------------------*)
   224     (* prove additional theorems:                     *)
   225     (*  inj_on dt_Abs_i rep_set_i  and  inj dt_Rep_i  *)
   226     (*------------------------------------------------*)
   227 
   228     fun prove_newT_iso_inj_thm (((s, (thm1, thm2, _)), T), rep_set_name) =
   229       let
   230         val sg = sign_of thy4;
   231         val RepT = T --> Univ_elT;
   232         val Rep_name = Sign.intern_const sg ("Rep_" ^ s);
   233         val AbsT = Univ_elT --> T;
   234         val Abs_name = Sign.intern_const sg ("Abs_" ^ s);
   235 
   236         val inj_on_Abs_thm = prove_goalw_cterm [] (cterm_of sg
   237           (HOLogic.mk_Trueprop (Const (inj_on_name, [AbsT, UnivT] ---> HOLogic.boolT) $
   238             Const (Abs_name, AbsT) $ Const (rep_set_name, UnivT))))
   239               (fn _ => [rtac inj_on_inverseI 1, etac thm1 1]);
   240 
   241         val inj_Rep_thm = prove_goalw_cterm [] (cterm_of sg
   242           (HOLogic.mk_Trueprop (Const (inj_name, RepT --> HOLogic.boolT) $
   243             Const (Rep_name, RepT))))
   244               (fn _ => [rtac inj_inverseI 1, rtac thm2 1])
   245 
   246       in (inj_on_Abs_thm, inj_Rep_thm) end;
   247 
   248     val newT_iso_inj_thms = map prove_newT_iso_inj_thm
   249       (new_type_names ~~ newT_iso_axms ~~ newTs ~~
   250         take (length newTs, rep_set_names));
   251 
   252     (********* isomorphisms between existing types and "unfolded" types *******)
   253 
   254     (*---------------------------------------------------------------------*)
   255     (* isomorphisms are defined using primrec-combinators:                 *)
   256     (* generate appropriate functions for instantiating primrec-combinator *)
   257     (*                                                                     *)
   258     (*   e.g.  dt_Rep_i = list_rec ... (%h t y. In1 ((Leaf h) $ y))        *)
   259     (*                                                                     *)
   260     (* also generate characteristic equations for isomorphisms             *)
   261     (*                                                                     *)
   262     (*   e.g.  dt_Rep_i (cons h t) = In1 ((dt_Rep_j h) $ (dt_Rep_i t))     *)
   263     (*---------------------------------------------------------------------*)
   264 
   265     fun make_iso_def k ks n ((fs, eqns, i), (cname, cargs)) =
   266       let
   267         val argTs = map (typ_of_dtyp descr' sorts) cargs;
   268         val T = nth_elem (k, recTs);
   269         val rep_name = nth_elem (k, all_rep_names);
   270         val rep_const = Const (rep_name, T --> Univ_elT);
   271         val constr = Const (cname, argTs ---> T);
   272 
   273         fun process_arg ks' ((i2, i2', ts), dt) =
   274           let val T' = typ_of_dtyp descr' sorts dt
   275           in (case dt of
   276               DtRec j => if j mem ks' then
   277                   (i2 + 1, i2' + 1, ts @ [mk_Free "y" Univ_elT i2'])
   278                 else
   279                   (i2 + 1, i2', ts @ [Const (nth_elem (j, all_rep_names),
   280                     T' --> Univ_elT) $ mk_Free "x" T' i2])
   281             | _ => (i2 + 1, i2', ts @ [Leaf $ mk_inj T' (mk_Free "x" T' i2)]))
   282           end;
   283 
   284         val (i2, i2', ts) = foldl (process_arg ks) ((1, 1, []), cargs);
   285         val xs = map (uncurry (mk_Free "x")) (argTs ~~ (1 upto (i2 - 1)));
   286         val ys = map (mk_Free "y" Univ_elT) (1 upto (i2' - 1));
   287         val f = list_abs_free (map dest_Free (xs @ ys), mk_univ_inj ts n i);
   288 
   289         val (_, _, ts') = foldl (process_arg []) ((1, 1, []), cargs);
   290         val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
   291           (rep_const $ list_comb (constr, xs), mk_univ_inj ts' n i))
   292 
   293       in (fs @ [f], eqns @ [eqn], i + 1) end;
   294 
   295     (* define isomorphisms for all mutually recursive datatypes in list ds *)
   296 
   297     fun make_iso_defs (ds, (thy, char_thms)) =
   298       let
   299         val ks = map fst ds;
   300         val (_, (tname, _, _)) = hd ds;
   301         val {rec_rewrites, rec_names, ...} = the (Symtab.lookup (dt_info, tname));
   302 
   303         fun process_dt ((fs, eqns, isos), (k, (tname, _, constrs))) =
   304           let
   305             val (fs', eqns', _) = foldl (make_iso_def k ks (length constrs))
   306               ((fs, eqns, 1), constrs);
   307             val iso = (nth_elem (k, recTs), nth_elem (k, all_rep_names))
   308           in (fs', eqns', isos @ [iso]) end;
   309         
   310         val (fs, eqns, isos) = foldl process_dt (([], [], []), ds);
   311         val fTs = map fastype_of fs;
   312         val defs = map (fn (rec_name, (T, iso_name)) => ((Sign.base_name iso_name) ^ "_def",
   313           equals (T --> Univ_elT) $ Const (iso_name, T --> Univ_elT) $
   314             list_comb (Const (rec_name, fTs @ [T] ---> Univ_elT), fs))) (rec_names ~~ isos);
   315         val thy' = Theory.add_defs_i defs thy;
   316         val def_thms = map (get_axiom thy') (map fst defs);
   317 
   318         (* prove characteristic equations *)
   319 
   320         val rewrites = def_thms @ (map mk_meta_eq rec_rewrites);
   321         val char_thms' = map (fn eqn => prove_goalw_cterm rewrites
   322           (cterm_of (sign_of thy') eqn) (fn _ => [rtac refl 1])) eqns;
   323 
   324       in (thy', char_thms' @ char_thms) end;
   325 
   326     val (thy5, iso_char_thms) = foldr make_iso_defs (tl descr, (thy4, []));
   327 
   328     (* prove isomorphism properties *)
   329 
   330     (* prove  x : dt_rep_set_i --> x : range dt_Rep_i *)
   331 
   332     fun mk_iso_t (((set_name, iso_name), i), T) =
   333       let val isoT = T --> Univ_elT
   334       in HOLogic.imp $ 
   335         HOLogic.mk_mem (mk_Free "x" Univ_elT i, Const (set_name, UnivT)) $
   336           (if i < length newTs then Const ("True", HOLogic.boolT)
   337            else HOLogic.mk_mem (mk_Free "x" Univ_elT i,
   338              Const (image_name, [isoT, HOLogic.mk_setT T] ---> UnivT) $
   339                Const (iso_name, isoT) $ Const (UNIV_name, HOLogic.mk_setT T)))
   340       end;
   341 
   342     val iso_t = HOLogic.mk_Trueprop (mk_conj (map mk_iso_t
   343       (rep_set_names ~~ all_rep_names ~~ (0 upto (length descr' - 1)) ~~ recTs)));
   344 
   345     val newT_Abs_inverse_thms = map (fn (iso, _, _) => iso RS subst) newT_iso_axms;
   346 
   347     (* all the theorems are proved by one single simultaneous induction *)
   348 
   349     val iso_thms = if length descr = 1 then [] else
   350       drop (length newTs, split_conj_thm
   351         (prove_goalw_cterm [] (cterm_of (sign_of thy5) iso_t) (fn _ =>
   352            [indtac rep_induct 1,
   353             REPEAT (rtac TrueI 1),
   354             REPEAT (EVERY
   355               [REPEAT (etac rangeE 1),
   356                REPEAT (eresolve_tac newT_Abs_inverse_thms 1),
   357                TRY (hyp_subst_tac 1),
   358                rtac (sym RS range_eqI) 1,
   359                resolve_tac iso_char_thms 1])])));
   360 
   361     val Abs_inverse_thms = newT_Abs_inverse_thms @ (map (fn r =>
   362       r RS mp RS f_inv_f RS subst) iso_thms);
   363 
   364     (* prove  inj dt_Rep_i  and  dt_Rep_i x : dt_rep_set_i *)
   365 
   366     fun prove_iso_thms (ds, (inj_thms, elem_thms)) =
   367       let
   368         val (_, (tname, _, _)) = hd ds;
   369         val {induction, ...} = the (Symtab.lookup (dt_info, tname));
   370 
   371         fun mk_ind_concl (i, _) =
   372           let
   373             val T = nth_elem (i, recTs);
   374             val Rep_t = Const (nth_elem (i, all_rep_names), T --> Univ_elT);
   375             val rep_set_name = nth_elem (i, rep_set_names)
   376           in (HOLogic.all_const T $ Abs ("y", T, HOLogic.imp $
   377                 HOLogic.mk_eq (Rep_t $ mk_Free "x" T i, Rep_t $ Bound 0) $
   378                   HOLogic.mk_eq (mk_Free "x" T i, Bound 0)),
   379               HOLogic.mk_mem (Rep_t $ mk_Free "x" T i, Const (rep_set_name, UnivT)))
   380           end;
   381 
   382         val (ind_concl1, ind_concl2) = ListPair.unzip (map mk_ind_concl ds);
   383 
   384         val rewrites = map mk_meta_eq iso_char_thms;
   385         val inj_thms' = map (fn r => r RS injD) inj_thms;
   386 
   387         val inj_thm = prove_goalw_cterm [] (cterm_of (sign_of thy5)
   388           (HOLogic.mk_Trueprop (mk_conj ind_concl1))) (fn _ =>
   389             [indtac induction 1,
   390              REPEAT (EVERY
   391                [rtac allI 1, rtac impI 1,
   392                 exh_tac (exh_thm_of dt_info) 1,
   393                 REPEAT (EVERY
   394                   [hyp_subst_tac 1,
   395                    rewrite_goals_tac rewrites,
   396                    REPEAT (dresolve_tac [In0_inject, In1_inject] 1),
   397                    (eresolve_tac [In0_not_In1 RS notE, In1_not_In0 RS notE] 1)
   398                    ORELSE (EVERY
   399                      [REPEAT (etac Scons_inject 1),
   400                       REPEAT (dresolve_tac
   401                         (inj_thms' @ [Leaf_inject, Inl_inject, Inr_inject]) 1),
   402                       REPEAT (EVERY [etac allE 1, dtac mp 1, atac 1]),
   403                       TRY (hyp_subst_tac 1),
   404                       rtac refl 1])])])]);
   405 
   406         val inj_thms'' = map (fn r =>
   407           r RS (allI RS (inj_def RS meta_eq_to_obj_eq RS iffD2)))
   408             (split_conj_thm inj_thm);
   409 
   410         val elem_thm = prove_goalw_cterm [] (cterm_of (sign_of thy5)
   411           (HOLogic.mk_Trueprop (mk_conj ind_concl2))) (fn _ =>
   412             [indtac induction 1,
   413              rewrite_goals_tac rewrites,
   414              REPEAT (EVERY
   415                [resolve_tac rep_intrs 1,
   416                 REPEAT ((atac 1) ORELSE (resolve_tac elem_thms 1))])]);
   417 
   418       in (inj_thms @ inj_thms'', elem_thms @ (split_conj_thm elem_thm))
   419       end;
   420 
   421     val (iso_inj_thms, iso_elem_thms) = foldr prove_iso_thms
   422       (tl descr, (map snd newT_iso_inj_thms, map #3 newT_iso_axms));
   423 
   424     (******************* freeness theorems for constructors *******************)
   425 
   426     val _ = writeln "Proving freeness of constructors...";
   427 
   428     (* prove theorem  Rep_i (Constr_j ...) = Inj_j ...  *)
   429     
   430     fun prove_constr_rep_thm eqn =
   431       let
   432         val inj_thms = map (fn (r, _) => r RS inj_onD) newT_iso_inj_thms;
   433         val rewrites = constr_defs @ (map (mk_meta_eq o #2) newT_iso_axms)
   434       in prove_goalw_cterm [] (cterm_of (sign_of thy5) eqn) (fn _ =>
   435         [resolve_tac inj_thms 1,
   436          rewrite_goals_tac rewrites,
   437          rtac refl 1,
   438          resolve_tac rep_intrs 2,
   439          REPEAT (resolve_tac iso_elem_thms 1)])
   440       end;
   441 
   442     (*--------------------------------------------------------------*)
   443     (* constr_rep_thms and rep_congs are used to prove distinctness *)
   444     (* of constructors internally.                                  *)
   445     (* the external version uses dt_case which is not defined yet   *)
   446     (*--------------------------------------------------------------*)
   447 
   448     val constr_rep_thms = map (map prove_constr_rep_thm) constr_rep_eqns;
   449 
   450     val dist_rewrites = map (fn (rep_thms, dist_lemma) =>
   451       dist_lemma::(rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0]))
   452         (constr_rep_thms ~~ dist_lemmas);
   453 
   454     (* prove injectivity of constructors *)
   455 
   456     fun prove_constr_inj_thm rep_thms t =
   457       let val inj_thms = Scons_inject::(map make_elim
   458         ((map (fn r => r RS injD) iso_inj_thms) @
   459           [In0_inject, In1_inject, Leaf_inject, Inl_inject, Inr_inject]))
   460       in prove_goalw_cterm [] (cterm_of (sign_of thy5) t) (fn _ =>
   461         [rtac iffI 1,
   462          REPEAT (etac conjE 2), hyp_subst_tac 2, rtac refl 2,
   463          dresolve_tac rep_congs 1, dtac box_equals 1,
   464          REPEAT (resolve_tac rep_thms 1),
   465          REPEAT (eresolve_tac inj_thms 1),
   466          hyp_subst_tac 1,
   467          REPEAT (resolve_tac [conjI, refl] 1)])
   468       end;
   469 
   470     val constr_inject = map (fn (ts, thms) => map (prove_constr_inj_thm thms) ts)
   471       ((DatatypeProp.make_injs descr sorts) ~~ constr_rep_thms);
   472 
   473     val thy6 = store_thmss "inject" new_type_names constr_inject thy5;
   474 
   475     (*************************** induction theorem ****************************)
   476 
   477     val _ = writeln "Proving induction rule for datatypes...";
   478 
   479     val Rep_inverse_thms = (map (fn (_, iso, _) => iso RS subst) newT_iso_axms) @
   480       (map (fn r => r RS inv_f_f RS subst) (drop (length newTs, iso_inj_thms)));
   481     val Rep_inverse_thms' = map (fn r => r RS inv_f_f)
   482       (drop (length newTs, iso_inj_thms));
   483 
   484     fun mk_indrule_lemma ((prems, concls), ((i, _), T)) =
   485       let
   486         val Rep_t = Const (nth_elem (i, all_rep_names), T --> Univ_elT) $
   487           mk_Free "x" T i;
   488 
   489         val Abs_t = if i < length newTs then
   490             Const (Sign.intern_const (sign_of thy6)
   491               ("Abs_" ^ (nth_elem (i, new_type_names))), Univ_elT --> T)
   492           else Const (inv_name, [T --> Univ_elT, Univ_elT] ---> T) $
   493             Const (nth_elem (i, all_rep_names), T --> Univ_elT)
   494 
   495       in (prems @ [HOLogic.imp $ HOLogic.mk_mem (Rep_t,
   496             Const (nth_elem (i, rep_set_names), UnivT)) $
   497               (mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))],
   498           concls @ [mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ mk_Free "x" T i])
   499       end;
   500 
   501     val (indrule_lemma_prems, indrule_lemma_concls) =
   502       foldl mk_indrule_lemma (([], []), (descr' ~~ recTs));
   503 
   504     val cert = cterm_of (sign_of thy6);
   505 
   506     val indrule_lemma = prove_goalw_cterm [] (cert
   507       (Logic.mk_implies
   508         (HOLogic.mk_Trueprop (mk_conj indrule_lemma_prems),
   509          HOLogic.mk_Trueprop (mk_conj indrule_lemma_concls)))) (fn prems =>
   510            [cut_facts_tac prems 1, REPEAT (etac conjE 1),
   511             REPEAT (EVERY
   512               [TRY (rtac conjI 1), resolve_tac Rep_inverse_thms 1,
   513                etac mp 1, resolve_tac iso_elem_thms 1])]);
   514 
   515     val Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of indrule_lemma)));
   516     val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else
   517       map (Free o apfst fst o dest_Var) Ps;
   518     val indrule_lemma' = cterm_instantiate (map cert Ps ~~ map cert frees) indrule_lemma;
   519 
   520     val dt_induct = prove_goalw_cterm [] (cert
   521       (DatatypeProp.make_ind descr sorts)) (fn prems =>
   522         [rtac indrule_lemma' 1, indtac rep_induct 1,
   523          EVERY (map (fn (prem, r) => (EVERY
   524            [REPEAT (eresolve_tac Abs_inverse_thms 1),
   525             simp_tac (HOL_basic_ss addsimps ((symmetric r)::Rep_inverse_thms')) 1,
   526             DEPTH_SOLVE_1 (ares_tac [prem] 1)]))
   527               (prems ~~ (constr_defs @ (map mk_meta_eq iso_char_thms))))]);
   528 
   529     val thy7 = PureThy.add_tthms [(("induct", Attribute.tthm_of dt_induct), [])] thy6;
   530 
   531   in (thy7, constr_inject, dist_rewrites, dt_induct)
   532   end;
   533 
   534 end;