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