src/HOL/Tools/Datatype/datatype_aux.ML
author wenzelm
Thu, 25 Feb 2010 22:32:09 +0100
changeset 35364 b8c62d60195c
parent 33971 9c7fa7f76950
child 36692 54b64d4ad524
permissions -rw-r--r--
more antiquotations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
     1
(*  Title:      HOL/Tools/Datatype/datatype_aux.ML
11539
0f17da240450 tuned headers;
wenzelm
parents: 10120
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     3
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
     4
Datatype package: auxiliary data structures and functions.
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     5
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     6
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
     7
signature DATATYPE_COMMON =
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
     8
sig
32964
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
     9
  type config = {strict : bool, quiet : bool}
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    10
  val default_config : config
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    11
  datatype dtyp =
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    12
      DtTFree of string
32964
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    13
    | DtType of string * dtyp list
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    14
    | DtRec of int
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    15
  type descr = (int * (string * dtyp list * (string * dtyp list) list)) list
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    16
  type info =
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    17
   {index : int,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    18
    alt_names : string list option,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    19
    descr : descr,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    20
    sorts : (string * sort) list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    21
    inject : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    22
    distinct : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    23
    induct : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    24
    inducts : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    25
    exhaust : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    26
    nchotomy : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    27
    rec_names : string list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    28
    rec_rewrites : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    29
    case_name : string,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    30
    case_rewrites : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    31
    case_cong : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    32
    weak_case_cong : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    33
    split : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    34
    split_asm: thm}
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    35
end
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    36
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    37
signature DATATYPE_AUX =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    38
sig
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    39
  include DATATYPE_COMMON
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    40
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    41
  val message : config -> string -> unit
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    42
  
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18377
diff changeset
    43
  val store_thmss_atts : string -> string list -> attribute list list -> thm list list
18349
58de95a16d3c Added store_thmss_atts to signature again.
berghofe
parents: 18319
diff changeset
    44
    -> theory -> thm list list * theory
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    45
  val store_thmss : string -> string list -> thm list list -> theory -> thm list list * theory
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18377
diff changeset
    46
  val store_thms_atts : string -> string list -> attribute list list -> thm list
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    47
    -> theory -> thm list * theory
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    48
  val store_thms : string -> string list -> thm list -> theory -> thm list * theory
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    49
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    50
  val split_conj_thm : thm -> thm list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    51
  val mk_conj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    52
  val mk_disj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    53
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    54
  val app_bnds : term -> int -> term
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    55
25676
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
    56
  val indtac : thm -> string list -> int -> tactic
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    57
  val exh_tac : (string -> thm) -> int -> tactic
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    58
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    59
  exception Datatype
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
    60
  exception Datatype_Empty of string
9740
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
    61
  val name_of_typ : typ -> string
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    62
  val dtyp_of_typ : (string * string list) list -> typ -> dtyp
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    63
  val mk_Free : string -> typ -> int -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    64
  val is_rec_type : dtyp -> bool
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    65
  val typ_of_dtyp : descr -> (string * sort) list -> dtyp -> typ
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    66
  val dest_DtTFree : dtyp -> string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    67
  val dest_DtRec : dtyp -> int
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    68
  val strip_dtyp : dtyp -> dtyp list * dtyp
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    69
  val body_index : dtyp -> int
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    70
  val mk_fun_dtyp : dtyp list -> dtyp -> dtyp
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    71
  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
    72
  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
    73
  val get_arities : descr -> int list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    74
  val get_rec_types : descr -> (string * sort) list -> typ list
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
    75
  val interpret_construction : descr -> (string * sort) list
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
    76
    -> { atyp: typ -> 'a, dtyp: typ list -> int * bool -> string * typ list -> 'a }
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33042
diff changeset
    77
    -> ((string * typ list) * (string * 'a list) list) list
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    78
  val check_nonempty : descr list -> unit
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    79
  val unfold_datatypes : 
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    80
    theory -> descr -> (string * sort) list -> info Symtab.table ->
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    81
      descr -> int -> descr list * int
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
    82
  val find_shortest_path : descr -> int -> (string * int) option
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    83
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    84
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
    85
structure Datatype_Aux : DATATYPE_AUX =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    86
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    87
31668
a616e56a5ec8 datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents: 31605
diff changeset
    88
(* datatype option flags *)
a616e56a5ec8 datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents: 31605
diff changeset
    89
32964
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    90
type config = {strict : bool, quiet : bool};
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    91
val default_config : config = {strict = true, quiet = false};
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    92
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    93
fun message ({quiet = true, ...} : config) s = writeln s
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    94
  | message _ _ = ();
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    95
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    96
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    97
(* store theorems in theory *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    98
18377
0e1d025d57b3 oriented result pairs in PureThy
haftmann
parents: 18349
diff changeset
    99
fun store_thmss_atts label tnames attss thmss =
0e1d025d57b3 oriented result pairs in PureThy
haftmann
parents: 18349
diff changeset
   100
  fold_map (fn ((tname, atts), thms) =>
24712
64ed05609568 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24699
diff changeset
   101
    Sign.add_path tname
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 29265
diff changeset
   102
    #> PureThy.add_thmss [((Binding.name label, thms), atts)]
28361
232fcbba2e4e explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents: 26939
diff changeset
   103
    #-> (fn thm::_ => Sign.parent_path #> pair thm)) (tnames ~~ attss ~~ thmss)
232fcbba2e4e explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents: 26939
diff changeset
   104
  ##> Theory.checkpoint;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   105
18101
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
   106
fun store_thmss label tnames = store_thmss_atts label tnames (replicate (length tnames) []);
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
   107
18377
0e1d025d57b3 oriented result pairs in PureThy
haftmann
parents: 18349
diff changeset
   108
fun store_thms_atts label tnames attss thmss =
0e1d025d57b3 oriented result pairs in PureThy
haftmann
parents: 18349
diff changeset
   109
  fold_map (fn ((tname, atts), thms) =>
24712
64ed05609568 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24699
diff changeset
   110
    Sign.add_path tname
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 29265
diff changeset
   111
    #> PureThy.add_thms [((Binding.name label, thms), atts)]
28361
232fcbba2e4e explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents: 26939
diff changeset
   112
    #-> (fn thm::_ => Sign.parent_path #> pair thm)) (tnames ~~ attss ~~ thmss)
232fcbba2e4e explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents: 26939
diff changeset
   113
  ##> Theory.checkpoint;
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
   114
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
   115
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
   116
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   117
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   118
(* split theorem thm_1 & ... & thm_n into n theorems *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   119
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   120
fun split_conj_thm th =
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   121
  ((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th];
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   122
35364
b8c62d60195c more antiquotations;
wenzelm
parents: 33971
diff changeset
   123
val mk_conj = foldr1 (HOLogic.mk_binop @{const_name "op &"});
b8c62d60195c more antiquotations;
wenzelm
parents: 33971
diff changeset
   124
val mk_disj = foldr1 (HOLogic.mk_binop @{const_name "op |"});
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   125
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   126
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
   127
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   128
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   129
(* instantiate induction rule *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   130
25676
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   131
fun indtac indrule indnames i st =
5177
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;
25676
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   138
    val flt = if null indnames then I else
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   139
      filter (fn Free (s, _) => s mem indnames | _ => false);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   140
    fun abstr (t1, t2) = (case t1 of
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28361
diff changeset
   141
        NONE => (case flt (OldTerm.term_frees t2) of
25676
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   142
            [Free (s, T)] => SOME (absfree (s, T, t2))
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   143
          | _ => NONE)
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   144
      | SOME (_ $ t') => SOME (Abs ("x", fastype_of t', abstract_over (t', t2))))
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 21420
diff changeset
   145
    val cert = cterm_of (Thm.theory_of_thm st);
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32900
diff changeset
   146
    val insts = map_filter (fn (t, u) => case abstr (getP u) of
25676
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   147
        NONE => NONE
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   148
      | SOME u' => SOME (t |> getP |> snd |> head_of |> cert, cert u')) (ts ~~ ts');
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   149
    val indrule' = cterm_instantiate insts indrule
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   150
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   151
    rtac indrule' i st
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   152
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   154
(* perform exhaustive case analysis on last parameter of subgoal i *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   155
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   156
fun exh_tac exh_thm_of i state =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
  let
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 21420
diff changeset
   158
    val thy = Thm.theory_of_thm state;
21420
8b15e5e66813 cleanup
haftmann
parents: 21021
diff changeset
   159
    val prem = nth (prems_of state) (i - 1);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   160
    val params = Logic.strip_params prem;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   161
    val (_, Type (tname, _)) = hd (rev params);
18145
6757627acf59 renamed Thm.cgoal_of to Thm.cprem_of;
wenzelm
parents: 18101
diff changeset
   162
    val exhaustion = Thm.lift_rule (Thm.cprem_of state i) (exh_thm_of tname);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   163
    val prem' = hd (prems_of exhaustion);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   164
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
21420
8b15e5e66813 cleanup
haftmann
parents: 21021
diff changeset
   165
    val exhaustion' = cterm_instantiate [(cterm_of thy (head_of lhs),
33338
de76079f973a eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   166
      cterm_of thy (fold_rev (fn (_, T) => fn t => Abs ("z", T, t)) params (Bound 0)))] exhaustion
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   167
  in compose_tac (false, exhaustion', nprems_of exhaustion) i state
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   168
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   169
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   170
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   171
(********************** Internal description of datatypes *********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   172
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   173
datatype dtyp =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
    DtTFree of string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   175
  | DtType of string * (dtyp list)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   176
  | DtRec of int;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   178
(* information about datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   179
16901
d649ff14096a Tuned comment.
berghofe
parents: 15574
diff changeset
   180
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *)
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   181
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list;
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   182
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
   183
type info =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   184
  {index : int,
25676
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   185
   alt_names : string list option,
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   186
   descr : descr,
18319
c52b139ebde0 Added new component "sorts" to record datatype_info.
berghofe
parents: 18314
diff changeset
   187
   sorts : (string * sort) list,
32718
45929374f1bd more appropriate order of field in dt_info
haftmann
parents: 32712
diff changeset
   188
   inject : thm list,
32900
dc883c6126d4 dropped simproc_dist formally
haftmann
parents: 32733
diff changeset
   189
   distinct : thm list,
32727
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   190
   induct : thm,
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   191
   inducts : thm list,
32718
45929374f1bd more appropriate order of field in dt_info
haftmann
parents: 32712
diff changeset
   192
   exhaust : thm,
45929374f1bd more appropriate order of field in dt_info
haftmann
parents: 32712
diff changeset
   193
   nchotomy : thm,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   194
   rec_names : string list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   195
   rec_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
   case_name : string,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   197
   case_rewrites : thm list,
10120
0f315aeee16e info: weak_case_cong;
wenzelm
parents: 9740
diff changeset
   198
   case_cong : thm,
32718
45929374f1bd more appropriate order of field in dt_info
haftmann
parents: 32712
diff changeset
   199
   weak_case_cong : thm,
32727
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   200
   split : thm,
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   201
   split_asm: thm};
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   202
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   203
fun mk_Free s T i = Free (s ^ (string_of_int i), T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   204
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   205
fun subst_DtTFree _ substs (T as (DtTFree name)) =
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   206
      AList.lookup (op =) substs name |> the_default T
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   207
  | subst_DtTFree i substs (DtType (name, ts)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   208
      DtType (name, map (subst_DtTFree i substs) ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   209
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   210
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   211
exception Datatype;
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
   212
exception Datatype_Empty of string;
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   213
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   214
fun dest_DtTFree (DtTFree a) = a
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   215
  | dest_DtTFree _ = raise Datatype;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   216
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   217
fun dest_DtRec (DtRec i) = i
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   218
  | dest_DtRec _ = raise Datatype;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   221
  | is_rec_type (DtRec _) = true
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   222
  | is_rec_type _ = false;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   223
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   224
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
   225
  | strip_dtyp T = ([], T);
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
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
   228
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   229
fun mk_fun_dtyp [] U = U
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   230
  | 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
   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)) =
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   233
      let val s' = Long_Name.base_name s
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33244
diff changeset
   234
      in space_implode "_" (filter_out (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)
25888
48cc198b9ac5 Eliminated DatatypeAux.dest_TFree to avoid clashes
berghofe
parents: 25676
diff changeset
   244
       | SOME vs => if map (try (fst o 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 =
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   258
  map (typ_of_dtyp descr sorts) (fold (fn (_, (_, _, constrs)) =>
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   259
    fold (fn (_, cargs) => union (op =) (filter_out is_rec_type cargs)) constrs) descr []);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   260
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
(* get all recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   263
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   264
  Type (s, map (typ_of_dtyp descr sorts) ds)) descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   266
(* get all branching types *)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   267
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   268
fun get_branching_types descr sorts =
26823
f426b9c2a90d Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents: 26532
diff changeset
   269
  map (typ_of_dtyp descr sorts) (fold (fn (_, (_, _, constrs)) =>
f426b9c2a90d Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents: 26532
diff changeset
   270
    fold (fn (_, cargs) => fold (strip_dtyp #> fst #> fold (insert op =)) cargs)
f426b9c2a90d Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents: 26532
diff changeset
   271
      constrs) descr []);
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   272
26823
f426b9c2a90d Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents: 26532
diff changeset
   273
fun get_arities descr = fold (fn (_, (_, _, constrs)) =>
f426b9c2a90d Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents: 26532
diff changeset
   274
  fold (fn (_, cargs) => fold (insert op =) (map (length o fst o strip_dtyp)
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33244
diff changeset
   275
    (filter is_rec_type cargs))) constrs) descr [];
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   276
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   277
(* interpret construction of datatype *)
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   278
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   279
fun interpret_construction descr vs { atyp, dtyp } =
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   280
  let
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   281
    val typ_of_dtyp = typ_of_dtyp descr vs;
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   282
    fun interpT dT = case strip_dtyp dT
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   283
     of (dTs, DtRec l) =>
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   284
          let
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   285
            val (tyco, dTs', _) = (the o AList.lookup (op =) descr) l;
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   286
            val Ts = map typ_of_dtyp dTs;
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   287
            val Ts' = map typ_of_dtyp dTs';
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   288
            val is_proper = forall (can dest_TFree) Ts';
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   289
          in dtyp Ts (l, is_proper) (tyco, Ts') end
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   290
      | _ => atyp (typ_of_dtyp dT);
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   291
    fun interpC (c, dTs) = (c, map interpT dTs);
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   292
    fun interpD (_, (tyco, dTs, cs)) = ((tyco, map typ_of_dtyp dTs), map interpC cs);
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   293
  in map interpD descr end;
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   294
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   295
(* nonemptiness check for datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   296
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   297
fun check_nonempty descr =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   298
  let
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32900
diff changeset
   299
    val descr' = flat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   300
    fun is_nonempty_dt is i =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   301
      let
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   302
        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
   303
        fun arg_nonempty (_, DtRec i) = if i mem is then false
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   304
              else is_nonempty_dt (i::is) i
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   305
          | arg_nonempty _ = true;
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   306
      in exists ((forall (arg_nonempty o strip_dtyp)) o snd) constrs
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   307
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   308
  in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr)
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
   309
    (fn (_, (s, _, _)) => raise Datatype_Empty s)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   310
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   311
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   312
(* unfold a list of mutually recursive datatype specifications *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   313
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   314
(* need to be unfolded                                         *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   315
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
   316
fun unfold_datatypes sign orig_descr sorts (dt_info : info Symtab.table) descr i =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   317
  let
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   318
    fun typ_error T msg = error ("Non-admissible type expression\n" ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26823
diff changeset
   319
      Syntax.string_of_typ_global sign (typ_of_dtyp (orig_descr @ descr) sorts T) ^ "\n" ^ msg);
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   320
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   321
    fun get_dt_descr T i tname dts =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
   322
      (case Symtab.lookup dt_info tname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   323
         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
   324
           \ nested recursion")
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15379
diff changeset
   325
       | (SOME {index, descr, ...}) =>
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   326
           let val (_, vars, _) = (the o AList.lookup (op =) descr) index;
19841
f2fa72c13186 avoid unqualified exception;
wenzelm
parents: 19250
diff changeset
   327
               val subst = ((map dest_DtTFree vars) ~~ dts) handle Library.UnequalLengths =>
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   328
                 typ_error T ("Type constructor " ^ tname ^ " used with wrong\
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   329
                  \ number of arguments")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   330
           in (i + index, map (fn (j, (tn, args, cs)) => (i + j,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   331
             (tn, map (subst_DtTFree i subst) args,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   332
              map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   333
           end);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   334
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   335
    (* unfold a single constructor argument *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   336
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   337
    fun unfold_arg T (i, Ts, descrs) =
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   338
      if is_rec_type T then
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   339
        let val (Us, U) = strip_dtyp T
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   340
        in if exists is_rec_type Us then
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   341
            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
   342
          else (case U of
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   343
              DtType (tname, dts) =>  
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   344
                let
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   345
                  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
   346
                  val (descr', i') = unfold_datatypes sign orig_descr sorts
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   347
                    dt_info descr (i + length descr)
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   348
                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
   349
            | _ => (i, Ts @ [T], descrs))
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   350
        end
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   351
      else (i, Ts @ [T], descrs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   352
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   353
    (* unfold a constructor *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   354
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   355
    fun unfold_constr (cname, cargs) (i, constrs, descrs) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   356
      let val (i', cargs', descrs') = fold unfold_arg cargs (i, [], descrs)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   357
      in (i', constrs @ [(cname, cargs')], descrs') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   358
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   359
    (* unfold a single datatype *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   360
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   361
    fun unfold_datatype (j, (tname, tvars, constrs)) (i, dtypes, descrs) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   362
      let val (i', constrs', descrs') = fold unfold_constr constrs (i, [], descrs)
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   363
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs') end;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   364
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   365
    val (i', descr', descrs) = fold unfold_datatype descr (i, [], []);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   366
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   367
  in (descr' :: descrs, i') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   368
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   369
(* find shortest path to constructor with no recursive arguments *)
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   370
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   371
fun find_nonempty descr is i =
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   372
  let
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   373
    fun arg_nonempty (_, DtRec i) = if member (op =) is i
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   374
          then NONE
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   375
          else Option.map (Integer.add 1 o snd) (find_nonempty descr (i::is) i)
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   376
      | arg_nonempty _ = SOME 0;
33971
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33970
diff changeset
   377
    fun max_inf (SOME i) (SOME j) = SOME (Integer.max i j)
33970
haftmann
parents: 33968
diff changeset
   378
      | max_inf _ _ = NONE;
haftmann
parents: 33968
diff changeset
   379
    fun max xs = fold max_inf xs (SOME 0);
haftmann
parents: 33968
diff changeset
   380
    val (_, _, constrs) = the (AList.lookup (op =) descr i);
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   381
    val xs = sort (int_ord o pairself snd)
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   382
      (map_filter (fn (s, dts) => Option.map (pair s)
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   383
        (max (map (arg_nonempty o strip_dtyp) dts))) constrs)
33970
haftmann
parents: 33968
diff changeset
   384
  in if null xs then NONE else SOME (hd xs) end;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   385
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   386
fun find_shortest_path descr i = find_nonempty descr [i] i;
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   387
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   388
end;