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