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