src/Pure/defs.ML
author wenzelm
Wed Jun 07 02:01:28 2006 +0200 (2006-06-07)
changeset 19806 f860b7a98445
parent 19760 c7e9cc10acc8
child 20390 c80247278cb6
permissions -rw-r--r--
renamed Type.(un)varifyT to Logic.(un)varifyT;
made (un)varify strict wrt. global context -- may use legacy_(un)varify as workaround;
     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 Logic.unvarifyT) 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 val prt = Pretty.string_of oo pretty_const;
   115 fun err pp (c, args) (d, Us) s1 s2 =
   116   error (s1 ^ " dependency of constant " ^ prt pp (c, args) ^ " -> " ^ prt pp (d, Us) ^ s2);
   117 
   118 fun contained (U as TVar _) (Type (_, Ts)) = exists (fn T => T = U orelse contained U T) Ts
   119   | contained _ _ = false;
   120 
   121 fun acyclic pp defs (c, args) (d, Us) =
   122   c <> d orelse
   123   exists (fn U => exists (contained U) args) Us orelse
   124   is_none (match_args (args, Us)) orelse
   125   err pp (c, args) (d, Us) "Circular" "";
   126 
   127 fun wellformed pp defs (c, args) (d, Us) =
   128   forall is_TVar Us orelse
   129   (case find_first (fn (Ts, _) => not (disjoint_args (Ts, Us))) (restricts_of defs d) of
   130     SOME (Ts, name) =>
   131       err pp (c, args) (d, Us) "Malformed"
   132         ("\n(restriction " ^ prt pp (d, Ts) ^ " from " ^ quote name ^ ")")
   133   | NONE => true);
   134 
   135 fun reduction pp defs const deps =
   136   let
   137     fun reduct Us (Ts, rhs) =
   138       (case match_args (Ts, Us) of
   139         NONE => NONE
   140       | SOME subst => SOME (map (apsnd (map subst)) rhs));
   141     fun reducts (d, Us) = get_first (reduct Us) (reducts_of defs d);
   142 
   143     fun add (NONE, dp) = insert (op =) dp
   144       | add (SOME dps, _) = fold (insert (op =)) dps;
   145     val reds = map (`reducts) deps;
   146     val deps' =
   147       if forall (is_none o #1) reds then NONE
   148       else SOME (fold_rev add reds []);
   149     val _ = forall (acyclic pp defs const) (the_default deps deps');
   150   in deps' end;
   151 
   152 in
   153 
   154 fun normalize pp =
   155   let
   156     fun norm_update (c, {reducts, ...}: def) (changed, defs) =
   157       let
   158         val reducts' = reducts |> map (fn (args, deps) =>
   159           (args, perhaps (reduction pp defs (c, args)) deps));
   160       in
   161         if reducts = reducts' then (changed, defs)
   162         else (true, defs |> map_def c (fn (specs, restricts, reducts) =>
   163           (specs, restricts, reducts')))
   164       end;
   165     fun norm_all defs =
   166       (case Symtab.fold norm_update defs (false, defs) of
   167         (true, defs') => norm_all defs'
   168       | (false, _) => defs);
   169     fun check defs (c, {reducts, ...}: def) =
   170       reducts |> forall (fn (args, deps) => forall (wellformed pp defs (c, args)) deps);
   171   in norm_all #> (fn defs => tap (Symtab.forall (check defs)) defs) end;
   172 
   173 fun dependencies pp (c, args) restr deps =
   174   map_def c (fn (specs, restricts, reducts) =>
   175     let
   176       val restricts' = Library.merge (op =) (restricts, restr);
   177       val reducts' = insert (op =) (args, deps) reducts;
   178     in (specs, restricts', reducts') end)
   179   #> normalize pp;
   180 
   181 end;
   182 
   183 
   184 (* merge *)
   185 
   186 fun merge pp (Defs defs1, Defs defs2) =
   187   let
   188     fun add_deps (c, args) restr deps defs =
   189       if AList.defined (op =) (reducts_of defs c) args then defs
   190       else dependencies pp (c, args) restr deps defs;
   191     fun add_def (c, {restricts, reducts, ...}: def) =
   192       fold (fn (args, deps) => add_deps (c, args) restricts deps) reducts;
   193   in
   194     Defs (Symtab.join join_specs (defs1, defs2)
   195       |> normalize pp |> Symtab.fold add_def defs2)
   196   end;
   197 
   198 
   199 (* define *)
   200 
   201 fun define pp unchecked is_def module name (c, args) deps (Defs defs) =
   202   let
   203     val restr =
   204       if plain_args args orelse
   205         (case args of [Type (a, rec_args)] => plain_args rec_args | _ => false)
   206       then [] else [(args, name)];
   207     val spec =
   208       (serial (), {is_def = is_def, module = module, name = name, lhs = args, rhs = deps});
   209     val defs' = defs |> update_specs c spec;
   210   in Defs (defs' |> (if unchecked then I else dependencies pp (c, args) restr deps)) end;
   211 
   212 end;