src/HOL/Tools/datatype_aux.ML
author oheimb
Wed, 03 Apr 2002 10:21:13 +0200
changeset 13076 70704dd48bd5
parent 11539 0f17da240450
child 13641 63d1790a43ed
permissions -rw-r--r--
bugfix concerning claset(), added limited support for ALLGOALS + fast_tac etc.
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
0f17da240450 tuned headers;
wenzelm
parents: 10120
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     5
11539
0f17da240450 tuned headers;
wenzelm
parents: 10120
diff changeset
     6
Auxiliary functions for defining datatypes.
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     7
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     8
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     9
signature DATATYPE_AUX =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    10
sig
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    11
  val quiet_mode : bool ref
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    12
  val message : string -> unit
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    13
  
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    14
  val foldl1 : ('a * 'a -> 'a) -> 'a list -> 'a
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    15
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    16
  val add_path : bool -> string -> theory -> theory
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    17
  val parent_path : bool -> theory -> theory
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    18
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    19
  val store_thmss : string -> string list -> thm list list -> theory -> theory * thm list list
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    20
  val store_thms_atts : string -> string list -> theory attribute list list -> thm list
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    21
    -> theory -> theory * thm list
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    22
  val store_thms : string -> string list -> thm list -> theory -> theory * thm list
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    23
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    24
  val split_conj_thm : thm -> thm list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    25
  val mk_conj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    26
  val mk_disj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    27
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    28
  val indtac : thm -> int -> tactic
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    29
  val exh_tac : (string -> thm) -> int -> tactic
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    30
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    31
  datatype simproc_dist = QuickAndDirty
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    32
                        | FewConstrs of thm list
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    33
                        | ManyConstrs of thm * simpset;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    34
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    35
  datatype dtyp =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    36
      DtTFree of string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    37
    | DtType of string * (dtyp list)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    38
    | DtRec of int;
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
    39
  type descr
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    40
  type datatype_info
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    41
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    42
  exception Datatype
9740
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
    43
  val name_of_typ : typ -> string
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    44
  val dtyp_of_typ : (string * string list) list -> typ -> dtyp
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    45
  val mk_Free : string -> typ -> int -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    46
  val is_rec_type : dtyp -> bool
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    47
  val typ_of_dtyp : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    48
    (string * dtyp list) list)) list -> (string * sort) list -> dtyp -> typ
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    49
  val dest_DtTFree : dtyp -> string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    50
  val dest_DtRec : dtyp -> int
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    51
  val dest_TFree : typ -> string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    52
  val get_nonrec_types : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    53
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    54
  val get_branching_types : (int * (string * dtyp list *
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    55
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    56
  val get_rec_types : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    57
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    58
  val check_nonempty : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    59
    (string * dtyp list) list)) list list -> unit
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    60
  val unfold_datatypes : 
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    61
    Sign.sg -> (int * (string * dtyp list * (string * dtyp list) list)) list ->
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    62
      (string * sort) list -> datatype_info Symtab.table ->
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    63
        (int * (string * dtyp list * (string * dtyp list) list)) list -> int ->
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    64
          (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    65
            (string * dtyp list) list)) list list * int
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    66
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    67
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    68
structure DatatypeAux : DATATYPE_AUX =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    69
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    70
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    71
val quiet_mode = ref false;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    72
fun message s = if !quiet_mode then () else writeln s;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    73
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    74
(* FIXME: move to library ? *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    75
fun foldl1 f (x::xs) = foldl f (x, xs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    76
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    77
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
    78
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
    79
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    80
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    81
(* store theorems in theory *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    82
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    83
fun store_thmss label tnames thmss thy =
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    84
  (thy, tnames ~~ thmss) |>
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    85
  foldl_map (fn (thy', (tname, thms)) => thy' |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    86
    Theory.add_path tname |>
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    87
    (apsnd hd o PureThy.add_thmss [((label, thms), [])]) |>>
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    88
    Theory.parent_path);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    89
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    90
fun store_thms_atts label tnames attss thms thy =
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    91
  (thy, tnames ~~ attss ~~ thms) |>
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    92
  foldl_map (fn (thy', ((tname, atts), thm)) => thy' |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    93
    Theory.add_path tname |>
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    94
    (apsnd hd o PureThy.add_thms [((label, thm), atts)]) |>>
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    95
    Theory.parent_path);
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    96
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    97
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
    98
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    99
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   100
(* split theorem thm_1 & ... & thm_n into n theorems *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   101
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   102
fun split_conj_thm th =
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   103
  ((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th];
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   104
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   105
val mk_conj = foldr1 (HOLogic.mk_binop "op &");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   106
val mk_disj = foldr1 (HOLogic.mk_binop "op |");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   107
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   108
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   109
(* instantiate induction rule *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   110
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   111
fun indtac indrule i st =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   112
  let
8305
93aa21ec5494 HOLogic.dest_conj;
wenzelm
parents: 7015
diff changeset
   113
    val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule));
93aa21ec5494 HOLogic.dest_conj;
wenzelm
parents: 7015
diff changeset
   114
    val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   115
      (Logic.strip_imp_concl (nth_elem (i - 1, prems_of st))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   116
    val getP = if can HOLogic.dest_imp (hd ts) then
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   117
      (apfst Some) o HOLogic.dest_imp else pair None;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   118
    fun abstr (t1, t2) = (case t1 of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   119
        None => let val [Free (s, T)] = add_term_frees (t2, [])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   120
          in absfree (s, T, t2) end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   121
      | Some (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2)))
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6092
diff changeset
   122
    val cert = cterm_of (Thm.sign_of_thm st);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   123
    val Ps = map (cert o head_of o snd o getP) ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   124
    val indrule' = cterm_instantiate (Ps ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   125
      (map (cert o abstr o getP) ts')) indrule
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   126
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   127
    rtac indrule' i st
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   128
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   129
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   130
(* perform exhaustive case analysis on last parameter of subgoal i *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   131
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   132
fun exh_tac exh_thm_of i state =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   133
  let
6394
3d9fd50fcc43 Theory.sign_of;
wenzelm
parents: 6092
diff changeset
   134
    val sg = Thm.sign_of_thm state;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   135
    val prem = nth_elem (i - 1, prems_of state);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   136
    val params = Logic.strip_params prem;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   137
    val (_, Type (tname, _)) = hd (rev params);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   138
    val exhaustion = lift_rule (state, i) (exh_thm_of tname);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   139
    val prem' = hd (prems_of exhaustion);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   140
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   141
    val exhaustion' = cterm_instantiate [(cterm_of sg (head_of lhs),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   142
      cterm_of sg (foldr (fn ((_, T), t) => Abs ("z", T, t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   143
        (params, Bound 0)))] exhaustion
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   144
  in compose_tac (false, exhaustion', nprems_of exhaustion) i state
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   145
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   146
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   147
(* handling of distinctness theorems *)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   148
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   149
datatype simproc_dist = QuickAndDirty
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   150
                      | FewConstrs of thm list
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   151
                      | ManyConstrs of thm * simpset;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   152
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
(********************** Internal description of datatypes *********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   154
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   155
datatype dtyp =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   156
    DtTFree of string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
  | DtType of string * (dtyp list)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   158
  | DtRec of int;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   159
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   160
(* information about datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   161
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   162
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list;
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   163
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   164
type datatype_info =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   165
  {index : int,
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   166
   descr : descr,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   167
   rec_names : string list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   168
   rec_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   169
   case_name : string,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   170
   case_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   171
   induction : thm,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   172
   exhaustion : thm,
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   173
   distinct : simproc_dist,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
   inject : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   175
   nchotomy : thm,
10120
0f315aeee16e info: weak_case_cong;
wenzelm
parents: 9740
diff changeset
   176
   case_cong : thm,
0f315aeee16e info: weak_case_cong;
wenzelm
parents: 9740
diff changeset
   177
   weak_case_cong : thm};
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   178
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   179
fun mk_Free s T i = Free (s ^ (string_of_int i), T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   180
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   181
fun subst_DtTFree _ substs (T as (DtTFree name)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   182
      (case assoc (substs, name) of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   183
         None => T
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   184
       | Some U => U)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   185
  | subst_DtTFree i substs (DtType (name, ts)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   186
      DtType (name, map (subst_DtTFree i substs) ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   187
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   188
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   189
exception Datatype;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   190
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   191
fun dest_DtTFree (DtTFree a) = a
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   192
  | dest_DtTFree _ = raise Datatype;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   193
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   194
fun dest_DtRec (DtRec i) = i
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   195
  | dest_DtRec _ = raise Datatype;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   197
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   198
  | is_rec_type (DtRec _) = true
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   199
  | is_rec_type _ = false;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   200
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   201
fun dest_TFree (TFree (n, _)) = n;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   202
9740
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   203
fun name_of_typ (Type (s, Ts)) = space_implode "_"
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   204
      (filter (not o equal "") (map name_of_typ Ts) @ [Sign.base_name s])
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   205
  | name_of_typ _ = "";
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   206
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   207
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   208
  | dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)"
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   209
  | dtyp_of_typ new_dts (Type (tname, Ts)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   210
      (case assoc (new_dts, tname) of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   211
         None => DtType (tname, map (dtyp_of_typ new_dts) Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   212
       | Some vs => if map (try dest_TFree) Ts = map Some vs then
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   213
             DtRec (find_index (curry op = tname o fst) new_dts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   214
           else error ("Illegal occurence of recursive type " ^ tname));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   215
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   216
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, the (assoc (sorts, a)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   217
  | typ_of_dtyp descr sorts (DtRec i) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   218
      let val (s, ds, _) = the (assoc (descr, i))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
      in Type (s, map (typ_of_dtyp descr sorts) ds) end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
  | typ_of_dtyp descr sorts (DtType (s, ds)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   221
      Type (s, map (typ_of_dtyp descr sorts) ds);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   222
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   223
(* find all non-recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   224
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   225
fun get_nonrec_types descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   226
  let fun add (Ts, T as DtTFree _) = T ins Ts
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   227
        | add (Ts, T as DtType ("fun", [_, DtRec _])) = Ts
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   228
        | add (Ts, T as DtType _) = T ins Ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   229
        | add (Ts, _) = Ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   230
  in map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   231
    foldl (fn (Ts', (_, cargs)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   232
      foldl add (Ts', cargs)) (Ts, constrs)) ([], descr))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   233
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   234
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   235
(* get all recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   236
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   237
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   238
  Type (s, map (typ_of_dtyp descr sorts) ds)) descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   239
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   240
(* get all branching types *)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   241
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   242
fun get_branching_types descr sorts = 
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   243
  let fun add (Ts, DtType ("fun", [T, DtRec _])) = T ins Ts
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   244
        | add (Ts, _) = Ts
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   245
  in map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) =>
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   246
    foldl (fn (Ts', (_, cargs)) =>
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   247
      foldl add (Ts', cargs)) (Ts, constrs)) ([], descr))
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   248
  end;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   249
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   250
(* nonemptiness check for datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   251
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   252
fun check_nonempty descr =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   253
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   254
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   255
    fun is_nonempty_dt is i =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   256
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   257
        val (_, _, constrs) = the (assoc (descr', i));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   258
        fun arg_nonempty (DtRec i) = if i mem is then false
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   259
              else is_nonempty_dt (i::is) i
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   260
          | arg_nonempty (DtType ("fun", [_, T])) = arg_nonempty T
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
          | arg_nonempty _ = true;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
      in exists ((forall arg_nonempty) o snd) constrs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   263
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   264
  in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
    (fn (_, (s, _, _)) => "Nonemptiness check failed for datatype " ^ s)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   266
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   267
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   268
(* unfold a list of mutually recursive datatype specifications *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   269
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   270
(* need to be unfolded                                         *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   271
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   272
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
   273
  let
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   274
    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
   275
      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
   276
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   277
    fun get_dt_descr T i tname dts =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   278
      (case Symtab.lookup (dt_info, tname) of
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   279
         None => typ_error T (tname ^ " is not a datatype - can't use it in\
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   280
           \ nested recursion")
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   281
       | (Some {index, descr, ...}) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   282
           let val (_, vars, _) = the (assoc (descr, index));
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   283
               val subst = ((map dest_DtTFree vars) ~~ dts) handle LIST _ =>
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   284
                 typ_error T ("Type constructor " ^ tname ^ " used with wrong\
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   285
                  \ number of arguments")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   286
           in (i + index, map (fn (j, (tn, args, cs)) => (i + j,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   287
             (tn, map (subst_DtTFree i subst) args,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   288
              map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   289
           end);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   290
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   291
    (* unfold a single constructor argument *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   292
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   293
    fun unfold_arg ((i, Ts, descrs), T as (DtType (tname, dts))) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   294
          if is_rec_type T then
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   295
            if tname = "fun" then
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   296
              if is_rec_type (hd dts) then
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   297
                typ_error T "Non-strictly positive recursive occurrence of type"
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   298
              else
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   299
                (case hd (tl dts) of
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   300
                   DtType ("fun", _) => typ_error T "Curried function types not allowed"
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   301
                 | T' => let val (i', [T''], descrs') = unfold_arg ((i, [], descrs), T')
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   302
                         in (i', Ts @ [DtType (tname, [hd dts, T''])], descrs') end)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   303
            else
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   304
              let val (index, descr) = get_dt_descr T i tname dts;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   305
                  val (descr', i') = unfold_datatypes sign orig_descr sorts dt_info descr (i + length descr)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   306
              in (i', Ts @ [DtRec index], descrs @ descr') end
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   307
          else (i, Ts @ [T], descrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   308
      | unfold_arg ((i, Ts, descrs), T) = (i, Ts @ [T], descrs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   309
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   310
    (* unfold a constructor *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   311
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   312
    fun unfold_constr ((i, constrs, descrs), (cname, cargs)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   313
      let val (i', cargs', descrs') = foldl unfold_arg ((i, [], descrs), cargs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   314
      in (i', constrs @ [(cname, cargs')], descrs') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   315
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   316
    (* unfold a single datatype *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   317
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   318
    fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   319
      let val (i', constrs', descrs') =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   320
        foldl unfold_constr ((i, [], descrs), constrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   321
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   322
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   323
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   324
    val (i', descr', descrs) = foldl unfold_datatype ((i, [],[]), descr);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   325
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   326
  in (descr' :: descrs, i') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   327
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   328
end;