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;
     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 -> 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 propagate_deps insts deps =
    54   let
    55     fun inst_item (Constant c) = Symtab.lookup_list insts c
    56       | inst_item (Instance _) = [];
    57     fun inst_edge i j =
    58       fold Items.add_edge_acyclic (tl (product (i :: inst_item i) (j :: inst_item j)));
    59   in Items.fold (fn (i, (_, (_, js))) => fold (inst_edge i) js) deps deps end;
    60 
    61 
    62 (* specifications *)
    63 
    64 type spec = {is_def: bool, module: string, name: string, lhs: typ, rhs: (string * typ) list};
    65 
    66 datatype T = Defs of
    67  {specs: (bool * spec Inttab.table) Symtab.table,
    68   insts: item list Symtab.table,
    69   deps: unit Items.T};
    70 
    71 fun no_overloading_of (Defs {specs, ...}) c =
    72   (case Symtab.lookup specs c of
    73     SOME (b, _) => b
    74   | NONE => false);
    75 
    76 fun specifications_of (Defs {specs, ...}) c =
    77   (case Symtab.lookup specs c of
    78     SOME (_, sps) => Inttab.dest sps
    79   | NONE => []);
    80 
    81 fun make_defs (specs, insts, deps) = Defs {specs = specs, insts = insts, deps = deps};
    82 fun map_defs f (Defs {specs, insts, deps}) = make_defs (f (specs, insts, deps));
    83 
    84 val empty = make_defs (Symtab.empty, Symtab.empty, Items.empty);
    85 
    86 
    87 (* disjoint specs *)
    88 
    89 fun disjoint_types T U =
    90   (Type.raw_unify (T, Logic.incr_tvar (maxidx_of_typ T + 1) U) Vartab.empty; false)
    91     handle Type.TUNIFY => true;
    92 
    93 fun disjoint_specs c (i, {lhs = T, name = a, ...}: spec) =
    94   Inttab.forall (fn (j, {lhs = U, name = b, ...}: spec) =>
    95     i = j orelse not (Type.could_unify (T, U)) orelse disjoint_types T U orelse
    96       error ("Type clash in specifications " ^ quote a ^ " and " ^ quote b ^
    97         " for constant " ^ quote c));
    98 
    99 
   100 (* merge *)
   101 
   102 fun cycle_msg css =
   103   let
   104     fun prt_cycle items = Pretty.block (flat
   105       (separate [Pretty.str " ->", Pretty.brk 1] (map (single o pretty_item) items)));
   106   in Pretty.string_of (Pretty.big_list "Cyclic dependency of constants:" (map prt_cycle css)) end;
   107 
   108 fun merge
   109    (Defs {specs = specs1, insts = insts1, deps = deps1},
   110     Defs {specs = specs2, insts = insts2, deps = deps2}) =
   111   let
   112     val specs' = (specs1, specs2) |> Symtab.join (fn c => fn ((b, sps1), (_, sps2)) =>
   113       (b, Inttab.fold (fn sp2 => (disjoint_specs c sp2 sps1; Inttab.update sp2)) sps2 sps1));
   114     val insts' = Symtab.merge_list (op =) (insts1, insts2);
   115     val items' = propagate_deps insts' (Items.merge_acyclic (K true) (deps1, deps2))
   116       handle Items.CYCLES cycles => error (cycle_msg cycles);
   117   in make_defs (specs', insts', items') end;
   118 
   119 
   120 (* define *)
   121 
   122 fun pure_args args = forall Term.is_TVar args andalso not (has_duplicates (op =) args);
   123 
   124 fun define const_typargs unchecked is_def module name lhs rhs defs = defs
   125     |> map_defs (fn (specs, insts, deps) =>
   126   let
   127     val (c, T) = lhs;
   128     val args = const_typargs lhs;
   129     val no_overloading = pure_args args;
   130     val rec_args = (case args of [Type (_, Ts)] => if pure_args Ts then Ts else [] | _ => []);
   131 
   132     val lhs' = make_item (c, args);
   133     val rhs' =
   134       if unchecked then []
   135       else rhs |> map_filter (fn (c', T') =>
   136         let val args' = const_typargs (c', T') in
   137           if gen_subset (op =) (args', rec_args) then NONE
   138           else SOME (make_item (c', if no_overloading_of defs c' then [] else args'))
   139         end);
   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     val insts' = insts |> fold (fn i as Instance (c, _) =>
   147         Symtab.insert_list (op =) (c, i) | _ => I) (lhs' :: rhs');
   148     val deps' = deps
   149       |> fold (Items.default_node o rpair ()) (lhs' :: rhs')
   150       |> Items.add_deps_acyclic (lhs', rhs')
   151       |> propagate_deps insts'
   152       handle Items.CYCLES cycles => error (cycle_msg cycles);
   153 
   154   in (specs', insts', deps') end);
   155 
   156 end;