src/HOL/Tools/datatype_aux.ML
author berghofe
Fri, 16 Oct 1998 18:54:55 +0200
changeset 5661 6ecb6ea25f19
parent 5177 0d3a168e4d44
child 5891 92e0f5e6fd17
permissions -rw-r--r--
- Changed structure of name spaces - Proofs for datatypes with unneeded parameters are working now - added additional parameter flat_names - added quiet_mode flag
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$
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     4
    Copyright   1998  TU Muenchen
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     5
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     6
Auxiliary functions for defining datatypes
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
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    16
  val get_thy : string -> theory -> theory option
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    17
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    18
  val add_path : bool -> string -> theory -> theory
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    19
  val parent_path : bool -> theory -> theory
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    20
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    21
  val store_thmss : string -> string list -> thm list list -> theory -> theory
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    22
  val store_thms : string -> string list -> thm list -> theory -> theory
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
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    31
  datatype dtyp =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    32
      DtTFree of string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    33
    | DtType of string * (dtyp list)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    34
    | DtRec of int;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    35
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    36
  type datatype_info
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    37
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    38
  val dtyp_of_typ : (string * string list) list -> typ -> dtyp
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    39
  val mk_Free : string -> typ -> int -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    40
  val is_rec_type : dtyp -> bool
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    41
  val typ_of_dtyp : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    42
    (string * dtyp list) list)) list -> (string * sort) list -> dtyp -> typ
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    43
  val dest_DtTFree : dtyp -> string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    44
  val dest_DtRec : dtyp -> int
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    45
  val dest_TFree : typ -> string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    46
  val dest_conj : term -> term list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    47
  val get_nonrec_types : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    48
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    49
  val get_rec_types : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    50
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    51
  val check_nonempty : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    52
    (string * dtyp list) list)) list list -> unit
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    53
  val unfold_datatypes : 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    54
    datatype_info Symtab.table ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    55
      (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    56
        (string * dtyp list) list)) list -> int ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    57
          (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    58
            (string * dtyp list) list)) list list * int
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    59
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    60
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    61
structure DatatypeAux : DATATYPE_AUX =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    62
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    63
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    64
val quiet_mode = ref false;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    65
fun message s = if !quiet_mode then () else writeln s;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    66
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    67
(* FIXME: move to library ? *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    68
fun foldl1 f (x::xs) = foldl f (x, xs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    69
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    70
fun get_thy name thy = find_first
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    71
  (equal name o Sign.name_of o sign_of) (ancestors_of thy);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    72
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    73
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
    74
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
    75
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    76
(* store theorems in theory *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    77
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    78
fun store_thmss label tnames thmss thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    79
  foldr (fn ((tname, thms), thy') => thy' |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    80
    Theory.add_path tname |>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    81
    PureThy.add_tthmss [((label, map Attribute.tthm_of thms), [])] |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    82
    Theory.parent_path)
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    83
      (tnames ~~ thmss, thy);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    84
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    85
fun store_thms label tnames thms thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    86
  foldr (fn ((tname, thm), thy') => thy' |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    87
    Theory.add_path tname |>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    88
    PureThy.add_tthms [((label, Attribute.tthm_of thm), [])] |>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    89
    Theory.parent_path)
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    90
      (tnames ~~ thms, thy);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    91
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    92
(* split theorem thm_1 & ... & thm_n into n theorems *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    93
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    94
fun split_conj_thm th =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    95
  ((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle _ => [th];
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    96
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    97
val mk_conj = foldr1 (HOLogic.mk_binop "op &");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    98
val mk_disj = foldr1 (HOLogic.mk_binop "op |");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    99
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   100
fun dest_conj (Const ("op &", _) $ t $ t') = t::(dest_conj t')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   101
  | dest_conj t = [t];
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   102
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   103
(* instantiate induction rule *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   104
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   105
fun indtac indrule i st =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   106
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   107
    val ts = dest_conj (HOLogic.dest_Trueprop (concl_of indrule));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   108
    val ts' = dest_conj (HOLogic.dest_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   109
      (Logic.strip_imp_concl (nth_elem (i - 1, prems_of st))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   110
    val getP = if can HOLogic.dest_imp (hd ts) then
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   111
      (apfst Some) o HOLogic.dest_imp else pair None;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   112
    fun abstr (t1, t2) = (case t1 of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   113
        None => let val [Free (s, T)] = add_term_frees (t2, [])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   114
          in absfree (s, T, t2) end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   115
      | Some (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   116
    val cert = cterm_of (sign_of_thm st);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   117
    val Ps = map (cert o head_of o snd o getP) ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   118
    val indrule' = cterm_instantiate (Ps ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   119
      (map (cert o abstr o getP) ts')) indrule
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   120
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   121
    rtac indrule' i st
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   122
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   123
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   124
(* perform exhaustive case analysis on last parameter of subgoal i *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   125
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   126
fun exh_tac exh_thm_of i state =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   127
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   128
    val sg = sign_of_thm state;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   129
    val prem = nth_elem (i - 1, prems_of state);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   130
    val params = Logic.strip_params prem;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   131
    val (_, Type (tname, _)) = hd (rev params);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   132
    val exhaustion = lift_rule (state, i) (exh_thm_of tname);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   133
    val prem' = hd (prems_of exhaustion);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   134
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   135
    val exhaustion' = cterm_instantiate [(cterm_of sg (head_of lhs),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   136
      cterm_of sg (foldr (fn ((_, T), t) => Abs ("z", T, t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   137
        (params, Bound 0)))] exhaustion
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   138
  in compose_tac (false, exhaustion', nprems_of exhaustion) i state
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   139
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   140
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   141
(********************** Internal description of datatypes *********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   142
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   143
datatype dtyp =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   144
    DtTFree of string
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   145
  | DtType of string * (dtyp list)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   146
  | DtRec of int;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   147
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   148
(* information about datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   149
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   150
type datatype_info =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   151
  {index : int,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   152
   descr : (int * (string * dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
     (string * dtyp list) list)) list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   154
   rec_names : string list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   155
   rec_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   156
   case_name : string,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
   case_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   158
   induction : thm,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   159
   exhaustion : thm,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   160
   distinct : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   161
   inject : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   162
   nchotomy : thm,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   163
   case_cong : thm};
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   164
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   165
fun mk_Free s T i = Free (s ^ (string_of_int i), T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   166
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   167
fun subst_DtTFree _ substs (T as (DtTFree name)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   168
      (case assoc (substs, name) of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   169
         None => T
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   170
       | Some U => U)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   171
  | subst_DtTFree i substs (DtType (name, ts)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   172
      DtType (name, map (subst_DtTFree i substs) ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   173
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   175
fun dest_DtTFree (DtTFree a) = a;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   176
fun dest_DtRec (DtRec i) = i;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   178
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   179
  | is_rec_type (DtRec _) = true
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   180
  | is_rec_type _ = false;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   181
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   182
fun dest_TFree (TFree (n, _)) = n;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   183
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   184
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   185
  | dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)"
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   186
  | dtyp_of_typ new_dts (Type (tname, Ts)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   187
      (case assoc (new_dts, tname) of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   188
         None => DtType (tname, map (dtyp_of_typ new_dts) Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   189
       | Some vs => if map (try dest_TFree) Ts = map Some vs then
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   190
             DtRec (find_index (curry op = tname o fst) new_dts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   191
           else error ("Illegal occurence of recursive type " ^ tname));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   192
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   193
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, the (assoc (sorts, a)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   194
  | typ_of_dtyp descr sorts (DtRec i) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   195
      let val (s, ds, _) = the (assoc (descr, i))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
      in Type (s, map (typ_of_dtyp descr sorts) ds) end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   197
  | typ_of_dtyp descr sorts (DtType (s, ds)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   198
      Type (s, map (typ_of_dtyp descr sorts) ds);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   199
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   200
(* find all non-recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   201
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   202
fun get_nonrec_types descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   203
  let fun add (Ts, T as DtTFree _) = T ins Ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   204
        | add (Ts, T as DtType _) = T ins Ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   205
        | add (Ts, _) = Ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   206
  in map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   207
    foldl (fn (Ts', (_, cargs)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   208
      foldl add (Ts', cargs)) (Ts, constrs)) ([], descr))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   209
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   210
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   211
(* get all recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   212
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   213
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   214
  Type (s, map (typ_of_dtyp descr sorts) ds)) descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   215
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   216
(* nonemptiness check for datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   217
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   218
fun check_nonempty descr =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   221
    fun is_nonempty_dt is i =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   222
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   223
        val (_, _, constrs) = the (assoc (descr', i));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   224
        fun arg_nonempty (DtRec i) = if i mem is then false
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   225
              else is_nonempty_dt (i::is) i
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   226
          | arg_nonempty _ = true;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   227
      in exists ((forall arg_nonempty) o snd) constrs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   228
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   229
  in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   230
    (fn (_, (s, _, _)) => "Nonemptiness check failed for datatype " ^ s)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   231
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   232
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   233
(* unfold a list of mutually recursive datatype specifications *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   234
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   235
(* need to be unfolded                                         *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   236
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   237
fun unfold_datatypes (dt_info : datatype_info Symtab.table) descr i =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   238
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   239
    fun get_dt_descr i tname dts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   240
      (case Symtab.lookup (dt_info, tname) of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   241
         None => error (tname ^ " is not a datatype - can't use it in\
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   242
           \ indirect recursion")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   243
       | (Some {index, descr, ...}) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   244
           let val (_, vars, _) = the (assoc (descr, index));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   245
               val subst = ((map dest_DtTFree vars) ~~ dts) handle _ =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   246
                 error ("Type constructor " ^ tname ^ " used with wrong\
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   247
                  \ number of arguments")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   248
           in (i + index, map (fn (j, (tn, args, cs)) => (i + j,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   249
             (tn, map (subst_DtTFree i subst) args,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   250
              map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   251
           end);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   252
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   253
    (* unfold a single constructor argument *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   254
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   255
    fun unfold_arg ((i, Ts, descrs), T as (DtType (tname, dts))) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   256
          if is_rec_type T then
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   257
            let val (index, descr) = get_dt_descr i tname dts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   258
                val (descr', i') = unfold_datatypes dt_info descr (i + length descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   259
            in (i', Ts @ [DtRec index], descrs @ descr') end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   260
          else (i, Ts @ [T], descrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
      | unfold_arg ((i, Ts, descrs), T) = (i, Ts @ [T], descrs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   263
    (* unfold a constructor *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   264
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
    fun unfold_constr ((i, constrs, descrs), (cname, cargs)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   266
      let val (i', cargs', descrs') = foldl unfold_arg ((i, [], descrs), cargs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   267
      in (i', constrs @ [(cname, cargs')], descrs') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   268
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   269
    (* unfold a single datatype *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   270
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   271
    fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   272
      let val (i', constrs', descrs') =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   273
        foldl unfold_constr ((i, [], descrs), constrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   274
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   275
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   276
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   277
    val (i', descr', descrs) = foldl unfold_datatype ((i, [],[]), descr);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   278
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   279
  in (descr' :: descrs, i') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   280
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   281
end;