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