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