src/HOL/Tools/datatype_aux.ML
author berghofe
Mon, 05 Dec 2005 00:38:07 +0100
changeset 18349 58de95a16d3c
parent 18319 c52b139ebde0
child 18377 0e1d025d57b3
permissions -rw-r--r--
Added store_thmss_atts to signature again.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Tools/datatype_aux.ML
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     2
    ID:         $Id$
11539
0f17da240450 tuned headers;
wenzelm
parents: 10120
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     4
11539
0f17da240450 tuned headers;
wenzelm
parents: 10120
diff changeset
     5
Auxiliary functions for defining datatypes.
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     6
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     7
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     8
signature DATATYPE_AUX =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     9
sig
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    10
  val quiet_mode : bool ref
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    11
  val message : string -> unit
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    12
  
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    13
  val foldl1 : ('a * 'a -> 'a) -> 'a list -> 'a
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    14
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    15
  val add_path : bool -> string -> theory -> theory
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    16
  val parent_path : bool -> theory -> theory
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    17
18349
58de95a16d3c Added store_thmss_atts to signature again.
berghofe
parents: 18319
diff changeset
    18
  val store_thmss_atts : string -> string list -> theory attribute list list -> thm list list
58de95a16d3c Added store_thmss_atts to signature again.
berghofe
parents: 18319
diff changeset
    19
    -> theory -> thm list list * theory
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    20
  val store_thmss : string -> string list -> thm list list -> theory -> thm list list * theory
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    21
  val store_thms_atts : string -> string list -> theory attribute list list -> thm list
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    22
    -> theory -> thm list * theory
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    23
  val store_thms : string -> string list -> thm list -> theory -> thm list * theory
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    24
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    25
  val split_conj_thm : thm -> thm list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    26
  val mk_conj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    27
  val mk_disj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    28
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    29
  val app_bnds : term -> int -> term
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    30
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    31
  val cong_tac : int -> tactic
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    32
  val indtac : thm -> int -> tactic
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    33
  val exh_tac : (string -> thm) -> int -> tactic
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    34
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    35
  datatype simproc_dist = QuickAndDirty
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    36
                        | FewConstrs of thm list
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    37
                        | ManyConstrs of thm * simpset;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    38
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    39
  datatype dtyp =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    40
      DtTFree of string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    41
    | DtType of string * (dtyp list)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    42
    | DtRec of int;
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
    43
  type descr
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    44
  type datatype_info
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    45
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    46
  exception Datatype
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
    47
  exception Datatype_Empty of string
9740
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
    48
  val name_of_typ : typ -> string
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    49
  val dtyp_of_typ : (string * string list) list -> typ -> dtyp
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    50
  val mk_Free : string -> typ -> int -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    51
  val is_rec_type : dtyp -> bool
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    52
  val typ_of_dtyp : descr -> (string * sort) list -> dtyp -> typ
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    53
  val dest_DtTFree : dtyp -> string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    54
  val dest_DtRec : dtyp -> int
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    55
  val strip_dtyp : dtyp -> dtyp list * dtyp
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    56
  val body_index : dtyp -> int
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    57
  val mk_fun_dtyp : dtyp list -> dtyp -> dtyp
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    58
  val dest_TFree : typ -> string
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    59
  val get_nonrec_types : descr -> (string * sort) list -> typ list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    60
  val get_branching_types : descr -> (string * sort) list -> typ list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    61
  val get_arities : descr -> int list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    62
  val get_rec_types : descr -> (string * sort) list -> typ list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    63
  val check_nonempty : descr list -> unit
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    64
  val unfold_datatypes : 
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    65
    Sign.sg -> descr -> (string * sort) list -> datatype_info Symtab.table ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    66
      descr -> int -> descr list * int
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    67
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    68
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    69
structure DatatypeAux : DATATYPE_AUX =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    70
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    71
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    72
val quiet_mode = ref false;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    73
fun message s = if !quiet_mode then () else writeln s;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    74
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    75
(* FIXME: move to library ? *)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    76
fun foldl1 f (x::xs) = Library.foldl f (x, xs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    77
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    78
fun add_path flat_names s = if flat_names then I else Theory.add_path s;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    79
fun parent_path flat_names = if flat_names then I else Theory.parent_path;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    80
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    81
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    82
(* store theorems in theory *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    83
18101
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
    84
fun store_thmss_atts label tnames attss thmss thy =
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
    85
  (thy, tnames ~~ attss ~~ thmss) |>
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
    86
  foldl_map (fn (thy', ((tname, atts), thms)) => thy' |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    87
    Theory.add_path tname |>
18101
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
    88
    (apsnd hd o PureThy.add_thmss [((label, thms), atts)]) |>>
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    89
    Theory.parent_path) |> Library.swap;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    90
18101
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
    91
fun store_thmss label tnames = store_thmss_atts label tnames (replicate (length tnames) []);
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
    92
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    93
fun store_thms_atts label tnames attss thms thy =
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    94
  (thy, tnames ~~ attss ~~ thms) |>
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    95
  foldl_map (fn (thy', ((tname, atts), thm)) => thy' |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    96
    Theory.add_path tname |>
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    97
    (apsnd hd o PureThy.add_thms [((label, thm), atts)]) |>>
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    98
    Theory.parent_path) |> Library.swap;
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    99
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
   100
fun store_thms label tnames = store_thms_atts label tnames (replicate (length tnames) []);
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
   101
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   102
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   103
(* split theorem thm_1 & ... & thm_n into n theorems *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   104
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   105
fun split_conj_thm th =
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   106
  ((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th];
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   107
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   108
val mk_conj = foldr1 (HOLogic.mk_binop "op &");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   109
val mk_disj = foldr1 (HOLogic.mk_binop "op |");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   110
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   111
fun app_bnds t i = list_comb (t, map Bound (i - 1 downto 0));
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   112
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   113
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   114
fun cong_tac i st = (case Logic.strip_assums_concl
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   115
  (List.nth (prems_of st, i - 1)) of
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   116
    _ $ (_ $ (f $ x) $ (g $ y)) =>
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   117
      let
18145
6757627acf59 renamed Thm.cgoal_of to Thm.cprem_of;
wenzelm
parents: 18101
diff changeset
   118
        val cong' = Thm.lift_rule (Thm.cprem_of st i) cong;
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   119
        val _ $ (_ $ (f' $ x') $ (g' $ y')) =
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   120
          Logic.strip_assums_concl (prop_of cong');
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   121
        val insts = map (pairself (cterm_of (#sign (rep_thm st))) o
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   122
          apsnd (curry list_abs (Logic.strip_params (concl_of cong'))) o
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   123
            apfst head_of) [(f', f), (g', g), (x', x), (y', y)]
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   124
      in compose_tac (false, cterm_instantiate insts cong', 2) i st
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   125
        handle THM _ => no_tac st
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   126
      end
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   127
  | _ => no_tac st);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   128
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   129
(* instantiate induction rule *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   130
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   131
fun indtac indrule i st =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   132
  let
8305
93aa21ec5494 HOLogic.dest_conj;
wenzelm
parents: 7015
diff changeset
   133
    val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule));
93aa21ec5494 HOLogic.dest_conj;
wenzelm
parents: 7015
diff changeset
   134
    val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   135
      (Logic.strip_imp_concl (List.nth (prems_of st, i - 1))));
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   136
    val getP = if can HOLogic.dest_imp (hd ts) then
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   137
      (apfst SOME) o HOLogic.dest_imp else pair NONE;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   138
    fun abstr (t1, t2) = (case t1 of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   139
        NONE => let val [Free (s, T)] = add_term_frees (t2, [])
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   140
          in absfree (s, T, t2) end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   141
      | SOME (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2)))
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6092
diff changeset
   142
    val cert = cterm_of (Thm.sign_of_thm st);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   143
    val Ps = map (cert o head_of o snd o getP) ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   144
    val indrule' = cterm_instantiate (Ps ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   145
      (map (cert o abstr o getP) ts')) indrule
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   146
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   147
    rtac indrule' i st
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   148
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   149
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   150
(* perform exhaustive case analysis on last parameter of subgoal i *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   151
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   152
fun exh_tac exh_thm_of i state =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
  let
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6092
diff changeset
   154
    val sg = Thm.sign_of_thm state;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   155
    val prem = List.nth (prems_of state, i - 1);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   156
    val params = Logic.strip_params prem;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
    val (_, Type (tname, _)) = hd (rev params);
18145
6757627acf59 renamed Thm.cgoal_of to Thm.cprem_of;
wenzelm
parents: 18101
diff changeset
   158
    val exhaustion = Thm.lift_rule (Thm.cprem_of state i) (exh_thm_of tname);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   159
    val prem' = hd (prems_of exhaustion);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   160
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   161
    val exhaustion' = cterm_instantiate [(cterm_of sg (head_of lhs),
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   162
      cterm_of sg (foldr (fn ((_, T), t) => Abs ("z", T, t))
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   163
        (Bound 0) params))] exhaustion
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   164
  in compose_tac (false, exhaustion', nprems_of exhaustion) i state
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   165
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   166
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   167
(* handling of distinctness theorems *)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   168
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   169
datatype simproc_dist = QuickAndDirty
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   170
                      | FewConstrs of thm list
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   171
                      | ManyConstrs of thm * simpset;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   172
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   173
(********************** Internal description of datatypes *********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   175
datatype dtyp =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   176
    DtTFree of string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   177
  | DtType of string * (dtyp list)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   178
  | DtRec of int;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   179
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   180
(* information about datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   181
16901
d649ff14096a Tuned comment.
berghofe
parents: 15574
diff changeset
   182
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *)
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   183
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list;
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   184
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   185
type datatype_info =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   186
  {index : int,
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   187
   descr : descr,
18319
c52b139ebde0 Added new component "sorts" to record datatype_info.
berghofe
parents: 18314
diff changeset
   188
   sorts : (string * sort) list,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   189
   rec_names : string list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   190
   rec_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   191
   case_name : string,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   192
   case_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   193
   induction : thm,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   194
   exhaustion : thm,
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   195
   distinct : simproc_dist,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
   inject : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   197
   nchotomy : thm,
10120
0f315aeee16e info: weak_case_cong;
wenzelm
parents: 9740
diff changeset
   198
   case_cong : thm,
0f315aeee16e info: weak_case_cong;
wenzelm
parents: 9740
diff changeset
   199
   weak_case_cong : thm};
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   200
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   201
fun mk_Free s T i = Free (s ^ (string_of_int i), T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   202
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   203
fun subst_DtTFree _ substs (T as (DtTFree name)) =
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   204
      AList.lookup (op =) substs name |> the_default T
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   205
  | subst_DtTFree i substs (DtType (name, ts)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   206
      DtType (name, map (subst_DtTFree i substs) ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   207
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   208
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   209
exception Datatype;
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
   210
exception Datatype_Empty of string;
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   211
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   212
fun dest_DtTFree (DtTFree a) = a
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   213
  | dest_DtTFree _ = raise Datatype;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   214
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   215
fun dest_DtRec (DtRec i) = i
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   216
  | dest_DtRec _ = raise Datatype;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   217
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   218
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
  | is_rec_type (DtRec _) = true
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
  | is_rec_type _ = false;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   221
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   222
fun strip_dtyp (DtType ("fun", [T, U])) = apfst (cons T) (strip_dtyp U)
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   223
  | strip_dtyp T = ([], T);
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   224
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   225
val body_index = dest_DtRec o snd o strip_dtyp;
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   226
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   227
fun mk_fun_dtyp [] U = U
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   228
  | mk_fun_dtyp (T :: Ts) U = DtType ("fun", [T, mk_fun_dtyp Ts U]);
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   229
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   230
fun dest_TFree (TFree (n, _)) = n;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   231
13707
55670a70a5f9 name_of_type now replaces non-identifiers by dummy names.
berghofe
parents: 13641
diff changeset
   232
fun name_of_typ (Type (s, Ts)) =
55670a70a5f9 name_of_type now replaces non-identifiers by dummy names.
berghofe
parents: 13641
diff changeset
   233
      let val s' = Sign.base_name s
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   234
      in space_implode "_" (List.filter (not o equal "") (map name_of_typ Ts) @
14673
3d760a971fde use Syntax.is_identifier;
wenzelm
parents: 13707
diff changeset
   235
        [if Syntax.is_identifier s' then s' else "x"])
13707
55670a70a5f9 name_of_type now replaces non-identifiers by dummy names.
berghofe
parents: 13641
diff changeset
   236
      end
9740
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   237
  | name_of_typ _ = "";
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   238
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   239
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   240
  | dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)"
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   241
  | dtyp_of_typ new_dts (Type (tname, Ts)) =
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   242
      (case AList.lookup (op =) new_dts tname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   243
         NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   244
       | SOME vs => if map (try dest_TFree) Ts = map SOME vs then
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   245
             DtRec (find_index (curry op = tname o fst) new_dts)
18069
f2c8f68a45e6 fix spelling
huffman
parents: 18022
diff changeset
   246
           else error ("Illegal occurrence of recursive type " ^ tname));
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   247
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   248
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, (the o AList.lookup (op =) sorts) a)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   249
  | typ_of_dtyp descr sorts (DtRec i) =
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   250
      let val (s, ds, _) = (the o AList.lookup (op =) descr) i
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   251
      in Type (s, map (typ_of_dtyp descr sorts) ds) end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   252
  | typ_of_dtyp descr sorts (DtType (s, ds)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   253
      Type (s, map (typ_of_dtyp descr sorts) ds);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   254
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   255
(* find all non-recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   256
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   257
fun get_nonrec_types descr sorts =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   258
  map (typ_of_dtyp descr sorts) (Library.foldl (fn (Ts, (_, (_, _, constrs))) =>
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   259
    Library.foldl (fn (Ts', (_, cargs)) =>
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   260
      filter_out is_rec_type cargs union Ts') (Ts, constrs)) ([], descr));
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
(* get all recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   263
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   264
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
  Type (s, map (typ_of_dtyp descr sorts) ds)) descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   266
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   267
(* get all branching types *)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   268
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   269
fun get_branching_types descr sorts =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   270
  map (typ_of_dtyp descr sorts) (Library.foldl (fn (Ts, (_, (_, _, constrs))) =>
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   271
    Library.foldl (fn (Ts', (_, cargs)) => foldr op union Ts' (map (fst o strip_dtyp)
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   272
      cargs)) (Ts, constrs)) ([], descr));
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   273
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   274
fun get_arities descr = Library.foldl (fn (is, (_, (_, _, constrs))) =>
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   275
  Library.foldl (fn (is', (_, cargs)) => map (length o fst o strip_dtyp)
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   276
    (List.filter is_rec_type cargs) union is') (is, constrs)) ([], descr);
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   277
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   278
(* nonemptiness check for datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   279
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   280
fun check_nonempty descr =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   281
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   282
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   283
    fun is_nonempty_dt is i =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   284
      let
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   285
        val (_, _, constrs) = (the o AList.lookup (op =) descr') i;
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   286
        fun arg_nonempty (_, DtRec i) = if i mem is then false
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   287
              else is_nonempty_dt (i::is) i
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   288
          | arg_nonempty _ = true;
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   289
      in exists ((forall (arg_nonempty o strip_dtyp)) o snd) constrs
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   290
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   291
  in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr)
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
   292
    (fn (_, (s, _, _)) => raise Datatype_Empty s)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   293
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   294
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   295
(* unfold a list of mutually recursive datatype specifications *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   296
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   297
(* need to be unfolded                                         *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   298
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   299
fun unfold_datatypes sign orig_descr sorts (dt_info : datatype_info Symtab.table) descr i =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   300
  let
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   301
    fun typ_error T msg = error ("Non-admissible type expression\n" ^
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   302
      Sign.string_of_typ sign (typ_of_dtyp (orig_descr @ descr) sorts T) ^ "\n" ^ msg);
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   303
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   304
    fun get_dt_descr T i tname dts =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
   305
      (case Symtab.lookup dt_info tname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   306
         NONE => typ_error T (tname ^ " is not a datatype - can't use it in\
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   307
           \ nested recursion")
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   308
       | (SOME {index, descr, ...}) =>
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   309
           let val (_, vars, _) = (the o AList.lookup (op =) descr) index;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   310
               val subst = ((map dest_DtTFree vars) ~~ dts) handle UnequalLengths =>
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   311
                 typ_error T ("Type constructor " ^ tname ^ " used with wrong\
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   312
                  \ number of arguments")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   313
           in (i + index, map (fn (j, (tn, args, cs)) => (i + j,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   314
             (tn, map (subst_DtTFree i subst) args,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   315
              map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   316
           end);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   317
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   318
    (* unfold a single constructor argument *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   319
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   320
    fun unfold_arg ((i, Ts, descrs), T) =
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   321
      if is_rec_type T then
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   322
        let val (Us, U) = strip_dtyp T
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   323
        in if exists is_rec_type Us then
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   324
            typ_error T "Non-strictly positive recursive occurrence of type"
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   325
          else (case U of
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   326
              DtType (tname, dts) =>  
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   327
                let
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   328
                  val (index, descr) = get_dt_descr T i tname dts;
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   329
                  val (descr', i') = unfold_datatypes sign orig_descr sorts
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   330
                    dt_info descr (i + length descr)
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   331
                in (i', Ts @ [mk_fun_dtyp Us (DtRec index)], descrs @ descr') end
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   332
            | _ => (i, Ts @ [T], descrs))
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   333
        end
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   334
      else (i, Ts @ [T], descrs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   335
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   336
    (* unfold a constructor *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   337
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   338
    fun unfold_constr ((i, constrs, descrs), (cname, cargs)) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   339
      let val (i', cargs', descrs') = Library.foldl unfold_arg ((i, [], descrs), cargs)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   340
      in (i', constrs @ [(cname, cargs')], descrs') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   341
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   342
    (* unfold a single datatype *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   343
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   344
    fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   345
      let val (i', constrs', descrs') =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   346
        Library.foldl unfold_constr ((i, [], descrs), constrs)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   347
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   348
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   349
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   350
    val (i', descr', descrs) = Library.foldl unfold_datatype ((i, [],[]), descr);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   351
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   352
  in (descr' :: descrs, i') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   353
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   354
end;