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