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