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