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