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