src/Pure/type.ML
author wenzelm
Thu May 31 23:47:36 2007 +0200 (2007-05-31 ago)
changeset 23178 07ba6b58b3d2
parent 21934 ba683b0b2456
child 23655 d2d1138e0ddc
permissions -rw-r--r--
simplified/unified list fold;
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@20674
    20
    types: (decl * serial) 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
haftmann@21116
    43
  val varify: (string * sort) list -> term -> ((string * sort) * indexname) list * term
wenzelm@16289
    44
  val freeze_thaw_type: typ -> typ * (typ -> typ)
wenzelm@16289
    45
  val freeze_type: typ -> typ
wenzelm@16289
    46
  val freeze_thaw: term -> term * (term -> term)
wenzelm@16289
    47
  val freeze: term -> term
wenzelm@2964
    48
wenzelm@14790
    49
  (*matching and unification*)
wenzelm@2964
    50
  exception TYPE_MATCH
berghofe@15797
    51
  type tyenv
berghofe@15797
    52
  val lookup: tyenv * (indexname * sort) -> typ option
wenzelm@16946
    53
  val typ_match: tsig -> typ * typ -> tyenv -> tyenv
wenzelm@14790
    54
  val typ_instance: tsig -> typ * typ -> bool
wenzelm@16946
    55
  val raw_match: typ * typ -> tyenv -> tyenv
wenzelm@19694
    56
  val raw_matches: typ list * typ list -> tyenv -> tyenv
wenzelm@16946
    57
  val raw_instance: typ * typ -> bool
wenzelm@2964
    58
  exception TUNIFY
wenzelm@16946
    59
  val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int
wenzelm@16946
    60
  val raw_unify: typ * typ -> tyenv -> tyenv
wenzelm@19696
    61
  val raw_unifys: typ list * typ list -> tyenv -> tyenv
wenzelm@17804
    62
  val could_unify: typ * typ -> bool
wenzelm@19696
    63
  val could_unifys: typ list * typ list -> 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@20674
   102
    types: (decl * serial) 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@21934
   118
      (case Sorts.witness_sorts classes log_types [] [Sorts.minimal_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@19806
   229
(* varify *)
wenzelm@621
   230
haftmann@21116
   231
fun varify fixed t =
wenzelm@621
   232
  let
wenzelm@19305
   233
    val fs = Term.fold_types (Term.fold_atyps
wenzelm@19305
   234
      (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t [];
wenzelm@621
   235
    val ixns = add_term_tvar_ixns (t, []);
wenzelm@20071
   236
    val fmap = fs ~~ map (rpair 0) (Name.variant_list (map #1 ixns) (map fst fs))
wenzelm@2964
   237
    fun thaw (f as (a, S)) =
wenzelm@17184
   238
      (case AList.lookup (op =) fmap f of
skalberg@15531
   239
        NONE => TFree f
wenzelm@16946
   240
      | SOME xi => TVar (xi, S));
haftmann@21116
   241
  in (fmap, map_types (map_type_tfree thaw) t) end;
wenzelm@2964
   242
wenzelm@2964
   243
wenzelm@7641
   244
(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *)
paulson@3411
   245
wenzelm@7641
   246
local
wenzelm@7641
   247
wenzelm@16289
   248
fun new_name (ix, (pairs, used)) =
wenzelm@20071
   249
  let val v = Name.variant used (string_of_indexname ix)
wenzelm@16289
   250
  in ((ix, v) :: pairs, v :: used) end;
wenzelm@621
   251
wenzelm@16289
   252
fun freeze_one alist (ix, sort) =
wenzelm@17184
   253
  TFree (the (AList.lookup (op =) alist ix), sort)
skalberg@15531
   254
    handle Option =>
wenzelm@3790
   255
      raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
wenzelm@2964
   256
wenzelm@17184
   257
fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort)
wenzelm@16289
   258
  handle Option => TFree (a, sort);
wenzelm@416
   259
wenzelm@10495
   260
in
wenzelm@10495
   261
wenzelm@10495
   262
(*this sort of code could replace unvarifyT*)
wenzelm@7641
   263
fun freeze_thaw_type T =
wenzelm@7641
   264
  let
wenzelm@7641
   265
    val used = add_typ_tfree_names (T, [])
wenzelm@7641
   266
    and tvars = map #1 (add_typ_tvars (T, []));
wenzelm@23178
   267
    val (alist, _) = List.foldr new_name ([], used) tvars;
wenzelm@7641
   268
  in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;
wenzelm@7641
   269
wenzelm@16289
   270
val freeze_type = #1 o freeze_thaw_type;
wenzelm@16289
   271
paulson@3411
   272
fun freeze_thaw t =
wenzelm@7641
   273
  let
wenzelm@7641
   274
    val used = it_term_types add_typ_tfree_names (t, [])
wenzelm@7641
   275
    and tvars = map #1 (it_term_types add_typ_tvars (t, []));
wenzelm@23178
   276
    val (alist, _) = List.foldr new_name ([], used) tvars;
wenzelm@7641
   277
  in
wenzelm@7641
   278
    (case alist of
wenzelm@7641
   279
      [] => (t, fn x => x) (*nothing to do!*)
wenzelm@20548
   280
    | _ => (map_types (map_type_tvar (freeze_one alist)) t,
wenzelm@20548
   281
      map_types (map_type_tfree (thaw_one (map swap alist)))))
wenzelm@7641
   282
  end;
wenzelm@7641
   283
wenzelm@16289
   284
val freeze = #1 o freeze_thaw;
wenzelm@16289
   285
wenzelm@7641
   286
end;
wenzelm@7641
   287
wenzelm@256
   288
wenzelm@256
   289
wenzelm@14790
   290
(** matching and unification of types **)
wenzelm@8899
   291
berghofe@15797
   292
type tyenv = (sort * typ) Vartab.table;
wenzelm@256
   293
berghofe@15797
   294
fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
berghofe@15797
   295
  quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
berghofe@15797
   296
  [TVar (ixn, S), TVar (ixn, S')], []);
clasohm@0
   297
wenzelm@16289
   298
fun lookup (tye, (ixn, S)) =
wenzelm@17412
   299
  (case Vartab.lookup tye ixn of
berghofe@15797
   300
    NONE => NONE
wenzelm@16289
   301
  | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');
wenzelm@16289
   302
clasohm@0
   303
wenzelm@14790
   304
(* matching *)
clasohm@0
   305
wenzelm@2964
   306
exception TYPE_MATCH;
clasohm@0
   307
wenzelm@16946
   308
fun typ_match tsig =
wenzelm@2964
   309
  let
wenzelm@16340
   310
    fun match (TVar (v, S), T) subs =
berghofe@15797
   311
          (case lookup (subs, (v, S)) of
skalberg@15531
   312
            NONE =>
wenzelm@17412
   313
              if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs
wenzelm@14993
   314
              else raise TYPE_MATCH
skalberg@15531
   315
          | SOME U => if U = T then subs else raise TYPE_MATCH)
wenzelm@16340
   316
      | match (Type (a, Ts), Type (b, Us)) subs =
wenzelm@2964
   317
          if a <> b then raise TYPE_MATCH
wenzelm@16885
   318
          else matches (Ts, Us) subs
wenzelm@16340
   319
      | match (TFree x, TFree y) subs =
wenzelm@2964
   320
          if x = y then subs else raise TYPE_MATCH
wenzelm@16885
   321
      | match _ _ = raise TYPE_MATCH
wenzelm@16885
   322
    and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs)
wenzelm@16885
   323
      | matches _ subs = subs;
wenzelm@16946
   324
  in match end;
clasohm@0
   325
wenzelm@14790
   326
fun typ_instance tsig (T, U) =
wenzelm@16946
   327
  (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
wenzelm@16946
   328
wenzelm@16946
   329
(*purely structural matching*)
wenzelm@16946
   330
fun raw_match (TVar (v, S), T) subs =
wenzelm@16946
   331
      (case lookup (subs, (v, S)) of
wenzelm@17412
   332
        NONE => Vartab.update_new (v, (S, T)) subs
wenzelm@16946
   333
      | SOME U => if U = T then subs else raise TYPE_MATCH)
wenzelm@16946
   334
  | raw_match (Type (a, Ts), Type (b, Us)) subs =
wenzelm@16946
   335
      if a <> b then raise TYPE_MATCH
wenzelm@16946
   336
      else raw_matches (Ts, Us) subs
wenzelm@16946
   337
  | raw_match (TFree x, TFree y) subs =
wenzelm@16946
   338
      if x = y then subs else raise TYPE_MATCH
wenzelm@16946
   339
  | raw_match _ _ = raise TYPE_MATCH
wenzelm@16946
   340
and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs)
wenzelm@19694
   341
  | raw_matches ([], []) subs = subs
wenzelm@19694
   342
  | raw_matches _ _ = raise TYPE_MATCH;
wenzelm@16946
   343
wenzelm@16946
   344
fun raw_instance (T, U) =
wenzelm@16946
   345
  (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
wenzelm@2964
   346
clasohm@0
   347
wenzelm@14790
   348
(* unification *)
wenzelm@2964
   349
clasohm@0
   350
exception TUNIFY;
clasohm@0
   351
wenzelm@14790
   352
(*occurs_check*)
wenzelm@2964
   353
fun occurs v tye =
wenzelm@2964
   354
  let
wenzelm@2964
   355
    fun occ (Type (_, Ts)) = exists occ Ts
wenzelm@2964
   356
      | occ (TFree _) = false
berghofe@15797
   357
      | occ (TVar (w, S)) =
wenzelm@2964
   358
          eq_ix (v, w) orelse
berghofe@15797
   359
            (case lookup (tye, (w, S)) of
skalberg@15531
   360
              NONE => false
skalberg@15531
   361
            | SOME U => occ U);
clasohm@0
   362
  in occ end;
clasohm@0
   363
wenzelm@14790
   364
(*chase variable assignments; if devar returns a type var then it must be unassigned*)
wenzelm@16885
   365
fun devar tye (T as TVar v) =
wenzelm@16885
   366
      (case lookup (tye, v) of
wenzelm@16885
   367
        SOME U => devar tye U
skalberg@15531
   368
      | NONE => T)
wenzelm@16885
   369
  | devar tye T = T;
clasohm@0
   370
wenzelm@17804
   371
(*order-sorted unification*)
wenzelm@19642
   372
fun unify (tsig as TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
wenzelm@2964
   373
  let
wenzelm@2964
   374
    val tyvar_count = ref maxidx;
wenzelm@2964
   375
    fun gen_tyvar S = TVar (("'a", inc tyvar_count), S);
wenzelm@2964
   376
wenzelm@19642
   377
    fun mg_domain a S = Sorts.mg_domain classes a S
wenzelm@19642
   378
      handle Sorts.CLASS_ERROR _ => raise TUNIFY;
wenzelm@2964
   379
wenzelm@16885
   380
    fun meet (_, []) tye = tye
wenzelm@16885
   381
      | meet (TVar (xi, S'), S) tye =
wenzelm@14790
   382
          if Sorts.sort_le classes (S', S) then tye
wenzelm@17412
   383
          else Vartab.update_new
wenzelm@17221
   384
            (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye
wenzelm@16885
   385
      | meet (TFree (_, S'), S) tye =
wenzelm@14790
   386
          if Sorts.sort_le classes (S', S) then tye
wenzelm@2964
   387
          else raise TUNIFY
wenzelm@16885
   388
      | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye
wenzelm@16885
   389
    and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye)
wenzelm@16885
   390
      | meets _ tye = tye;
wenzelm@2964
   391
wenzelm@16885
   392
    fun unif (ty1, ty2) tye =
wenzelm@16885
   393
      (case (devar tye ty1, devar tye ty2) of
wenzelm@2964
   394
        (T as TVar (v, S1), U as TVar (w, S2)) =>
berghofe@15797
   395
          if eq_ix (v, w) then
berghofe@15797
   396
            if S1 = S2 then tye else tvar_clash v S1 S2
wenzelm@14790
   397
          else if Sorts.sort_le classes (S1, S2) then
wenzelm@17412
   398
            Vartab.update_new (w, (S2, T)) tye
wenzelm@14790
   399
          else if Sorts.sort_le classes (S2, S1) then
wenzelm@17412
   400
            Vartab.update_new (v, (S1, U)) tye
wenzelm@2964
   401
          else
wenzelm@14790
   402
            let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
wenzelm@17412
   403
              Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye)
wenzelm@2964
   404
            end
wenzelm@2964
   405
      | (TVar (v, S), T) =>
wenzelm@2964
   406
          if occurs v tye T then raise TUNIFY
wenzelm@17412
   407
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
wenzelm@2964
   408
      | (T, TVar (v, S)) =>
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
      | (Type (a, Ts), Type (b, Us)) =>
wenzelm@2964
   412
          if a <> b then raise TUNIFY
wenzelm@16885
   413
          else unifs (Ts, Us) tye
wenzelm@16885
   414
      | (T, U) => if T = U then tye else raise TUNIFY)
wenzelm@16885
   415
    and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye)
wenzelm@16885
   416
      | unifs _ tye = tye;
wenzelm@16885
   417
  in (unif TU tyenv, ! tyvar_count) end;
clasohm@0
   418
wenzelm@16946
   419
(*purely structural unification*)
wenzelm@16946
   420
fun raw_unify (ty1, ty2) tye =
wenzelm@16946
   421
  (case (devar tye ty1, devar tye ty2) of
wenzelm@16946
   422
    (T as TVar (v, S1), U as TVar (w, S2)) =>
wenzelm@16946
   423
      if eq_ix (v, w) then
wenzelm@16946
   424
        if S1 = S2 then tye else tvar_clash v S1 S2
wenzelm@17412
   425
      else Vartab.update_new (w, (S2, T)) tye
wenzelm@16946
   426
  | (TVar (v, S), T) =>
wenzelm@16946
   427
      if occurs v tye T then raise TUNIFY
wenzelm@17412
   428
      else Vartab.update_new (v, (S, T)) tye
wenzelm@16946
   429
  | (T, TVar (v, S)) =>
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
  | (Type (a, Ts), Type (b, Us)) =>
wenzelm@16946
   433
      if a <> b then raise TUNIFY
wenzelm@16946
   434
      else raw_unifys (Ts, Us) tye
wenzelm@16946
   435
  | (T, U) => if T = U then tye else raise TUNIFY)
wenzelm@16946
   436
and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye)
wenzelm@19696
   437
  | raw_unifys ([], []) tye = tye
wenzelm@19696
   438
  | raw_unifys _ _ = raise TUNIFY;
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@19696
   447
  | could_unifys ([], []) = true
wenzelm@19696
   448
  | could_unifys _ = false;
wenzelm@450
   449
wenzelm@17804
   450
wenzelm@17804
   451
(*equality with respect to a type environment*)
berghofe@16650
   452
fun eq_type tye (T, T') =
wenzelm@16885
   453
  (case (devar tye T, devar tye T') of
berghofe@16650
   454
     (Type (s, Ts), Type (s', Ts')) =>
berghofe@16650
   455
       s = s' andalso ListPair.all (eq_type tye) (Ts, Ts')
berghofe@16650
   456
   | (U, U') => U = U');
berghofe@16650
   457
wenzelm@450
   458
clasohm@0
   459
wenzelm@14790
   460
(** extend and merge type signatures **)
wenzelm@14790
   461
wenzelm@14790
   462
(* classes *)
wenzelm@14790
   463
wenzelm@16370
   464
fun add_class pp naming (c, cs) tsig =
wenzelm@19642
   465
  tsig |> map_tsig (fn ((space, classes), default, types) =>
wenzelm@16370
   466
    let
wenzelm@16370
   467
      val c' = NameSpace.full naming c;
wenzelm@16370
   468
      val cs' = map (cert_class tsig) cs
wenzelm@16370
   469
        handle TYPE (msg, _, _) => error msg;
wenzelm@16370
   470
      val space' = space |> NameSpace.declare naming c';
wenzelm@19515
   471
      val classes' = classes |> Sorts.add_class pp (c', cs');
wenzelm@19642
   472
    in ((space', classes'), default, types) end);
wenzelm@14790
   473
wenzelm@19642
   474
fun hide_classes fully cs = map_tsig (fn ((space, classes), default, types) =>
wenzelm@19642
   475
  ((fold (NameSpace.hide fully) cs space, classes), default, types));
wenzelm@16370
   476
wenzelm@14790
   477
wenzelm@19515
   478
(* arities *)
wenzelm@19515
   479
wenzelm@19642
   480
fun add_arity pp (t, Ss, S) tsig = tsig |> map_tsig (fn ((space, classes), default, types) =>
wenzelm@19515
   481
  let
wenzelm@19515
   482
    val _ =
wenzelm@19515
   483
      (case Symtab.lookup (#2 types) t of
wenzelm@19515
   484
        SOME (LogicalType n, _) => if length Ss <> n then error (bad_nargs t) else ()
wenzelm@19515
   485
      | SOME (decl, _) => error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t)
wenzelm@19515
   486
      | NONE => error (undecl_type t));
wenzelm@19515
   487
    val (Ss', S') = (map (cert_sort tsig) Ss, cert_sort tsig S)
wenzelm@19515
   488
      handle TYPE (msg, _, _) => error msg;
wenzelm@19642
   489
    val classes' = classes |> Sorts.add_arities pp ((t, map (fn c' => (c', Ss')) S'));
wenzelm@19642
   490
  in ((space, classes'), default, types) end);
wenzelm@19515
   491
wenzelm@19515
   492
wenzelm@19515
   493
(* classrel *)
wenzelm@19515
   494
wenzelm@19515
   495
fun add_classrel pp rel tsig =
wenzelm@19642
   496
  tsig |> map_tsig (fn ((space, classes), default, types) =>
wenzelm@19515
   497
    let
wenzelm@19515
   498
      val rel' = pairself (cert_class tsig) rel
wenzelm@19515
   499
        handle TYPE (msg, _, _) => error msg;
wenzelm@19515
   500
      val classes' = classes |> Sorts.add_classrel pp rel;
wenzelm@19642
   501
    in ((space, classes'), default, types) end);
wenzelm@19515
   502
wenzelm@19515
   503
wenzelm@14790
   504
(* default sort *)
wenzelm@14790
   505
wenzelm@19642
   506
fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) =>
wenzelm@19642
   507
  (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types));
clasohm@0
   508
clasohm@0
   509
wenzelm@14790
   510
(* types *)
wenzelm@14790
   511
wenzelm@14790
   512
local
wenzelm@14790
   513
wenzelm@14790
   514
fun err_neg_args c =
wenzelm@14790
   515
  error ("Negative number of arguments in type constructor declaration: " ^ quote c);
nipkow@949
   516
wenzelm@14790
   517
fun err_in_decls c decl decl' =
wenzelm@14906
   518
  let val s = str_of_decl decl and s' = str_of_decl decl' in
wenzelm@14906
   519
    if s = s' then error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
wenzelm@14830
   520
    else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
wenzelm@14790
   521
  end;
wenzelm@14790
   522
wenzelm@16370
   523
fun new_decl naming (c, decl) (space, types) =
wenzelm@16370
   524
  let
wenzelm@16370
   525
    val c' = NameSpace.full naming c;
wenzelm@16370
   526
    val space' = NameSpace.declare naming c' space;
wenzelm@16370
   527
    val types' =
wenzelm@17412
   528
      (case Symtab.lookup types c' of
wenzelm@16370
   529
        SOME (decl', _) => err_in_decls c' decl decl'
wenzelm@20674
   530
      | NONE => Symtab.update (c', (decl, serial ())) types);
wenzelm@16370
   531
  in (space', types') end;
wenzelm@14790
   532
wenzelm@17412
   533
fun the_decl (_, types) = fst o the o Symtab.lookup types;
wenzelm@14790
   534
wenzelm@19642
   535
fun map_types f = map_tsig (fn (classes, default, types) =>
wenzelm@19579
   536
  let
wenzelm@19579
   537
    val (space', tab') = f types;
wenzelm@21858
   538
    val _ = NameSpace.intern space' "dummy" = "dummy" orelse
wenzelm@21858
   539
      error "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));
haftmann@20951
   556
    (case subtract (op =) vs (map (#1 o #1) (typ_tvars rhs')) 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) =
wenzelm@20674
   572
  NameSpace.merge_tables (Library.eq_snd (op = : serial * serial -> bool)) (types1, types2)
wenzelm@20674
   573
    handle Symtab.DUPS (d :: _) => 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;