src/HOL/Tools/datatype_prop.ML
author paulson
Thu Sep 26 10:51:29 2002 +0200 (2002-09-26)
changeset 13585 db4005b40cc6
parent 13465 08e3fe248ba9
child 13641 63d1790a43ed
permissions -rw-r--r--
Converted Fun to Isar style.
Moved Pi, funcset, restrict from Fun.thy to Library/FuncSet.thy.
Renamed constant "Fun.op o" to "Fun.comp"
     1 (*  Title:      HOL/Tools/datatype_prop.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Characteristic properties of datatypes.
     7 *)
     8 
     9 signature DATATYPE_PROP =
    10 sig
    11   val dtK : int ref
    12   val indexify_names: string list -> string list
    13   val make_tnames: typ list -> string list
    14   val make_injs : (int * (string * DatatypeAux.dtyp list *
    15     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    16       term list list
    17   val make_ind : (int * (string * DatatypeAux.dtyp list *
    18     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term
    19   val make_casedists : (int * (string * DatatypeAux.dtyp list *
    20     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list
    21   val make_primrecs : string list -> (int * (string * DatatypeAux.dtyp list *
    22     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    23       theory -> term list
    24   val make_cases : string list -> (int * (string * DatatypeAux.dtyp list *
    25     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    26       theory -> term list list
    27   val make_distincts : string list -> (int * (string * DatatypeAux.dtyp list *
    28     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    29       theory -> term list list
    30   val make_splits : string list -> (int * (string * DatatypeAux.dtyp list *
    31     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    32       theory -> (term * term) list
    33   val make_case_trrules : string list -> (int * (string * DatatypeAux.dtyp list *
    34     (string * DatatypeAux.dtyp list) list)) list list -> ast Syntax.trrule list
    35   val make_size : (int * (string * DatatypeAux.dtyp list *
    36     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    37       theory -> term list
    38   val make_weak_case_congs : string list -> (int * (string * DatatypeAux.dtyp list *
    39     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    40       theory -> term list
    41   val make_case_congs : string list -> (int * (string * DatatypeAux.dtyp list *
    42     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
    43       theory -> term list
    44   val make_nchotomys : (int * (string * DatatypeAux.dtyp list *
    45     (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list
    46 end;
    47 
    48 structure DatatypeProp : DATATYPE_PROP =
    49 struct
    50 
    51 open DatatypeAux;
    52 
    53 (*the kind of distinctiveness axioms depends on number of constructors*)
    54 val dtK = ref 7;
    55 
    56 fun indexify_names names =
    57   let
    58     fun index (x :: xs) tab =
    59       (case assoc (tab, x) of
    60         None => if x mem xs then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
    61       | Some i => (x ^ Library.string_of_int i) :: index xs ((x, i + 1) :: tab))
    62     | index [] _ = [];
    63   in index names [] end;
    64 
    65 fun make_tnames Ts =
    66   let
    67     fun type_name (TFree (name, _)) = implode (tl (explode name))
    68       | type_name (Type (name, _)) = 
    69           let val name' = Sign.base_name name
    70           in if Syntax.is_identifier name' then name' else "x" end;
    71   in indexify_names (map type_name Ts) end;
    72 
    73 
    74 
    75 (************************* injectivity of constructors ************************)
    76 
    77 fun make_injs descr sorts =
    78   let
    79     val descr' = flat descr;
    80 
    81     fun make_inj T ((cname, cargs), injs) =
    82       if null cargs then injs else
    83         let
    84           val Ts = map (typ_of_dtyp descr' sorts) cargs;
    85           val constr_t = Const (cname, Ts ---> T);
    86           val tnames = make_tnames Ts;
    87           val frees = map Free (tnames ~~ Ts);
    88           val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
    89         in (HOLogic.mk_Trueprop (HOLogic.mk_eq
    90           (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
    91            foldr1 (HOLogic.mk_binop "op &")
    92              (map HOLogic.mk_eq (frees ~~ frees')))))::injs
    93         end;
    94 
    95   in map (fn (d, T) => foldr (make_inj T) (#3 (snd d), []))
    96     ((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts))
    97   end;
    98 
    99 (********************************* induction **********************************)
   100 
   101 fun make_ind descr sorts =
   102   let
   103     val descr' = flat descr;
   104     val recTs = get_rec_types descr' sorts;
   105     val pnames = if length descr' = 1 then ["P"]
   106       else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
   107 
   108     fun make_pred i T =
   109       let val T' = T --> HOLogic.boolT
   110       in Free (nth_elem (i, pnames), T') end;
   111 
   112     fun make_ind_prem k T (cname, cargs) =
   113       let
   114         fun mk_prem ((DtRec k, s), T) = HOLogic.mk_Trueprop
   115               (make_pred k T $ Free (s, T))
   116           | mk_prem ((DtType ("fun", [_, DtRec k]), s), T' as Type ("fun", [T, U])) =
   117               all T $ Abs ("x", T, HOLogic.mk_Trueprop
   118                 (make_pred k U $ (Free (s, T') $ Bound 0)));
   119 
   120         val recs = filter is_rec_type cargs;
   121         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   122         val recTs' = map (typ_of_dtyp descr' sorts) recs;
   123         val tnames = variantlist (make_tnames Ts, pnames);
   124         val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
   125         val frees = tnames ~~ Ts;
   126         val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
   127 
   128       in list_all_free (frees, Logic.list_implies (prems,
   129         HOLogic.mk_Trueprop (make_pred k T $ 
   130           list_comb (Const (cname, Ts ---> T), map Free frees))))
   131       end;
   132 
   133     val prems = flat (map (fn ((i, (_, _, constrs)), T) =>
   134       map (make_ind_prem i T) constrs) (descr' ~~ recTs));
   135     val tnames = make_tnames recTs;
   136     val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
   137       (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
   138         (descr' ~~ recTs ~~ tnames)))
   139 
   140   in Logic.list_implies (prems, concl) end;
   141 
   142 (******************************* case distinction *****************************)
   143 
   144 fun make_casedists descr sorts =
   145   let
   146     val descr' = flat descr;
   147 
   148     fun make_casedist_prem T (cname, cargs) =
   149       let
   150         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   151         val frees = variantlist (make_tnames Ts, ["P", "y"]) ~~ Ts;
   152         val free_ts = map Free frees
   153       in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
   154         (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
   155           HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
   156       end;
   157 
   158     fun make_casedist ((_, (_, _, constrs)), T) =
   159       let val prems = map (make_casedist_prem T) constrs
   160       in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
   161       end
   162 
   163   in map make_casedist
   164     ((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts))
   165   end;
   166 
   167 (*************** characteristic equations for primrec combinator **************)
   168 
   169 fun make_primrecs new_type_names descr sorts thy =
   170   let
   171     val o_name = "Fun.comp";
   172 
   173     val sign = Theory.sign_of thy;
   174 
   175     val descr' = flat descr;
   176     val recTs = get_rec_types descr' sorts;
   177     val used = foldr add_typ_tfree_names (recTs, []);
   178 
   179     val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~
   180       replicate (length descr') HOLogic.typeS);
   181 
   182     val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) =>
   183       map (fn (_, cargs) =>
   184         let
   185           val Ts = map (typ_of_dtyp descr' sorts) cargs;
   186           val recs = filter (is_rec_type o fst) (cargs ~~ Ts);
   187 
   188           fun mk_argT (DtRec k, _) = nth_elem (k, rec_result_Ts)
   189             | mk_argT (DtType ("fun", [_, DtRec k]), Type ("fun", [T, _])) =
   190                T --> nth_elem (k, rec_result_Ts);
   191 
   192           val argTs = Ts @ map mk_argT recs
   193         in argTs ---> nth_elem (i, rec_result_Ts)
   194         end) constrs) descr');
   195 
   196     val rec_fns = map (uncurry (mk_Free "f"))
   197       (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
   198 
   199     val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
   200     val reccomb_names = map (Sign.intern_const sign)
   201       (if length descr' = 1 then [big_reccomb_name] else
   202         (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
   203           (1 upto (length descr'))));
   204     val reccombs = map (fn ((name, T), T') => list_comb
   205       (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
   206         (reccomb_names ~~ recTs ~~ rec_result_Ts);
   207 
   208     fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
   209       let
   210         val recs = filter is_rec_type cargs;
   211         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   212         val recTs' = map (typ_of_dtyp descr' sorts) recs;
   213         val tnames = make_tnames Ts;
   214         val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
   215         val frees = map Free (tnames ~~ Ts);
   216         val frees' = map Free (rec_tnames ~~ recTs');
   217 
   218         fun mk_reccomb (DtRec i, _) = nth_elem (i, reccombs)
   219           | mk_reccomb (DtType ("fun", [_, DtRec i]), Type ("fun", [T, U])) =
   220               let val T' = nth_elem (i, rec_result_Ts)
   221               in Const (o_name, [U --> T', T --> U, T] ---> T') $ nth_elem (i, reccombs)
   222               end;
   223 
   224         val reccombs' = map mk_reccomb (recs ~~ recTs')
   225 
   226       in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
   227         (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
   228          list_comb (f, frees @ (map (op $) (reccombs' ~~ frees')))))], fs)
   229       end
   230 
   231   in fst (foldl (fn (x, ((dt, T), comb_t)) =>
   232     foldl (make_primrec T comb_t) (x, #3 (snd dt)))
   233       (([], rec_fns), descr' ~~ recTs ~~ reccombs))
   234   end;
   235 
   236 (****************** make terms of form  t_case f1 ... fn  *********************)
   237 
   238 fun make_case_combs new_type_names descr sorts thy fname =
   239   let
   240     val descr' = flat descr;
   241     val recTs = get_rec_types descr' sorts;
   242     val used = foldr add_typ_tfree_names (recTs, []);
   243     val newTs = take (length (hd descr), recTs);
   244     val T' = TFree (variant used "'t", HOLogic.typeS);
   245 
   246     val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
   247       map (fn (_, cargs) =>
   248         let val Ts = map (typ_of_dtyp descr' sorts) cargs
   249         in Ts ---> T' end) constrs) (hd descr);
   250 
   251     val case_names = map (fn s =>
   252       Sign.intern_const (Theory.sign_of thy) (s ^ "_case")) new_type_names
   253   in
   254     map (fn ((name, Ts), T) => list_comb
   255       (Const (name, Ts @ [T] ---> T'),
   256         map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
   257           (case_names ~~ case_fn_Ts ~~ newTs)
   258   end;
   259 
   260 (**************** characteristic equations for case combinator ****************)
   261 
   262 fun make_cases new_type_names descr sorts thy =
   263   let
   264     val descr' = flat descr;
   265     val recTs = get_rec_types descr' sorts;
   266     val newTs = take (length (hd descr), recTs);
   267 
   268     fun make_case T comb_t ((cname, cargs), f) =
   269       let
   270         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   271         val frees = map Free ((make_tnames Ts) ~~ Ts)
   272       in HOLogic.mk_Trueprop (HOLogic.mk_eq
   273         (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
   274          list_comb (f, frees)))
   275       end
   276 
   277   in map (fn (((_, (_, _, constrs)), T), comb_t) =>
   278     map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
   279       ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
   280   end;
   281 
   282 (************************* distinctness of constructors ***********************)
   283 
   284 fun make_distincts new_type_names descr sorts thy =
   285   let
   286     val descr' = flat descr;
   287     val recTs = get_rec_types descr' sorts;
   288     val newTs = take (length (hd descr), recTs);
   289 
   290     (**** number of constructors < dtK : C_i ... ~= C_j ... ****)
   291 
   292     fun make_distincts_1 _ [] = []
   293       | make_distincts_1 T ((cname, cargs)::constrs) =
   294           let
   295             val Ts = map (typ_of_dtyp descr' sorts) cargs;
   296             val frees = map Free ((make_tnames Ts) ~~ Ts);
   297             val t = list_comb (Const (cname, Ts ---> T), frees);
   298 
   299             fun make_distincts' [] = []
   300               | make_distincts' ((cname', cargs')::constrs') =
   301                   let
   302                     val Ts' = map (typ_of_dtyp descr' sorts) cargs';
   303                     val frees' = map Free ((map ((op ^) o (rpair "'"))
   304                       (make_tnames Ts')) ~~ Ts');
   305                     val t' = list_comb (Const (cname', Ts' ---> T), frees')
   306                   in
   307                     (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')))::
   308                     (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t)))::
   309                       (make_distincts' constrs')
   310                   end
   311 
   312           in (make_distincts' constrs) @ (make_distincts_1 T constrs)
   313           end;
   314 
   315   in map (fn (((_, (_, _, constrs)), T), tname) =>
   316       if length constrs < !dtK then make_distincts_1 T constrs else [])
   317         ((hd descr) ~~ newTs ~~ new_type_names)
   318   end;
   319 
   320 
   321 (*************************** the "split" - equations **************************)
   322 
   323 fun make_splits new_type_names descr sorts thy =
   324   let
   325     val descr' = flat descr;
   326     val recTs = get_rec_types descr' sorts;
   327     val used' = foldr add_typ_tfree_names (recTs, []);
   328     val newTs = take (length (hd descr), recTs);
   329     val T' = TFree (variant used' "'t", HOLogic.typeS);
   330     val P = Free ("P", T' --> HOLogic.boolT);
   331 
   332     fun make_split (((_, (_, _, constrs)), T), comb_t) =
   333       let
   334         val (_, fs) = strip_comb comb_t;
   335         val used = ["P", "x"] @ (map (fst o dest_Free) fs);
   336 
   337         fun process_constr (((cname, cargs), f), (t1s, t2s)) =
   338           let
   339             val Ts = map (typ_of_dtyp descr' sorts) cargs;
   340             val frees = map Free (variantlist (make_tnames Ts, used) ~~ Ts);
   341             val eqn = HOLogic.mk_eq (Free ("x", T),
   342               list_comb (Const (cname, Ts ---> T), frees));
   343             val P' = P $ list_comb (f, frees)
   344           in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
   345                 (frees, HOLogic.imp $ eqn $ P'))::t1s,
   346               (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
   347                 (frees, HOLogic.conj $ eqn $ (HOLogic.Not $ P')))::t2s)
   348           end;
   349 
   350         val (t1s, t2s) = foldr process_constr (constrs ~~ fs, ([], []));
   351         val lhs = P $ (comb_t $ Free ("x", T))
   352       in
   353         (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
   354          HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s)))
   355       end
   356 
   357   in map make_split ((hd descr) ~~ newTs ~~
   358     (make_case_combs new_type_names descr sorts thy "f"))
   359   end;
   360 
   361 (************************ translation rules for case **************************)
   362 
   363 fun make_case_trrules new_type_names descr =
   364   let
   365     fun mk_asts i j ((cname, cargs)::constrs) =
   366       let
   367         val k = length cargs;
   368         val xs = map (fn i => Variable ("x" ^ string_of_int i)) (i upto i + k - 1);
   369         val t = Variable ("t" ^ string_of_int j);
   370         val ast = Syntax.mk_appl (Constant "_case1")
   371           [Syntax.mk_appl (Constant (Sign.base_name cname)) xs, t];
   372         val ast' = foldr (fn (x, y) =>
   373           Syntax.mk_appl (Constant "_abs") [x, y]) (xs, t)
   374       in
   375         (case constrs of
   376             [] => (ast, [ast'])
   377           | cs => let val (ast'', asts) = mk_asts (i + k) (j + 1) cs
   378               in (Syntax.mk_appl (Constant "_case2") [ast, ast''],
   379                   ast'::asts)
   380               end)
   381       end;
   382 
   383     fun mk_trrule ((_, (_, _, constrs)), tname) =
   384       let val (ast, asts) = mk_asts 1 1 constrs
   385       in Syntax.ParsePrintRule
   386         (Syntax.mk_appl (Constant "_case_syntax") [Variable "t", ast],
   387          Syntax.mk_appl (Constant (tname ^ "_case"))
   388            (asts @ [Variable "t"]))
   389       end
   390 
   391   in
   392     map mk_trrule (hd descr ~~ new_type_names)
   393   end;
   394 
   395 (******************************* size functions *******************************)
   396 
   397 fun make_size descr sorts thy =
   398   let
   399     val descr' = flat descr;
   400     val recTs = get_rec_types descr' sorts;
   401 
   402     val size_name = "Nat.size";
   403     val size_names = replicate (length (hd descr)) size_name @
   404       map (Sign.intern_const (Theory.sign_of thy)) (indexify_names
   405         (map (fn T => name_of_typ T ^ "_size") (drop (length (hd descr), recTs))));
   406     val size_consts = map (fn (s, T) =>
   407       Const (s, T --> HOLogic.natT)) (size_names ~~ recTs);
   408 
   409     fun plus (t1, t2) = Const ("op +", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT) $ t1 $ t2;
   410 
   411     fun make_size_eqn size_const T (cname, cargs) =
   412       let
   413         val recs = filter is_rec_type cargs;
   414         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   415         val recTs = map (typ_of_dtyp descr' sorts) recs;
   416         val tnames = make_tnames Ts;
   417         val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
   418         val ts = map (fn ((r, s), T) => nth_elem (dest_DtRec r, size_consts) $
   419           Free (s, T)) (recs ~~ rec_tnames ~~ recTs);
   420         val t = if ts = [] then HOLogic.zero else
   421           foldl1 plus (ts @ [HOLogic.mk_nat 1])
   422       in
   423         HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
   424           list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t))
   425       end
   426 
   427   in
   428     flat (map (fn (((_, (_, _, constrs)), size_const), T) =>
   429       map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs))
   430   end;
   431 
   432 (************************* additional rules for TFL ***************************)
   433 
   434 fun make_weak_case_congs new_type_names descr sorts thy =
   435   let
   436     val case_combs = make_case_combs new_type_names descr sorts thy "f";
   437 
   438     fun mk_case_cong comb =
   439       let 
   440         val Type ("fun", [T, _]) = fastype_of comb;
   441         val M = Free ("M", T);
   442         val M' = Free ("M'", T);
   443       in
   444         Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
   445           HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
   446       end
   447   in
   448     map mk_case_cong case_combs
   449   end;
   450  
   451 
   452 (*---------------------------------------------------------------------------
   453  * Structure of case congruence theorem looks like this:
   454  *
   455  *    (M = M') 
   456  *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
   457  *    ==> ... 
   458  *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
   459  *    ==>
   460  *      (ty_case f1..fn M = ty_case g1..gn M')
   461  *---------------------------------------------------------------------------*)
   462 
   463 fun make_case_congs new_type_names descr sorts thy =
   464   let
   465     val case_combs = make_case_combs new_type_names descr sorts thy "f";
   466     val case_combs' = make_case_combs new_type_names descr sorts thy "g";
   467 
   468     fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
   469       let
   470         val Type ("fun", [T, _]) = fastype_of comb;
   471         val (_, fs) = strip_comb comb;
   472         val (_, gs) = strip_comb comb';
   473         val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
   474         val M = Free ("M", T);
   475         val M' = Free ("M'", T);
   476 
   477         fun mk_clause ((f, g), (cname, _)) =
   478           let
   479             val (Ts, _) = strip_type (fastype_of f);
   480             val tnames = variantlist (make_tnames Ts, used);
   481             val frees = map Free (tnames ~~ Ts)
   482           in
   483             list_all_free (tnames ~~ Ts, Logic.mk_implies
   484               (HOLogic.mk_Trueprop
   485                 (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
   486                HOLogic.mk_Trueprop
   487                 (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
   488           end
   489 
   490       in
   491         Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
   492           map mk_clause (fs ~~ gs ~~ constrs),
   493             HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
   494       end
   495 
   496   in
   497     map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
   498   end;
   499 
   500 (*---------------------------------------------------------------------------
   501  * Structure of exhaustion theorem looks like this:
   502  *
   503  *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
   504  *---------------------------------------------------------------------------*)
   505 
   506 fun make_nchotomys descr sorts =
   507   let
   508     val descr' = flat descr;
   509     val recTs = get_rec_types descr' sorts;
   510     val newTs = take (length (hd descr), recTs);
   511 
   512     fun mk_eqn T (cname, cargs) =
   513       let
   514         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   515         val tnames = variantlist (make_tnames Ts, ["v"]);
   516         val frees = tnames ~~ Ts
   517       in
   518         foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
   519           (frees, HOLogic.mk_eq (Free ("v", T),
   520             list_comb (Const (cname, Ts ---> T), map Free frees)))
   521       end
   522 
   523   in map (fn ((_, (_, _, constrs)), T) =>
   524     HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
   525       (hd descr ~~ newTs)
   526   end;
   527 
   528 end;