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