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