src/Pure/defs.ML
author haftmann
Wed Feb 15 17:09:06 2006 +0100 (2006-02-15)
changeset 19040 88d25a6c346a
parent 19025 596fb1eb7856
child 19050 527bc006f2b6
permissions -rw-r--r--
exported specifications_of
wenzelm@17707
     1
(*  Title:      Pure/defs.ML
obua@16108
     2
    ID:         $Id$
wenzelm@17707
     3
    Author:     Makarius
obua@16108
     4
wenzelm@17707
     5
Global well-formedness checks for constant definitions.  Dependencies
wenzelm@17707
     6
are only tracked for non-overloaded definitions!
obua@16108
     7
*)
obua@16108
     8
wenzelm@16877
     9
signature DEFS =
wenzelm@16877
    10
sig
wenzelm@17707
    11
  type T
wenzelm@17712
    12
  val define: (string -> typ) -> string -> string * typ -> (string * typ) list -> T -> T
wenzelm@17707
    13
  val empty: T
haftmann@19040
    14
  val specifications_of: T -> string -> typ list
wenzelm@17707
    15
  val merge: Pretty.pp -> T * T -> T
obua@16108
    16
end
obua@16108
    17
wenzelm@17711
    18
structure Defs: DEFS =
wenzelm@17707
    19
struct
obua@16108
    20
wenzelm@17707
    21
(** datatype T **)
wenzelm@16877
    22
wenzelm@17707
    23
datatype T = Defs of
wenzelm@17707
    24
 {consts: typ Graph.T,                                 (*constant declarations and dependencies*)
wenzelm@17994
    25
  specified: (string * typ) Inttab.table Symtab.table}; (*specification name and const type*)
wenzelm@16877
    26
wenzelm@17994
    27
fun make_defs (consts, specified) = Defs {consts = consts, specified = specified};
wenzelm@17994
    28
fun map_defs f (Defs {consts, specified}) = make_defs (f (consts, specified));
wenzelm@16877
    29
wenzelm@16877
    30
wenzelm@17707
    31
(* specified consts *)
wenzelm@16877
    32
wenzelm@17707
    33
fun disjoint_types T U =
wenzelm@17707
    34
  (Type.raw_unify (T, Logic.incr_tvar (maxidx_of_typ T + 1) U) Vartab.empty; false)
wenzelm@17707
    35
    handle Type.TUNIFY => true;
obua@16308
    36
wenzelm@17803
    37
fun check_specified c (i, (a, T)) = Inttab.forall (fn (j, (b, U)) =>
wenzelm@17803
    38
  i = j orelse not (Type.could_unify (T, U)) orelse disjoint_types T U orelse
wenzelm@17803
    39
    error ("Type clash in specifications " ^ quote a ^ " and " ^ quote b ^
wenzelm@17803
    40
      " for constant " ^ quote c));
wenzelm@16877
    41
wenzelm@16982
    42
wenzelm@17707
    43
(* define consts *)
wenzelm@17707
    44
wenzelm@17707
    45
fun err_cyclic cycles =
wenzelm@17707
    46
  error ("Cyclic dependency of constants:\n" ^
wenzelm@17707
    47
    cat_lines (map (space_implode " -> " o map quote o rev) cycles));
wenzelm@16877
    48
wenzelm@17994
    49
fun define const_type name lhs rhs = map_defs (fn (consts, specified) =>
wenzelm@17707
    50
  let
wenzelm@17707
    51
    fun declare (a, _) = Graph.default_node (a, const_type a);
wenzelm@17707
    52
    fun add_deps (a, bs) G = Graph.add_deps_acyclic (a, bs) G
wenzelm@17707
    53
      handle Graph.CYCLES cycles => err_cyclic cycles;
wenzelm@17707
    54
wenzelm@17707
    55
    val (c, T) = lhs;
wenzelm@17803
    56
    val spec = (serial (), (name, T));
wenzelm@17707
    57
    val no_overloading = Type.raw_instance (const_type c, T);
wenzelm@16877
    58
wenzelm@17707
    59
    val consts' =
wenzelm@17707
    60
      consts |> declare lhs |> fold declare rhs
wenzelm@17707
    61
      |> K no_overloading ? add_deps (c, map #1 rhs);
wenzelm@17803
    62
    val specified' = specified
wenzelm@17803
    63
      |> Symtab.default (c, Inttab.empty)
wenzelm@17803
    64
      |> Symtab.map_entry c (fn specs =>
wenzelm@17803
    65
        (check_specified c spec specs; Inttab.update spec specs));
wenzelm@17994
    66
  in (consts', specified') end);
wenzelm@17707
    67
haftmann@19040
    68
fun specifications_of (Defs { specified, ... }) c =
haftmann@19040
    69
  (map (snd o snd) o Inttab.dest o the o Symtab.lookup specified) c;
wenzelm@17707
    70
wenzelm@17707
    71
(* empty and merge *)
wenzelm@16877
    72
wenzelm@17994
    73
val empty = make_defs (Graph.empty, Symtab.empty);
wenzelm@16877
    74
wenzelm@17707
    75
fun merge pp
wenzelm@17994
    76
   (Defs {consts = consts1, specified = specified1},
wenzelm@17994
    77
    Defs {consts = consts2, specified = specified2}) =
wenzelm@17707
    78
  let
wenzelm@17707
    79
    val consts' = (consts1, consts2) |> Graph.merge_acyclic (K true)
wenzelm@17707
    80
      handle Graph.CYCLES cycles => err_cyclic cycles;
wenzelm@17803
    81
    val specified' = (specified1, specified2) |> Symtab.join (fn c => fn (orig_specs1, specs2) =>
wenzelm@19025
    82
      Inttab.fold (fn spec2 => fn specs1 =>
wenzelm@19025
    83
        (check_specified c spec2 orig_specs1; Inttab.update spec2 specs1)) specs2 orig_specs1);
wenzelm@17994
    84
  in make_defs (consts', specified') end;
wenzelm@16877
    85
obua@16108
    86
end;