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