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