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