src/Pure/defs.ML
author wenzelm
Thu May 25 16:51:39 2006 +0200 (2006-05-25)
changeset 19727 f5895f998402
parent 19713 69c71d40f8a8
child 19729 cb9e2f0c7658
permissions -rw-r--r--
tuned;
     1 (*  Title:      Pure/defs.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Global well-formedness checks for constant definitions.  Covers plain
     6 definitions and simple sub-structural overloading.
     7 *)
     8 
     9 signature DEFS =
    10 sig
    11   val pretty_const: Pretty.pp -> string * typ list -> Pretty.T
    12   val plain_args: typ list -> bool
    13   type T
    14   val specifications_of: T -> string -> (serial * {is_def: bool, module: string, name: string,
    15     lhs: typ list, rhs: (string * typ list) list}) list
    16   val dest: T ->
    17    {restricts: ((string * typ list) * string) list,
    18     reducts: ((string * typ list) * (string * typ list) list) list}
    19   val empty: T
    20   val merge: Pretty.pp -> T * T -> T
    21   val define: Pretty.pp -> bool -> bool -> string -> string ->
    22     string * typ list -> (string * typ list) list -> T -> T
    23 end
    24 
    25 structure Defs: DEFS =
    26 struct
    27 
    28 (* type arguments *)
    29 
    30 type args = typ list;
    31 
    32 fun pretty_const pp (c, args) =
    33   let
    34     val prt_args =
    35       if null args then []
    36       else [Pretty.list "(" ")" (map (Pretty.typ pp o Type.freeze_type) args)];
    37   in Pretty.block (Pretty.str c :: prt_args) end;
    38 
    39 fun plain_args args =
    40   forall Term.is_TVar args andalso not (has_duplicates (op =) args);
    41 
    42 fun disjoint_args (Ts, Us) =
    43   not (Type.could_unifys (Ts, Us)) orelse
    44     ((Type.raw_unifys (Ts, map (Logic.incr_tvar (maxidx_of_typs Ts + 1)) Us) Vartab.empty; false)
    45       handle Type.TUNIFY => true);
    46 
    47 fun match_args (Ts, Us) =
    48   Option.map Envir.typ_subst_TVars
    49     (SOME (Type.raw_matches (Ts, Us) Vartab.empty) handle Type.TYPE_MATCH => NONE);
    50 
    51 
    52 (* datatype defs *)
    53 
    54 type spec = {is_def: bool, module: string, name: string, lhs: args, rhs: (string * args) list};
    55 
    56 type def =
    57  {specs: spec Inttab.table,                 (*source specifications*)
    58   restricts: (args * string) list,          (*global restrictions imposed by incomplete patterns*)
    59   reducts: (args * (string * args) list) list};  (*specifications as reduction system*)
    60 
    61 fun make_def (specs, restricts, reducts) =
    62   {specs = specs, restricts = restricts, reducts = reducts}: def;
    63 
    64 fun map_def c f =
    65   Symtab.default (c, make_def (Inttab.empty, [], [])) #>
    66   Symtab.map_entry c (fn {specs, restricts, reducts}: def =>
    67     make_def (f (specs, restricts, reducts)));
    68 
    69 
    70 datatype T = Defs of def Symtab.table;
    71 
    72 fun lookup_list which defs c =
    73   (case Symtab.lookup defs c of
    74     SOME (def: def) => which def
    75   | NONE => []);
    76 
    77 fun specifications_of (Defs defs) = lookup_list (Inttab.dest o #specs) defs;
    78 val restricts_of = lookup_list #restricts;
    79 val reducts_of = lookup_list #reducts;
    80 
    81 fun dest (Defs defs) =
    82   let
    83     val restricts = Symtab.fold (fn (c, {restricts, ...}) =>
    84       fold (fn (args, name) => cons ((c, args), name)) restricts) defs [];
    85     val reducts = Symtab.fold (fn (c, {reducts, ...}) =>
    86       fold (fn (args, deps) => cons ((c, args), deps)) reducts) defs [];
    87   in {restricts = restricts, reducts = reducts} end;
    88 
    89 val empty = Defs Symtab.empty;
    90 
    91 
    92 (* specifications *)
    93 
    94 fun disjoint_specs c (i, {lhs = Ts, name = a, ...}: spec) =
    95   Inttab.forall (fn (j, {lhs = Us, name = b, ...}: spec) =>
    96     i = j orelse disjoint_args (Ts, Us) orelse
    97       error ("Type clash in specifications " ^ quote a ^ " and " ^ quote b ^
    98         " for constant " ^ quote c));
    99 
   100 fun join_specs c ({specs = specs1, restricts, reducts}, {specs = specs2, ...}: def) =
   101   let
   102     val specs' =
   103       Inttab.fold (fn spec2 => (disjoint_specs c spec2 specs1; Inttab.update spec2)) specs2 specs1;
   104   in make_def (specs', restricts, reducts) end;
   105 
   106 fun update_specs c spec = map_def c (fn (specs, restricts, reducts) =>
   107   (disjoint_specs c spec specs; (Inttab.update spec specs, restricts, reducts)));
   108 
   109 
   110 (* normalized dependencies: reduction with well-formedness check *)
   111 
   112 local
   113 
   114 fun contained (U as TVar _) (Type (_, Ts)) = exists (fn T => T = U orelse contained U T) Ts
   115   | contained _ _ = false;
   116 
   117 fun wellformed pp defs (c, args) (d, Us) =
   118   let
   119     val prt = Pretty.string_of o pretty_const pp;
   120     fun err s1 s2 =
   121       error (s1 ^ " dependency of constant " ^ prt (c, args) ^ " -> " ^ prt (d, Us) ^ s2);
   122   in
   123     exists (fn U => exists (contained U) args) Us orelse
   124     (c <> d andalso forall is_TVar Us) orelse
   125     (if c = d andalso is_some (match_args (args, Us)) then err "Circular" "" else
   126       (case find_first (fn (Ts, _) => not (disjoint_args (Ts, Us))) (restricts_of defs d) of
   127         SOME (Ts, name) =>
   128           is_some (match_args (Ts, Us)) orelse
   129           err "Malformed" ("\n(restriction " ^ prt (d, Ts) ^ " from " ^ quote name ^ ")")
   130       | NONE => true))
   131   end;
   132 
   133 fun reduction pp defs const deps =
   134   let
   135     fun reduct Us (Ts, rhs) =
   136       (case match_args (Ts, Us) of
   137         NONE => NONE
   138       | SOME subst => SOME (map (apsnd (map subst)) rhs));
   139     fun reducts (d, Us) = get_first (reduct Us) (reducts_of defs d);
   140 
   141     fun add (NONE, dp) = insert (op =) dp
   142       | add (SOME dps, _) = fold (insert (op =)) dps;
   143     val reds = map (`reducts) deps;
   144     val deps' =
   145       if forall (is_none o #1) reds then NONE
   146       else SOME (fold_rev add reds []);
   147     val _ = forall (wellformed pp defs const) (the_default deps deps');
   148   in deps' end;
   149 
   150 fun normalize pp =
   151   let
   152     fun norm_update (c, {reducts, ...}: def) (changed, defs) =
   153       let
   154         val reducts' = reducts |> map (fn (args, deps) =>
   155           (args, perhaps (reduction pp defs (c, args)) deps));
   156       in
   157         if reducts = reducts' then (changed, defs)
   158         else (true, defs |> map_def c (fn (specs, restricts, reducts) =>
   159           (specs, restricts, reducts')))
   160       end;
   161     fun norm_all defs =
   162       (case Symtab.fold norm_update defs (false, defs) of
   163         (true, defs') => norm_all defs'
   164       | (false, _) => defs);
   165   in norm_all end;
   166 
   167 in
   168 
   169 fun dependencies pp (c, args) restr deps =
   170   map_def c (fn (specs, restricts, reducts) =>
   171     let
   172       val restricts' = Library.merge (op =) (restricts, restr);
   173       val reducts' = insert (op =) (args, deps) reducts;
   174     in (specs, restricts', reducts') end)
   175   #> normalize pp;
   176 
   177 end;
   178 
   179 
   180 (* merge *)
   181 
   182 fun merge pp (Defs defs1, Defs defs2) =
   183   let
   184     fun add_deps (c, args) restr deps defs =
   185       if AList.defined (op =) (reducts_of defs c) args then defs
   186       else dependencies pp (c, args) restr deps defs;
   187     fun add_def (c, {restricts, reducts, ...}: def) =
   188       fold (fn (args, deps) => add_deps (c, args) restricts deps) reducts;
   189   in Defs (Symtab.join join_specs (defs1, defs2) |> Symtab.fold add_def defs2) end;
   190 
   191 
   192 (* define *)
   193 
   194 fun define pp unchecked is_def module name (c, args) deps (Defs defs) =
   195   let
   196     val restr =
   197       if plain_args args orelse
   198         (case args of [Type (a, rec_args)] => plain_args rec_args | _ => false)
   199       then [] else [(args, name)];
   200     val spec =
   201       (serial (), {is_def = is_def, module = module, name = name, lhs = args, rhs = deps});
   202     val defs' = defs |> update_specs c spec;
   203   in Defs (defs' |> (if unchecked then I else dependencies pp (c, args) restr deps)) end;
   204 
   205 end;