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