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