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