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