src/Pure/type.ML
author wenzelm
Thu Feb 25 22:05:34 2010 +0100 (2010-02-25 ago)
changeset 35359 3ec03a3cd9d0
parent 34272 95df5e6dd41c
child 35669 a91c7ed801b8
permissions -rw-r--r--
provide direct access to the different kinds of type declarations;
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@35359
    38
  val the_decl: tsig -> string -> decl
wenzelm@24274
    39
  val cert_typ_mode: mode -> tsig -> typ -> typ
wenzelm@14993
    40
  val cert_typ: tsig -> typ -> typ
wenzelm@19464
    41
  val arity_number: tsig -> string -> int
wenzelm@19464
    42
  val arity_sorts: Pretty.pp -> tsig -> string -> sort -> sort list
wenzelm@14790
    43
wenzelm@14790
    44
  (*special treatment of type vars*)
wenzelm@14790
    45
  val strip_sorts: typ -> typ
wenzelm@621
    46
  val no_tvars: typ -> typ
haftmann@21116
    47
  val varify: (string * sort) list -> term -> ((string * sort) * indexname) list * term
wenzelm@33832
    48
  val legacy_freeze_thaw_type: typ -> typ * (typ -> typ)
wenzelm@33832
    49
  val legacy_freeze_type: typ -> typ
wenzelm@33832
    50
  val legacy_freeze_thaw: term -> term * (term -> term)
wenzelm@33832
    51
  val legacy_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@33519
   150
structure Mode = Proof_Data
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@35359
   167
fun the_decl tsig c =
wenzelm@35359
   168
  (case lookup_type tsig c of
wenzelm@35359
   169
    NONE => error (undecl_type c)
wenzelm@35359
   170
  | SOME decl => decl);
wenzelm@35359
   171
wenzelm@27313
   172
wenzelm@14790
   173
(* certified types *)
wenzelm@14790
   174
wenzelm@14790
   175
fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
wenzelm@14830
   176
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@14790
   187
    fun err msg = raise TYPE (msg, [ty], []);
wenzelm@14790
   188
wenzelm@24274
   189
    val check_logical =
wenzelm@24274
   190
      if logical then fn c => err ("Illegal occurrence of syntactic type: " ^ quote c)
wenzelm@24274
   191
      else fn _ => ();
wenzelm@14989
   192
wenzelm@14989
   193
    fun cert (T as Type (c, Ts)) =
wenzelm@14989
   194
          let
wenzelm@14989
   195
            val Ts' = map cert Ts;
wenzelm@14989
   196
            fun nargs n = if length Ts <> n then err (bad_nargs c) else ();
wenzelm@14989
   197
          in
wenzelm@35359
   198
            (case the_decl tsig c of
wenzelm@35359
   199
              LogicalType n => (nargs n; Type (c, Ts'))
wenzelm@35359
   200
            | Abbreviation (vs, U, syn) =>
wenzelm@24274
   201
               (nargs (length vs);
wenzelm@24274
   202
                if syn then check_logical c else ();
wenzelm@14993
   203
                if normalize then inst_typ (vs ~~ Ts') U
wenzelm@14989
   204
                else Type (c, Ts'))
wenzelm@35359
   205
            | Nonterminal => (nargs 0; check_logical c; T))
wenzelm@14790
   206
          end
wenzelm@19515
   207
      | cert (TFree (x, S)) = TFree (x, cert_sort tsig S)
wenzelm@14989
   208
      | cert (TVar (xi as (_, i), S)) =
wenzelm@14993
   209
          if i < 0 then
wenzelm@14993
   210
            err ("Malformed type variable: " ^ quote (Term.string_of_vname xi))
wenzelm@19515
   211
          else TVar (xi, cert_sort tsig S);
wenzelm@14790
   212
wenzelm@14989
   213
    val ty' = cert ty;
wenzelm@14993
   214
  in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
wenzelm@14830
   215
wenzelm@24274
   216
val cert_typ = cert_typ_mode mode_default;
wenzelm@14790
   217
wenzelm@14998
   218
end;
wenzelm@14998
   219
wenzelm@14790
   220
wenzelm@19464
   221
(* type arities *)
wenzelm@19464
   222
haftmann@27302
   223
fun arity_number tsig a =
haftmann@27302
   224
  (case lookup_type tsig a of
wenzelm@33173
   225
    SOME (LogicalType n) => n
wenzelm@19464
   226
  | _ => error (undecl_type a));
wenzelm@19464
   227
wenzelm@19464
   228
fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) []
wenzelm@19642
   229
  | arity_sorts pp (TSig {classes, ...}) a S = Sorts.mg_domain (#2 classes) a S
wenzelm@26641
   230
      handle Sorts.CLASS_ERROR err => error (Sorts.class_error pp err);
wenzelm@19464
   231
wenzelm@19464
   232
wenzelm@14790
   233
wenzelm@14790
   234
(** special treatment of type vars **)
wenzelm@14790
   235
wenzelm@14790
   236
(* strip_sorts *)
wenzelm@14790
   237
wenzelm@14790
   238
fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts)
wenzelm@14790
   239
  | strip_sorts (TFree (x, _)) = TFree (x, [])
wenzelm@14790
   240
  | strip_sorts (TVar (xi, _)) = TVar (xi, []);
wenzelm@14790
   241
wenzelm@14790
   242
wenzelm@14790
   243
(* no_tvars *)
wenzelm@621
   244
wenzelm@621
   245
fun no_tvars T =
wenzelm@29275
   246
  (case Term.add_tvarsT T [] of [] => T
wenzelm@12501
   247
  | vs => raise TYPE ("Illegal schematic type variable(s): " ^
wenzelm@29275
   248
      commas_quote (map (Term.string_of_vname o #1) (rev vs)), [T], []));
wenzelm@621
   249
wenzelm@7641
   250
wenzelm@19806
   251
(* varify *)
wenzelm@621
   252
haftmann@21116
   253
fun varify fixed t =
wenzelm@621
   254
  let
wenzelm@19305
   255
    val fs = Term.fold_types (Term.fold_atyps
wenzelm@19305
   256
      (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t [];
wenzelm@29260
   257
    val used = Name.context
wenzelm@29260
   258
      |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t;
wenzelm@29260
   259
    val fmap = fs ~~ map (rpair 0) (#1 (Name.variants (map fst fs) used));
wenzelm@32784
   260
    fun thaw (f as (_, S)) =
wenzelm@17184
   261
      (case AList.lookup (op =) fmap f of
skalberg@15531
   262
        NONE => TFree f
wenzelm@16946
   263
      | SOME xi => TVar (xi, S));
haftmann@21116
   264
  in (fmap, map_types (map_type_tfree thaw) t) end;
wenzelm@2964
   265
wenzelm@2964
   266
wenzelm@7641
   267
(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *)
paulson@3411
   268
wenzelm@7641
   269
local
wenzelm@7641
   270
wenzelm@16289
   271
fun new_name (ix, (pairs, used)) =
wenzelm@20071
   272
  let val v = Name.variant used (string_of_indexname ix)
wenzelm@16289
   273
  in ((ix, v) :: pairs, v :: used) end;
wenzelm@621
   274
wenzelm@16289
   275
fun freeze_one alist (ix, sort) =
wenzelm@17184
   276
  TFree (the (AList.lookup (op =) alist ix), sort)
skalberg@15531
   277
    handle Option =>
wenzelm@3790
   278
      raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
wenzelm@2964
   279
wenzelm@17184
   280
fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort)
wenzelm@16289
   281
  handle Option => TFree (a, sort);
wenzelm@416
   282
wenzelm@10495
   283
in
wenzelm@10495
   284
wenzelm@33832
   285
fun legacy_freeze_thaw_type T =
wenzelm@7641
   286
  let
wenzelm@29275
   287
    val used = OldTerm.add_typ_tfree_names (T, [])
wenzelm@29275
   288
    and tvars = map #1 (OldTerm.add_typ_tvars (T, []));
wenzelm@23178
   289
    val (alist, _) = List.foldr new_name ([], used) tvars;
wenzelm@7641
   290
  in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;
wenzelm@7641
   291
wenzelm@33832
   292
val legacy_freeze_type = #1 o legacy_freeze_thaw_type;
wenzelm@16289
   293
wenzelm@33832
   294
fun legacy_freeze_thaw t =
wenzelm@7641
   295
  let
wenzelm@29275
   296
    val used = OldTerm.it_term_types OldTerm.add_typ_tfree_names (t, [])
wenzelm@29275
   297
    and tvars = map #1 (OldTerm.it_term_types OldTerm.add_typ_tvars (t, []));
wenzelm@23178
   298
    val (alist, _) = List.foldr new_name ([], used) tvars;
wenzelm@7641
   299
  in
wenzelm@7641
   300
    (case alist of
wenzelm@7641
   301
      [] => (t, fn x => x) (*nothing to do!*)
wenzelm@20548
   302
    | _ => (map_types (map_type_tvar (freeze_one alist)) t,
wenzelm@20548
   303
      map_types (map_type_tfree (thaw_one (map swap alist)))))
wenzelm@7641
   304
  end;
wenzelm@7641
   305
wenzelm@33832
   306
val legacy_freeze = #1 o legacy_freeze_thaw;
wenzelm@16289
   307
wenzelm@7641
   308
end;
wenzelm@7641
   309
wenzelm@256
   310
wenzelm@256
   311
wenzelm@14790
   312
(** matching and unification of types **)
wenzelm@8899
   313
berghofe@15797
   314
type tyenv = (sort * typ) Vartab.table;
wenzelm@256
   315
berghofe@15797
   316
fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
berghofe@15797
   317
  quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
berghofe@15797
   318
  [TVar (ixn, S), TVar (ixn, S')], []);
clasohm@0
   319
haftmann@26327
   320
fun lookup tye (ixn, S) =
wenzelm@17412
   321
  (case Vartab.lookup tye ixn of
berghofe@15797
   322
    NONE => NONE
wenzelm@16289
   323
  | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');
wenzelm@16289
   324
clasohm@0
   325
wenzelm@14790
   326
(* matching *)
clasohm@0
   327
wenzelm@2964
   328
exception TYPE_MATCH;
clasohm@0
   329
wenzelm@16946
   330
fun typ_match tsig =
wenzelm@2964
   331
  let
wenzelm@16340
   332
    fun match (TVar (v, S), T) subs =
haftmann@26327
   333
          (case lookup subs (v, S) of
skalberg@15531
   334
            NONE =>
wenzelm@17412
   335
              if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs
wenzelm@14993
   336
              else raise TYPE_MATCH
skalberg@15531
   337
          | SOME U => if U = T then subs else raise TYPE_MATCH)
wenzelm@16340
   338
      | match (Type (a, Ts), Type (b, Us)) subs =
wenzelm@2964
   339
          if a <> b then raise TYPE_MATCH
wenzelm@16885
   340
          else matches (Ts, Us) subs
wenzelm@16340
   341
      | match (TFree x, TFree y) subs =
wenzelm@2964
   342
          if x = y then subs else raise TYPE_MATCH
wenzelm@16885
   343
      | match _ _ = raise TYPE_MATCH
wenzelm@16885
   344
    and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs)
wenzelm@16885
   345
      | matches _ subs = subs;
wenzelm@16946
   346
  in match end;
clasohm@0
   347
wenzelm@14790
   348
fun typ_instance tsig (T, U) =
wenzelm@16946
   349
  (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
wenzelm@16946
   350
wenzelm@16946
   351
(*purely structural matching*)
wenzelm@16946
   352
fun raw_match (TVar (v, S), T) subs =
haftmann@26327
   353
      (case lookup subs (v, S) of
wenzelm@17412
   354
        NONE => Vartab.update_new (v, (S, T)) subs
wenzelm@16946
   355
      | SOME U => if U = T then subs else raise TYPE_MATCH)
wenzelm@16946
   356
  | raw_match (Type (a, Ts), Type (b, Us)) subs =
wenzelm@16946
   357
      if a <> b then raise TYPE_MATCH
wenzelm@16946
   358
      else raw_matches (Ts, Us) subs
wenzelm@16946
   359
  | raw_match (TFree x, TFree y) subs =
wenzelm@16946
   360
      if x = y then subs else raise TYPE_MATCH
wenzelm@16946
   361
  | raw_match _ _ = raise TYPE_MATCH
wenzelm@16946
   362
and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs)
wenzelm@19694
   363
  | raw_matches ([], []) subs = subs
wenzelm@19694
   364
  | raw_matches _ _ = raise TYPE_MATCH;
wenzelm@16946
   365
wenzelm@16946
   366
fun raw_instance (T, U) =
wenzelm@16946
   367
  (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
wenzelm@2964
   368
clasohm@0
   369
wenzelm@14790
   370
(* unification *)
wenzelm@2964
   371
clasohm@0
   372
exception TUNIFY;
clasohm@0
   373
wenzelm@26641
   374
(*occurs check*)
wenzelm@2964
   375
fun occurs v tye =
wenzelm@2964
   376
  let
wenzelm@2964
   377
    fun occ (Type (_, Ts)) = exists occ Ts
wenzelm@2964
   378
      | occ (TFree _) = false
berghofe@15797
   379
      | occ (TVar (w, S)) =
wenzelm@29269
   380
          Term.eq_ix (v, w) orelse
haftmann@26327
   381
            (case lookup tye (w, S) of
skalberg@15531
   382
              NONE => false
skalberg@15531
   383
            | SOME U => occ U);
clasohm@0
   384
  in occ end;
clasohm@0
   385
wenzelm@14790
   386
(*chase variable assignments; if devar returns a type var then it must be unassigned*)
wenzelm@16885
   387
fun devar tye (T as TVar v) =
haftmann@26327
   388
      (case lookup tye v of
wenzelm@16885
   389
        SOME U => devar tye U
skalberg@15531
   390
      | NONE => T)
wenzelm@32784
   391
  | devar _ T = T;
clasohm@0
   392
wenzelm@17804
   393
(*order-sorted unification*)
wenzelm@32784
   394
fun unify (TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
wenzelm@2964
   395
  let
wenzelm@32738
   396
    val tyvar_count = Unsynchronized.ref maxidx;
wenzelm@32738
   397
    fun gen_tyvar S = TVar ((Name.aT, Unsynchronized.inc tyvar_count), S);
wenzelm@2964
   398
wenzelm@19642
   399
    fun mg_domain a S = Sorts.mg_domain classes a S
wenzelm@19642
   400
      handle Sorts.CLASS_ERROR _ => raise TUNIFY;
wenzelm@2964
   401
wenzelm@16885
   402
    fun meet (_, []) tye = tye
wenzelm@16885
   403
      | meet (TVar (xi, S'), S) tye =
wenzelm@14790
   404
          if Sorts.sort_le classes (S', S) then tye
wenzelm@17412
   405
          else Vartab.update_new
wenzelm@17221
   406
            (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye
wenzelm@16885
   407
      | meet (TFree (_, S'), S) tye =
wenzelm@14790
   408
          if Sorts.sort_le classes (S', S) then tye
wenzelm@2964
   409
          else raise TUNIFY
wenzelm@16885
   410
      | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye
wenzelm@16885
   411
    and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye)
wenzelm@16885
   412
      | meets _ tye = tye;
wenzelm@2964
   413
wenzelm@16885
   414
    fun unif (ty1, ty2) tye =
wenzelm@16885
   415
      (case (devar tye ty1, devar tye ty2) of
wenzelm@2964
   416
        (T as TVar (v, S1), U as TVar (w, S2)) =>
wenzelm@29269
   417
          if Term.eq_ix (v, w) then
berghofe@15797
   418
            if S1 = S2 then tye else tvar_clash v S1 S2
wenzelm@14790
   419
          else if Sorts.sort_le classes (S1, S2) then
wenzelm@17412
   420
            Vartab.update_new (w, (S2, T)) tye
wenzelm@14790
   421
          else if Sorts.sort_le classes (S2, S1) then
wenzelm@17412
   422
            Vartab.update_new (v, (S1, U)) tye
wenzelm@2964
   423
          else
wenzelm@14790
   424
            let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
wenzelm@17412
   425
              Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye)
wenzelm@2964
   426
            end
wenzelm@2964
   427
      | (TVar (v, S), T) =>
wenzelm@2964
   428
          if occurs v tye T then raise TUNIFY
wenzelm@17412
   429
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
wenzelm@2964
   430
      | (T, TVar (v, S)) =>
wenzelm@2964
   431
          if occurs v tye T then raise TUNIFY
wenzelm@17412
   432
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
wenzelm@2964
   433
      | (Type (a, Ts), Type (b, Us)) =>
wenzelm@2964
   434
          if a <> b then raise TUNIFY
wenzelm@16885
   435
          else unifs (Ts, Us) tye
wenzelm@16885
   436
      | (T, U) => if T = U then tye else raise TUNIFY)
wenzelm@16885
   437
    and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye)
wenzelm@16885
   438
      | unifs _ tye = tye;
wenzelm@16885
   439
  in (unif TU tyenv, ! tyvar_count) end;
clasohm@0
   440
wenzelm@16946
   441
(*purely structural unification*)
wenzelm@16946
   442
fun raw_unify (ty1, ty2) tye =
wenzelm@16946
   443
  (case (devar tye ty1, devar tye ty2) of
wenzelm@32784
   444
    (T as TVar (v, S1), TVar (w, S2)) =>
wenzelm@29269
   445
      if Term.eq_ix (v, w) then
wenzelm@16946
   446
        if S1 = S2 then tye else tvar_clash v S1 S2
wenzelm@17412
   447
      else Vartab.update_new (w, (S2, T)) tye
wenzelm@16946
   448
  | (TVar (v, S), T) =>
wenzelm@16946
   449
      if occurs v tye T then raise TUNIFY
wenzelm@17412
   450
      else Vartab.update_new (v, (S, T)) tye
wenzelm@16946
   451
  | (T, TVar (v, S)) =>
wenzelm@16946
   452
      if occurs v tye T then raise TUNIFY
wenzelm@17412
   453
      else Vartab.update_new (v, (S, T)) tye
wenzelm@16946
   454
  | (Type (a, Ts), Type (b, Us)) =>
wenzelm@16946
   455
      if a <> b then raise TUNIFY
wenzelm@16946
   456
      else raw_unifys (Ts, Us) tye
wenzelm@16946
   457
  | (T, U) => if T = U then tye else raise TUNIFY)
wenzelm@16946
   458
and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye)
wenzelm@19696
   459
  | raw_unifys ([], []) tye = tye
wenzelm@19696
   460
  | raw_unifys _ _ = raise TUNIFY;
wenzelm@16946
   461
wenzelm@17804
   462
(*fast unification filter*)
wenzelm@17804
   463
fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us)
wenzelm@17804
   464
  | could_unify (TFree (a, _), TFree (b, _)) = a = b
wenzelm@17804
   465
  | could_unify (TVar _, _) = true
wenzelm@17804
   466
  | could_unify (_, TVar _) = true
wenzelm@17804
   467
  | could_unify _ = false
wenzelm@17804
   468
and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us)
wenzelm@19696
   469
  | could_unifys ([], []) = true
wenzelm@19696
   470
  | could_unifys _ = false;
wenzelm@450
   471
wenzelm@17804
   472
wenzelm@17804
   473
(*equality with respect to a type environment*)
wenzelm@32030
   474
fun equal_type tye (T, T') =
wenzelm@16885
   475
  (case (devar tye T, devar tye T') of
berghofe@16650
   476
     (Type (s, Ts), Type (s', Ts')) =>
wenzelm@32030
   477
       s = s' andalso ListPair.all (equal_type tye) (Ts, Ts')
berghofe@16650
   478
   | (U, U') => U = U');
berghofe@16650
   479
wenzelm@32030
   480
fun eq_type tye =
wenzelm@32030
   481
  if Vartab.is_empty tye then op = else equal_type tye;
wenzelm@32030
   482
wenzelm@450
   483
clasohm@0
   484
wenzelm@14790
   485
(** extend and merge type signatures **)
wenzelm@14790
   486
wenzelm@14790
   487
(* classes *)
wenzelm@14790
   488
wenzelm@16370
   489
fun add_class pp naming (c, cs) tsig =
wenzelm@19642
   490
  tsig |> map_tsig (fn ((space, classes), default, types) =>
wenzelm@16370
   491
    let
wenzelm@16370
   492
      val cs' = map (cert_class tsig) cs
wenzelm@16370
   493
        handle TYPE (msg, _, _) => error msg;
wenzelm@33095
   494
      val (c', space') = space |> Name_Space.declare true 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@26669
   498
fun hide_class fully c = map_tsig (fn ((space, classes), default, types) =>
wenzelm@33095
   499
  ((Name_Space.hide fully c 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 _ =
haftmann@27302
   507
      (case lookup_type tsig t of
wenzelm@33173
   508
        SOME (LogicalType n) => if length Ss <> n then error (bad_nargs t) else ()
wenzelm@33094
   509
      | SOME _ => error ("Logical type constructor expected: " ^ 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@32784
   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@33173
   538
fun new_decl naming (c, decl) types =
wenzelm@33173
   539
  #2 (Name_Space.define true naming (c, decl) types);
wenzelm@14790
   540
wenzelm@19642
   541
fun map_types f = map_tsig (fn (classes, default, types) =>
wenzelm@19579
   542
  let
wenzelm@19579
   543
    val (space', tab') = f types;
wenzelm@33095
   544
    val _ = Name_Space.intern space' "dummy" = "dummy" orelse
wenzelm@21858
   545
      error "Illegal declaration of dummy type";
wenzelm@19642
   546
  in (classes, default, (space', tab')) end);
wenzelm@2964
   547
wenzelm@14989
   548
fun syntactic types (Type (c, Ts)) =
wenzelm@33173
   549
      (case Symtab.lookup types c of SOME Nonterminal => true | _ => false)
wenzelm@14989
   550
        orelse exists (syntactic types) Ts
wenzelm@14989
   551
  | syntactic _ _ = false;
wenzelm@14989
   552
haftmann@27302
   553
in
haftmann@27302
   554
wenzelm@33173
   555
fun add_type naming (c, n) =
wenzelm@33094
   556
  if n < 0 then error ("Bad type constructor declaration " ^ quote (Binding.str_of c))
wenzelm@33173
   557
  else map_types (new_decl naming (c, LogicalType n));
haftmann@27302
   558
wenzelm@33173
   559
fun add_abbrev naming (a, vs, rhs) tsig = tsig |> map_types (fn types =>
wenzelm@14790
   560
  let
wenzelm@30343
   561
    fun err msg =
wenzelm@33173
   562
      cat_error msg ("The error(s) above occurred in type abbreviation " ^
wenzelm@33173
   563
        quote (Binding.str_of a));
wenzelm@24274
   564
    val rhs' = strip_sorts (no_tvars (cert_typ_mode mode_syntax tsig rhs))
wenzelm@14790
   565
      handle TYPE (msg, _, _) => err msg;
wenzelm@33094
   566
    val _ =
wenzelm@33094
   567
      (case duplicates (op =) vs of
wenzelm@33094
   568
        [] => []
wenzelm@33094
   569
      | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
wenzelm@33094
   570
    val _ =
wenzelm@33094
   571
      (case subtract (op =) vs (map #1 (Term.add_tfreesT rhs' [])) of
wenzelm@33094
   572
        [] => []
wenzelm@33094
   573
      | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
wenzelm@33173
   574
  in types |> new_decl naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) end);
clasohm@0
   575
wenzelm@33173
   576
fun add_nonterminal naming = map_types o new_decl naming o rpair Nonterminal;
wenzelm@14790
   577
wenzelm@14790
   578
end;
clasohm@0
   579
wenzelm@26669
   580
fun hide_type fully c = map_tsig (fn (classes, default, (space, types)) =>
wenzelm@33095
   581
  (classes, default, (Name_Space.hide fully c space, types)));
wenzelm@16370
   582
wenzelm@3790
   583
wenzelm@14790
   584
(* merge type signatures *)
wenzelm@14790
   585
wenzelm@14830
   586
fun merge_tsigs pp (tsig1, tsig2) =
wenzelm@14790
   587
  let
wenzelm@19515
   588
    val (TSig {classes = (space1, classes1), default = default1, types = types1,
wenzelm@26641
   589
      log_types = _}) = tsig1;
wenzelm@19515
   590
    val (TSig {classes = (space2, classes2), default = default2, types = types2,
wenzelm@26641
   591
      log_types = _}) = tsig2;
wenzelm@14790
   592
wenzelm@33095
   593
    val space' = Name_Space.merge (space1, space2);
wenzelm@19642
   594
    val classes' = Sorts.merge_algebra pp (classes1, classes2);
wenzelm@19515
   595
    val default' = Sorts.inter_sort classes' (default1, default2);
wenzelm@33095
   596
    val types' = Name_Space.merge_tables (types1, types2);
wenzelm@19642
   597
  in build_tsig ((space', classes'), default', types') end;
wenzelm@14790
   598
clasohm@0
   599
end;