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