src/HOL/Tools/Datatype/datatype_aux.ML
author haftmann
Tue Jun 23 12:09:30 2009 +0200 (2009-06-23 ago)
changeset 31775 2b04504fcb69
parent 31737 src/HOL/Tools/datatype_package/datatype_aux.ML@b3f63611784e
child 32124 954321008785
permissions -rw-r--r--
uniformly capitialized names for subdirectories
berghofe@5177
     1
(*  Title:      HOL/Tools/datatype_aux.ML
wenzelm@11539
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@5177
     3
wenzelm@11539
     4
Auxiliary functions for defining datatypes.
berghofe@5177
     5
*)
berghofe@5177
     6
haftmann@31737
     7
signature DATATYPE_COMMON =
haftmann@31737
     8
sig
haftmann@31737
     9
  type config
haftmann@31737
    10
  val default_config : config
haftmann@31737
    11
  datatype dtyp =
haftmann@31737
    12
      DtTFree of string
haftmann@31737
    13
    | DtType of string * (dtyp list)
haftmann@31737
    14
    | DtRec of int;
haftmann@31737
    15
  type descr
haftmann@31737
    16
  type info
haftmann@31737
    17
end
haftmann@31737
    18
berghofe@5177
    19
signature DATATYPE_AUX =
berghofe@5177
    20
sig
haftmann@31737
    21
  include DATATYPE_COMMON
haftmann@31737
    22
haftmann@31737
    23
  val message : config -> string -> unit
berghofe@5661
    24
  
berghofe@5661
    25
  val add_path : bool -> string -> theory -> theory
berghofe@5661
    26
  val parent_path : bool -> theory -> theory
berghofe@5661
    27
wenzelm@18728
    28
  val store_thmss_atts : string -> string list -> attribute list list -> thm list list
berghofe@18349
    29
    -> theory -> thm list list * theory
haftmann@18314
    30
  val store_thmss : string -> string list -> thm list list -> theory -> thm list list * theory
wenzelm@18728
    31
  val store_thms_atts : string -> string list -> attribute list list -> thm list
haftmann@18314
    32
    -> theory -> thm list * theory
haftmann@18314
    33
  val store_thms : string -> string list -> thm list -> theory -> thm list * theory
berghofe@5177
    34
berghofe@5177
    35
  val split_conj_thm : thm -> thm list
berghofe@5177
    36
  val mk_conj : term list -> term
berghofe@5177
    37
  val mk_disj : term list -> term
berghofe@5177
    38
berghofe@13641
    39
  val app_bnds : term -> int -> term
berghofe@13641
    40
berghofe@13641
    41
  val cong_tac : int -> tactic
berghofe@25676
    42
  val indtac : thm -> string list -> int -> tactic
berghofe@5177
    43
  val exh_tac : (string -> thm) -> int -> tactic
berghofe@5177
    44
berghofe@26532
    45
  datatype simproc_dist = FewConstrs of thm list
berghofe@7015
    46
                        | ManyConstrs of thm * simpset;
berghofe@7015
    47
berghofe@5177
    48
berghofe@7015
    49
  exception Datatype
berghofe@14887
    50
  exception Datatype_Empty of string
berghofe@9740
    51
  val name_of_typ : typ -> string
berghofe@5177
    52
  val dtyp_of_typ : (string * string list) list -> typ -> dtyp
berghofe@5177
    53
  val mk_Free : string -> typ -> int -> term
berghofe@5177
    54
  val is_rec_type : dtyp -> bool
berghofe@13641
    55
  val typ_of_dtyp : descr -> (string * sort) list -> dtyp -> typ
berghofe@5177
    56
  val dest_DtTFree : dtyp -> string
berghofe@5177
    57
  val dest_DtRec : dtyp -> int
berghofe@13641
    58
  val strip_dtyp : dtyp -> dtyp list * dtyp
berghofe@13641
    59
  val body_index : dtyp -> int
berghofe@13641
    60
  val mk_fun_dtyp : dtyp list -> dtyp -> dtyp
berghofe@13641
    61
  val get_nonrec_types : descr -> (string * sort) list -> typ list
berghofe@13641
    62
  val get_branching_types : descr -> (string * sort) list -> typ list
berghofe@13641
    63
  val get_arities : descr -> int list
berghofe@13641
    64
  val get_rec_types : descr -> (string * sort) list -> typ list
haftmann@31605
    65
  val interpret_construction : descr -> (string * sort) list
haftmann@31605
    66
    -> { atyp: typ -> 'a, dtyp: typ list -> int * bool -> string * typ list -> 'a }
haftmann@31605
    67
    -> ((string * Term.typ list) * (string * 'a list) list) list
berghofe@13641
    68
  val check_nonempty : descr list -> unit
berghofe@5177
    69
  val unfold_datatypes : 
haftmann@31737
    70
    theory -> descr -> (string * sort) list -> info Symtab.table ->
berghofe@13641
    71
      descr -> int -> descr list * int
berghofe@5177
    72
end;
berghofe@5177
    73
berghofe@5177
    74
structure DatatypeAux : DATATYPE_AUX =
berghofe@5177
    75
struct
berghofe@5177
    76
haftmann@31668
    77
(* datatype option flags *)
haftmann@31668
    78
haftmann@31737
    79
type config = { strict: bool, flat_names: bool, quiet: bool };
haftmann@31737
    80
val default_config : config =
haftmann@31668
    81
  { strict = true, flat_names = false, quiet = false };
haftmann@31737
    82
fun message ({ quiet, ...} : config) s =
haftmann@31668
    83
  if quiet then () else writeln s;
berghofe@5661
    84
wenzelm@24712
    85
fun add_path flat_names s = if flat_names then I else Sign.add_path s;
wenzelm@24712
    86
fun parent_path flat_names = if flat_names then I else Sign.parent_path;
berghofe@5661
    87
wenzelm@8435
    88
berghofe@5177
    89
(* store theorems in theory *)
berghofe@5177
    90
haftmann@18377
    91
fun store_thmss_atts label tnames attss thmss =
haftmann@18377
    92
  fold_map (fn ((tname, atts), thms) =>
wenzelm@24712
    93
    Sign.add_path tname
haftmann@29579
    94
    #> PureThy.add_thmss [((Binding.name label, thms), atts)]
wenzelm@28361
    95
    #-> (fn thm::_ => Sign.parent_path #> pair thm)) (tnames ~~ attss ~~ thmss)
wenzelm@28361
    96
  ##> Theory.checkpoint;
berghofe@5177
    97
berghofe@18101
    98
fun store_thmss label tnames = store_thmss_atts label tnames (replicate (length tnames) []);
berghofe@18101
    99
haftmann@18377
   100
fun store_thms_atts label tnames attss thmss =
haftmann@18377
   101
  fold_map (fn ((tname, atts), thms) =>
wenzelm@24712
   102
    Sign.add_path tname
haftmann@29579
   103
    #> PureThy.add_thms [((Binding.name label, thms), atts)]
wenzelm@28361
   104
    #-> (fn thm::_ => Sign.parent_path #> pair thm)) (tnames ~~ attss ~~ thmss)
wenzelm@28361
   105
  ##> Theory.checkpoint;
wenzelm@8435
   106
wenzelm@8435
   107
fun store_thms label tnames = store_thms_atts label tnames (replicate (length tnames) []);
wenzelm@8435
   108
berghofe@5177
   109
berghofe@5177
   110
(* split theorem thm_1 & ... & thm_n into n theorems *)
berghofe@5177
   111
berghofe@5177
   112
fun split_conj_thm th =
berghofe@7015
   113
  ((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th];
berghofe@5177
   114
berghofe@5177
   115
val mk_conj = foldr1 (HOLogic.mk_binop "op &");
berghofe@5177
   116
val mk_disj = foldr1 (HOLogic.mk_binop "op |");
berghofe@5177
   117
berghofe@13641
   118
fun app_bnds t i = list_comb (t, map Bound (i - 1 downto 0));
berghofe@13641
   119
berghofe@13641
   120
berghofe@13641
   121
fun cong_tac i st = (case Logic.strip_assums_concl
skalberg@15570
   122
  (List.nth (prems_of st, i - 1)) of
berghofe@13641
   123
    _ $ (_ $ (f $ x) $ (g $ y)) =>
berghofe@13641
   124
      let
wenzelm@18145
   125
        val cong' = Thm.lift_rule (Thm.cprem_of st i) cong;
berghofe@13641
   126
        val _ $ (_ $ (f' $ x') $ (g' $ y')) =
berghofe@13641
   127
          Logic.strip_assums_concl (prop_of cong');
wenzelm@22596
   128
        val insts = map (pairself (cterm_of (Thm.theory_of_thm st)) o
berghofe@13641
   129
          apsnd (curry list_abs (Logic.strip_params (concl_of cong'))) o
berghofe@13641
   130
            apfst head_of) [(f', f), (g', g), (x', x), (y', y)]
berghofe@13641
   131
      in compose_tac (false, cterm_instantiate insts cong', 2) i st
berghofe@13641
   132
        handle THM _ => no_tac st
berghofe@13641
   133
      end
berghofe@13641
   134
  | _ => no_tac st);
berghofe@5177
   135
berghofe@5177
   136
(* instantiate induction rule *)
berghofe@5177
   137
berghofe@25676
   138
fun indtac indrule indnames i st =
berghofe@5177
   139
  let
wenzelm@8305
   140
    val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule));
wenzelm@8305
   141
    val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop
skalberg@15570
   142
      (Logic.strip_imp_concl (List.nth (prems_of st, i - 1))));
berghofe@5177
   143
    val getP = if can HOLogic.dest_imp (hd ts) then
skalberg@15531
   144
      (apfst SOME) o HOLogic.dest_imp else pair NONE;
berghofe@25676
   145
    val flt = if null indnames then I else
berghofe@25676
   146
      filter (fn Free (s, _) => s mem indnames | _ => false);
berghofe@5177
   147
    fun abstr (t1, t2) = (case t1 of
wenzelm@29265
   148
        NONE => (case flt (OldTerm.term_frees t2) of
berghofe@25676
   149
            [Free (s, T)] => SOME (absfree (s, T, t2))
berghofe@25676
   150
          | _ => NONE)
berghofe@25676
   151
      | SOME (_ $ t') => SOME (Abs ("x", fastype_of t', abstract_over (t', t2))))
wenzelm@22578
   152
    val cert = cterm_of (Thm.theory_of_thm st);
berghofe@25676
   153
    val insts = List.mapPartial (fn (t, u) => case abstr (getP u) of
berghofe@25676
   154
        NONE => NONE
berghofe@25676
   155
      | SOME u' => SOME (t |> getP |> snd |> head_of |> cert, cert u')) (ts ~~ ts');
berghofe@25676
   156
    val indrule' = cterm_instantiate insts indrule
berghofe@5177
   157
  in
berghofe@5177
   158
    rtac indrule' i st
berghofe@5177
   159
  end;
berghofe@5177
   160
berghofe@5177
   161
(* perform exhaustive case analysis on last parameter of subgoal i *)
berghofe@5177
   162
berghofe@5177
   163
fun exh_tac exh_thm_of i state =
berghofe@5177
   164
  let
wenzelm@22578
   165
    val thy = Thm.theory_of_thm state;
haftmann@21420
   166
    val prem = nth (prems_of state) (i - 1);
berghofe@5177
   167
    val params = Logic.strip_params prem;
berghofe@5177
   168
    val (_, Type (tname, _)) = hd (rev params);
wenzelm@18145
   169
    val exhaustion = Thm.lift_rule (Thm.cprem_of state i) (exh_thm_of tname);
berghofe@5177
   170
    val prem' = hd (prems_of exhaustion);
berghofe@5177
   171
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
haftmann@21420
   172
    val exhaustion' = cterm_instantiate [(cterm_of thy (head_of lhs),
wenzelm@30190
   173
      cterm_of thy (List.foldr (fn ((_, T), t) => Abs ("z", T, t))
skalberg@15574
   174
        (Bound 0) params))] exhaustion
berghofe@5177
   175
  in compose_tac (false, exhaustion', nprems_of exhaustion) i state
berghofe@5177
   176
  end;
berghofe@5177
   177
berghofe@7015
   178
(* handling of distinctness theorems *)
berghofe@7015
   179
berghofe@26532
   180
datatype simproc_dist = FewConstrs of thm list
berghofe@7015
   181
                      | ManyConstrs of thm * simpset;
berghofe@7015
   182
berghofe@5177
   183
(********************** Internal description of datatypes *********************)
berghofe@5177
   184
berghofe@5177
   185
datatype dtyp =
berghofe@5177
   186
    DtTFree of string
berghofe@5177
   187
  | DtType of string * (dtyp list)
berghofe@5177
   188
  | DtRec of int;
berghofe@5177
   189
berghofe@5177
   190
(* information about datatypes *)
berghofe@5177
   191
berghofe@16901
   192
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *)
wenzelm@8404
   193
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list;
wenzelm@8404
   194
haftmann@31737
   195
type info =
berghofe@5177
   196
  {index : int,
berghofe@25676
   197
   alt_names : string list option,
wenzelm@8404
   198
   descr : descr,
berghofe@18319
   199
   sorts : (string * sort) list,
berghofe@5177
   200
   rec_names : string list,
berghofe@5177
   201
   rec_rewrites : thm list,
berghofe@5177
   202
   case_name : string,
berghofe@5177
   203
   case_rewrites : thm list,
berghofe@5177
   204
   induction : thm,
berghofe@5177
   205
   exhaustion : thm,
berghofe@7015
   206
   distinct : simproc_dist,
berghofe@5177
   207
   inject : thm list,
berghofe@5177
   208
   nchotomy : thm,
wenzelm@10120
   209
   case_cong : thm,
wenzelm@10120
   210
   weak_case_cong : thm};
berghofe@5177
   211
berghofe@5177
   212
fun mk_Free s T i = Free (s ^ (string_of_int i), T);
berghofe@5177
   213
berghofe@5177
   214
fun subst_DtTFree _ substs (T as (DtTFree name)) =
haftmann@17485
   215
      AList.lookup (op =) substs name |> the_default T
berghofe@5177
   216
  | subst_DtTFree i substs (DtType (name, ts)) =
berghofe@5177
   217
      DtType (name, map (subst_DtTFree i substs) ts)
berghofe@5177
   218
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
berghofe@5177
   219
berghofe@7015
   220
exception Datatype;
berghofe@14887
   221
exception Datatype_Empty of string;
berghofe@7015
   222
berghofe@7015
   223
fun dest_DtTFree (DtTFree a) = a
berghofe@7015
   224
  | dest_DtTFree _ = raise Datatype;
berghofe@7015
   225
berghofe@7015
   226
fun dest_DtRec (DtRec i) = i
berghofe@7015
   227
  | dest_DtRec _ = raise Datatype;
berghofe@5177
   228
berghofe@5177
   229
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
berghofe@5177
   230
  | is_rec_type (DtRec _) = true
berghofe@5177
   231
  | is_rec_type _ = false;
berghofe@5177
   232
berghofe@13641
   233
fun strip_dtyp (DtType ("fun", [T, U])) = apfst (cons T) (strip_dtyp U)
berghofe@13641
   234
  | strip_dtyp T = ([], T);
berghofe@13641
   235
berghofe@13641
   236
val body_index = dest_DtRec o snd o strip_dtyp;
berghofe@13641
   237
berghofe@13641
   238
fun mk_fun_dtyp [] U = U
berghofe@13641
   239
  | mk_fun_dtyp (T :: Ts) U = DtType ("fun", [T, mk_fun_dtyp Ts U]);
berghofe@13641
   240
berghofe@13707
   241
fun name_of_typ (Type (s, Ts)) =
wenzelm@30364
   242
      let val s' = Long_Name.base_name s
skalberg@15570
   243
      in space_implode "_" (List.filter (not o equal "") (map name_of_typ Ts) @
wenzelm@14673
   244
        [if Syntax.is_identifier s' then s' else "x"])
berghofe@13707
   245
      end
berghofe@9740
   246
  | name_of_typ _ = "";
berghofe@9740
   247
berghofe@5177
   248
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n
berghofe@5177
   249
  | dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)"
berghofe@5177
   250
  | dtyp_of_typ new_dts (Type (tname, Ts)) =
haftmann@17485
   251
      (case AList.lookup (op =) new_dts tname of
skalberg@15531
   252
         NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts)
berghofe@25888
   253
       | SOME vs => if map (try (fst o dest_TFree)) Ts = map SOME vs then
berghofe@5177
   254
             DtRec (find_index (curry op = tname o fst) new_dts)
huffman@18069
   255
           else error ("Illegal occurrence of recursive type " ^ tname));
berghofe@5177
   256
haftmann@17485
   257
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, (the o AList.lookup (op =) sorts) a)
berghofe@5177
   258
  | typ_of_dtyp descr sorts (DtRec i) =
haftmann@17485
   259
      let val (s, ds, _) = (the o AList.lookup (op =) descr) i
berghofe@5177
   260
      in Type (s, map (typ_of_dtyp descr sorts) ds) end
berghofe@5177
   261
  | typ_of_dtyp descr sorts (DtType (s, ds)) =
berghofe@5177
   262
      Type (s, map (typ_of_dtyp descr sorts) ds);
berghofe@5177
   263
berghofe@5177
   264
(* find all non-recursive types in datatype description *)
berghofe@5177
   265
berghofe@5177
   266
fun get_nonrec_types descr sorts =
skalberg@15570
   267
  map (typ_of_dtyp descr sorts) (Library.foldl (fn (Ts, (_, (_, _, constrs))) =>
skalberg@15570
   268
    Library.foldl (fn (Ts', (_, cargs)) =>
berghofe@13641
   269
      filter_out is_rec_type cargs union Ts') (Ts, constrs)) ([], descr));
berghofe@5177
   270
berghofe@5177
   271
(* get all recursive types in datatype description *)
berghofe@5177
   272
berghofe@5177
   273
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) =>
berghofe@5177
   274
  Type (s, map (typ_of_dtyp descr sorts) ds)) descr;
berghofe@5177
   275
berghofe@7015
   276
(* get all branching types *)
berghofe@7015
   277
berghofe@13641
   278
fun get_branching_types descr sorts =
berghofe@26823
   279
  map (typ_of_dtyp descr sorts) (fold (fn (_, (_, _, constrs)) =>
berghofe@26823
   280
    fold (fn (_, cargs) => fold (strip_dtyp #> fst #> fold (insert op =)) cargs)
berghofe@26823
   281
      constrs) descr []);
berghofe@13641
   282
berghofe@26823
   283
fun get_arities descr = fold (fn (_, (_, _, constrs)) =>
berghofe@26823
   284
  fold (fn (_, cargs) => fold (insert op =) (map (length o fst o strip_dtyp)
berghofe@26823
   285
    (List.filter is_rec_type cargs))) constrs) descr [];
berghofe@7015
   286
haftmann@31605
   287
(* interpret construction of datatype *)
haftmann@31605
   288
haftmann@31605
   289
fun interpret_construction descr vs { atyp, dtyp } =
haftmann@31605
   290
  let
haftmann@31605
   291
    val typ_of_dtyp = typ_of_dtyp descr vs;
haftmann@31605
   292
    fun interpT dT = case strip_dtyp dT
haftmann@31605
   293
     of (dTs, DtRec l) =>
haftmann@31605
   294
          let
haftmann@31605
   295
            val (tyco, dTs', _) = (the o AList.lookup (op =) descr) l;
haftmann@31605
   296
            val Ts = map typ_of_dtyp dTs;
haftmann@31605
   297
            val Ts' = map typ_of_dtyp dTs';
haftmann@31605
   298
            val is_proper = forall (can dest_TFree) Ts';
haftmann@31605
   299
          in dtyp Ts (l, is_proper) (tyco, Ts') end
haftmann@31605
   300
      | _ => atyp (typ_of_dtyp dT);
haftmann@31605
   301
    fun interpC (c, dTs) = (c, map interpT dTs);
haftmann@31605
   302
    fun interpD (_, (tyco, dTs, cs)) = ((tyco, map typ_of_dtyp dTs), map interpC cs);
haftmann@31605
   303
  in map interpD descr end;
haftmann@31605
   304
berghofe@5177
   305
(* nonemptiness check for datatypes *)
berghofe@5177
   306
berghofe@5177
   307
fun check_nonempty descr =
berghofe@5177
   308
  let
skalberg@15570
   309
    val descr' = List.concat descr;
berghofe@5177
   310
    fun is_nonempty_dt is i =
berghofe@5177
   311
      let
haftmann@17485
   312
        val (_, _, constrs) = (the o AList.lookup (op =) descr') i;
berghofe@13641
   313
        fun arg_nonempty (_, DtRec i) = if i mem is then false
berghofe@5177
   314
              else is_nonempty_dt (i::is) i
berghofe@5177
   315
          | arg_nonempty _ = true;
berghofe@13641
   316
      in exists ((forall (arg_nonempty o strip_dtyp)) o snd) constrs
berghofe@5177
   317
      end
berghofe@5177
   318
  in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr)
berghofe@14887
   319
    (fn (_, (s, _, _)) => raise Datatype_Empty s)
berghofe@5177
   320
  end;
berghofe@5177
   321
berghofe@5177
   322
(* unfold a list of mutually recursive datatype specifications *)
berghofe@5177
   323
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
berghofe@5177
   324
(* need to be unfolded                                         *)
berghofe@5177
   325
haftmann@31737
   326
fun unfold_datatypes sign orig_descr sorts (dt_info : info Symtab.table) descr i =
berghofe@5177
   327
  let
berghofe@7015
   328
    fun typ_error T msg = error ("Non-admissible type expression\n" ^
wenzelm@26939
   329
      Syntax.string_of_typ_global sign (typ_of_dtyp (orig_descr @ descr) sorts T) ^ "\n" ^ msg);
berghofe@7015
   330
berghofe@7015
   331
    fun get_dt_descr T i tname dts =
wenzelm@17412
   332
      (case Symtab.lookup dt_info tname of
skalberg@15531
   333
         NONE => typ_error T (tname ^ " is not a datatype - can't use it in\
berghofe@7015
   334
           \ nested recursion")
skalberg@15531
   335
       | (SOME {index, descr, ...}) =>
haftmann@17485
   336
           let val (_, vars, _) = (the o AList.lookup (op =) descr) index;
wenzelm@19841
   337
               val subst = ((map dest_DtTFree vars) ~~ dts) handle Library.UnequalLengths =>
berghofe@7015
   338
                 typ_error T ("Type constructor " ^ tname ^ " used with wrong\
berghofe@5177
   339
                  \ number of arguments")
berghofe@5177
   340
           in (i + index, map (fn (j, (tn, args, cs)) => (i + j,
berghofe@5177
   341
             (tn, map (subst_DtTFree i subst) args,
berghofe@5177
   342
              map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
berghofe@5177
   343
           end);
berghofe@5177
   344
berghofe@5177
   345
    (* unfold a single constructor argument *)
berghofe@5177
   346
berghofe@13641
   347
    fun unfold_arg ((i, Ts, descrs), T) =
berghofe@13641
   348
      if is_rec_type T then
berghofe@13641
   349
        let val (Us, U) = strip_dtyp T
berghofe@13641
   350
        in if exists is_rec_type Us then
berghofe@13641
   351
            typ_error T "Non-strictly positive recursive occurrence of type"
berghofe@13641
   352
          else (case U of
berghofe@13641
   353
              DtType (tname, dts) =>  
berghofe@13641
   354
                let
berghofe@13641
   355
                  val (index, descr) = get_dt_descr T i tname dts;
berghofe@13641
   356
                  val (descr', i') = unfold_datatypes sign orig_descr sorts
berghofe@13641
   357
                    dt_info descr (i + length descr)
berghofe@13641
   358
                in (i', Ts @ [mk_fun_dtyp Us (DtRec index)], descrs @ descr') end
berghofe@13641
   359
            | _ => (i, Ts @ [T], descrs))
berghofe@13641
   360
        end
berghofe@13641
   361
      else (i, Ts @ [T], descrs);
berghofe@5177
   362
berghofe@5177
   363
    (* unfold a constructor *)
berghofe@5177
   364
berghofe@5177
   365
    fun unfold_constr ((i, constrs, descrs), (cname, cargs)) =
skalberg@15570
   366
      let val (i', cargs', descrs') = Library.foldl unfold_arg ((i, [], descrs), cargs)
berghofe@5177
   367
      in (i', constrs @ [(cname, cargs')], descrs') end;
berghofe@5177
   368
berghofe@5177
   369
    (* unfold a single datatype *)
berghofe@5177
   370
berghofe@5177
   371
    fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) =
berghofe@5177
   372
      let val (i', constrs', descrs') =
skalberg@15570
   373
        Library.foldl unfold_constr ((i, [], descrs), constrs)
berghofe@5177
   374
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs')
berghofe@5177
   375
      end;
berghofe@5177
   376
skalberg@15570
   377
    val (i', descr', descrs) = Library.foldl unfold_datatype ((i, [],[]), descr);
berghofe@5177
   378
berghofe@5177
   379
  in (descr' :: descrs, i') end;
berghofe@5177
   380
berghofe@5177
   381
end;