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