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