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