src/Pure/type.ML
author wenzelm
Wed Jun 09 18:55:28 2004 +0200 (2004-06-09)
changeset 14906 2da524f3d785
parent 14830 faa4865ba1ce
child 14989 5a5d076a9863
permissions -rw-r--r--
tuned messages;
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@14790
    14
    Abbreviation of string list * typ |
wenzelm@14790
    15
    Nonterminal
wenzelm@14790
    16
  type tsig
wenzelm@14790
    17
  val rep_tsig: tsig ->
wenzelm@14790
    18
   {classes: Sorts.classes,
wenzelm@14790
    19
    default: sort,
wenzelm@14790
    20
    types: (decl * stamp) Symtab.table,
wenzelm@14790
    21
    arities: Sorts.arities,
wenzelm@14790
    22
    log_types: string list,
wenzelm@14790
    23
    witness: (typ * sort) option}
wenzelm@14790
    24
  val empty_tsig: tsig
wenzelm@14790
    25
  val classes: tsig -> class list
wenzelm@14790
    26
  val defaultS: tsig -> sort
wenzelm@14790
    27
  val logical_types: tsig -> string list
wenzelm@14790
    28
  val universal_witness: tsig -> (typ * sort) option
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@14790
    32
  val cert_class: tsig -> class -> class
wenzelm@14790
    33
  val cert_sort: tsig -> sort -> sort
wenzelm@14790
    34
  val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list
wenzelm@14790
    35
  val cert_typ: tsig -> typ -> typ
wenzelm@14790
    36
  val cert_typ_syntax: tsig -> typ -> typ
wenzelm@14790
    37
  val cert_typ_raw: tsig -> typ -> typ
wenzelm@14790
    38
wenzelm@14790
    39
  (*special treatment of type vars*)
wenzelm@14790
    40
  val strip_sorts: typ -> typ
wenzelm@621
    41
  val no_tvars: typ -> typ
wenzelm@621
    42
  val varifyT: typ -> typ
wenzelm@621
    43
  val unvarifyT: typ -> typ
wenzelm@12501
    44
  val varify: term * string list -> term * (string * indexname) list
wenzelm@10495
    45
  val freeze_thaw_type : typ -> typ * (typ -> typ)
paulson@3411
    46
  val freeze_thaw : term -> term * (term -> term)
wenzelm@2964
    47
wenzelm@14790
    48
  (*matching and unification*)
wenzelm@14830
    49
  val inst_typ_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> typ -> typ
wenzelm@14830
    50
  val inst_term_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> term -> term
wenzelm@2964
    51
  exception TYPE_MATCH
wenzelm@14790
    52
  val typ_match: tsig -> typ Vartab.table * (typ * typ) -> typ Vartab.table
wenzelm@14790
    53
  val typ_instance: tsig -> typ * typ -> bool
wenzelm@2964
    54
  exception TUNIFY
wenzelm@14790
    55
  val unify: tsig -> typ Vartab.table * int -> typ * typ -> typ Vartab.table * int
wenzelm@450
    56
  val raw_unify: typ * typ -> bool
clasohm@0
    57
wenzelm@14790
    58
  (*extend and merge type signatures*)
wenzelm@14830
    59
  val add_classes: Pretty.pp -> (class * class list) list -> tsig -> tsig
wenzelm@14830
    60
  val add_classrel: Pretty.pp -> (class * class) list -> tsig -> tsig
wenzelm@14790
    61
  val set_defsort: sort -> tsig -> tsig
wenzelm@14790
    62
  val add_types: (string * int) list -> tsig -> tsig
wenzelm@14790
    63
  val add_abbrs: (string * string list * typ) list -> tsig -> tsig
wenzelm@14790
    64
  val add_nonterminals: string list -> tsig -> tsig
wenzelm@14830
    65
  val add_arities: Pretty.pp -> arity list -> tsig -> tsig
wenzelm@14830
    66
  val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig
wenzelm@2964
    67
end;
wenzelm@2964
    68
wenzelm@2964
    69
structure Type: TYPE =
clasohm@0
    70
struct
clasohm@0
    71
wenzelm@14790
    72
(** type signatures and certified types **)
wenzelm@2964
    73
wenzelm@14790
    74
(* type declarations *)
wenzelm@14790
    75
wenzelm@14790
    76
datatype decl =
wenzelm@14790
    77
  LogicalType of int |
wenzelm@14790
    78
  Abbreviation of string list * typ |
wenzelm@14790
    79
  Nonterminal;
wenzelm@14790
    80
wenzelm@14790
    81
fun str_of_decl (LogicalType _) = "logical type constructor"
wenzelm@14790
    82
  | str_of_decl (Abbreviation _) = "type abbreviation"
wenzelm@14790
    83
  | str_of_decl Nonterminal = "syntactic type";
wenzelm@14790
    84
wenzelm@14790
    85
wenzelm@14790
    86
(* type tsig *)
wenzelm@14790
    87
wenzelm@14790
    88
datatype tsig =
wenzelm@14790
    89
  TSig of {
wenzelm@14790
    90
    classes: Sorts.classes,              (*declared classes with proper subclass relation*)
wenzelm@14790
    91
    default: sort,                       (*default sort on input*)
wenzelm@14790
    92
    types: (decl * stamp) Symtab.table,  (*declared types*)
wenzelm@14790
    93
    arities: Sorts.arities,              (*image specification of types wrt. sorts*)
wenzelm@14790
    94
    log_types: string list,              (*logical types sorted by number of arguments*)
wenzelm@14790
    95
    witness: (typ * sort) option};       (*witness for non-emptiness of strictest sort*)
wenzelm@14790
    96
wenzelm@14790
    97
fun rep_tsig (TSig comps) = comps;
wenzelm@14790
    98
wenzelm@14790
    99
fun make_tsig (classes, default, types, arities, log_types, witness) =
wenzelm@14790
   100
  TSig {classes = classes, default = default, types = types, arities = arities,
wenzelm@14790
   101
    log_types = log_types, witness = witness};
wenzelm@14790
   102
wenzelm@14790
   103
fun map_tsig f (TSig {classes, default, types, arities, log_types, witness}) =
wenzelm@14790
   104
  make_tsig (f (classes, default, types, arities, log_types, witness));
wenzelm@14790
   105
wenzelm@14790
   106
fun build_tsig (classes, default, types, arities) =
wenzelm@14790
   107
  let
wenzelm@14790
   108
    fun add_log_type (ts, (c, (LogicalType n, _))) = (c, n) :: ts
wenzelm@14790
   109
      | add_log_type (ts, _) = ts;
wenzelm@14790
   110
    val log_types =
wenzelm@14790
   111
      Symtab.foldl add_log_type ([], types)
wenzelm@14790
   112
      |> Library.sort (Library.int_ord o pairself #2) |> map #1;
wenzelm@14790
   113
    val witness =
wenzelm@14790
   114
      (case Sorts.witness_sorts (classes, arities) log_types [] [Graph.keys classes] of
wenzelm@14790
   115
        [w] => Some w | _ => None);
wenzelm@14790
   116
  in make_tsig (classes, default, types, arities, log_types, witness) end;
wenzelm@14790
   117
wenzelm@14790
   118
fun change_tsig f (TSig {classes, default, types, arities, log_types = _, witness = _}) =
wenzelm@14790
   119
  build_tsig (f (classes, default, types, arities));
wenzelm@14790
   120
wenzelm@14790
   121
val empty_tsig = build_tsig (Graph.empty, [], Symtab.empty, Symtab.empty);
wenzelm@14790
   122
wenzelm@14790
   123
wenzelm@14790
   124
(* classes and sorts *)
wenzelm@14790
   125
wenzelm@14790
   126
fun classes (TSig {classes = C, ...}) = Graph.keys C;
wenzelm@14790
   127
fun defaultS (TSig {default, ...}) = default;
wenzelm@14790
   128
fun logical_types (TSig {log_types, ...}) = log_types;
wenzelm@14790
   129
fun universal_witness (TSig {witness, ...}) = witness;
wenzelm@14790
   130
wenzelm@14790
   131
fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq classes;
wenzelm@14790
   132
fun subsort (TSig {classes, ...}) = Sorts.sort_le classes;
wenzelm@14790
   133
fun of_sort (TSig {classes, arities, ...}) = Sorts.of_sort (classes, arities);
wenzelm@14790
   134
fun norm_sort (TSig {classes, ...}) = Sorts.norm_sort classes;
wenzelm@14790
   135
wenzelm@14790
   136
fun cert_class (TSig {classes, ...}) c =
wenzelm@14790
   137
  if can (Graph.get_node classes) c then c
wenzelm@14790
   138
  else raise TYPE ("Undeclared class: " ^ quote c, [], []);
wenzelm@14790
   139
wenzelm@14790
   140
fun cert_sort tsig = norm_sort tsig o map (cert_class tsig);
wenzelm@14790
   141
wenzelm@14790
   142
fun witness_sorts (tsig as TSig {classes, arities, log_types, ...}) =
wenzelm@14790
   143
  Sorts.witness_sorts (classes, arities) log_types;
wenzelm@14790
   144
wenzelm@14790
   145
wenzelm@14790
   146
(* certified types *)
wenzelm@14790
   147
wenzelm@14790
   148
fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
wenzelm@14830
   149
fun undecl_type c = "Undeclared type constructor: " ^ quote c;
wenzelm@14830
   150
wenzelm@14830
   151
local
wenzelm@14790
   152
wenzelm@14790
   153
fun inst_typ tye =
wenzelm@14790
   154
  let
wenzelm@14790
   155
    fun inst (var as (v, _)) =
wenzelm@14790
   156
      (case assoc_string_int (tye, v) of
wenzelm@14790
   157
        Some U => inst_typ tye U
wenzelm@14790
   158
      | None => TVar var);
wenzelm@14790
   159
  in map_type_tvar inst end;
wenzelm@14790
   160
wenzelm@14790
   161
fun norm_typ (tsig as TSig {types, ...}) ty =
wenzelm@14790
   162
  let
wenzelm@14790
   163
    val idx = Term.maxidx_of_typ ty + 1;
wenzelm@14790
   164
wenzelm@14790
   165
    fun norm (Type (a, Ts)) =
wenzelm@14790
   166
          (case Symtab.lookup (types, a) of
wenzelm@14790
   167
            Some (Abbreviation (vs, U), _) =>
wenzelm@14790
   168
              norm (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U))
wenzelm@14790
   169
          | _ => Type (a, map norm Ts))
wenzelm@14790
   170
      | norm (TFree (x, S)) = TFree (x, norm_sort tsig S)
wenzelm@14790
   171
      | norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S);
wenzelm@14790
   172
wenzelm@14790
   173
    val ty' = norm ty;
wenzelm@14790
   174
  in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
wenzelm@14790
   175
wenzelm@14790
   176
fun certify_typ normalize syntax tsig ty =
wenzelm@14790
   177
  let
wenzelm@14790
   178
    val TSig {types, ...} = tsig;
wenzelm@14790
   179
    fun err msg = raise TYPE (msg, [ty], []);
wenzelm@14790
   180
wenzelm@14790
   181
    fun check_sort S = (map (cert_class tsig) S; ());
wenzelm@14790
   182
wenzelm@14790
   183
    fun check_typ (Type (c, Ts)) =
wenzelm@14790
   184
          let fun nargs n = if length Ts <> n then err (bad_nargs c) else () in
wenzelm@14790
   185
            (case Symtab.lookup (types, c) of
wenzelm@14790
   186
              Some (LogicalType n, _) => nargs n
wenzelm@14790
   187
            | Some (Abbreviation (vs, _), _) => nargs (length vs)
wenzelm@14790
   188
            | Some (Nonterminal, _) => nargs 0
wenzelm@14830
   189
            | None => err (undecl_type c));
wenzelm@14790
   190
            seq check_typ Ts
wenzelm@14790
   191
          end
wenzelm@14790
   192
    | check_typ (TFree (_, S)) = check_sort S
wenzelm@14790
   193
    | check_typ (TVar ((x, i), S)) =
wenzelm@14790
   194
        if i < 0 then err ("Malformed type variable: " ^ quote (Term.string_of_vname (x, i)))
wenzelm@14790
   195
        else check_sort S;
wenzelm@14790
   196
wenzelm@14790
   197
    fun no_syntax (Type (c, Ts)) =
wenzelm@14790
   198
          (case Symtab.lookup (types, c) of
wenzelm@14790
   199
            Some (Nonterminal, _) =>
wenzelm@14790
   200
              err ("Illegal occurrence of syntactic type: " ^ quote c)
wenzelm@14790
   201
          | _ => seq no_syntax Ts)
wenzelm@14790
   202
      | no_syntax _ = ();
wenzelm@14790
   203
wenzelm@14790
   204
    val _ = check_typ ty;
wenzelm@14790
   205
    val ty' = if normalize orelse not syntax then norm_typ tsig ty else ty;
wenzelm@14790
   206
    val _ = if not syntax then no_syntax ty' else ();
wenzelm@14790
   207
  in ty' end;
wenzelm@14790
   208
wenzelm@14830
   209
in
wenzelm@14830
   210
wenzelm@14790
   211
val cert_typ         = certify_typ true false;
wenzelm@14790
   212
val cert_typ_syntax  = certify_typ true true;
wenzelm@14790
   213
val cert_typ_raw     = certify_typ false true;
wenzelm@14790
   214
wenzelm@14830
   215
end;
wenzelm@14790
   216
wenzelm@14790
   217
wenzelm@14790
   218
(** special treatment of type vars **)
wenzelm@14790
   219
wenzelm@14790
   220
(* strip_sorts *)
wenzelm@14790
   221
wenzelm@14790
   222
fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts)
wenzelm@14790
   223
  | strip_sorts (TFree (x, _)) = TFree (x, [])
wenzelm@14790
   224
  | strip_sorts (TVar (xi, _)) = TVar (xi, []);
wenzelm@14790
   225
wenzelm@14790
   226
wenzelm@14790
   227
(* no_tvars *)
wenzelm@621
   228
wenzelm@621
   229
fun no_tvars T =
wenzelm@12501
   230
  (case typ_tvars T of [] => T
wenzelm@12501
   231
  | vs => raise TYPE ("Illegal schematic type variable(s): " ^
wenzelm@14830
   232
      commas_quote (map (Term.string_of_vname o #1) vs), [T], []));
wenzelm@621
   233
wenzelm@7641
   234
wenzelm@2964
   235
(* varify, unvarify *)
wenzelm@621
   236
wenzelm@2964
   237
val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S));
wenzelm@2964
   238
wenzelm@621
   239
fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts)
wenzelm@621
   240
  | unvarifyT (TVar ((a, 0), S)) = TFree (a, S)
wenzelm@621
   241
  | unvarifyT T = T;
wenzelm@621
   242
wenzelm@621
   243
fun varify (t, fixed) =
wenzelm@621
   244
  let
wenzelm@621
   245
    val fs = add_term_tfree_names (t, []) \\ fixed;
wenzelm@621
   246
    val ixns = add_term_tvar_ixns (t, []);
wenzelm@12501
   247
    val fmap = fs ~~ map (rpair 0) (variantlist (fs, map #1 ixns))
wenzelm@2964
   248
    fun thaw (f as (a, S)) =
wenzelm@2964
   249
      (case assoc (fmap, a) of
wenzelm@2964
   250
        None => TFree f
wenzelm@12501
   251
      | Some b => TVar (b, S));
wenzelm@12501
   252
  in (map_term_types (map_type_tfree thaw) t, fmap) end;
wenzelm@2964
   253
wenzelm@2964
   254
wenzelm@7641
   255
(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *)
paulson@3411
   256
wenzelm@7641
   257
local
wenzelm@7641
   258
wenzelm@7641
   259
fun new_name (ix, (pairs,used)) =
paulson@3411
   260
      let val v = variant used (string_of_indexname ix)
paulson@3411
   261
      in  ((ix,v)::pairs, v::used)  end;
wenzelm@621
   262
wenzelm@7641
   263
fun freeze_one alist (ix,sort) =
wenzelm@3790
   264
  TFree (the (assoc (alist, ix)), sort)
wenzelm@4142
   265
    handle OPTION =>
wenzelm@3790
   266
      raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
wenzelm@2964
   267
wenzelm@7641
   268
fun thaw_one alist (a,sort) = TVar (the (assoc (alist,a)), sort)
wenzelm@14830
   269
  handle OPTION => TFree(a, sort);
wenzelm@416
   270
wenzelm@10495
   271
in
wenzelm@10495
   272
wenzelm@10495
   273
(*this sort of code could replace unvarifyT*)
wenzelm@7641
   274
fun freeze_thaw_type T =
wenzelm@7641
   275
  let
wenzelm@7641
   276
    val used = add_typ_tfree_names (T, [])
wenzelm@7641
   277
    and tvars = map #1 (add_typ_tvars (T, []));
wenzelm@7641
   278
    val (alist, _) = foldr new_name (tvars, ([], used));
wenzelm@7641
   279
  in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;
wenzelm@7641
   280
paulson@3411
   281
fun freeze_thaw t =
wenzelm@7641
   282
  let
wenzelm@7641
   283
    val used = it_term_types add_typ_tfree_names (t, [])
wenzelm@7641
   284
    and tvars = map #1 (it_term_types add_typ_tvars (t, []));
wenzelm@7641
   285
    val (alist, _) = foldr new_name (tvars, ([], used));
wenzelm@7641
   286
  in
wenzelm@7641
   287
    (case alist of
wenzelm@7641
   288
      [] => (t, fn x => x) (*nothing to do!*)
wenzelm@7641
   289
    | _ => (map_term_types (map_type_tvar (freeze_one alist)) t,
wenzelm@7641
   290
      map_term_types (map_type_tfree (thaw_one (map swap alist)))))
wenzelm@7641
   291
  end;
wenzelm@7641
   292
wenzelm@7641
   293
end;
wenzelm@7641
   294
wenzelm@256
   295
wenzelm@256
   296
wenzelm@14790
   297
(** matching and unification of types **)
wenzelm@8899
   298
wenzelm@14790
   299
(* instantiation *)
wenzelm@256
   300
wenzelm@14830
   301
fun type_of_sort pp tsig (T, S) =
wenzelm@14790
   302
  if of_sort tsig (T, S) then T
wenzelm@14830
   303
  else raise TYPE ("Type not of sort " ^ Pretty.string_of_sort pp S, [T], []);
clasohm@0
   304
wenzelm@14830
   305
fun inst_typ_tvars pp tsig tye =
wenzelm@14790
   306
  let
wenzelm@14790
   307
    fun inst (var as (v, S)) =
wenzelm@14790
   308
      (case assoc_string_int (tye, v) of
wenzelm@14830
   309
        Some U => type_of_sort pp tsig (U, S)
wenzelm@14790
   310
      | None => TVar var);
nipkow@949
   311
  in map_type_tvar inst end;
clasohm@0
   312
wenzelm@14830
   313
fun inst_term_tvars _ _ [] t = t
wenzelm@14830
   314
  | inst_term_tvars pp tsig tye t = map_term_types (inst_typ_tvars pp tsig tye) t;
clasohm@0
   315
clasohm@0
   316
wenzelm@14790
   317
(* matching *)
clasohm@0
   318
wenzelm@2964
   319
exception TYPE_MATCH;
clasohm@0
   320
wenzelm@2964
   321
fun typ_match tsig =
wenzelm@2964
   322
  let
wenzelm@2964
   323
    fun match (subs, (TVar (v, S), T)) =
berghofe@8406
   324
          (case Vartab.lookup (subs, v) of
wenzelm@14830
   325
            None => (Vartab.update_new ((v, type_of_sort Pretty.pp_undef tsig (T, S)), subs)
wenzelm@2964
   326
              handle TYPE _ => raise TYPE_MATCH)
wenzelm@2964
   327
          | Some U => if U = T then subs else raise TYPE_MATCH)
wenzelm@2964
   328
      | match (subs, (Type (a, Ts), Type (b, Us))) =
wenzelm@2964
   329
          if a <> b then raise TYPE_MATCH
wenzelm@2964
   330
          else foldl match (subs, Ts ~~ Us)
wenzelm@2964
   331
      | match (subs, (TFree x, TFree y)) =
wenzelm@2964
   332
          if x = y then subs else raise TYPE_MATCH
wenzelm@2964
   333
      | match _ = raise TYPE_MATCH;
wenzelm@2964
   334
  in match end;
clasohm@0
   335
wenzelm@14790
   336
fun typ_instance tsig (T, U) =
berghofe@8406
   337
  (typ_match tsig (Vartab.empty, (U, T)); true) handle TYPE_MATCH => false;
wenzelm@2964
   338
clasohm@0
   339
wenzelm@14790
   340
(* unification *)
wenzelm@2964
   341
clasohm@0
   342
exception TUNIFY;
clasohm@0
   343
wenzelm@14790
   344
(*occurs_check*)
wenzelm@2964
   345
fun occurs v tye =
wenzelm@2964
   346
  let
wenzelm@2964
   347
    fun occ (Type (_, Ts)) = exists occ Ts
wenzelm@2964
   348
      | occ (TFree _) = false
wenzelm@2964
   349
      | occ (TVar (w, _)) =
wenzelm@2964
   350
          eq_ix (v, w) orelse
berghofe@8406
   351
            (case Vartab.lookup (tye, w) of
wenzelm@2964
   352
              None => false
wenzelm@2964
   353
            | Some U => occ U);
clasohm@0
   354
  in occ end;
clasohm@0
   355
wenzelm@14790
   356
(*chase variable assignments; if devar returns a type var then it must be unassigned*)
wenzelm@2964
   357
fun devar (T as TVar (v, _), tye) =
berghofe@8406
   358
      (case  Vartab.lookup (tye, v) of
wenzelm@2964
   359
        Some U => devar (U, tye)
wenzelm@2964
   360
      | None => T)
wenzelm@256
   361
  | devar (T, tye) = T;
clasohm@0
   362
wenzelm@14790
   363
fun unify (tsig as TSig {classes, arities, ...}) (tyenv, maxidx) TU =
wenzelm@2964
   364
  let
wenzelm@2964
   365
    val tyvar_count = ref maxidx;
wenzelm@2964
   366
    fun gen_tyvar S = TVar (("'a", inc tyvar_count), S);
wenzelm@2964
   367
wenzelm@2964
   368
    fun mg_domain a S =
wenzelm@14790
   369
      Sorts.mg_domain (classes, arities) a S handle Sorts.DOMAIN _ => raise TUNIFY;
wenzelm@2964
   370
wenzelm@2964
   371
    fun meet ((_, []), tye) = tye
wenzelm@2964
   372
      | meet ((TVar (xi, S'), S), tye) =
wenzelm@14790
   373
          if Sorts.sort_le classes (S', S) then tye
berghofe@13666
   374
          else Vartab.update_new ((xi,
wenzelm@14790
   375
            gen_tyvar (Sorts.inter_sort classes (S', S))), tye)
wenzelm@2964
   376
      | meet ((TFree (_, S'), S), tye) =
wenzelm@14790
   377
          if Sorts.sort_le classes (S', S) then tye
wenzelm@2964
   378
          else raise TUNIFY
wenzelm@2964
   379
      | meet ((Type (a, Ts), S), tye) = meets ((Ts, mg_domain a S), tye)
wenzelm@2964
   380
    and meets (([], []), tye) = tye
wenzelm@2964
   381
      | meets ((T :: Ts, S :: Ss), tye) =
wenzelm@2964
   382
          meets ((Ts, Ss), meet ((devar (T, tye), S), tye))
wenzelm@2964
   383
      | meets _ = sys_error "meets";
wenzelm@2964
   384
wenzelm@2964
   385
    fun unif ((ty1, ty2), tye) =
wenzelm@2964
   386
      (case (devar (ty1, tye), devar (ty2, tye)) of
wenzelm@2964
   387
        (T as TVar (v, S1), U as TVar (w, S2)) =>
wenzelm@2964
   388
          if eq_ix (v, w) then tye
wenzelm@14790
   389
          else if Sorts.sort_le classes (S1, S2) then
berghofe@13666
   390
            Vartab.update_new ((w, T), tye)
wenzelm@14790
   391
          else if Sorts.sort_le classes (S2, S1) then
berghofe@13666
   392
            Vartab.update_new ((v, U), tye)
wenzelm@2964
   393
          else
wenzelm@14790
   394
            let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
berghofe@13666
   395
              Vartab.update_new ((v, S), Vartab.update_new ((w, S), tye))
wenzelm@2964
   396
            end
wenzelm@2964
   397
      | (TVar (v, S), T) =>
wenzelm@2964
   398
          if occurs v tye T then raise TUNIFY
berghofe@13666
   399
          else meet ((T, S), Vartab.update_new ((v, T), tye))
wenzelm@2964
   400
      | (T, TVar (v, S)) =>
wenzelm@2964
   401
          if occurs v tye T then raise TUNIFY
berghofe@13666
   402
          else meet ((T, S), Vartab.update_new ((v, T), tye))
wenzelm@2964
   403
      | (Type (a, Ts), Type (b, Us)) =>
wenzelm@2964
   404
          if a <> b then raise TUNIFY
wenzelm@2964
   405
          else foldr unif (Ts ~~ Us, tye)
wenzelm@2964
   406
      | (T, U) => if T = U then tye else raise TUNIFY);
wenzelm@12528
   407
  in (unif (TU, tyenv), ! tyvar_count) end;
clasohm@0
   408
wenzelm@14790
   409
(*purely structural unification *)
wenzelm@450
   410
fun raw_unify (ty1, ty2) =
wenzelm@14790
   411
  (unify empty_tsig (Vartab.empty, 0) (strip_sorts ty1, strip_sorts ty2); true)
wenzelm@450
   412
    handle TUNIFY => false;
wenzelm@450
   413
wenzelm@450
   414
clasohm@0
   415
wenzelm@14790
   416
(** extend and merge type signatures **)
wenzelm@14790
   417
wenzelm@14790
   418
(* arities *)
wenzelm@14790
   419
wenzelm@14790
   420
local
nipkow@1435
   421
wenzelm@14790
   422
fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t);
wenzelm@14830
   423
wenzelm@14830
   424
fun for_classes _ None = ""
wenzelm@14830
   425
  | for_classes pp (Some (c1, c2)) =
wenzelm@14830
   426
      " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
wenzelm@3790
   427
wenzelm@14830
   428
fun err_conflict pp t cc (c, Ss) (c', Ss') =
wenzelm@14830
   429
  error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
wenzelm@14830
   430
    Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
wenzelm@14830
   431
    Pretty.string_of_arity pp (t, Ss', [c']));
wenzelm@14790
   432
wenzelm@14830
   433
fun coregular pp C t (c, Ss) ars =
wenzelm@3790
   434
  let
wenzelm@14790
   435
    fun conflict (c', Ss') =
wenzelm@14790
   436
      if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then
wenzelm@14790
   437
        Some ((c, c'), (c', Ss'))
wenzelm@14790
   438
      else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then
wenzelm@14790
   439
        Some ((c', c), (c', Ss'))
wenzelm@14790
   440
      else None;
wenzelm@14790
   441
  in
wenzelm@14790
   442
    (case Library.get_first conflict ars of
wenzelm@14830
   443
      Some ((c1, c2), (c', Ss')) => err_conflict pp t (Some (c1, c2)) (c, Ss) (c', Ss')
wenzelm@14790
   444
    | None => (c, Ss) :: ars)
wenzelm@14790
   445
  end;
wenzelm@3790
   446
wenzelm@14830
   447
fun insert pp C t ((c, Ss), ars) =
wenzelm@14790
   448
  (case assoc_string (ars, c) of
wenzelm@14830
   449
    None => coregular pp C t (c, Ss) ars
wenzelm@14790
   450
  | Some Ss' =>
wenzelm@14790
   451
      if Sorts.sorts_le C (Ss, Ss') then ars
wenzelm@14790
   452
      else if Sorts.sorts_le C (Ss', Ss)
wenzelm@14830
   453
      then coregular pp C t (c, Ss) (ars \ (c, Ss'))
wenzelm@14830
   454
      else err_conflict pp t None (c, Ss) (c, Ss'));
wenzelm@2587
   455
wenzelm@14790
   456
fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]);
wenzelm@3790
   457
wenzelm@14830
   458
fun insert_arities pp classes (arities, (t, ars)) =
wenzelm@14790
   459
  let val ars' =
wenzelm@14790
   460
    Symtab.lookup_multi (arities, t)
wenzelm@14830
   461
    |> curry (foldr (insert pp classes t)) (flat (map (complete classes) ars))
wenzelm@14790
   462
  in Symtab.update ((t, ars'), arities) end;
wenzelm@3790
   463
wenzelm@14830
   464
fun insert_table pp classes = Symtab.foldl (fn (arities, (t, ars)) =>
wenzelm@14830
   465
  insert_arities pp classes (arities, (t, map (apsnd (map (Sorts.norm_sort classes))) ars)));
wenzelm@14790
   466
wenzelm@14790
   467
in
wenzelm@2587
   468
wenzelm@14830
   469
fun add_arities pp decls tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
wenzelm@14790
   470
  let
wenzelm@14790
   471
    fun prep (t, Ss, S) =
wenzelm@14790
   472
      (case Symtab.lookup (types, t) of
wenzelm@14790
   473
        Some (LogicalType n, _) =>
wenzelm@14790
   474
          if length Ss = n then
wenzelm@14790
   475
            (t, map (cert_sort tsig) Ss, cert_sort tsig S)
wenzelm@14790
   476
              handle TYPE (msg, _, _) => error msg
wenzelm@14790
   477
          else error (bad_nargs t)
wenzelm@14790
   478
      | Some (decl, _) => err_decl t decl
wenzelm@14830
   479
      | None => error (undecl_type t));
wenzelm@4603
   480
wenzelm@14790
   481
    val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep);
wenzelm@14830
   482
    val arities' = foldl (insert_arities pp classes) (arities, ars);
wenzelm@14790
   483
  in (classes, default, types, arities') end);
wenzelm@12528
   484
wenzelm@14830
   485
fun rebuild_arities pp classes arities =
wenzelm@14830
   486
  insert_table pp classes (Symtab.empty, arities);
wenzelm@14790
   487
wenzelm@14830
   488
fun merge_arities pp classes (arities1, arities2) =
wenzelm@14830
   489
  insert_table pp classes (insert_table pp classes (Symtab.empty, arities1), arities2);
wenzelm@14790
   490
wenzelm@14790
   491
end;
wenzelm@4603
   492
wenzelm@4603
   493
wenzelm@14790
   494
(* classes *)
wenzelm@14790
   495
wenzelm@14790
   496
local
clasohm@0
   497
wenzelm@14790
   498
fun err_dup_classes cs =
wenzelm@14790
   499
  error ("Duplicate declaration of class(es): " ^ commas_quote cs);
wenzelm@14790
   500
wenzelm@14830
   501
fun err_cyclic_classes pp css =
wenzelm@14790
   502
  error (cat_lines (map (fn cs =>
wenzelm@14830
   503
    "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
wenzelm@14790
   504
wenzelm@14830
   505
fun add_class pp (c, cs) tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
wenzelm@2964
   506
  let
wenzelm@14790
   507
    val cs' = map (cert_class tsig) cs
wenzelm@14790
   508
      handle TYPE (msg, _, _) => error msg;
wenzelm@14790
   509
    val classes' = classes |> Graph.new_node (c, stamp ())
wenzelm@14790
   510
      handle Graph.DUP d => err_dup_classes [d];
wenzelm@14790
   511
    val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs')
wenzelm@14830
   512
      handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@14790
   513
  in (classes'', default, types, arities) end);
wenzelm@2964
   514
wenzelm@14790
   515
in
clasohm@0
   516
wenzelm@14830
   517
val add_classes = fold o add_class;
wenzelm@14790
   518
wenzelm@14830
   519
fun add_classrel pp ps tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
wenzelm@14790
   520
  let
wenzelm@14790
   521
    val ps' = map (pairself (cert_class tsig)) ps
wenzelm@14790
   522
      handle TYPE (msg, _, _) => error msg;
wenzelm@14790
   523
    val classes' = classes |> fold Graph.add_edge_trans_acyclic ps'
wenzelm@14830
   524
      handle Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@14790
   525
    val default' = default |> Sorts.norm_sort classes';
wenzelm@14830
   526
    val arities' = arities |> rebuild_arities pp classes';
wenzelm@14790
   527
  in (classes', default', types, arities') end);
wenzelm@14790
   528
wenzelm@14830
   529
fun merge_classes pp CC = Graph.merge_trans_acyclic (op =) CC
wenzelm@14790
   530
  handle Graph.DUPS cs => err_dup_classes cs
wenzelm@14830
   531
    | Graph.CYCLES css => err_cyclic_classes pp css;
wenzelm@14790
   532
wenzelm@14790
   533
end;
wenzelm@14790
   534
wenzelm@14790
   535
wenzelm@14790
   536
(* default sort *)
wenzelm@14790
   537
wenzelm@14790
   538
fun set_defsort S tsig = tsig |> change_tsig (fn (classes, _, types, arities) =>
wenzelm@14790
   539
  (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types, arities));
clasohm@0
   540
clasohm@0
   541
wenzelm@14790
   542
(* types *)
wenzelm@14790
   543
wenzelm@14790
   544
local
wenzelm@14790
   545
wenzelm@14790
   546
fun err_neg_args c =
wenzelm@14790
   547
  error ("Negative number of arguments in type constructor declaration: " ^ quote c);
nipkow@949
   548
wenzelm@14790
   549
fun err_in_decls c decl decl' =
wenzelm@14906
   550
  let val s = str_of_decl decl and s' = str_of_decl decl' in
wenzelm@14906
   551
    if s = s' then error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
wenzelm@14830
   552
    else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
wenzelm@14790
   553
  end;
wenzelm@14790
   554
wenzelm@14790
   555
fun new_decl (c, decl) types =
wenzelm@14790
   556
  (case Symtab.lookup (types, c) of
wenzelm@14790
   557
    Some (decl', _) => err_in_decls c decl decl'
wenzelm@14790
   558
  | None => Symtab.update ((c, (decl, stamp ())), types));
wenzelm@14790
   559
wenzelm@14790
   560
fun the_decl types c = fst (the (Symtab.lookup (types, c)));
wenzelm@14790
   561
wenzelm@14790
   562
fun change_types f = change_tsig (fn (classes, default, types, arities) =>
wenzelm@14790
   563
  (classes, default, f types, arities));
wenzelm@2964
   564
wenzelm@14790
   565
fun add_abbr (a, vs, rhs) tsig = tsig |> change_types (fn types =>
wenzelm@14790
   566
  let
wenzelm@14790
   567
    fun err msg =
wenzelm@14906
   568
      error (msg ^ "\nThe error(s) above occurred in type abbreviation: " ^ quote a);
wenzelm@14790
   569
    val rhs' = strip_sorts (varifyT (no_tvars (cert_typ_syntax tsig rhs)))
wenzelm@14790
   570
      handle TYPE (msg, _, _) => err msg;
wenzelm@14790
   571
  in
wenzelm@14790
   572
    (case duplicates vs of
wenzelm@14790
   573
      [] => []
wenzelm@14790
   574
    | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
wenzelm@14790
   575
    (case gen_rems (op =) (map (#1 o #1) (typ_tvars rhs'), vs) of
wenzelm@14790
   576
      [] => []
wenzelm@14790
   577
    | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
wenzelm@14790
   578
    types |> new_decl (a, Abbreviation (vs, rhs'))
wenzelm@14790
   579
  end);
clasohm@0
   580
wenzelm@14790
   581
in
wenzelm@14790
   582
wenzelm@14790
   583
fun add_types ps = change_types (fold new_decl (ps |> map (fn (c, n) =>
wenzelm@14790
   584
  if n < 0 then err_neg_args c else (c, LogicalType n))));
wenzelm@14790
   585
wenzelm@14790
   586
val add_abbrs = fold add_abbr;
wenzelm@14790
   587
val add_nonterminals = change_types o fold new_decl o map (rpair Nonterminal);
wenzelm@14790
   588
wenzelm@14790
   589
fun merge_types (types1, types2) =
wenzelm@14790
   590
  Symtab.merge Library.eq_snd (types1, types2) handle Symtab.DUPS (d :: _) =>
wenzelm@14790
   591
    err_in_decls d (the_decl types1 d) (the_decl types2 d);
wenzelm@14790
   592
wenzelm@14790
   593
end;
clasohm@0
   594
wenzelm@3790
   595
wenzelm@14790
   596
(* merge type signatures *)
wenzelm@14790
   597
wenzelm@14830
   598
fun merge_tsigs pp (tsig1, tsig2) =
wenzelm@14790
   599
  let
wenzelm@14790
   600
    val (TSig {classes = classes1, default = default1, types = types1, arities = arities1,
wenzelm@14790
   601
      log_types = _, witness = _}) = tsig1;
wenzelm@14790
   602
    val (TSig {classes = classes2, default = default2, types = types2, arities = arities2,
wenzelm@14790
   603
      log_types = _, witness = _}) = tsig2;
wenzelm@14790
   604
wenzelm@14830
   605
    val classes' = merge_classes pp (classes1, classes2);
wenzelm@14790
   606
    val default' = Sorts.inter_sort classes' (default1, default2);
wenzelm@14790
   607
    val types' = merge_types (types1, types2);
wenzelm@14830
   608
    val arities' = merge_arities pp classes' (arities1, arities2);
wenzelm@14790
   609
  in build_tsig (classes', default', types', arities') end;
wenzelm@14790
   610
clasohm@0
   611
end;