src/Pure/type.ML
author wenzelm
Sat Nov 21 15:49:29 2009 +0100 (2009-11-21)
changeset 33832 cff42395c246
parent 33537 06c87d2c5b5a
child 33941 40408e6b833b
permissions -rw-r--r--
explicitly mark some legacy freeze operations;
wenzelm@256
     1
(*  Title:      Pure/type.ML
wenzelm@14790
     2
    Author:     Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel
clasohm@0
     3
wenzelm@14790
     4
Type signatures and certified types, special treatment of type vars,
wenzelm@14790
     5
matching and unification of types, extend and merge type signatures.
clasohm@0
     6
*)
clasohm@0
     7
clasohm@0
     8
signature TYPE =
wenzelm@2964
     9
sig
wenzelm@14790
    10
  (*type signatures and certified types*)
wenzelm@14790
    11
  datatype decl =
wenzelm@14790
    12
    LogicalType of int |
wenzelm@14989
    13
    Abbreviation of string list * typ * bool |
wenzelm@14790
    14
    Nonterminal
wenzelm@14790
    15
  type tsig
wenzelm@14790
    16
  val rep_tsig: tsig ->
wenzelm@33095
    17
   {classes: Name_Space.T * Sorts.algebra,
wenzelm@14790
    18
    default: sort,
wenzelm@33173
    19
    types: decl Name_Space.table,
wenzelm@26641
    20
    log_types: string list}
wenzelm@14790
    21
  val empty_tsig: tsig
wenzelm@14790
    22
  val defaultS: tsig -> sort
wenzelm@14790
    23
  val logical_types: tsig -> string list
wenzelm@14790
    24
  val eq_sort: tsig -> sort * sort -> bool
wenzelm@14790
    25
  val subsort: tsig -> sort * sort -> bool
wenzelm@14790
    26
  val of_sort: tsig -> typ * sort -> bool
wenzelm@19464
    27
  val inter_sort: tsig -> sort * sort -> sort
wenzelm@14790
    28
  val cert_class: tsig -> class -> class
wenzelm@14790
    29
  val cert_sort: tsig -> sort -> sort
wenzelm@31946
    30
  val witness_sorts: tsig -> (typ * sort) list -> sort list -> (typ * sort) list
wenzelm@24274
    31
  type mode
wenzelm@24274
    32
  val mode_default: mode
wenzelm@24274
    33
  val mode_syntax: mode
wenzelm@24274
    34
  val mode_abbrev: mode
wenzelm@24484
    35
  val get_mode: Proof.context -> mode
wenzelm@24484
    36
  val set_mode: mode -> Proof.context -> Proof.context
wenzelm@24484
    37
  val restore_mode: Proof.context -> Proof.context -> Proof.context
wenzelm@24274
    38
  val cert_typ_mode: mode -> tsig -> typ -> typ
wenzelm@14993
    39
  val cert_typ: tsig -> typ -> typ
wenzelm@19464
    40
  val arity_number: tsig -> string -> int
wenzelm@19464
    41
  val arity_sorts: Pretty.pp -> tsig -> string -> sort -> sort list
wenzelm@14790
    42
wenzelm@14790
    43
  (*special treatment of type vars*)
wenzelm@14790
    44
  val strip_sorts: typ -> typ
wenzelm@621
    45
  val no_tvars: typ -> typ
haftmann@21116
    46
  val varify: (string * sort) list -> term -> ((string * sort) * indexname) list * term
wenzelm@33832
    47
  val legacy_freeze_thaw_type: typ -> typ * (typ -> typ)
wenzelm@33832
    48
  val legacy_freeze_type: typ -> typ
wenzelm@33832
    49
  val legacy_freeze_thaw: term -> term * (term -> term)
wenzelm@33832
    50
  val legacy_freeze: term -> term
wenzelm@2964
    51
wenzelm@14790
    52
  (*matching and unification*)
wenzelm@2964
    53
  exception TYPE_MATCH
wenzelm@25324
    54
  type tyenv = (sort * typ) Vartab.table
haftmann@26327
    55
  val lookup: tyenv -> indexname * sort -> typ option
paulson@32648
    56
  val devar: tyenv -> typ -> typ
wenzelm@16946
    57
  val typ_match: tsig -> typ * typ -> tyenv -> tyenv
wenzelm@14790
    58
  val typ_instance: tsig -> typ * typ -> bool
wenzelm@16946
    59
  val raw_match: typ * typ -> tyenv -> tyenv
wenzelm@19694
    60
  val raw_matches: typ list * typ list -> tyenv -> tyenv
wenzelm@16946
    61
  val raw_instance: typ * typ -> bool
wenzelm@2964
    62
  exception TUNIFY
wenzelm@16946
    63
  val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int
wenzelm@16946
    64
  val raw_unify: typ * typ -> tyenv -> tyenv
wenzelm@19696
    65
  val raw_unifys: typ list * typ list -> tyenv -> tyenv
wenzelm@17804
    66
  val could_unify: typ * typ -> bool
wenzelm@19696
    67
  val could_unifys: typ list * typ list -> bool
berghofe@16650
    68
  val eq_type: tyenv -> typ * typ -> bool
clasohm@0
    69
wenzelm@14790
    70
  (*extend and merge type signatures*)
wenzelm@33095
    71
  val add_class: Pretty.pp -> Name_Space.naming -> binding * class list -> tsig -> tsig
wenzelm@26669
    72
  val hide_class: bool -> string -> tsig -> tsig
wenzelm@14790
    73
  val set_defsort: sort -> tsig -> tsig
wenzelm@33173
    74
  val add_type: Name_Space.naming -> binding * int -> tsig -> tsig
wenzelm@33173
    75
  val add_abbrev: Name_Space.naming -> binding * string list * typ -> tsig -> tsig
wenzelm@33173
    76
  val add_nonterminal: Name_Space.naming -> binding -> tsig -> tsig
wenzelm@26669
    77
  val hide_type: bool -> string -> tsig -> tsig
wenzelm@19515
    78
  val add_arity: Pretty.pp -> arity -> tsig -> tsig
wenzelm@19515
    79
  val add_classrel: Pretty.pp -> class * class -> tsig -> tsig
wenzelm@14830
    80
  val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig
wenzelm@2964
    81
end;
wenzelm@2964
    82
wenzelm@2964
    83
structure Type: TYPE =
clasohm@0
    84
struct
clasohm@0
    85
wenzelm@14790
    86
(** type signatures and certified types **)
wenzelm@2964
    87
wenzelm@14790
    88
(* type declarations *)
wenzelm@14790
    89
wenzelm@14790
    90
datatype decl =
wenzelm@14790
    91
  LogicalType of int |
wenzelm@14989
    92
  Abbreviation of string list * typ * bool |
wenzelm@14790
    93
  Nonterminal;
wenzelm@14790
    94
wenzelm@14790
    95
wenzelm@14790
    96
(* type tsig *)
wenzelm@14790
    97
wenzelm@14790
    98
datatype tsig =
wenzelm@14790
    99
  TSig of {
wenzelm@33095
   100
    classes: Name_Space.T * Sorts.algebra,  (*order-sorted algebra of type classes*)
wenzelm@16370
   101
    default: sort,                          (*default sort on input*)
wenzelm@33173
   102
    types: decl Name_Space.table,           (*declared types*)
wenzelm@26641
   103
    log_types: string list};                (*logical types sorted by number of arguments*)
wenzelm@14790
   104
wenzelm@14790
   105
fun rep_tsig (TSig comps) = comps;
wenzelm@14790
   106
wenzelm@26641
   107
fun make_tsig (classes, default, types, log_types) =
wenzelm@26641
   108
  TSig {classes = classes, default = default, types = types, log_types = log_types};
wenzelm@14790
   109
wenzelm@33094
   110
fun build_tsig (classes, default, types) =
wenzelm@14790
   111
  let
wenzelm@14790
   112
    val log_types =
wenzelm@33173
   113
      Symtab.fold (fn (c, LogicalType n) => cons (c, n) | _ => I) (snd types) []
wenzelm@33094
   114
      |> Library.sort (int_ord o pairself snd) |> map fst;
wenzelm@33094
   115
  in make_tsig (classes, default, types, log_types) end;
wenzelm@14790
   116
wenzelm@26641
   117
fun map_tsig f (TSig {classes, default, types, log_types = _}) =
wenzelm@19642
   118
  build_tsig (f (classes, default, types));
wenzelm@14790
   119
wenzelm@16370
   120
val empty_tsig =
wenzelm@33096
   121
  build_tsig ((Name_Space.empty "class", Sorts.empty_algebra), [], Name_Space.empty_table "type");
wenzelm@14790
   122
wenzelm@14790
   123
wenzelm@14790
   124
(* classes and sorts *)
wenzelm@14790
   125
wenzelm@14790
   126
fun defaultS (TSig {default, ...}) = default;
wenzelm@14790
   127
fun logical_types (TSig {log_types, ...}) = log_types;
wenzelm@14790
   128
wenzelm@16370
   129
fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes);
wenzelm@16370
   130
fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
wenzelm@19642
   131
fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes);
wenzelm@19464
   132
fun inter_sort (TSig {classes, ...}) = Sorts.inter_sort (#2 classes);
wenzelm@14790
   133
wenzelm@19642
   134
fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes);
wenzelm@19642
   135
fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 classes);
wenzelm@19515
   136
wenzelm@32784
   137
fun witness_sorts (TSig {classes, log_types, ...}) =
wenzelm@19642
   138
  Sorts.witness_sorts (#2 classes) log_types;
wenzelm@14790
   139
wenzelm@14790
   140
wenzelm@24274
   141
(* certification mode *)
wenzelm@24274
   142
wenzelm@24274
   143
datatype mode = Mode of {normalize: bool, logical: bool};
wenzelm@24274
   144
wenzelm@24274
   145
val mode_default = Mode {normalize = true, logical = true};
wenzelm@24274
   146
val mode_syntax = Mode {normalize = true, logical = false};
wenzelm@24274
   147
val mode_abbrev = Mode {normalize = false, logical = false};
wenzelm@24274
   148
wenzelm@33519
   149
structure Mode = Proof_Data
wenzelm@24484
   150
(
wenzelm@24484
   151
  type T = mode;
wenzelm@24484
   152
  fun init _ = mode_default;
wenzelm@24484
   153
);
wenzelm@24484
   154
wenzelm@24484
   155
val get_mode = Mode.get;
wenzelm@24484
   156
fun set_mode mode = Mode.map (K mode);
wenzelm@24484
   157
fun restore_mode ctxt = set_mode (get_mode ctxt);
wenzelm@24484
   158
wenzelm@24274
   159
wenzelm@27313
   160
(* lookup types *)
wenzelm@27313
   161
wenzelm@27313
   162
fun undecl_type c = "Undeclared type constructor: " ^ quote c;
wenzelm@27313
   163
wenzelm@33094
   164
fun lookup_type (TSig {types = (_, types), ...}) = Symtab.lookup types;
wenzelm@27313
   165
wenzelm@27313
   166
wenzelm@14790
   167
(* certified types *)
wenzelm@14790
   168
wenzelm@14790
   169
fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
wenzelm@14830
   170
wenzelm@14998
   171
local
wenzelm@14998
   172
wenzelm@14998
   173
fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts)
wenzelm@18957
   174
  | inst_typ env (T as TFree (x, _)) = the_default T (AList.lookup (op =) env x)
wenzelm@14998
   175
  | inst_typ _ T = T;
wenzelm@14998
   176
wenzelm@24274
   177
in
wenzelm@24274
   178
wenzelm@24274
   179
fun cert_typ_mode (Mode {normalize, logical}) tsig ty =
wenzelm@14790
   180
  let
wenzelm@14790
   181
    fun err msg = raise TYPE (msg, [ty], []);
wenzelm@14790
   182
wenzelm@24274
   183
    val check_logical =
wenzelm@24274
   184
      if logical then fn c => err ("Illegal occurrence of syntactic type: " ^ quote c)
wenzelm@24274
   185
      else fn _ => ();
wenzelm@14989
   186
wenzelm@14989
   187
    fun cert (T as Type (c, Ts)) =
wenzelm@14989
   188
          let
wenzelm@14989
   189
            val Ts' = map cert Ts;
wenzelm@14989
   190
            fun nargs n = if length Ts <> n then err (bad_nargs c) else ();
wenzelm@14989
   191
          in
haftmann@27302
   192
            (case lookup_type tsig c of
wenzelm@33173
   193
              SOME (LogicalType n) => (nargs n; Type (c, Ts'))
wenzelm@33173
   194
            | SOME (Abbreviation (vs, U, syn)) =>
wenzelm@24274
   195
               (nargs (length vs);
wenzelm@24274
   196
                if syn then check_logical c else ();
wenzelm@14993
   197
                if normalize then inst_typ (vs ~~ Ts') U
wenzelm@14989
   198
                else Type (c, Ts'))
wenzelm@33173
   199
            | SOME Nonterminal => (nargs 0; check_logical c; T)
skalberg@15531
   200
            | NONE => err (undecl_type c))
wenzelm@14790
   201
          end
wenzelm@19515
   202
      | cert (TFree (x, S)) = TFree (x, cert_sort tsig S)
wenzelm@14989
   203
      | cert (TVar (xi as (_, i), S)) =
wenzelm@14993
   204
          if i < 0 then
wenzelm@14993
   205
            err ("Malformed type variable: " ^ quote (Term.string_of_vname xi))
wenzelm@19515
   206
          else TVar (xi, cert_sort tsig S);
wenzelm@14790
   207
wenzelm@14989
   208
    val ty' = cert ty;
wenzelm@14993
   209
  in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
wenzelm@14830
   210
wenzelm@24274
   211
val cert_typ = cert_typ_mode mode_default;
wenzelm@14790
   212
wenzelm@14998
   213
end;
wenzelm@14998
   214
wenzelm@14790
   215
wenzelm@19464
   216
(* type arities *)
wenzelm@19464
   217
haftmann@27302
   218
fun arity_number tsig a =
haftmann@27302
   219
  (case lookup_type tsig a of
wenzelm@33173
   220
    SOME (LogicalType n) => n
wenzelm@19464
   221
  | _ => error (undecl_type a));
wenzelm@19464
   222
wenzelm@19464
   223
fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) []
wenzelm@19642
   224
  | arity_sorts pp (TSig {classes, ...}) a S = Sorts.mg_domain (#2 classes) a S
wenzelm@26641
   225
      handle Sorts.CLASS_ERROR err => error (Sorts.class_error pp err);
wenzelm@19464
   226
wenzelm@19464
   227
wenzelm@14790
   228
wenzelm@14790
   229
(** special treatment of type vars **)
wenzelm@14790
   230
wenzelm@14790
   231
(* strip_sorts *)
wenzelm@14790
   232
wenzelm@14790
   233
fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts)
wenzelm@14790
   234
  | strip_sorts (TFree (x, _)) = TFree (x, [])
wenzelm@14790
   235
  | strip_sorts (TVar (xi, _)) = TVar (xi, []);
wenzelm@14790
   236
wenzelm@14790
   237
wenzelm@14790
   238
(* no_tvars *)
wenzelm@621
   239
wenzelm@621
   240
fun no_tvars T =
wenzelm@29275
   241
  (case Term.add_tvarsT T [] of [] => T
wenzelm@12501
   242
  | vs => raise TYPE ("Illegal schematic type variable(s): " ^
wenzelm@29275
   243
      commas_quote (map (Term.string_of_vname o #1) (rev vs)), [T], []));
wenzelm@621
   244
wenzelm@7641
   245
wenzelm@19806
   246
(* varify *)
wenzelm@621
   247
haftmann@21116
   248
fun varify fixed t =
wenzelm@621
   249
  let
wenzelm@19305
   250
    val fs = Term.fold_types (Term.fold_atyps
wenzelm@19305
   251
      (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t [];
wenzelm@29260
   252
    val used = Name.context
wenzelm@29260
   253
      |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t;
wenzelm@29260
   254
    val fmap = fs ~~ map (rpair 0) (#1 (Name.variants (map fst fs) used));
wenzelm@32784
   255
    fun thaw (f as (_, S)) =
wenzelm@17184
   256
      (case AList.lookup (op =) fmap f of
skalberg@15531
   257
        NONE => TFree f
wenzelm@16946
   258
      | SOME xi => TVar (xi, S));
haftmann@21116
   259
  in (fmap, map_types (map_type_tfree thaw) t) end;
wenzelm@2964
   260
wenzelm@2964
   261
wenzelm@7641
   262
(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *)
paulson@3411
   263
wenzelm@7641
   264
local
wenzelm@7641
   265
wenzelm@16289
   266
fun new_name (ix, (pairs, used)) =
wenzelm@20071
   267
  let val v = Name.variant used (string_of_indexname ix)
wenzelm@16289
   268
  in ((ix, v) :: pairs, v :: used) end;
wenzelm@621
   269
wenzelm@16289
   270
fun freeze_one alist (ix, sort) =
wenzelm@17184
   271
  TFree (the (AList.lookup (op =) alist ix), sort)
skalberg@15531
   272
    handle Option =>
wenzelm@3790
   273
      raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
wenzelm@2964
   274
wenzelm@17184
   275
fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort)
wenzelm@16289
   276
  handle Option => TFree (a, sort);
wenzelm@416
   277
wenzelm@10495
   278
in
wenzelm@10495
   279
wenzelm@33832
   280
fun legacy_freeze_thaw_type T =
wenzelm@7641
   281
  let
wenzelm@29275
   282
    val used = OldTerm.add_typ_tfree_names (T, [])
wenzelm@29275
   283
    and tvars = map #1 (OldTerm.add_typ_tvars (T, []));
wenzelm@23178
   284
    val (alist, _) = List.foldr new_name ([], used) tvars;
wenzelm@7641
   285
  in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;
wenzelm@7641
   286
wenzelm@33832
   287
val legacy_freeze_type = #1 o legacy_freeze_thaw_type;
wenzelm@16289
   288
wenzelm@33832
   289
fun legacy_freeze_thaw t =
wenzelm@7641
   290
  let
wenzelm@29275
   291
    val used = OldTerm.it_term_types OldTerm.add_typ_tfree_names (t, [])
wenzelm@29275
   292
    and tvars = map #1 (OldTerm.it_term_types OldTerm.add_typ_tvars (t, []));
wenzelm@23178
   293
    val (alist, _) = List.foldr new_name ([], used) tvars;
wenzelm@7641
   294
  in
wenzelm@7641
   295
    (case alist of
wenzelm@7641
   296
      [] => (t, fn x => x) (*nothing to do!*)
wenzelm@20548
   297
    | _ => (map_types (map_type_tvar (freeze_one alist)) t,
wenzelm@20548
   298
      map_types (map_type_tfree (thaw_one (map swap alist)))))
wenzelm@7641
   299
  end;
wenzelm@7641
   300
wenzelm@33832
   301
val legacy_freeze = #1 o legacy_freeze_thaw;
wenzelm@16289
   302
wenzelm@7641
   303
end;
wenzelm@7641
   304
wenzelm@256
   305
wenzelm@256
   306
wenzelm@14790
   307
(** matching and unification of types **)
wenzelm@8899
   308
berghofe@15797
   309
type tyenv = (sort * typ) Vartab.table;
wenzelm@256
   310
berghofe@15797
   311
fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
berghofe@15797
   312
  quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
berghofe@15797
   313
  [TVar (ixn, S), TVar (ixn, S')], []);
clasohm@0
   314
haftmann@26327
   315
fun lookup tye (ixn, S) =
wenzelm@17412
   316
  (case Vartab.lookup tye ixn of
berghofe@15797
   317
    NONE => NONE
wenzelm@16289
   318
  | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');
wenzelm@16289
   319
clasohm@0
   320
wenzelm@14790
   321
(* matching *)
clasohm@0
   322
wenzelm@2964
   323
exception TYPE_MATCH;
clasohm@0
   324
wenzelm@16946
   325
fun typ_match tsig =
wenzelm@2964
   326
  let
wenzelm@16340
   327
    fun match (TVar (v, S), T) subs =
haftmann@26327
   328
          (case lookup subs (v, S) of
skalberg@15531
   329
            NONE =>
wenzelm@17412
   330
              if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs
wenzelm@14993
   331
              else raise TYPE_MATCH
skalberg@15531
   332
          | SOME U => if U = T then subs else raise TYPE_MATCH)
wenzelm@16340
   333
      | match (Type (a, Ts), Type (b, Us)) subs =
wenzelm@2964
   334
          if a <> b then raise TYPE_MATCH
wenzelm@16885
   335
          else matches (Ts, Us) subs
wenzelm@16340
   336
      | match (TFree x, TFree y) subs =
wenzelm@2964
   337
          if x = y then subs else raise TYPE_MATCH
wenzelm@16885
   338
      | match _ _ = raise TYPE_MATCH
wenzelm@16885
   339
    and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs)
wenzelm@16885
   340
      | matches _ subs = subs;
wenzelm@16946
   341
  in match end;
clasohm@0
   342
wenzelm@14790
   343
fun typ_instance tsig (T, U) =
wenzelm@16946
   344
  (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
wenzelm@16946
   345
wenzelm@16946
   346
(*purely structural matching*)
wenzelm@16946
   347
fun raw_match (TVar (v, S), T) subs =
haftmann@26327
   348
      (case lookup subs (v, S) of
wenzelm@17412
   349
        NONE => Vartab.update_new (v, (S, T)) subs
wenzelm@16946
   350
      | SOME U => if U = T then subs else raise TYPE_MATCH)
wenzelm@16946
   351
  | raw_match (Type (a, Ts), Type (b, Us)) subs =
wenzelm@16946
   352
      if a <> b then raise TYPE_MATCH
wenzelm@16946
   353
      else raw_matches (Ts, Us) subs
wenzelm@16946
   354
  | raw_match (TFree x, TFree y) subs =
wenzelm@16946
   355
      if x = y then subs else raise TYPE_MATCH
wenzelm@16946
   356
  | raw_match _ _ = raise TYPE_MATCH
wenzelm@16946
   357
and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs)
wenzelm@19694
   358
  | raw_matches ([], []) subs = subs
wenzelm@19694
   359
  | raw_matches _ _ = raise TYPE_MATCH;
wenzelm@16946
   360
wenzelm@16946
   361
fun raw_instance (T, U) =
wenzelm@16946
   362
  (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
wenzelm@2964
   363
clasohm@0
   364
wenzelm@14790
   365
(* unification *)
wenzelm@2964
   366
clasohm@0
   367
exception TUNIFY;
clasohm@0
   368
wenzelm@26641
   369
(*occurs check*)
wenzelm@2964
   370
fun occurs v tye =
wenzelm@2964
   371
  let
wenzelm@2964
   372
    fun occ (Type (_, Ts)) = exists occ Ts
wenzelm@2964
   373
      | occ (TFree _) = false
berghofe@15797
   374
      | occ (TVar (w, S)) =
wenzelm@29269
   375
          Term.eq_ix (v, w) orelse
haftmann@26327
   376
            (case lookup tye (w, S) of
skalberg@15531
   377
              NONE => false
skalberg@15531
   378
            | SOME U => occ U);
clasohm@0
   379
  in occ end;
clasohm@0
   380
wenzelm@14790
   381
(*chase variable assignments; if devar returns a type var then it must be unassigned*)
wenzelm@16885
   382
fun devar tye (T as TVar v) =
haftmann@26327
   383
      (case lookup tye v of
wenzelm@16885
   384
        SOME U => devar tye U
skalberg@15531
   385
      | NONE => T)
wenzelm@32784
   386
  | devar _ T = T;
clasohm@0
   387
wenzelm@17804
   388
(*order-sorted unification*)
wenzelm@32784
   389
fun unify (TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
wenzelm@2964
   390
  let
wenzelm@32738
   391
    val tyvar_count = Unsynchronized.ref maxidx;
wenzelm@32738
   392
    fun gen_tyvar S = TVar ((Name.aT, Unsynchronized.inc tyvar_count), S);
wenzelm@2964
   393
wenzelm@19642
   394
    fun mg_domain a S = Sorts.mg_domain classes a S
wenzelm@19642
   395
      handle Sorts.CLASS_ERROR _ => raise TUNIFY;
wenzelm@2964
   396
wenzelm@16885
   397
    fun meet (_, []) tye = tye
wenzelm@16885
   398
      | meet (TVar (xi, S'), S) tye =
wenzelm@14790
   399
          if Sorts.sort_le classes (S', S) then tye
wenzelm@17412
   400
          else Vartab.update_new
wenzelm@17221
   401
            (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye
wenzelm@16885
   402
      | meet (TFree (_, S'), S) tye =
wenzelm@14790
   403
          if Sorts.sort_le classes (S', S) then tye
wenzelm@2964
   404
          else raise TUNIFY
wenzelm@16885
   405
      | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye
wenzelm@16885
   406
    and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye)
wenzelm@16885
   407
      | meets _ tye = tye;
wenzelm@2964
   408
wenzelm@16885
   409
    fun unif (ty1, ty2) tye =
wenzelm@16885
   410
      (case (devar tye ty1, devar tye ty2) of
wenzelm@2964
   411
        (T as TVar (v, S1), U as TVar (w, S2)) =>
wenzelm@29269
   412
          if Term.eq_ix (v, w) then
berghofe@15797
   413
            if S1 = S2 then tye else tvar_clash v S1 S2
wenzelm@14790
   414
          else if Sorts.sort_le classes (S1, S2) then
wenzelm@17412
   415
            Vartab.update_new (w, (S2, T)) tye
wenzelm@14790
   416
          else if Sorts.sort_le classes (S2, S1) then
wenzelm@17412
   417
            Vartab.update_new (v, (S1, U)) tye
wenzelm@2964
   418
          else
wenzelm@14790
   419
            let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
wenzelm@17412
   420
              Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye)
wenzelm@2964
   421
            end
wenzelm@2964
   422
      | (TVar (v, S), T) =>
wenzelm@2964
   423
          if occurs v tye T then raise TUNIFY
wenzelm@17412
   424
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
wenzelm@2964
   425
      | (T, TVar (v, S)) =>
wenzelm@2964
   426
          if occurs v tye T then raise TUNIFY
wenzelm@17412
   427
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
wenzelm@2964
   428
      | (Type (a, Ts), Type (b, Us)) =>
wenzelm@2964
   429
          if a <> b then raise TUNIFY
wenzelm@16885
   430
          else unifs (Ts, Us) tye
wenzelm@16885
   431
      | (T, U) => if T = U then tye else raise TUNIFY)
wenzelm@16885
   432
    and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye)
wenzelm@16885
   433
      | unifs _ tye = tye;
wenzelm@16885
   434
  in (unif TU tyenv, ! tyvar_count) end;
clasohm@0
   435
wenzelm@16946
   436
(*purely structural unification*)
wenzelm@16946
   437
fun raw_unify (ty1, ty2) tye =
wenzelm@16946
   438
  (case (devar tye ty1, devar tye ty2) of
wenzelm@32784
   439
    (T as TVar (v, S1), TVar (w, S2)) =>
wenzelm@29269
   440
      if Term.eq_ix (v, w) then
wenzelm@16946
   441
        if S1 = S2 then tye else tvar_clash v S1 S2
wenzelm@17412
   442
      else Vartab.update_new (w, (S2, T)) tye
wenzelm@16946
   443
  | (TVar (v, S), T) =>
wenzelm@16946
   444
      if occurs v tye T then raise TUNIFY
wenzelm@17412
   445
      else Vartab.update_new (v, (S, T)) tye
wenzelm@16946
   446
  | (T, TVar (v, S)) =>
wenzelm@16946
   447
      if occurs v tye T then raise TUNIFY
wenzelm@17412
   448
      else Vartab.update_new (v, (S, T)) tye
wenzelm@16946
   449
  | (Type (a, Ts), Type (b, Us)) =>
wenzelm@16946
   450
      if a <> b then raise TUNIFY
wenzelm@16946
   451
      else raw_unifys (Ts, Us) tye
wenzelm@16946
   452
  | (T, U) => if T = U then tye else raise TUNIFY)
wenzelm@16946
   453
and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye)
wenzelm@19696
   454
  | raw_unifys ([], []) tye = tye
wenzelm@19696
   455
  | raw_unifys _ _ = raise TUNIFY;
wenzelm@16946
   456
wenzelm@17804
   457
(*fast unification filter*)
wenzelm@17804
   458
fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us)
wenzelm@17804
   459
  | could_unify (TFree (a, _), TFree (b, _)) = a = b
wenzelm@17804
   460
  | could_unify (TVar _, _) = true
wenzelm@17804
   461
  | could_unify (_, TVar _) = true
wenzelm@17804
   462
  | could_unify _ = false
wenzelm@17804
   463
and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us)
wenzelm@19696
   464
  | could_unifys ([], []) = true
wenzelm@19696
   465
  | could_unifys _ = false;
wenzelm@450
   466
wenzelm@17804
   467
wenzelm@17804
   468
(*equality with respect to a type environment*)
wenzelm@32030
   469
fun equal_type tye (T, T') =
wenzelm@16885
   470
  (case (devar tye T, devar tye T') of
berghofe@16650
   471
     (Type (s, Ts), Type (s', Ts')) =>
wenzelm@32030
   472
       s = s' andalso ListPair.all (equal_type tye) (Ts, Ts')
berghofe@16650
   473
   | (U, U') => U = U');
berghofe@16650
   474
wenzelm@32030
   475
fun eq_type tye =
wenzelm@32030
   476
  if Vartab.is_empty tye then op = else equal_type tye;
wenzelm@32030
   477
wenzelm@450
   478
clasohm@0
   479
wenzelm@14790
   480
(** extend and merge type signatures **)
wenzelm@14790
   481
wenzelm@14790
   482
(* classes *)
wenzelm@14790
   483
wenzelm@16370
   484
fun add_class pp naming (c, cs) tsig =
wenzelm@19642
   485
  tsig |> map_tsig (fn ((space, classes), default, types) =>
wenzelm@16370
   486
    let
wenzelm@16370
   487
      val cs' = map (cert_class tsig) cs
wenzelm@16370
   488
        handle TYPE (msg, _, _) => error msg;
wenzelm@33095
   489
      val (c', space') = space |> Name_Space.declare true naming c;
wenzelm@19515
   490
      val classes' = classes |> Sorts.add_class pp (c', cs');
wenzelm@19642
   491
    in ((space', classes'), default, types) end);
wenzelm@14790
   492
wenzelm@26669
   493
fun hide_class fully c = map_tsig (fn ((space, classes), default, types) =>
wenzelm@33095
   494
  ((Name_Space.hide fully c space, classes), default, types));
wenzelm@16370
   495
wenzelm@14790
   496
wenzelm@19515
   497
(* arities *)
wenzelm@19515
   498
wenzelm@19642
   499
fun add_arity pp (t, Ss, S) tsig = tsig |> map_tsig (fn ((space, classes), default, types) =>
wenzelm@19515
   500
  let
wenzelm@19515
   501
    val _ =
haftmann@27302
   502
      (case lookup_type tsig t of
wenzelm@33173
   503
        SOME (LogicalType n) => if length Ss <> n then error (bad_nargs t) else ()
wenzelm@33094
   504
      | SOME _ => error ("Logical type constructor expected: " ^ quote t)
wenzelm@19515
   505
      | NONE => error (undecl_type t));
wenzelm@19515
   506
    val (Ss', S') = (map (cert_sort tsig) Ss, cert_sort tsig S)
wenzelm@19515
   507
      handle TYPE (msg, _, _) => error msg;
wenzelm@19642
   508
    val classes' = classes |> Sorts.add_arities pp ((t, map (fn c' => (c', Ss')) S'));
wenzelm@19642
   509
  in ((space, classes'), default, types) end);
wenzelm@19515
   510
wenzelm@19515
   511
wenzelm@19515
   512
(* classrel *)
wenzelm@19515
   513
wenzelm@19515
   514
fun add_classrel pp rel tsig =
wenzelm@19642
   515
  tsig |> map_tsig (fn ((space, classes), default, types) =>
wenzelm@19515
   516
    let
wenzelm@19515
   517
      val rel' = pairself (cert_class tsig) rel
wenzelm@19515
   518
        handle TYPE (msg, _, _) => error msg;
wenzelm@32784
   519
      val classes' = classes |> Sorts.add_classrel pp rel';
wenzelm@19642
   520
    in ((space, classes'), default, types) end);
wenzelm@19515
   521
wenzelm@19515
   522
wenzelm@14790
   523
(* default sort *)
wenzelm@14790
   524
wenzelm@19642
   525
fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) =>
wenzelm@19642
   526
  (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types));
clasohm@0
   527
clasohm@0
   528
wenzelm@14790
   529
(* types *)
wenzelm@14790
   530
wenzelm@14790
   531
local
wenzelm@14790
   532
wenzelm@33173
   533
fun new_decl naming (c, decl) types =
wenzelm@33173
   534
  #2 (Name_Space.define true naming (c, decl) types);
wenzelm@14790
   535
wenzelm@19642
   536
fun map_types f = map_tsig (fn (classes, default, types) =>
wenzelm@19579
   537
  let
wenzelm@19579
   538
    val (space', tab') = f types;
wenzelm@33095
   539
    val _ = Name_Space.intern space' "dummy" = "dummy" orelse
wenzelm@21858
   540
      error "Illegal declaration of dummy type";
wenzelm@19642
   541
  in (classes, default, (space', tab')) end);
wenzelm@2964
   542
wenzelm@14989
   543
fun syntactic types (Type (c, Ts)) =
wenzelm@33173
   544
      (case Symtab.lookup types c of SOME Nonterminal => true | _ => false)
wenzelm@14989
   545
        orelse exists (syntactic types) Ts
wenzelm@14989
   546
  | syntactic _ _ = false;
wenzelm@14989
   547
haftmann@27302
   548
in
haftmann@27302
   549
wenzelm@33173
   550
fun add_type naming (c, n) =
wenzelm@33094
   551
  if n < 0 then error ("Bad type constructor declaration " ^ quote (Binding.str_of c))
wenzelm@33173
   552
  else map_types (new_decl naming (c, LogicalType n));
haftmann@27302
   553
wenzelm@33173
   554
fun add_abbrev naming (a, vs, rhs) tsig = tsig |> map_types (fn types =>
wenzelm@14790
   555
  let
wenzelm@30343
   556
    fun err msg =
wenzelm@33173
   557
      cat_error msg ("The error(s) above occurred in type abbreviation " ^
wenzelm@33173
   558
        quote (Binding.str_of a));
wenzelm@24274
   559
    val rhs' = strip_sorts (no_tvars (cert_typ_mode mode_syntax tsig rhs))
wenzelm@14790
   560
      handle TYPE (msg, _, _) => err msg;
wenzelm@33094
   561
    val _ =
wenzelm@33094
   562
      (case duplicates (op =) vs of
wenzelm@33094
   563
        [] => []
wenzelm@33094
   564
      | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
wenzelm@33094
   565
    val _ =
wenzelm@33094
   566
      (case subtract (op =) vs (map #1 (Term.add_tfreesT rhs' [])) of
wenzelm@33094
   567
        [] => []
wenzelm@33094
   568
      | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
wenzelm@33173
   569
  in types |> new_decl naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) end);
clasohm@0
   570
wenzelm@33173
   571
fun add_nonterminal naming = map_types o new_decl naming o rpair Nonterminal;
wenzelm@14790
   572
wenzelm@14790
   573
end;
clasohm@0
   574
wenzelm@26669
   575
fun hide_type fully c = map_tsig (fn (classes, default, (space, types)) =>
wenzelm@33095
   576
  (classes, default, (Name_Space.hide fully c space, types)));
wenzelm@16370
   577
wenzelm@3790
   578
wenzelm@14790
   579
(* merge type signatures *)
wenzelm@14790
   580
wenzelm@14830
   581
fun merge_tsigs pp (tsig1, tsig2) =
wenzelm@14790
   582
  let
wenzelm@19515
   583
    val (TSig {classes = (space1, classes1), default = default1, types = types1,
wenzelm@26641
   584
      log_types = _}) = tsig1;
wenzelm@19515
   585
    val (TSig {classes = (space2, classes2), default = default2, types = types2,
wenzelm@26641
   586
      log_types = _}) = tsig2;
wenzelm@14790
   587
wenzelm@33095
   588
    val space' = Name_Space.merge (space1, space2);
wenzelm@19642
   589
    val classes' = Sorts.merge_algebra pp (classes1, classes2);
wenzelm@19515
   590
    val default' = Sorts.inter_sort classes' (default1, default2);
wenzelm@33095
   591
    val types' = Name_Space.merge_tables (types1, types2);
wenzelm@19642
   592
  in build_tsig ((space', classes'), default', types') end;
wenzelm@14790
   593
clasohm@0
   594
end;