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