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