src/Pure/type.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 3411 163f8f4a42d7
child 3790 95a47d8bcd69
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
wenzelm@256
     1
(*  Title:      Pure/type.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@416
     3
    Author:     Tobias Nipkow & Lawrence C Paulson
clasohm@0
     4
wenzelm@2964
     5
Type signatures, unification of types, interface to type inference.
clasohm@0
     6
*)
clasohm@0
     7
clasohm@0
     8
signature TYPE =
wenzelm@2964
     9
sig
wenzelm@2964
    10
  (*TFrees vs TVars*)
wenzelm@621
    11
  val no_tvars: typ -> typ
wenzelm@621
    12
  val varifyT: typ -> typ
wenzelm@621
    13
  val unvarifyT: typ -> typ
wenzelm@621
    14
  val varify: term * string list -> term
paulson@3411
    15
  val freeze_thaw : term -> term * (term -> term)
wenzelm@2964
    16
wenzelm@2964
    17
  (*type signatures*)
clasohm@0
    18
  type type_sig
nipkow@200
    19
  val rep_tsig: type_sig ->
wenzelm@256
    20
    {classes: class list,
wenzelm@2964
    21
     classrel: (class * class list) list,
wenzelm@256
    22
     default: sort,
nipkow@963
    23
     tycons: (string * int) list,
wenzelm@621
    24
     abbrs: (string * (string list * typ)) list,
nipkow@963
    25
     arities: (string * (class * sort list) list) list}
clasohm@0
    26
  val defaultS: type_sig -> sort
wenzelm@2964
    27
  val logical_types: type_sig -> string list
wenzelm@2964
    28
wenzelm@2964
    29
  val subsort: type_sig -> sort * sort -> bool
wenzelm@2964
    30
  val eq_sort: type_sig -> sort * sort -> bool
wenzelm@2964
    31
  val norm_sort: type_sig -> sort -> sort
wenzelm@2964
    32
  val nonempty_sort: type_sig -> sort list -> sort -> bool
wenzelm@2964
    33
  val rem_sorts: typ -> typ
wenzelm@2964
    34
wenzelm@416
    35
  val tsig0: type_sig
wenzelm@621
    36
  val ext_tsig_classes: type_sig -> (class * class list) list -> type_sig
wenzelm@2964
    37
  val ext_tsig_classrel: type_sig -> (class * class) list -> type_sig
wenzelm@422
    38
  val ext_tsig_defsort: type_sig -> sort -> type_sig
wenzelm@582
    39
  val ext_tsig_types: type_sig -> (string * int) list -> type_sig
wenzelm@621
    40
  val ext_tsig_abbrs: type_sig -> (string * string list * typ) list -> type_sig
nipkow@963
    41
  val ext_tsig_arities: type_sig -> (string * sort list * sort)list -> type_sig
wenzelm@256
    42
  val merge_tsigs: type_sig * type_sig -> type_sig
wenzelm@2964
    43
wenzelm@2964
    44
  val typ_errors: type_sig -> typ * string list -> string list
wenzelm@256
    45
  val cert_typ: type_sig -> typ -> typ
wenzelm@256
    46
  val norm_typ: type_sig -> typ -> typ
wenzelm@2964
    47
wenzelm@256
    48
  val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term
wenzelm@2964
    49
wenzelm@2964
    50
  (*type matching*)
wenzelm@2964
    51
  exception TYPE_MATCH
wenzelm@256
    52
  val typ_match: type_sig -> (indexname * typ) list * (typ * typ)
wenzelm@256
    53
    -> (indexname * typ) list
wenzelm@2964
    54
  val typ_instance: type_sig * typ * typ -> bool
wenzelm@3175
    55
  val of_sort: type_sig -> typ * sort -> bool
wenzelm@2964
    56
wenzelm@2964
    57
  (*type unification*)
wenzelm@2964
    58
  exception TUNIFY
nipkow@1435
    59
  val unify: type_sig -> int -> (indexname * typ) list -> (typ * typ)
nipkow@1435
    60
    -> (indexname * typ) list * int
wenzelm@450
    61
  val raw_unify: typ * typ -> bool
clasohm@0
    62
wenzelm@2964
    63
  (*type inference*)
wenzelm@2964
    64
  val get_sort: type_sig -> (indexname -> sort option) -> (indexname * sort) list
wenzelm@2964
    65
    -> indexname -> sort
wenzelm@2964
    66
  val constrain: term -> typ -> term
wenzelm@2979
    67
  val infer_types: (term -> Pretty.T) -> (typ -> Pretty.T)
wenzelm@2979
    68
    -> type_sig -> (string -> typ option) -> (indexname -> typ option)
wenzelm@2979
    69
    -> (indexname -> sort option) -> string list -> bool -> typ list -> term list
wenzelm@2964
    70
    -> term list * (indexname * typ) list
wenzelm@2964
    71
end;
wenzelm@2964
    72
wenzelm@2964
    73
structure Type: TYPE =
clasohm@0
    74
struct
clasohm@0
    75
wenzelm@2964
    76
wenzelm@621
    77
(*** TFrees vs TVars ***)
wenzelm@621
    78
wenzelm@621
    79
(*disallow TVars*)
wenzelm@621
    80
fun no_tvars T =
wenzelm@621
    81
  if null (typ_tvars T) then T
wenzelm@621
    82
  else raise_type "Illegal schematic type variable(s)" [T] [];
wenzelm@621
    83
wenzelm@2964
    84
(* varify, unvarify *)
wenzelm@621
    85
wenzelm@2964
    86
val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S));
wenzelm@2964
    87
wenzelm@621
    88
fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts)
wenzelm@621
    89
  | unvarifyT (TVar ((a, 0), S)) = TFree (a, S)
wenzelm@621
    90
  | unvarifyT T = T;
wenzelm@621
    91
wenzelm@621
    92
fun varify (t, fixed) =
wenzelm@621
    93
  let
wenzelm@621
    94
    val fs = add_term_tfree_names (t, []) \\ fixed;
wenzelm@621
    95
    val ixns = add_term_tvar_ixns (t, []);
wenzelm@621
    96
    val fmap = fs ~~ variantlist (fs, map #1 ixns)
wenzelm@2964
    97
    fun thaw (f as (a, S)) =
wenzelm@2964
    98
      (case assoc (fmap, a) of
wenzelm@2964
    99
        None => TFree f
wenzelm@2964
   100
      | Some b => TVar ((b, 0), S));
wenzelm@2964
   101
  in
wenzelm@2964
   102
    map_term_types (map_type_tfree thaw) t
wenzelm@2964
   103
  end;
wenzelm@2964
   104
wenzelm@2964
   105
paulson@3411
   106
(** Freeze TVars in a term; return the "thaw" inverse **)
paulson@3411
   107
paulson@3411
   108
fun newName (ix, (pairs,used)) = 
paulson@3411
   109
      let val v = variant used (string_of_indexname ix)
paulson@3411
   110
      in  ((ix,v)::pairs, v::used)  end;
wenzelm@621
   111
paulson@3411
   112
fun freezeOne alist (ix,sort) = TFree (the (assoc (alist, ix)), sort)
paulson@3411
   113
      handle OPTION _ => 
paulson@3411
   114
	  raise_type ("Failure during freezing of ?" ^
paulson@3411
   115
		      string_of_indexname ix) [] [];
wenzelm@2964
   116
paulson@3411
   117
fun thawOne alist (a,sort) = TVar (the (assoc (alist,a)), sort)
paulson@3411
   118
      handle OPTION _ => TFree(a,sort);
wenzelm@416
   119
paulson@3411
   120
(*This sort of code could replace unvarifyT (?)
paulson@3411
   121
    fun freeze_thaw_type T =
paulson@3411
   122
	let val used = add_typ_tfree_names (T, [])
paulson@3411
   123
	  and tvars = map #1 (add_typ_tvars (T, []))
paulson@3411
   124
	  val (alist, _) = foldr newName (tvars, ([], used))
paulson@3411
   125
	in  (map_type_tvar (freezeOne alist) T, 
paulson@3411
   126
	   map_type_tfree (thawOne (map swap alist)))  
paulson@3411
   127
	end;
paulson@3411
   128
*)
wenzelm@416
   129
paulson@3411
   130
fun freeze_thaw t =
paulson@3411
   131
    let val used = it_term_types add_typ_tfree_names (t, [])
paulson@3411
   132
	and tvars = map #1 (it_term_types add_typ_tvars (t, []))
paulson@3411
   133
	val (alist, _) = foldr newName (tvars, ([], used))
paulson@3411
   134
    in  (map_term_types (map_type_tvar (freezeOne alist)) t,
paulson@3411
   135
	 map_term_types (map_type_tfree (thawOne (map swap alist)))) 
wenzelm@2964
   136
    end;
wenzelm@256
   137
wenzelm@256
   138
wenzelm@416
   139
(*** type signatures ***)
wenzelm@256
   140
wenzelm@2964
   141
(* type type_sig *)
wenzelm@2964
   142
wenzelm@256
   143
(*
wenzelm@256
   144
  classes:
wenzelm@2964
   145
    list of all declared classes;
clasohm@0
   146
wenzelm@2964
   147
  classrel:
wenzelm@2964
   148
    (see Pure/sorts.ML)
wenzelm@256
   149
wenzelm@256
   150
  default:
wenzelm@2964
   151
    default sort attached to all unconstrained type vars;
wenzelm@256
   152
nipkow@963
   153
  tycons:
wenzelm@2964
   154
    association list of all declared types with the number of their
wenzelm@256
   155
    arguments;
wenzelm@256
   156
wenzelm@256
   157
  abbrs:
wenzelm@2964
   158
    association list of type abbreviations;
wenzelm@256
   159
nipkow@963
   160
  arities:
wenzelm@2964
   161
    (see Pure/sorts.ML)
clasohm@0
   162
*)
clasohm@0
   163
wenzelm@256
   164
datatype type_sig =
wenzelm@256
   165
  TySg of {
wenzelm@256
   166
    classes: class list,
wenzelm@2964
   167
    classrel: (class * class list) list,
wenzelm@256
   168
    default: sort,
nipkow@963
   169
    tycons: (string * int) list,
wenzelm@621
   170
    abbrs: (string * (string list * typ)) list,
wenzelm@2964
   171
    arities: (string * (class * sort list) list) list};
wenzelm@256
   172
nipkow@189
   173
fun rep_tsig (TySg comps) = comps;
clasohm@0
   174
wenzelm@256
   175
fun defaultS (TySg {default, ...}) = default;
wenzelm@256
   176
wenzelm@2964
   177
fun logical_types (TySg {classrel, arities, tycons, ...}) =
wenzelm@2964
   178
  let
wenzelm@2964
   179
    fun log_class c = Sorts.class_le classrel (c, logicC);
wenzelm@2964
   180
    fun log_type t = exists (log_class o #1) (assocs arities t);
wenzelm@2964
   181
  in
wenzelm@2964
   182
    filter log_type (map #1 tycons)
wenzelm@2964
   183
  end;
wenzelm@2964
   184
wenzelm@2964
   185
wenzelm@2964
   186
(* sorts *)
wenzelm@2964
   187
wenzelm@2964
   188
(* FIXME declared!? *)
wenzelm@2964
   189
wenzelm@2964
   190
fun subsort (TySg {classrel, ...}) = Sorts.sort_le classrel;
wenzelm@2964
   191
fun eq_sort (TySg {classrel, ...}) = Sorts.sort_eq classrel;
wenzelm@2964
   192
fun norm_sort (TySg {classrel, ...}) = Sorts.norm_sort classrel;
wenzelm@2964
   193
wenzelm@2964
   194
fun nonempty_sort (tsig as TySg {classrel, arities, ...}) hyps S =
wenzelm@2964
   195
  Sorts.nonempty_sort classrel arities hyps S;
wenzelm@2964
   196
wenzelm@2964
   197
fun rem_sorts (Type (a, tys)) = Type (a, map rem_sorts tys)
wenzelm@2964
   198
  | rem_sorts (TFree (x, _)) = TFree (x, [])
wenzelm@2964
   199
  | rem_sorts (TVar (xi, _)) = TVar (xi, []);
wenzelm@2964
   200
wenzelm@256
   201
wenzelm@582
   202
(* error messages *)
wenzelm@256
   203
wenzelm@416
   204
fun undcl_class c = "Undeclared class " ^ quote c;
paulson@2233
   205
fun err_undcl_class s = error (undcl_class s);
clasohm@0
   206
wenzelm@422
   207
fun err_dup_classes cs =
wenzelm@422
   208
  error ("Duplicate declaration of class(es) " ^ commas_quote cs);
wenzelm@416
   209
wenzelm@416
   210
fun undcl_type c = "Undeclared type constructor " ^ quote c;
wenzelm@256
   211
wenzelm@582
   212
fun err_neg_args c =
wenzelm@582
   213
  error ("Negative number of arguments of type constructor " ^ quote c);
wenzelm@582
   214
wenzelm@416
   215
fun err_dup_tycon c =
wenzelm@416
   216
  error ("Duplicate declaration of type constructor " ^ quote c);
wenzelm@416
   217
wenzelm@621
   218
fun dup_tyabbrs ts =
wenzelm@621
   219
  "Duplicate declaration of type abbreviation(s) " ^ commas_quote ts;
wenzelm@416
   220
wenzelm@416
   221
fun ty_confl c = "Conflicting type constructor and abbreviation " ^ quote c;
clasohm@0
   222
clasohm@0
   223
wenzelm@2964
   224
(* FIXME err_undcl_class! *)
clasohm@0
   225
(* 'leq' checks the partial order on classes according to the
wenzelm@2964
   226
   statements in the association list 'a' (i.e. 'classrel')
clasohm@0
   227
*)
clasohm@0
   228
wenzelm@256
   229
fun less a (C, D) = case assoc (a, C) of
paulson@2182
   230
     Some ss => D mem_string ss
wenzelm@621
   231
   | None => err_undcl_class C;
clasohm@0
   232
wenzelm@256
   233
fun leq a (C, D)  =  C = D orelse less a (C, D);
clasohm@0
   234
clasohm@0
   235
clasohm@0
   236
wenzelm@2964
   237
(* FIXME *)
nipkow@200
   238
(*Instantiation of type variables in types*)
nipkow@200
   239
(*Pre: instantiations obey restrictions! *)
nipkow@200
   240
fun inst_typ tye =
nipkow@949
   241
  let fun inst(var as (v, _)) = case assoc(tye, v) of
nipkow@949
   242
                                  Some U => inst_typ tye U
nipkow@949
   243
                                | None => TVar(var)
nipkow@949
   244
  in map_type_tvar inst end;
clasohm@0
   245
clasohm@0
   246
wenzelm@3175
   247
wenzelm@3175
   248
fun of_sort (TySg {classrel, arities, ...}) = Sorts.of_sort classrel arities;
wenzelm@3175
   249
wenzelm@3175
   250
fun check_has_sort (tsig, T, S) =
wenzelm@3175
   251
  if of_sort tsig (T, S) then ()
wenzelm@2991
   252
  else raise_type ("Type not of sort " ^ Sorts.str_of_sort S) [T] [];
clasohm@0
   253
clasohm@0
   254
clasohm@0
   255
(*Instantiation of type variables in types *)
wenzelm@256
   256
fun inst_typ_tvars(tsig, tye) =
nipkow@949
   257
  let fun inst(var as (v, S)) = case assoc(tye, v) of
nipkow@949
   258
              Some U => (check_has_sort(tsig, U, S); U)
nipkow@949
   259
            | None => TVar(var)
nipkow@949
   260
  in map_type_tvar inst end;
clasohm@0
   261
clasohm@0
   262
(*Instantiation of type variables in terms *)
paulson@2617
   263
fun inst_term_tvars (_,[]) t = t
paulson@2617
   264
  | inst_term_tvars arg    t = map_term_types (inst_typ_tvars arg) t;
nipkow@200
   265
nipkow@200
   266
clasohm@1484
   267
(* norm_typ *)
nipkow@200
   268
wenzelm@2991
   269
(*expand abbreviations and normalize sorts*)
wenzelm@2991
   270
fun norm_typ (tsig as TySg {abbrs, ...}) ty =
wenzelm@256
   271
  let
wenzelm@621
   272
    val idx = maxidx_of_typ ty + 1;
wenzelm@621
   273
wenzelm@2991
   274
    fun norm (Type (a, Ts)) =
wenzelm@256
   275
          (case assoc (abbrs, a) of
wenzelm@2991
   276
            Some (vs, U) => norm (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U))
wenzelm@2991
   277
          | None => Type (a, map norm Ts))
wenzelm@2991
   278
      | norm (TFree (x, S)) = TFree (x, norm_sort tsig S)
wenzelm@2991
   279
      | norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S);
wenzelm@256
   280
  in
wenzelm@2991
   281
    norm ty
wenzelm@256
   282
  end;
wenzelm@256
   283
wenzelm@256
   284
wenzelm@256
   285
wenzelm@256
   286
wenzelm@256
   287
wenzelm@256
   288
(** build type signatures **)
wenzelm@256
   289
wenzelm@2964
   290
fun make_tsig (classes, classrel, default, tycons, abbrs, arities) =
wenzelm@2964
   291
  TySg {classes = classes, classrel = classrel, default = default,
nipkow@963
   292
    tycons = tycons, abbrs = abbrs, arities = arities};
wenzelm@416
   293
wenzelm@416
   294
val tsig0 = make_tsig ([], [], [], [], [], []);
wenzelm@256
   295
clasohm@0
   296
wenzelm@1215
   297
wenzelm@1215
   298
wenzelm@416
   299
(* typ_errors *)
wenzelm@256
   300
wenzelm@416
   301
(*check validity of (not necessarily normal) type; accumulate error messages*)
wenzelm@256
   302
wenzelm@416
   303
fun typ_errors tsig (typ, errors) =
wenzelm@256
   304
  let
nipkow@963
   305
    val TySg {classes, tycons, abbrs, ...} = tsig;
wenzelm@416
   306
wenzelm@416
   307
    fun class_err (errs, c) =
paulson@2182
   308
      if c mem_string classes then errs
paulson@2182
   309
      else undcl_class c ins_string errs;
wenzelm@256
   310
wenzelm@256
   311
    val sort_err = foldl class_err;
clasohm@0
   312
wenzelm@256
   313
    fun typ_errs (Type (c, Us), errs) =
wenzelm@256
   314
          let
wenzelm@256
   315
            val errs' = foldr typ_errs (Us, errs);
wenzelm@256
   316
            fun nargs n =
wenzelm@256
   317
              if n = length Us then errs'
paulson@2182
   318
              else ("Wrong number of arguments: " ^ quote c) ins_string errs';
wenzelm@256
   319
          in
nipkow@963
   320
            (case assoc (tycons, c) of
wenzelm@256
   321
              Some n => nargs n
wenzelm@256
   322
            | None =>
wenzelm@256
   323
                (case assoc (abbrs, c) of
wenzelm@256
   324
                  Some (vs, _) => nargs (length vs)
paulson@2182
   325
                | None => undcl_type c ins_string errs))
wenzelm@256
   326
          end
wenzelm@256
   327
    | typ_errs (TFree (_, S), errs) = sort_err (errs, S)
wenzelm@416
   328
    | typ_errs (TVar ((x, i), S), errs) =
wenzelm@416
   329
        if i < 0 then
paulson@2182
   330
          ("Negative index for TVar " ^ quote x) ins_string sort_err (errs, S)
wenzelm@416
   331
        else sort_err (errs, S);
wenzelm@256
   332
  in
wenzelm@416
   333
    typ_errs (typ, errors)
wenzelm@256
   334
  end;
wenzelm@256
   335
wenzelm@256
   336
wenzelm@256
   337
(* cert_typ *)
wenzelm@256
   338
wenzelm@2964
   339
(*check and normalize typ wrt. tsig*)           (*exception TYPE*)
wenzelm@2964
   340
fun cert_typ tsig T =
wenzelm@2964
   341
  (case typ_errors tsig (T, []) of
wenzelm@2964
   342
    [] => norm_typ tsig T
wenzelm@2964
   343
  | errs => raise_type (cat_lines errs) [T] []);
wenzelm@256
   344
wenzelm@256
   345
wenzelm@256
   346
wenzelm@422
   347
(** merge type signatures **)
wenzelm@256
   348
wenzelm@422
   349
(*'assoc_union' merges two association lists if the contents associated
wenzelm@422
   350
  the keys are lists*)
clasohm@0
   351
wenzelm@422
   352
fun assoc_union (as1, []) = as1
wenzelm@422
   353
  | assoc_union (as1, (key, l2) :: as2) =
paulson@2182
   354
      (case assoc_string (as1, key) of
wenzelm@2964
   355
        Some l1 => assoc_union
wenzelm@2964
   356
                      (overwrite (as1, (key, l1 union_string l2)), as2)
wenzelm@422
   357
      | None => assoc_union ((key, l2) :: as1, as2));
clasohm@0
   358
clasohm@0
   359
wenzelm@2964
   360
(* merge classrel *)
clasohm@0
   361
wenzelm@2964
   362
fun merge_classrel (classrel1, classrel2) =
wenzelm@2964
   363
  let val classrel = transitive_closure (assoc_union (classrel1, classrel2))
paulson@2182
   364
  in
wenzelm@2964
   365
    if exists (op mem_string) classrel then
wenzelm@422
   366
      error ("Cyclic class structure!")   (* FIXME improve msg, raise TERM *)
wenzelm@2964
   367
    else classrel
wenzelm@416
   368
  end;
wenzelm@416
   369
wenzelm@416
   370
wenzelm@422
   371
(* coregularity *)
clasohm@0
   372
clasohm@0
   373
(* 'is_unique_decl' checks if there exists just one declaration t:(Ss)C *)
clasohm@0
   374
nipkow@963
   375
fun is_unique_decl ars (t,(C,w)) = case assoc (ars, C) of
clasohm@0
   376
      Some(w1) => if w = w1 then () else
wenzelm@256
   377
        error("There are two declarations\n" ^
wenzelm@2964
   378
              Sorts.str_of_arity(t, w, [C]) ^ " and\n" ^
wenzelm@2964
   379
              Sorts.str_of_arity(t, w1, [C]) ^ "\n" ^
clasohm@0
   380
              "with the same result class.")
clasohm@0
   381
    | None => ();
clasohm@0
   382
nipkow@963
   383
(* 'coreg' checks if there are two declarations t:(Ss1)C1 and t:(Ss2)C2
clasohm@0
   384
   such that C1 >= C2 then Ss1 >= Ss2 (elementwise) *)
clasohm@0
   385
nipkow@963
   386
fun coreg_err(t, (C1,w1), (C2,w2)) =
wenzelm@2964
   387
    error("Declarations " ^ Sorts.str_of_arity(t, w1, [C1]) ^ " and "
wenzelm@2964
   388
                          ^ Sorts.str_of_arity(t, w2, [C2]) ^ " are in conflict");
clasohm@0
   389
wenzelm@2964
   390
fun coreg classrel (t, Cw1) =
wenzelm@2964
   391
  let
wenzelm@2964
   392
    fun check1(Cw1 as (C1,w1), Cw2 as (C2,w2)) =
wenzelm@2964
   393
      if leq classrel (C1,C2) then
wenzelm@2964
   394
        if Sorts.sorts_le classrel (w1,w2) then ()
wenzelm@2964
   395
        else coreg_err(t, Cw1, Cw2)
wenzelm@2964
   396
      else ()
wenzelm@2964
   397
    fun check(Cw2) = (check1(Cw1,Cw2); check1(Cw2,Cw1))
nipkow@963
   398
  in seq check end;
clasohm@0
   399
wenzelm@2964
   400
fun add_arity classrel ars (tCw as (_,Cw)) =
wenzelm@2964
   401
      (is_unique_decl ars tCw; coreg classrel tCw ars; Cw ins ars);
clasohm@0
   402
wenzelm@256
   403
fun varying_decls t =
wenzelm@256
   404
  error ("Type constructor " ^ quote t ^ " has varying number of arguments");
clasohm@0
   405
clasohm@0
   406
nipkow@963
   407
(* 'merge_arities' builds the union of two 'arities' lists;
wenzelm@422
   408
   it only checks the two restriction conditions and inserts afterwards
wenzelm@422
   409
   all elements of the second list into the first one *)
wenzelm@422
   410
wenzelm@2964
   411
fun merge_arities classrel =
wenzelm@2964
   412
  let fun test_ar t (ars1, sw) = add_arity classrel ars1 (t,sw);
wenzelm@422
   413
nipkow@963
   414
      fun merge_c (arities1, (c as (t, ars2))) = case assoc (arities1, t) of
nipkow@963
   415
          Some(ars1) =>
nipkow@963
   416
            let val ars = foldl (test_ar t) (ars1, ars2)
nipkow@963
   417
            in overwrite (arities1, (t,ars)) end
nipkow@963
   418
        | None => c::arities1
wenzelm@422
   419
  in foldl merge_c end;
wenzelm@422
   420
nipkow@963
   421
fun add_tycons (tycons, tn as (t,n)) =
nipkow@963
   422
  (case assoc (tycons, t) of
nipkow@963
   423
    Some m => if m = n then tycons else varying_decls t
nipkow@963
   424
  | None => tn :: tycons);
wenzelm@422
   425
wenzelm@422
   426
fun merge_abbrs (abbrs1, abbrs2) =
wenzelm@621
   427
  let val abbrs = abbrs1 union abbrs2 in
wenzelm@621
   428
    (case gen_duplicates eq_fst abbrs of
wenzelm@422
   429
      [] => abbrs
wenzelm@621
   430
    | dups => raise_term (dup_tyabbrs (map fst dups)) [])
wenzelm@422
   431
  end;
wenzelm@422
   432
wenzelm@422
   433
wenzelm@422
   434
(* 'merge_tsigs' takes the above declared functions to merge two type
wenzelm@422
   435
  signatures *)
wenzelm@422
   436
wenzelm@2964
   437
fun merge_tsigs(TySg{classes=classes1, default=default1, classrel=classrel1,
nipkow@963
   438
                     tycons=tycons1, arities=arities1, abbrs=abbrs1},
wenzelm@2964
   439
                TySg{classes=classes2, default=default2, classrel=classrel2,
nipkow@963
   440
                     tycons=tycons2, arities=arities2, abbrs=abbrs2}) =
paulson@2182
   441
  let val classes' = classes1 union_string classes2;
wenzelm@2964
   442
      val classrel' = merge_classrel (classrel1, classrel2);
nipkow@963
   443
      val tycons' = foldl add_tycons (tycons1, tycons2)
wenzelm@2964
   444
      val arities' = merge_arities classrel' (arities1, arities2);
wenzelm@2964
   445
      val default' = Sorts.norm_sort classrel' (default1 @ default2);
wenzelm@422
   446
      val abbrs' = merge_abbrs(abbrs1, abbrs2);
wenzelm@2964
   447
  in make_tsig(classes', classrel', default', tycons', abbrs', arities') end;
wenzelm@422
   448
wenzelm@422
   449
wenzelm@422
   450
wenzelm@422
   451
(*** extend type signatures ***)
wenzelm@422
   452
wenzelm@2964
   453
(** add classes and classrel relations **)
wenzelm@422
   454
wenzelm@422
   455
fun add_classes classes cs =
paulson@2182
   456
  (case cs inter_string classes of
wenzelm@422
   457
    [] => cs @ classes
wenzelm@422
   458
  | dups => err_dup_classes cs);
wenzelm@422
   459
wenzelm@422
   460
wenzelm@2964
   461
(*'add_classrel' adds a tuple consisting of a new class (the new class has
wenzelm@422
   462
  already been inserted into the 'classes' list) and its superclasses (they
wenzelm@2964
   463
  must be declared in 'classes' too) to the 'classrel' list of the given type
wenzelm@422
   464
  signature; furthermore all inherited superclasses according to the
wenzelm@422
   465
  superclasses brought with are inserted and there is a check that there are
wenzelm@422
   466
  no cycles (i.e. C <= D <= C, with C <> D);*)
wenzelm@422
   467
wenzelm@2964
   468
fun add_classrel classes (classrel, (s, ges)) =
wenzelm@621
   469
  let
wenzelm@2964
   470
    fun upd (classrel, s') =
paulson@2182
   471
      if s' mem_string classes then
wenzelm@2964
   472
        let val ges' = the (assoc (classrel, s))
wenzelm@2964
   473
        in case assoc (classrel, s') of
paulson@2182
   474
             Some sups => if s mem_string sups
wenzelm@422
   475
                           then error(" Cycle :" ^ s^" <= "^ s'^" <= "^ s )
wenzelm@2964
   476
                           else overwrite
wenzelm@2964
   477
                                  (classrel, (s, sups union_string ges'))
wenzelm@2964
   478
           | None => classrel
wenzelm@621
   479
        end
wenzelm@621
   480
      else err_undcl_class s'
wenzelm@2964
   481
  in foldl upd (classrel @ [(s, ges)], ges) end;
wenzelm@422
   482
wenzelm@422
   483
wenzelm@422
   484
(* 'extend_classes' inserts all new classes into the corresponding
wenzelm@2964
   485
   lists ('classes', 'classrel') if possible *)
wenzelm@422
   486
wenzelm@2964
   487
fun extend_classes (classes, classrel, new_classes) =
wenzelm@621
   488
  let
wenzelm@621
   489
    val classes' = add_classes classes (map fst new_classes);
wenzelm@2964
   490
    val classrel' = foldl (add_classrel classes') (classrel, new_classes);
wenzelm@2964
   491
  in (classes', classrel') end;
wenzelm@422
   492
wenzelm@422
   493
wenzelm@621
   494
(* ext_tsig_classes *)
wenzelm@621
   495
wenzelm@621
   496
fun ext_tsig_classes tsig new_classes =
wenzelm@621
   497
  let
wenzelm@2964
   498
    val TySg {classes, classrel, default, tycons, abbrs, arities} = tsig;
wenzelm@2964
   499
    val (classes',classrel') = extend_classes (classes,classrel,new_classes);
wenzelm@621
   500
  in
wenzelm@2964
   501
    make_tsig (classes', classrel', default, tycons, abbrs, arities)
wenzelm@621
   502
  end;
wenzelm@621
   503
wenzelm@621
   504
wenzelm@2964
   505
(* ext_tsig_classrel *)
wenzelm@422
   506
wenzelm@2964
   507
fun ext_tsig_classrel tsig pairs =
wenzelm@422
   508
  let
wenzelm@2964
   509
    val TySg {classes, classrel, default, tycons, abbrs, arities} = tsig;
wenzelm@422
   510
wenzelm@422
   511
    (* FIXME clean! *)
wenzelm@2964
   512
    val classrel' =
wenzelm@2964
   513
      merge_classrel (classrel, map (fn (c1, c2) => (c1, [c2])) pairs);
wenzelm@422
   514
  in
wenzelm@2964
   515
    make_tsig (classes, classrel', default, tycons, abbrs, arities)
wenzelm@422
   516
  end;
wenzelm@422
   517
wenzelm@422
   518
wenzelm@422
   519
(* ext_tsig_defsort *)
wenzelm@422
   520
wenzelm@2964
   521
fun ext_tsig_defsort(TySg{classes,classrel,tycons,abbrs,arities,...}) default =
wenzelm@2964
   522
  make_tsig (classes, classrel, default, tycons, abbrs, arities);
wenzelm@422
   523
wenzelm@422
   524
wenzelm@422
   525
wenzelm@621
   526
(** add types **)
wenzelm@582
   527
wenzelm@2964
   528
fun ext_tsig_types (TySg {classes, classrel, default, tycons, abbrs, arities}) ts =
wenzelm@582
   529
  let
wenzelm@582
   530
    fun check_type (c, n) =
wenzelm@582
   531
      if n < 0 then err_neg_args c
nipkow@963
   532
      else if is_some (assoc (tycons, c)) then err_dup_tycon c
paulson@2233
   533
      else if is_some (assoc (abbrs, c)) then error (ty_confl c)
wenzelm@582
   534
      else ();
wenzelm@582
   535
  in
wenzelm@582
   536
    seq check_type ts;
wenzelm@2964
   537
    make_tsig (classes, classrel, default, ts @ tycons, abbrs,
nipkow@963
   538
      map (rpair [] o #1) ts @ arities)
wenzelm@582
   539
  end;
wenzelm@582
   540
wenzelm@582
   541
wenzelm@582
   542
wenzelm@582
   543
(** add type abbreviations **)
wenzelm@582
   544
wenzelm@582
   545
fun abbr_errors tsig (a, (lhs_vs, rhs)) =
wenzelm@582
   546
  let
nipkow@963
   547
    val TySg {tycons, abbrs, ...} = tsig;
wenzelm@621
   548
    val rhs_vs = map (#1 o #1) (typ_tvars rhs);
wenzelm@582
   549
wenzelm@582
   550
    val dup_lhs_vars =
wenzelm@582
   551
      (case duplicates lhs_vs of
wenzelm@582
   552
        [] => []
wenzelm@621
   553
      | vs => ["Duplicate variables on lhs: " ^ commas_quote vs]);
wenzelm@582
   554
wenzelm@582
   555
    val extra_rhs_vars =
wenzelm@582
   556
      (case gen_rems (op =) (rhs_vs, lhs_vs) of
wenzelm@582
   557
        [] => []
wenzelm@621
   558
      | vs => ["Extra variables on rhs: " ^ commas_quote vs]);
wenzelm@582
   559
wenzelm@582
   560
    val tycon_confl =
nipkow@963
   561
      if is_none (assoc (tycons, a)) then []
wenzelm@582
   562
      else [ty_confl a];
wenzelm@582
   563
wenzelm@582
   564
    val dup_abbr =
wenzelm@582
   565
      if is_none (assoc (abbrs, a)) then []
wenzelm@582
   566
      else ["Duplicate declaration of abbreviation"];
wenzelm@582
   567
  in
wenzelm@582
   568
    dup_lhs_vars @ extra_rhs_vars @ tycon_confl @ dup_abbr @
wenzelm@582
   569
      typ_errors tsig (rhs, [])
wenzelm@582
   570
  end;
wenzelm@582
   571
wenzelm@621
   572
fun prep_abbr tsig (a, vs, raw_rhs) =
wenzelm@621
   573
  let
wenzelm@621
   574
    fun err msgs = (seq writeln msgs;
wenzelm@621
   575
      error ("The error(s) above occurred in type abbreviation " ^ quote a));
wenzelm@621
   576
wenzelm@621
   577
    val rhs = rem_sorts (varifyT (no_tvars raw_rhs))
wenzelm@621
   578
      handle TYPE (msg, _, _) => err [msg];
wenzelm@621
   579
    val abbr = (a, (vs, rhs));
wenzelm@621
   580
  in
wenzelm@582
   581
    (case abbr_errors tsig abbr of
wenzelm@621
   582
      [] => abbr
wenzelm@621
   583
    | msgs => err msgs)
wenzelm@582
   584
  end;
wenzelm@582
   585
wenzelm@2964
   586
fun add_abbr (tsig as TySg{classes,classrel,default,tycons,arities,abbrs},
nipkow@963
   587
              abbr) =
wenzelm@621
   588
  make_tsig
wenzelm@2964
   589
    (classes,classrel,default,tycons, prep_abbr tsig abbr :: abbrs, arities);
wenzelm@621
   590
wenzelm@621
   591
fun ext_tsig_abbrs tsig raw_abbrs = foldl add_abbr (tsig, raw_abbrs);
wenzelm@582
   592
wenzelm@582
   593
wenzelm@582
   594
wenzelm@422
   595
(** add arities **)
wenzelm@422
   596
clasohm@0
   597
(* 'coregular' checks
nipkow@963
   598
   - the two restrictions 'is_unique_decl' and 'coreg'
wenzelm@256
   599
   - if the classes in the new type declarations are known in the
clasohm@0
   600
     given type signature
clasohm@0
   601
   - if one type constructor has always the same number of arguments;
wenzelm@256
   602
   if one type declaration has passed all checks it is inserted into
nipkow@963
   603
   the 'arities' association list of the given type signatrure  *)
clasohm@0
   604
wenzelm@2964
   605
fun coregular (classes, classrel, tycons) =
paulson@2182
   606
  let fun ex C = if C mem_string classes then () else err_undcl_class(C);
clasohm@0
   607
nipkow@963
   608
      fun addar(arities, (t, (w, C))) = case assoc(tycons, t) of
clasohm@0
   609
            Some(n) => if n <> length w then varying_decls(t) else
nipkow@963
   610
                     ((seq o seq) ex w; ex C;
nipkow@963
   611
                      let val ars = the (assoc(arities, t))
wenzelm@2964
   612
                          val ars' = add_arity classrel ars (t,(C,w))
nipkow@963
   613
                      in overwrite(arities, (t,ars')) end)
paulson@2233
   614
          | None => error (undcl_type t);
clasohm@0
   615
nipkow@963
   616
  in addar end;
clasohm@0
   617
clasohm@0
   618
nipkow@963
   619
(* 'close' extends the 'arities' association list after all new type
clasohm@0
   620
   declarations have been inserted successfully:
clasohm@0
   621
   for every declaration t:(Ss)C , for all classses D with C <= D:
clasohm@0
   622
      if there is no declaration t:(Ss')C' with C < C' and C' <= D
nipkow@963
   623
      then insert the declaration t:(Ss)D into 'arities'
clasohm@0
   624
   this means, if there exists a declaration t:(Ss)C and there is
clasohm@0
   625
   no declaration t:(Ss')D with C <=D then the declaration holds
wenzelm@256
   626
   for all range classes more general than C *)
wenzelm@256
   627
wenzelm@2964
   628
fun close classrel arities =
wenzelm@2964
   629
  let fun check sl (l, (s, dom)) = case assoc (classrel, s) of
wenzelm@621
   630
          Some sups =>
wenzelm@256
   631
            let fun close_sup (l, sup) =
wenzelm@2964
   632
                  if exists (fn s'' => less classrel (s, s'') andalso
wenzelm@2964
   633
                                       leq classrel (s'', sup)) sl
clasohm@0
   634
                  then l
wenzelm@256
   635
                  else (sup, dom)::l
wenzelm@256
   636
            in foldl close_sup (l, sups) end
clasohm@0
   637
        | None => l;
wenzelm@256
   638
      fun ext (s, l) = (s, foldl (check (map #1 l)) (l, l));
nipkow@963
   639
  in map ext arities end;
clasohm@0
   640
wenzelm@422
   641
wenzelm@621
   642
(* ext_tsig_arities *)
wenzelm@256
   643
wenzelm@2964
   644
fun norm_domain classrel =
wenzelm@2964
   645
  let fun one_min (f, (doms, ran)) = (f, (map (Sorts.norm_sort classrel) doms, ran))
wenzelm@2964
   646
  in map one_min end;
wenzelm@2964
   647
wenzelm@621
   648
fun ext_tsig_arities tsig sarities =
wenzelm@416
   649
  let
wenzelm@2964
   650
    val TySg {classes, classrel, default, tycons, arities, abbrs} = tsig;
nipkow@963
   651
    val arities1 =
wenzelm@2964
   652
      List.concat
paulson@2672
   653
          (map (fn (t, ss, cs) => map (fn c => (t, (ss, c))) cs) sarities);
wenzelm@2964
   654
    val arities2 = foldl (coregular (classes, classrel, tycons))
wenzelm@2964
   655
                         (arities, norm_domain classrel arities1)
wenzelm@2964
   656
      |> close classrel;
wenzelm@416
   657
  in
wenzelm@2964
   658
    make_tsig (classes, classrel, default, tycons, abbrs, arities2)
wenzelm@416
   659
  end;
clasohm@0
   660
clasohm@0
   661
wenzelm@416
   662
wenzelm@2964
   663
(*** type unification and friends ***)
clasohm@0
   664
wenzelm@2964
   665
(** matching **)
clasohm@0
   666
wenzelm@2964
   667
exception TYPE_MATCH;
clasohm@0
   668
wenzelm@2964
   669
fun typ_match tsig =
wenzelm@2964
   670
  let
wenzelm@2964
   671
    fun match (subs, (TVar (v, S), T)) =
wenzelm@2964
   672
          (case assoc (subs, v) of
wenzelm@2964
   673
            None => ((v, (check_has_sort (tsig, T, S); T)) :: subs
wenzelm@2964
   674
              handle TYPE _ => raise TYPE_MATCH)
wenzelm@2964
   675
          | Some U => if U = T then subs else raise TYPE_MATCH)
wenzelm@2964
   676
      | match (subs, (Type (a, Ts), Type (b, Us))) =
wenzelm@2964
   677
          if a <> b then raise TYPE_MATCH
wenzelm@2964
   678
          else foldl match (subs, Ts ~~ Us)
wenzelm@2964
   679
      | match (subs, (TFree x, TFree y)) =
wenzelm@2964
   680
          if x = y then subs else raise TYPE_MATCH
wenzelm@2964
   681
      | match _ = raise TYPE_MATCH;
wenzelm@2964
   682
  in match end;
clasohm@0
   683
wenzelm@2964
   684
fun typ_instance (tsig, T, U) =
wenzelm@2964
   685
  (typ_match tsig ([], (U, T)); true) handle TYPE_MATCH => false;
wenzelm@2964
   686
clasohm@0
   687
wenzelm@2964
   688
wenzelm@2964
   689
(** unification **)
wenzelm@2964
   690
clasohm@0
   691
exception TUNIFY;
clasohm@0
   692
clasohm@0
   693
wenzelm@2964
   694
(* occurs check *)
clasohm@0
   695
wenzelm@2964
   696
fun occurs v tye =
wenzelm@2964
   697
  let
wenzelm@2964
   698
    fun occ (Type (_, Ts)) = exists occ Ts
wenzelm@2964
   699
      | occ (TFree _) = false
wenzelm@2964
   700
      | occ (TVar (w, _)) =
wenzelm@2964
   701
          eq_ix (v, w) orelse
wenzelm@2964
   702
            (case assoc (tye, w) of
wenzelm@2964
   703
              None => false
wenzelm@2964
   704
            | Some U => occ U);
clasohm@0
   705
  in occ end;
clasohm@0
   706
wenzelm@2964
   707
wenzelm@2964
   708
(* chase variable assignments *)
wenzelm@2964
   709
wenzelm@2964
   710
(*if devar returns a type var then it must be unassigned*)
wenzelm@2964
   711
fun devar (T as TVar (v, _), tye) =
wenzelm@2964
   712
      (case  assoc (tye, v) of
wenzelm@2964
   713
        Some U => devar (U, tye)
wenzelm@2964
   714
      | None => T)
wenzelm@256
   715
  | devar (T, tye) = T;
clasohm@0
   716
berghofe@1627
   717
wenzelm@2964
   718
(* add_env *)
clasohm@0
   719
wenzelm@2964
   720
(*avoids chains 'a |-> 'b |-> 'c ...*)
wenzelm@2964
   721
fun add_env (p, []) = [p]
wenzelm@2964
   722
  | add_env (vT as (v, T), (xU as (x, TVar (w, S))) :: ps) =
wenzelm@2964
   723
      (if eq_ix (v, w) then (x, T) else xU) :: add_env (vT, ps)
wenzelm@2964
   724
  | add_env (v, x :: xs) = x :: add_env (v, xs);
clasohm@0
   725
clasohm@0
   726
wenzelm@2964
   727
(* unify *)
wenzelm@2964
   728
wenzelm@2964
   729
fun unify (tsig as TySg {classrel, arities, ...}) maxidx tyenv TU =
wenzelm@2964
   730
  let
wenzelm@2964
   731
    val tyvar_count = ref maxidx;
wenzelm@2964
   732
    fun gen_tyvar S = TVar (("'a", inc tyvar_count), S);
wenzelm@2964
   733
wenzelm@2964
   734
    fun mg_domain a S =
wenzelm@2964
   735
      Sorts.mg_domain classrel arities a S handle TYPE _ => raise TUNIFY;
wenzelm@2964
   736
wenzelm@2964
   737
    fun meet ((_, []), tye) = tye
wenzelm@2964
   738
      | meet ((TVar (xi, S'), S), tye) =
wenzelm@2964
   739
          if Sorts.sort_le classrel (S', S) then tye
wenzelm@2964
   740
          else add_env ((xi, gen_tyvar (Sorts.inter_sort classrel (S', S))), tye)
wenzelm@2964
   741
      | meet ((TFree (_, S'), S), tye) =
wenzelm@2964
   742
          if Sorts.sort_le classrel (S', S) then tye
wenzelm@2964
   743
          else raise TUNIFY
wenzelm@2964
   744
      | meet ((Type (a, Ts), S), tye) = meets ((Ts, mg_domain a S), tye)
wenzelm@2964
   745
    and meets (([], []), tye) = tye
wenzelm@2964
   746
      | meets ((T :: Ts, S :: Ss), tye) =
wenzelm@2964
   747
          meets ((Ts, Ss), meet ((devar (T, tye), S), tye))
wenzelm@2964
   748
      | meets _ = sys_error "meets";
wenzelm@2964
   749
wenzelm@2964
   750
    fun unif ((ty1, ty2), tye) =
wenzelm@2964
   751
      (case (devar (ty1, tye), devar (ty2, tye)) of
wenzelm@2964
   752
        (T as TVar (v, S1), U as TVar (w, S2)) =>
wenzelm@2964
   753
          if eq_ix (v, w) then tye
wenzelm@2964
   754
          else if Sorts.sort_le classrel (S1, S2) then add_env ((w, T), tye)
wenzelm@2964
   755
          else if Sorts.sort_le classrel (S2, S1) then add_env ((v, U), tye)
wenzelm@2964
   756
          else
wenzelm@2964
   757
            let val S = gen_tyvar (Sorts.inter_sort classrel (S1, S2)) in
wenzelm@2964
   758
              add_env ((v, S), add_env ((w, S), tye))
wenzelm@2964
   759
            end
wenzelm@2964
   760
      | (TVar (v, S), T) =>
wenzelm@2964
   761
          if occurs v tye T then raise TUNIFY
wenzelm@2964
   762
          else meet ((T, S), add_env ((v, T), tye))
wenzelm@2964
   763
      | (T, TVar (v, S)) =>
wenzelm@2964
   764
          if occurs v tye T then raise TUNIFY
wenzelm@2964
   765
          else meet ((T, S), add_env ((v, T), tye))
wenzelm@2964
   766
      | (Type (a, Ts), Type (b, Us)) =>
wenzelm@2964
   767
          if a <> b then raise TUNIFY
wenzelm@2964
   768
          else foldr unif (Ts ~~ Us, tye)
wenzelm@2964
   769
      | (T, U) => if T = U then tye else raise TUNIFY);
wenzelm@2964
   770
  in
wenzelm@2964
   771
    (unif (TU, tyenv), ! tyvar_count)
wenzelm@2964
   772
  end;
clasohm@0
   773
clasohm@0
   774
wenzelm@2964
   775
(* raw_unify *)
clasohm@0
   776
wenzelm@2964
   777
(*purely structural unification -- ignores sorts*)
wenzelm@450
   778
fun raw_unify (ty1, ty2) =
nipkow@1435
   779
  (unify tsig0 0 [] (rem_sorts ty1, rem_sorts ty2); true)
wenzelm@450
   780
    handle TUNIFY => false;
wenzelm@450
   781
wenzelm@450
   782
clasohm@0
   783
wenzelm@2964
   784
(** type inference **)
nipkow@1435
   785
wenzelm@2964
   786
(* constraints *)
wenzelm@2587
   787
wenzelm@2587
   788
fun get_sort tsig def_sort env xi =
wenzelm@2587
   789
  (case (assoc (env, xi), def_sort xi) of
wenzelm@2587
   790
    (None, None) => defaultS tsig
wenzelm@2587
   791
  | (None, Some S) => S
wenzelm@2587
   792
  | (Some S, None) => S
wenzelm@2587
   793
  | (Some S, Some S') =>
wenzelm@2587
   794
      if eq_sort tsig (S, S') then S
wenzelm@2587
   795
      else error ("Sort constraint inconsistent with default for type variable " ^
wenzelm@2587
   796
        quote (Syntax.string_of_vname' xi)));
wenzelm@2587
   797
wenzelm@2964
   798
fun constrain t T =
wenzelm@2964
   799
  if T = dummyT then t
wenzelm@2964
   800
  else Const ("_type_constraint_", T) $ t;
clasohm@0
   801
clasohm@0
   802
wenzelm@2964
   803
(* decode_types *)
clasohm@0
   804
wenzelm@2964
   805
(*transform parse tree into raw term (idempotent)*)
wenzelm@2964
   806
fun decode_types tsig is_const def_type def_sort tm =
wenzelm@2964
   807
  let
wenzelm@2964
   808
    fun get_type xi = if_none (def_type xi) dummyT;
wenzelm@2964
   809
    val sort_env = Syntax.raw_term_sorts (eq_sort tsig) tm;
wenzelm@2964
   810
wenzelm@2964
   811
    fun decodeT t =
wenzelm@2964
   812
      cert_typ tsig (Syntax.typ_of_term (get_sort tsig def_sort sort_env) t);
clasohm@0
   813
wenzelm@2964
   814
    fun decode (Const ("_constrain", _) $ t $ typ) =
wenzelm@2964
   815
          constrain (decode t) (decodeT typ)
wenzelm@2964
   816
      | decode (Const ("_constrainAbs", _) $ (abs as Abs (x, T, t)) $ typ) =
wenzelm@2964
   817
          if T = dummyT then Abs (x, decodeT typ, decode t)
wenzelm@2964
   818
          else constrain abs (decodeT typ --> dummyT)
wenzelm@2964
   819
      | decode (Abs (x, T, t)) = Abs (x, T, decode t)
wenzelm@2964
   820
      | decode (t $ u) = decode t $ decode u
wenzelm@2964
   821
      | decode (t as Free (x, T)) =
wenzelm@2964
   822
          if is_const x then Const (x, T)
wenzelm@2964
   823
          else if T = dummyT then Free (x, get_type (x, ~1))
wenzelm@2964
   824
          else constrain t (get_type (x, ~1))
wenzelm@2964
   825
      | decode (t as Var (xi, T)) =
wenzelm@2964
   826
          if T = dummyT then Var (xi, get_type xi)
wenzelm@2964
   827
          else constrain t (get_type xi)
wenzelm@2964
   828
      | decode (t as Bound _) = t
wenzelm@2964
   829
      | decode (t as Const _) = t;
wenzelm@2964
   830
  in
wenzelm@2964
   831
    decode tm
wenzelm@2964
   832
  end;
clasohm@0
   833
clasohm@0
   834
wenzelm@2964
   835
(* infer_types *)
nipkow@949
   836
nipkow@949
   837
(*
wenzelm@2964
   838
  Given [T1,...,Tn] and [t1,...,tn], ensure that the type of ti
wenzelm@2964
   839
  unifies with Ti (for i=1,...,n).
wenzelm@2964
   840
wenzelm@2964
   841
  tsig: type signature
wenzelm@2964
   842
  const_type: term signature
wenzelm@2964
   843
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2964
   844
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2964
   845
  used: list of already used type variables
wenzelm@2964
   846
  freeze: if true then generated parameters are turned into TFrees, else TVars
nipkow@949
   847
*)
clasohm@0
   848
wenzelm@2964
   849
(*user-supplied inference parameters*)
wenzelm@2964
   850
fun q_is_param (x, _) =
wenzelm@2964
   851
  (case explode x of
wenzelm@2964
   852
    "?" :: _ => true
wenzelm@2964
   853
  | _ => false);
clasohm@0
   854
wenzelm@2979
   855
fun infer_types prt prT tsig const_type def_type def_sort used freeze pat_Ts raw_ts =
wenzelm@565
   856
  let
wenzelm@2964
   857
    val TySg {classrel, arities, ...} = tsig;
wenzelm@2964
   858
    val pat_Ts' = map (cert_typ tsig) pat_Ts;
wenzelm@2964
   859
    val raw_ts' =
wenzelm@2964
   860
      map (decode_types tsig (is_some o const_type) def_type def_sort) raw_ts;
wenzelm@2964
   861
    val (ts, Ts, unifier) =
wenzelm@2979
   862
      TypeInfer.infer_types prt prT const_type classrel arities used freeze
wenzelm@2964
   863
        q_is_param raw_ts' pat_Ts';
wenzelm@565
   864
  in
wenzelm@2964
   865
    (ts, unifier)
wenzelm@565
   866
  end;
clasohm@0
   867
clasohm@0
   868
end;