src/Pure/defs.ML
author wenzelm
Sat May 13 02:51:37 2006 +0200 (2006-05-13)
changeset 19628 de019ddcd89e
parent 19624 3b6629701a79
child 19692 bad13b32c0f3
permissions -rw-r--r--
actually reject malformed defs;
added unchecked flag;
tuned;
wenzelm@17707
     1
(*  Title:      Pure/defs.ML
obua@16108
     2
    ID:         $Id$
wenzelm@17707
     3
    Author:     Makarius
obua@16108
     4
wenzelm@19613
     5
Global well-formedness checks for constant definitions.  Covers
wenzelm@19613
     6
dependencies of simple sub-structural overloading, where type
wenzelm@19613
     7
arguments are approximated by the outermost type constructor.
obua@16108
     8
*)
obua@16108
     9
wenzelm@16877
    10
signature DEFS =
wenzelm@16877
    11
sig
wenzelm@17707
    12
  type T
wenzelm@19569
    13
  val specifications_of: T -> string ->
wenzelm@19569
    14
   (serial * {is_def: bool, module: string, name: string, lhs: typ, rhs: (string * typ) list}) list
wenzelm@19590
    15
  val empty: T
wenzelm@19613
    16
  val merge: T * T -> T
wenzelm@19613
    17
  val define: (string * typ -> typ list) ->
wenzelm@19628
    18
    bool -> bool -> string -> string -> string * typ -> (string * typ) list -> T -> T
obua@16108
    19
end
obua@16108
    20
wenzelm@17711
    21
structure Defs: DEFS =
wenzelm@17707
    22
struct
obua@16108
    23
wenzelm@19613
    24
(* dependency items *)
wenzelm@19569
    25
wenzelm@19613
    26
(*
wenzelm@19613
    27
  Constant c covers all instances of c
wenzelm@19613
    28
wenzelm@19613
    29
  Instance (c, a) covers all instances of applications (c, [Type (a, _)])
wenzelm@19613
    30
wenzelm@19613
    31
  Different Constant/Constant or Instance/Instance items represent
wenzelm@19613
    32
  disjoint sets of instances.  The set Constant c subsumes any
wenzelm@19613
    33
  Instance (c, a) -- dependencies are propagated accordingly.
wenzelm@19613
    34
*)
wenzelm@19613
    35
wenzelm@19613
    36
datatype item =
wenzelm@19613
    37
  Constant of string |
wenzelm@19613
    38
  Instance of string * string;
wenzelm@19613
    39
wenzelm@19613
    40
fun make_item (c, [Type (a, _)]) = Instance (c, a)
wenzelm@19613
    41
  | make_item (c, _) = Constant c;
wenzelm@19613
    42
wenzelm@19613
    43
fun pretty_item (Constant c) = Pretty.str (quote c)
wenzelm@19613
    44
  | pretty_item (Instance (c, a)) = Pretty.str (quote c ^ " (type " ^ quote a ^ ")");
wenzelm@19613
    45
wenzelm@19613
    46
fun item_ord (Constant c, Constant c') = fast_string_ord (c, c')
wenzelm@19613
    47
  | item_ord (Instance ca, Instance ca') = prod_ord fast_string_ord fast_string_ord (ca, ca')
wenzelm@19613
    48
  | item_ord (Constant _, Instance _) = LESS
wenzelm@19613
    49
  | item_ord (Instance _, Constant _) = GREATER;
wenzelm@19613
    50
wenzelm@19613
    51
structure Items = GraphFun(type key = item val ord = item_ord);
wenzelm@19613
    52
wenzelm@19613
    53
fun propagate_deps insts deps =
wenzelm@19613
    54
  let
wenzelm@19620
    55
    fun inst_item (Constant c) = Symtab.lookup_list insts c
wenzelm@19620
    56
      | inst_item (Instance _) = [];
wenzelm@19620
    57
    fun inst_edge i j =
wenzelm@19628
    58
      fold Items.add_edge_acyclic (tl (product (i :: inst_item i) (j :: inst_item j)));
wenzelm@19613
    59
  in Items.fold (fn (i, (_, (_, js))) => fold (inst_edge i) js) deps deps end;
wenzelm@19613
    60
wenzelm@19613
    61
wenzelm@19613
    62
(* specifications *)
wenzelm@16877
    63
wenzelm@19569
    64
type spec = {is_def: bool, module: string, name: string, lhs: typ, rhs: (string * typ) list};
wenzelm@19569
    65
wenzelm@17707
    66
datatype T = Defs of
wenzelm@19613
    67
 {specs: (bool * spec Inttab.table) Symtab.table,
wenzelm@19620
    68
  insts: item list Symtab.table,
wenzelm@19613
    69
  deps: unit Items.T};
wenzelm@19613
    70
wenzelm@19613
    71
fun no_overloading_of (Defs {specs, ...}) c =
wenzelm@19613
    72
  (case Symtab.lookup specs c of
wenzelm@19613
    73
    SOME (b, _) => b
wenzelm@19613
    74
  | NONE => false);
wenzelm@16877
    75
wenzelm@19613
    76
fun specifications_of (Defs {specs, ...}) c =
wenzelm@19613
    77
  (case Symtab.lookup specs c of
wenzelm@19613
    78
    SOME (_, sps) => Inttab.dest sps
wenzelm@19613
    79
  | NONE => []);
wenzelm@16877
    80
wenzelm@19613
    81
fun make_defs (specs, insts, deps) = Defs {specs = specs, insts = insts, deps = deps};
wenzelm@19613
    82
fun map_defs f (Defs {specs, insts, deps}) = make_defs (f (specs, insts, deps));
wenzelm@19613
    83
wenzelm@19613
    84
val empty = make_defs (Symtab.empty, Symtab.empty, Items.empty);
wenzelm@19590
    85
wenzelm@16877
    86
wenzelm@19624
    87
(* disjoint specs *)
wenzelm@16877
    88
wenzelm@17707
    89
fun disjoint_types T U =
wenzelm@17707
    90
  (Type.raw_unify (T, Logic.incr_tvar (maxidx_of_typ T + 1) U) Vartab.empty; false)
wenzelm@17707
    91
    handle Type.TUNIFY => true;
obua@16308
    92
wenzelm@19613
    93
fun disjoint_specs c (i, {lhs = T, name = a, ...}: spec) =
wenzelm@19569
    94
  Inttab.forall (fn (j, {lhs = U, name = b, ...}: spec) =>
wenzelm@19569
    95
    i = j orelse not (Type.could_unify (T, U)) orelse disjoint_types T U orelse
wenzelm@19569
    96
      error ("Type clash in specifications " ^ quote a ^ " and " ^ quote b ^
wenzelm@19569
    97
        " for constant " ^ quote c));
wenzelm@16877
    98
wenzelm@19624
    99
wenzelm@19624
   100
(* merge *)
wenzelm@19624
   101
wenzelm@19613
   102
fun cycle_msg css =
wenzelm@19613
   103
  let
wenzelm@19613
   104
    fun prt_cycle items = Pretty.block (flat
wenzelm@19620
   105
      (separate [Pretty.str " ->", Pretty.brk 1] (map (single o pretty_item) items)));
wenzelm@19613
   106
  in Pretty.string_of (Pretty.big_list "Cyclic dependency of constants:" (map prt_cycle css)) end;
wenzelm@16982
   107
wenzelm@19613
   108
fun merge
wenzelm@19613
   109
   (Defs {specs = specs1, insts = insts1, deps = deps1},
wenzelm@19613
   110
    Defs {specs = specs2, insts = insts2, deps = deps2}) =
wenzelm@19613
   111
  let
wenzelm@19613
   112
    val specs' = (specs1, specs2) |> Symtab.join (fn c => fn ((b, sps1), (_, sps2)) =>
wenzelm@19613
   113
      (b, Inttab.fold (fn sp2 => (disjoint_specs c sp2 sps1; Inttab.update sp2)) sps2 sps1));
wenzelm@19613
   114
    val insts' = Symtab.merge_list (op =) (insts1, insts2);
wenzelm@19613
   115
    val items' = propagate_deps insts' (Items.merge_acyclic (K true) (deps1, deps2))
wenzelm@19613
   116
      handle Items.CYCLES cycles => error (cycle_msg cycles);
wenzelm@19613
   117
  in make_defs (specs', insts', items') end;
wenzelm@19613
   118
wenzelm@19613
   119
wenzelm@19613
   120
(* define *)
wenzelm@19590
   121
wenzelm@19624
   122
fun pure_args args = forall Term.is_TVar args andalso not (has_duplicates (op =) args);
wenzelm@19624
   123
wenzelm@19628
   124
fun define const_typargs unchecked is_def module name lhs rhs defs = defs
wenzelm@19613
   125
    |> map_defs (fn (specs, insts, deps) =>
wenzelm@17707
   126
  let
wenzelm@19590
   127
    val (c, T) = lhs;
wenzelm@19590
   128
    val args = const_typargs lhs;
wenzelm@19624
   129
    val no_overloading = pure_args args;
wenzelm@19624
   130
    val rec_args = (case args of [Type (_, Ts)] => if pure_args Ts then Ts else [] | _ => []);
wenzelm@17707
   131
wenzelm@19628
   132
    val lhs' = make_item (c, args);
wenzelm@19628
   133
    val rhs' =
wenzelm@19628
   134
      if unchecked then []
wenzelm@19628
   135
      else rhs |> map_filter (fn (c', T') =>
wenzelm@19628
   136
        let val args' = const_typargs (c', T') in
wenzelm@19628
   137
          if gen_subset (op =) (args', rec_args) then NONE
wenzelm@19628
   138
          else SOME (make_item (c', if no_overloading_of defs c' then [] else args'))
wenzelm@19628
   139
        end);
wenzelm@19628
   140
wenzelm@19569
   141
    val spec = (serial (), {is_def = is_def, module = module, name = name, lhs = T, rhs = rhs});
wenzelm@19613
   142
    val specs' = specs
wenzelm@19613
   143
      |> Symtab.default (c, (false, Inttab.empty))
wenzelm@19613
   144
      |> Symtab.map_entry c (fn (_, sps) =>
wenzelm@19613
   145
          (disjoint_specs c spec sps; (no_overloading, Inttab.update spec sps)));
wenzelm@19620
   146
    val insts' = insts |> fold (fn i as Instance (c, _) =>
wenzelm@19620
   147
        Symtab.insert_list (op =) (c, i) | _ => I) (lhs' :: rhs');
wenzelm@19613
   148
    val deps' = deps
wenzelm@19628
   149
      |> fold (Items.default_node o rpair ()) (lhs' :: rhs')
wenzelm@19628
   150
      |> Items.add_deps_acyclic (lhs', rhs')
wenzelm@19613
   151
      |> propagate_deps insts'
wenzelm@19628
   152
      handle Items.CYCLES cycles => error (cycle_msg cycles);
wenzelm@19613
   153
wenzelm@19613
   154
  in (specs', insts', deps') end);
wenzelm@16877
   155
obua@16108
   156
end;