author | wenzelm |
Thu, 21 Sep 1995 11:26:44 +0200 | |
changeset 1258 | 2a2d8c74a756 |
parent 1257 | ec738ecb911c |
child 1392 | 1b4ae50e0e0a |
permissions | -rw-r--r-- |
256 | 1 |
(* Title: Pure/type.ML |
0 | 2 |
ID: $Id$ |
416 | 3 |
Author: Tobias Nipkow & Lawrence C Paulson |
0 | 4 |
|
416 | 5 |
Type classes and sorts. Type signatures. Type unification and inference. |
256 | 6 |
|
7 |
TODO: |
|
1257 | 8 |
improve nonempty_sort! |
416 | 9 |
move type unification and inference to type_unify.ML (TypeUnify) (?) |
0 | 10 |
*) |
11 |
||
12 |
signature TYPE = |
|
13 |
sig |
|
256 | 14 |
structure Symtab: SYMTAB |
621 | 15 |
val no_tvars: typ -> typ |
16 |
val varifyT: typ -> typ |
|
17 |
val unvarifyT: typ -> typ |
|
18 |
val varify: term * string list -> term |
|
416 | 19 |
val str_of_sort: sort -> string |
20 |
val str_of_arity: string * sort list * sort -> string |
|
0 | 21 |
type type_sig |
200 | 22 |
val rep_tsig: type_sig -> |
256 | 23 |
{classes: class list, |
24 |
subclass: (class * class list) list, |
|
25 |
default: sort, |
|
963 | 26 |
tycons: (string * int) list, |
621 | 27 |
abbrs: (string * (string list * typ)) list, |
963 | 28 |
arities: (string * (class * sort list) list) list} |
0 | 29 |
val defaultS: type_sig -> sort |
416 | 30 |
val tsig0: type_sig |
256 | 31 |
val logical_types: type_sig -> string list |
621 | 32 |
val ext_tsig_classes: type_sig -> (class * class list) list -> type_sig |
422 | 33 |
val ext_tsig_subclass: type_sig -> (class * class) list -> type_sig |
34 |
val ext_tsig_defsort: type_sig -> sort -> type_sig |
|
582 | 35 |
val ext_tsig_types: type_sig -> (string * int) list -> type_sig |
621 | 36 |
val ext_tsig_abbrs: type_sig -> (string * string list * typ) list -> type_sig |
963 | 37 |
val ext_tsig_arities: type_sig -> (string * sort list * sort)list -> type_sig |
256 | 38 |
val merge_tsigs: type_sig * type_sig -> type_sig |
416 | 39 |
val subsort: type_sig -> sort * sort -> bool |
40 |
val norm_sort: type_sig -> sort -> sort |
|
41 |
val rem_sorts: typ -> typ |
|
1239 | 42 |
val nonempty_sort: type_sig -> sort list -> sort -> bool |
256 | 43 |
val cert_typ: type_sig -> typ -> typ |
44 |
val norm_typ: type_sig -> typ -> typ |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
45 |
val freeze: term -> term |
0 | 46 |
val freeze_vars: typ -> typ |
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
47 |
val infer_types: type_sig * (string -> typ option) * |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
48 |
(indexname -> typ option) * (indexname -> sort option) * |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
49 |
string list * bool * typ * term |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
50 |
-> term * (indexname * typ) list |
256 | 51 |
val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term |
0 | 52 |
val thaw_vars: typ -> typ |
256 | 53 |
val typ_errors: type_sig -> typ * string list -> string list |
0 | 54 |
val typ_instance: type_sig * typ * typ -> bool |
256 | 55 |
val typ_match: type_sig -> (indexname * typ) list * (typ * typ) |
56 |
-> (indexname * typ) list |
|
57 |
val unify: type_sig -> (typ * typ) * (indexname * typ) list |
|
58 |
-> (indexname * typ) list |
|
450 | 59 |
val raw_unify: typ * typ -> bool |
0 | 60 |
exception TUNIFY |
256 | 61 |
exception TYPE_MATCH |
0 | 62 |
end; |
63 |
||
416 | 64 |
functor TypeFun(structure Symtab: SYMTAB and Syntax: SYNTAX): TYPE = |
0 | 65 |
struct |
66 |
||
256 | 67 |
structure Symtab = Symtab; |
0 | 68 |
|
69 |
||
621 | 70 |
(*** TFrees vs TVars ***) |
71 |
||
72 |
(*disallow TVars*) |
|
73 |
fun no_tvars T = |
|
74 |
if null (typ_tvars T) then T |
|
75 |
else raise_type "Illegal schematic type variable(s)" [T] []; |
|
76 |
||
77 |
(*turn TFrees into TVars to allow types & axioms to be written without "?"*) |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
78 |
val varifyT = map_type_tfree (fn (a, S) => TVar((a, 0), S)); |
621 | 79 |
|
80 |
(*inverse of varifyT*) |
|
81 |
fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts) |
|
82 |
| unvarifyT (TVar ((a, 0), S)) = TFree (a, S) |
|
83 |
| unvarifyT T = T; |
|
84 |
||
85 |
(*turn TFrees except those in fixed into new TVars*) |
|
86 |
fun varify (t, fixed) = |
|
87 |
let |
|
88 |
val fs = add_term_tfree_names (t, []) \\ fixed; |
|
89 |
val ixns = add_term_tvar_ixns (t, []); |
|
90 |
val fmap = fs ~~ variantlist (fs, map #1 ixns) |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
91 |
fun thaw(f as (a,S)) = case assoc (fmap, a) of |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
92 |
None => TFree(f) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
93 |
| Some b => TVar((b, 0), S) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
94 |
in map_term_types (map_type_tfree thaw) t end; |
621 | 95 |
|
96 |
||
97 |
||
416 | 98 |
(*** type classes and sorts ***) |
99 |
||
100 |
(* |
|
101 |
Classes denote (possibly empty) collections of types (e.g. sets of types) |
|
102 |
and are partially ordered by 'inclusion'. They are represented by strings. |
|
103 |
||
104 |
Sorts are intersections of finitely many classes. They are represented by |
|
105 |
lists of classes. |
|
106 |
*) |
|
0 | 107 |
|
108 |
type domain = sort list; |
|
416 | 109 |
|
110 |
||
111 |
(* print sorts and arities *) |
|
0 | 112 |
|
416 | 113 |
fun str_of_sort [c] = c |
565 | 114 |
| str_of_sort cs = enclose "{" "}" (commas cs); |
416 | 115 |
|
565 | 116 |
fun str_of_dom dom = enclose "(" ")" (commas (map str_of_sort dom)); |
416 | 117 |
|
118 |
fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S |
|
119 |
| str_of_arity (t, SS, S) = |
|
120 |
t ^ " :: " ^ str_of_dom SS ^ " " ^ str_of_sort S; |
|
256 | 121 |
|
122 |
||
123 |
||
416 | 124 |
(*** type signatures ***) |
256 | 125 |
|
126 |
(* |
|
127 |
classes: |
|
128 |
a list of all declared classes; |
|
0 | 129 |
|
256 | 130 |
subclass: |
416 | 131 |
an association list representing the subclass relation; (c, cs) is |
256 | 132 |
interpreted as "c is a proper subclass of all elemenst of cs"; note that |
133 |
c itself is not a memeber of cs; |
|
134 |
||
135 |
default: |
|
136 |
the default sort attached to all unconstrained type vars; |
|
137 |
||
963 | 138 |
tycons: |
256 | 139 |
an association list of all declared types with the number of their |
140 |
arguments; |
|
141 |
||
142 |
abbrs: |
|
143 |
an association list of type abbreviations; |
|
144 |
||
963 | 145 |
arities: |
256 | 146 |
a two-fold association list of all type arities; (t, al) means that type |
147 |
constructor t has the arities in al; an element (c, ss) of al represents |
|
148 |
the arity (ss)c; |
|
0 | 149 |
*) |
150 |
||
256 | 151 |
datatype type_sig = |
152 |
TySg of { |
|
153 |
classes: class list, |
|
154 |
subclass: (class * class list) list, |
|
155 |
default: sort, |
|
963 | 156 |
tycons: (string * int) list, |
621 | 157 |
abbrs: (string * (string list * typ)) list, |
963 | 158 |
arities: (string * (class * domain) list) list}; |
256 | 159 |
|
189 | 160 |
fun rep_tsig (TySg comps) = comps; |
0 | 161 |
|
256 | 162 |
fun defaultS (TySg {default, ...}) = default; |
163 |
||
164 |
||
582 | 165 |
(* error messages *) |
256 | 166 |
|
416 | 167 |
fun undcl_class c = "Undeclared class " ^ quote c; |
256 | 168 |
val err_undcl_class = error o undcl_class; |
0 | 169 |
|
422 | 170 |
fun err_dup_classes cs = |
171 |
error ("Duplicate declaration of class(es) " ^ commas_quote cs); |
|
416 | 172 |
|
173 |
fun undcl_type c = "Undeclared type constructor " ^ quote c; |
|
256 | 174 |
val err_undcl_type = error o undcl_type; |
175 |
||
582 | 176 |
fun err_neg_args c = |
177 |
error ("Negative number of arguments of type constructor " ^ quote c); |
|
178 |
||
416 | 179 |
fun err_dup_tycon c = |
180 |
error ("Duplicate declaration of type constructor " ^ quote c); |
|
181 |
||
621 | 182 |
fun dup_tyabbrs ts = |
183 |
"Duplicate declaration of type abbreviation(s) " ^ commas_quote ts; |
|
416 | 184 |
|
185 |
fun ty_confl c = "Conflicting type constructor and abbreviation " ^ quote c; |
|
186 |
val err_ty_confl = error o ty_confl; |
|
0 | 187 |
|
188 |
||
189 |
(* 'leq' checks the partial order on classes according to the |
|
621 | 190 |
statements in the association list 'a' (i.e. 'subclass') |
0 | 191 |
*) |
192 |
||
256 | 193 |
fun less a (C, D) = case assoc (a, C) of |
621 | 194 |
Some ss => D mem ss |
195 |
| None => err_undcl_class C; |
|
0 | 196 |
|
256 | 197 |
fun leq a (C, D) = C = D orelse less a (C, D); |
0 | 198 |
|
199 |
||
416 | 200 |
(* logical_types *) |
0 | 201 |
|
416 | 202 |
(*return all logical types of tsig, i.e. all types t with some arity t::(ss)c |
203 |
and c <= logic*) |
|
0 | 204 |
|
416 | 205 |
fun logical_types tsig = |
206 |
let |
|
963 | 207 |
val TySg {subclass, arities, tycons, ...} = tsig; |
416 | 208 |
|
209 |
fun log_class c = leq subclass (c, logicC); |
|
963 | 210 |
fun log_type t = exists (log_class o #1) (assocs arities t); |
416 | 211 |
in |
963 | 212 |
filter log_type (map #1 tycons) |
0 | 213 |
end; |
214 |
||
162 | 215 |
|
256 | 216 |
(* 'sortorder' checks the ordering on sets of classes, i.e. on sorts: |
217 |
S1 <= S2 , iff for every class C2 in S2 there exists a class C1 in S1 |
|
0 | 218 |
with C1 <= C2 (according to an association list 'a') |
219 |
*) |
|
220 |
||
256 | 221 |
fun sortorder a (S1, S2) = |
222 |
forall (fn C2 => exists (fn C1 => leq a (C1, C2)) S1) S2; |
|
0 | 223 |
|
224 |
||
225 |
(* 'inj' inserts a new class C into a given class set S (i.e.sort) only if |
|
226 |
there exists no class in S which is <= C; |
|
227 |
the resulting set is minimal if S was minimal |
|
228 |
*) |
|
229 |
||
256 | 230 |
fun inj a (C, S) = |
0 | 231 |
let fun inj1 [] = [C] |
256 | 232 |
| inj1 (D::T) = if leq a (D, C) then D::T |
233 |
else if leq a (C, D) then inj1 T |
|
0 | 234 |
else D::(inj1 T) |
235 |
in inj1 S end; |
|
236 |
||
237 |
||
238 |
(* 'union_sort' forms the minimal union set of two sorts S1 and S2 |
|
239 |
under the assumption that S2 is minimal *) |
|
256 | 240 |
(* FIXME rename to inter_sort (?) *) |
0 | 241 |
|
242 |
fun union_sort a = foldr (inj a); |
|
243 |
||
244 |
||
245 |
(* 'elementwise_union' forms elementwise the minimal union set of two |
|
246 |
sort lists under the assumption that the two lists have the same length |
|
256 | 247 |
*) |
0 | 248 |
|
256 | 249 |
fun elementwise_union a (Ss1, Ss2) = map (union_sort a) (Ss1~~Ss2); |
250 |
||
0 | 251 |
|
252 |
(* 'lew' checks for two sort lists the ordering for all corresponding list |
|
253 |
elements (i.e. sorts) *) |
|
254 |
||
256 | 255 |
fun lew a (w1, w2) = forall (sortorder a) (w1~~w2); |
256 |
||
0 | 257 |
|
256 | 258 |
(* 'is_min' checks if a class C is minimal in a given sort S under the |
259 |
assumption that S contains C *) |
|
0 | 260 |
|
256 | 261 |
fun is_min a S C = not (exists (fn (D) => less a (D, C)) S); |
0 | 262 |
|
263 |
||
264 |
(* 'min_sort' reduces a sort to its minimal classes *) |
|
265 |
||
266 |
fun min_sort a S = distinct(filter (is_min a S) S); |
|
267 |
||
268 |
||
269 |
(* 'min_domain' minimizes the domain sorts of type declarationsl; |
|
256 | 270 |
the function will be applied on the type declarations in extensions *) |
0 | 271 |
|
272 |
fun min_domain subclass = |
|
256 | 273 |
let fun one_min (f, (doms, ran)) = (f, (map (min_sort subclass) doms, ran)) |
0 | 274 |
in map one_min end; |
275 |
||
276 |
||
277 |
(* 'min_filter' filters a list 'ars' consisting of arities (domain * class) |
|
256 | 278 |
and gives back a list of those range classes whose domains meet the |
0 | 279 |
predicate 'pred' *) |
256 | 280 |
|
0 | 281 |
fun min_filter a pred ars = |
256 | 282 |
let fun filt ([], l) = l |
283 |
| filt ((c, x)::xs, l) = if pred(x) then filt (xs, inj a (c, l)) |
|
284 |
else filt (xs, l) |
|
285 |
in filt (ars, []) end; |
|
0 | 286 |
|
287 |
||
288 |
(* 'cod_above' filters all arities whose domains are elementwise >= than |
|
256 | 289 |
a given domain 'w' and gives back a list of the corresponding range |
0 | 290 |
classes *) |
291 |
||
256 | 292 |
fun cod_above (a, w, ars) = min_filter a (fn w' => lew a (w, w')) ars; |
293 |
||
294 |
||
0 | 295 |
|
200 | 296 |
(*Instantiation of type variables in types*) |
297 |
(*Pre: instantiations obey restrictions! *) |
|
298 |
fun inst_typ tye = |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
299 |
let fun inst(var as (v, _)) = case assoc(tye, v) of |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
300 |
Some U => inst_typ tye U |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
301 |
| None => TVar(var) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
302 |
in map_type_tvar inst end; |
0 | 303 |
|
304 |
(* 'least_sort' returns for a given type its maximum sort: |
|
305 |
- type variables, free types: the sort brought with |
|
306 |
- type constructors: recursive determination of the maximum sort of the |
|
963 | 307 |
arguments if the type is declared in 'arities' of the |
256 | 308 |
given type signature *) |
0 | 309 |
|
963 | 310 |
fun least_sort (tsig as TySg{subclass, arities, ...}) = |
256 | 311 |
let fun ls(T as Type(a, Ts)) = |
963 | 312 |
(case assoc (arities, a) of |
256 | 313 |
Some(ars) => cod_above(subclass, map ls Ts, ars) |
314 |
| None => raise TYPE(undcl_type a, [T], [])) |
|
315 |
| ls(TFree(a, S)) = S |
|
316 |
| ls(TVar(a, S)) = S |
|
0 | 317 |
in ls end; |
318 |
||
319 |
||
963 | 320 |
fun check_has_sort(tsig as TySg{subclass, arities, ...}, T, S) = |
256 | 321 |
if sortorder subclass ((least_sort tsig T), S) then () |
322 |
else raise TYPE("Type not of sort " ^ (str_of_sort S), [T], []) |
|
0 | 323 |
|
324 |
||
325 |
(*Instantiation of type variables in types *) |
|
256 | 326 |
fun inst_typ_tvars(tsig, tye) = |
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
327 |
let fun inst(var as (v, S)) = case assoc(tye, v) of |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
328 |
Some U => (check_has_sort(tsig, U, S); U) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
329 |
| None => TVar(var) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
330 |
in map_type_tvar inst end; |
0 | 331 |
|
332 |
(*Instantiation of type variables in terms *) |
|
256 | 333 |
fun inst_term_tvars(tsig, tye) = map_term_types (inst_typ_tvars(tsig, tye)); |
200 | 334 |
|
335 |
||
336 |
(* expand_typ *) |
|
337 |
||
256 | 338 |
fun expand_typ (TySg {abbrs, ...}) ty = |
339 |
let |
|
621 | 340 |
val idx = maxidx_of_typ ty + 1; |
341 |
||
342 |
fun expand (Type (a, Ts)) = |
|
256 | 343 |
(case assoc (abbrs, a) of |
621 | 344 |
Some (vs, U) => |
345 |
expand (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U)) |
|
346 |
| None => Type (a, map expand Ts)) |
|
347 |
| expand T = T |
|
256 | 348 |
in |
621 | 349 |
expand ty |
256 | 350 |
end; |
351 |
||
352 |
val norm_typ = expand_typ; |
|
353 |
||
354 |
||
355 |
||
356 |
(** type matching **) |
|
200 | 357 |
|
0 | 358 |
exception TYPE_MATCH; |
359 |
||
256 | 360 |
(*typ_match (s, (U, T)) = s' <==> s'(U) = T and s' is an extension of s*) |
361 |
fun typ_match tsig = |
|
362 |
let |
|
363 |
fun match (subs, (TVar (v, S), T)) = |
|
364 |
(case assoc (subs, v) of |
|
365 |
None => ((v, (check_has_sort (tsig, T, S); T)) :: subs |
|
366 |
handle TYPE _ => raise TYPE_MATCH) |
|
422 | 367 |
| Some U => if U = T then subs else raise TYPE_MATCH) |
256 | 368 |
| match (subs, (Type (a, Ts), Type (b, Us))) = |
369 |
if a <> b then raise TYPE_MATCH |
|
370 |
else foldl match (subs, Ts ~~ Us) |
|
422 | 371 |
| match (subs, (TFree x, TFree y)) = |
256 | 372 |
if x = y then subs else raise TYPE_MATCH |
373 |
| match _ = raise TYPE_MATCH; |
|
374 |
in match end; |
|
0 | 375 |
|
376 |
||
256 | 377 |
fun typ_instance (tsig, T, U) = |
378 |
(typ_match tsig ([], (U, T)); true) handle TYPE_MATCH => false; |
|
379 |
||
380 |
||
381 |
||
382 |
(** build type signatures **) |
|
383 |
||
963 | 384 |
fun make_tsig (classes, subclass, default, tycons, abbrs, arities) = |
416 | 385 |
TySg {classes = classes, subclass = subclass, default = default, |
963 | 386 |
tycons = tycons, abbrs = abbrs, arities = arities}; |
416 | 387 |
|
388 |
val tsig0 = make_tsig ([], [], [], [], [], []); |
|
256 | 389 |
|
0 | 390 |
|
401 | 391 |
(* sorts *) |
392 |
||
416 | 393 |
fun subsort (TySg {subclass, ...}) (S1, S2) = |
394 |
sortorder subclass (S1, S2); |
|
395 |
||
401 | 396 |
fun norm_sort (TySg {subclass, ...}) S = |
397 |
sort_strings (min_sort subclass S); |
|
398 |
||
416 | 399 |
fun rem_sorts (Type (a, tys)) = Type (a, map rem_sorts tys) |
400 |
| rem_sorts (TFree (x, _)) = TFree (x, []) |
|
401 |
| rem_sorts (TVar (xi, _)) = TVar (xi, []); |
|
401 | 402 |
|
403 |
||
1215
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
404 |
(* nonempty_sort *) |
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
405 |
|
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
406 |
(* FIXME improve: proper sorts; non-base, non-ground types (vars from hyps) *) |
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
407 |
fun nonempty_sort _ _ [] = true |
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
408 |
| nonempty_sort (tsig as TySg {arities, ...}) hyps S = |
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
409 |
exists (exists (fn (c, ss) => [c] = S andalso null ss) o snd) arities |
1239 | 410 |
orelse exists (fn S' => subsort tsig (S', S)) hyps; |
1215
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
411 |
|
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
412 |
|
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
413 |
|
416 | 414 |
(* typ_errors *) |
256 | 415 |
|
416 | 416 |
(*check validity of (not necessarily normal) type; accumulate error messages*) |
256 | 417 |
|
416 | 418 |
fun typ_errors tsig (typ, errors) = |
256 | 419 |
let |
963 | 420 |
val TySg {classes, tycons, abbrs, ...} = tsig; |
416 | 421 |
|
422 |
fun class_err (errs, c) = |
|
423 |
if c mem classes then errs |
|
424 |
else undcl_class c ins errs; |
|
256 | 425 |
|
426 |
val sort_err = foldl class_err; |
|
0 | 427 |
|
256 | 428 |
fun typ_errs (Type (c, Us), errs) = |
429 |
let |
|
430 |
val errs' = foldr typ_errs (Us, errs); |
|
431 |
fun nargs n = |
|
432 |
if n = length Us then errs' |
|
416 | 433 |
else ("Wrong number of arguments: " ^ quote c) ins errs'; |
256 | 434 |
in |
963 | 435 |
(case assoc (tycons, c) of |
256 | 436 |
Some n => nargs n |
437 |
| None => |
|
438 |
(case assoc (abbrs, c) of |
|
439 |
Some (vs, _) => nargs (length vs) |
|
416 | 440 |
| None => undcl_type c ins errs)) |
256 | 441 |
end |
442 |
| typ_errs (TFree (_, S), errs) = sort_err (errs, S) |
|
416 | 443 |
| typ_errs (TVar ((x, i), S), errs) = |
444 |
if i < 0 then |
|
445 |
("Negative index for TVar " ^ quote x) ins sort_err (errs, S) |
|
446 |
else sort_err (errs, S); |
|
256 | 447 |
in |
416 | 448 |
typ_errs (typ, errors) |
256 | 449 |
end; |
450 |
||
451 |
||
452 |
(* cert_typ *) |
|
453 |
||
454 |
(*check and normalize typ wrt. tsig; errors are indicated by exception TYPE*) |
|
455 |
||
456 |
fun cert_typ tsig ty = |
|
457 |
(case typ_errors tsig (ty, []) of |
|
458 |
[] => norm_typ tsig ty |
|
459 |
| errs => raise_type (cat_lines errs) [ty] []); |
|
460 |
||
461 |
||
462 |
||
422 | 463 |
(** merge type signatures **) |
256 | 464 |
|
422 | 465 |
(*'assoc_union' merges two association lists if the contents associated |
466 |
the keys are lists*) |
|
0 | 467 |
|
422 | 468 |
fun assoc_union (as1, []) = as1 |
469 |
| assoc_union (as1, (key, l2) :: as2) = |
|
470 |
(case assoc (as1, key) of |
|
471 |
Some l1 => assoc_union (overwrite (as1, (key, l1 union l2)), as2) |
|
472 |
| None => assoc_union ((key, l2) :: as1, as2)); |
|
0 | 473 |
|
474 |
||
422 | 475 |
(* merge subclass *) |
0 | 476 |
|
422 | 477 |
fun merge_subclass (subclass1, subclass2) = |
478 |
let val subclass = transitive_closure (assoc_union (subclass1, subclass2)) in |
|
479 |
if exists (op mem) subclass then |
|
480 |
error ("Cyclic class structure!") (* FIXME improve msg, raise TERM *) |
|
481 |
else subclass |
|
416 | 482 |
end; |
483 |
||
484 |
||
422 | 485 |
(* coregularity *) |
0 | 486 |
|
487 |
(* 'is_unique_decl' checks if there exists just one declaration t:(Ss)C *) |
|
488 |
||
963 | 489 |
fun is_unique_decl ars (t,(C,w)) = case assoc (ars, C) of |
0 | 490 |
Some(w1) => if w = w1 then () else |
256 | 491 |
error("There are two declarations\n" ^ |
963 | 492 |
str_of_arity(t, w, [C]) ^ " and\n" ^ |
493 |
str_of_arity(t, w1, [C]) ^ "\n" ^ |
|
0 | 494 |
"with the same result class.") |
495 |
| None => (); |
|
496 |
||
963 | 497 |
(* 'coreg' checks if there are two declarations t:(Ss1)C1 and t:(Ss2)C2 |
0 | 498 |
such that C1 >= C2 then Ss1 >= Ss2 (elementwise) *) |
499 |
||
963 | 500 |
fun coreg_err(t, (C1,w1), (C2,w2)) = |
501 |
error("Declarations " ^ str_of_arity(t, w1, [C1]) ^ " and " |
|
502 |
^ str_of_arity(t, w2, [C2]) ^ " are in conflict"); |
|
0 | 503 |
|
963 | 504 |
fun coreg subclass (t, Cw1) = |
505 |
let fun check1(Cw1 as (C1,w1), Cw2 as (C2,w2)) = |
|
506 |
if leq subclass (C1,C2) |
|
507 |
then if lew subclass (w1,w2) then () else coreg_err(t, Cw1, Cw2) |
|
508 |
else () |
|
509 |
fun check(Cw2) = (check1(Cw1,Cw2); check1(Cw2,Cw1)) |
|
510 |
in seq check end; |
|
0 | 511 |
|
963 | 512 |
fun add_arity subclass ars (tCw as (_,Cw)) = |
513 |
(is_unique_decl ars tCw; coreg subclass tCw ars; Cw ins ars); |
|
0 | 514 |
|
256 | 515 |
fun varying_decls t = |
516 |
error ("Type constructor " ^ quote t ^ " has varying number of arguments"); |
|
0 | 517 |
|
518 |
||
963 | 519 |
(* 'merge_arities' builds the union of two 'arities' lists; |
422 | 520 |
it only checks the two restriction conditions and inserts afterwards |
521 |
all elements of the second list into the first one *) |
|
522 |
||
963 | 523 |
fun merge_arities subclass = |
524 |
let fun test_ar t (ars1, sw) = add_arity subclass ars1 (t,sw); |
|
422 | 525 |
|
963 | 526 |
fun merge_c (arities1, (c as (t, ars2))) = case assoc (arities1, t) of |
527 |
Some(ars1) => |
|
528 |
let val ars = foldl (test_ar t) (ars1, ars2) |
|
529 |
in overwrite (arities1, (t,ars)) end |
|
530 |
| None => c::arities1 |
|
422 | 531 |
in foldl merge_c end; |
532 |
||
963 | 533 |
fun add_tycons (tycons, tn as (t,n)) = |
534 |
(case assoc (tycons, t) of |
|
535 |
Some m => if m = n then tycons else varying_decls t |
|
536 |
| None => tn :: tycons); |
|
422 | 537 |
|
538 |
fun merge_abbrs (abbrs1, abbrs2) = |
|
621 | 539 |
let val abbrs = abbrs1 union abbrs2 in |
540 |
(case gen_duplicates eq_fst abbrs of |
|
422 | 541 |
[] => abbrs |
621 | 542 |
| dups => raise_term (dup_tyabbrs (map fst dups)) []) |
422 | 543 |
end; |
544 |
||
545 |
||
546 |
(* 'merge_tsigs' takes the above declared functions to merge two type |
|
547 |
signatures *) |
|
548 |
||
963 | 549 |
fun merge_tsigs(TySg{classes=classes1, default=default1, subclass=subclass1, |
550 |
tycons=tycons1, arities=arities1, abbrs=abbrs1}, |
|
551 |
TySg{classes=classes2, default=default2, subclass=subclass2, |
|
552 |
tycons=tycons2, arities=arities2, abbrs=abbrs2}) = |
|
422 | 553 |
let val classes' = classes1 union classes2; |
554 |
val subclass' = merge_subclass (subclass1, subclass2); |
|
963 | 555 |
val tycons' = foldl add_tycons (tycons1, tycons2) |
556 |
val arities' = merge_arities subclass' (arities1, arities2); |
|
422 | 557 |
val default' = min_sort subclass' (default1 @ default2); |
558 |
val abbrs' = merge_abbrs(abbrs1, abbrs2); |
|
963 | 559 |
in make_tsig(classes', subclass', default', tycons', abbrs', arities') end; |
422 | 560 |
|
561 |
||
562 |
||
563 |
(*** extend type signatures ***) |
|
564 |
||
621 | 565 |
(** add classes and subclass relations**) |
422 | 566 |
|
567 |
fun add_classes classes cs = |
|
568 |
(case cs inter classes of |
|
569 |
[] => cs @ classes |
|
570 |
| dups => err_dup_classes cs); |
|
571 |
||
572 |
||
573 |
(*'add_subclass' adds a tuple consisting of a new class (the new class has |
|
574 |
already been inserted into the 'classes' list) and its superclasses (they |
|
575 |
must be declared in 'classes' too) to the 'subclass' list of the given type |
|
576 |
signature; furthermore all inherited superclasses according to the |
|
577 |
superclasses brought with are inserted and there is a check that there are |
|
578 |
no cycles (i.e. C <= D <= C, with C <> D);*) |
|
579 |
||
580 |
fun add_subclass classes (subclass, (s, ges)) = |
|
621 | 581 |
let |
582 |
fun upd (subclass, s') = |
|
583 |
if s' mem classes then |
|
422 | 584 |
let val ges' = the (assoc (subclass, s)) |
585 |
in case assoc (subclass, s') of |
|
586 |
Some sups => if s mem sups |
|
587 |
then error(" Cycle :" ^ s^" <= "^ s'^" <= "^ s ) |
|
588 |
else overwrite (subclass, (s, sups union ges')) |
|
589 |
| None => subclass |
|
621 | 590 |
end |
591 |
else err_undcl_class s' |
|
592 |
in foldl upd (subclass @ [(s, ges)], ges) end; |
|
422 | 593 |
|
594 |
||
595 |
(* 'extend_classes' inserts all new classes into the corresponding |
|
596 |
lists ('classes', 'subclass') if possible *) |
|
597 |
||
621 | 598 |
fun extend_classes (classes, subclass, new_classes) = |
599 |
let |
|
600 |
val classes' = add_classes classes (map fst new_classes); |
|
601 |
val subclass' = foldl (add_subclass classes') (subclass, new_classes); |
|
422 | 602 |
in (classes', subclass') end; |
603 |
||
604 |
||
621 | 605 |
(* ext_tsig_classes *) |
606 |
||
607 |
fun ext_tsig_classes tsig new_classes = |
|
608 |
let |
|
963 | 609 |
val TySg {classes, subclass, default, tycons, abbrs, arities} = tsig; |
610 |
val (classes',subclass') = extend_classes (classes,subclass,new_classes); |
|
621 | 611 |
in |
963 | 612 |
make_tsig (classes', subclass', default, tycons, abbrs, arities) |
621 | 613 |
end; |
614 |
||
615 |
||
422 | 616 |
(* ext_tsig_subclass *) |
617 |
||
618 |
fun ext_tsig_subclass tsig pairs = |
|
619 |
let |
|
963 | 620 |
val TySg {classes, subclass, default, tycons, abbrs, arities} = tsig; |
422 | 621 |
|
622 |
(* FIXME clean! *) |
|
623 |
val subclass' = |
|
624 |
merge_subclass (subclass, map (fn (c1, c2) => (c1, [c2])) pairs); |
|
625 |
in |
|
963 | 626 |
make_tsig (classes, subclass', default, tycons, abbrs, arities) |
422 | 627 |
end; |
628 |
||
629 |
||
630 |
(* ext_tsig_defsort *) |
|
631 |
||
963 | 632 |
fun ext_tsig_defsort(TySg{classes,subclass,tycons,abbrs,arities,...}) default = |
633 |
make_tsig (classes, subclass, default, tycons, abbrs, arities); |
|
422 | 634 |
|
635 |
||
636 |
||
621 | 637 |
(** add types **) |
582 | 638 |
|
963 | 639 |
fun ext_tsig_types (TySg {classes, subclass, default, tycons, abbrs, arities}) ts = |
582 | 640 |
let |
641 |
fun check_type (c, n) = |
|
642 |
if n < 0 then err_neg_args c |
|
963 | 643 |
else if is_some (assoc (tycons, c)) then err_dup_tycon c |
582 | 644 |
else if is_some (assoc (abbrs, c)) then err_ty_confl c |
645 |
else (); |
|
646 |
in |
|
647 |
seq check_type ts; |
|
963 | 648 |
make_tsig (classes, subclass, default, ts @ tycons, abbrs, |
649 |
map (rpair [] o #1) ts @ arities) |
|
582 | 650 |
end; |
651 |
||
652 |
||
653 |
||
654 |
(** add type abbreviations **) |
|
655 |
||
656 |
fun abbr_errors tsig (a, (lhs_vs, rhs)) = |
|
657 |
let |
|
963 | 658 |
val TySg {tycons, abbrs, ...} = tsig; |
621 | 659 |
val rhs_vs = map (#1 o #1) (typ_tvars rhs); |
582 | 660 |
|
661 |
val dup_lhs_vars = |
|
662 |
(case duplicates lhs_vs of |
|
663 |
[] => [] |
|
621 | 664 |
| vs => ["Duplicate variables on lhs: " ^ commas_quote vs]); |
582 | 665 |
|
666 |
val extra_rhs_vars = |
|
667 |
(case gen_rems (op =) (rhs_vs, lhs_vs) of |
|
668 |
[] => [] |
|
621 | 669 |
| vs => ["Extra variables on rhs: " ^ commas_quote vs]); |
582 | 670 |
|
671 |
val tycon_confl = |
|
963 | 672 |
if is_none (assoc (tycons, a)) then [] |
582 | 673 |
else [ty_confl a]; |
674 |
||
675 |
val dup_abbr = |
|
676 |
if is_none (assoc (abbrs, a)) then [] |
|
677 |
else ["Duplicate declaration of abbreviation"]; |
|
678 |
in |
|
679 |
dup_lhs_vars @ extra_rhs_vars @ tycon_confl @ dup_abbr @ |
|
680 |
typ_errors tsig (rhs, []) |
|
681 |
end; |
|
682 |
||
621 | 683 |
fun prep_abbr tsig (a, vs, raw_rhs) = |
684 |
let |
|
685 |
fun err msgs = (seq writeln msgs; |
|
686 |
error ("The error(s) above occurred in type abbreviation " ^ quote a)); |
|
687 |
||
688 |
val rhs = rem_sorts (varifyT (no_tvars raw_rhs)) |
|
689 |
handle TYPE (msg, _, _) => err [msg]; |
|
690 |
val abbr = (a, (vs, rhs)); |
|
691 |
in |
|
582 | 692 |
(case abbr_errors tsig abbr of |
621 | 693 |
[] => abbr |
694 |
| msgs => err msgs) |
|
582 | 695 |
end; |
696 |
||
963 | 697 |
fun add_abbr (tsig as TySg{classes,subclass,default,tycons,arities,abbrs}, |
698 |
abbr) = |
|
621 | 699 |
make_tsig |
963 | 700 |
(classes,subclass,default,tycons, prep_abbr tsig abbr :: abbrs, arities); |
621 | 701 |
|
702 |
fun ext_tsig_abbrs tsig raw_abbrs = foldl add_abbr (tsig, raw_abbrs); |
|
582 | 703 |
|
704 |
||
705 |
||
422 | 706 |
(** add arities **) |
707 |
||
0 | 708 |
(* 'coregular' checks |
963 | 709 |
- the two restrictions 'is_unique_decl' and 'coreg' |
256 | 710 |
- if the classes in the new type declarations are known in the |
0 | 711 |
given type signature |
712 |
- if one type constructor has always the same number of arguments; |
|
256 | 713 |
if one type declaration has passed all checks it is inserted into |
963 | 714 |
the 'arities' association list of the given type signatrure *) |
0 | 715 |
|
963 | 716 |
fun coregular (classes, subclass, tycons) = |
256 | 717 |
let fun ex C = if C mem classes then () else err_undcl_class(C); |
0 | 718 |
|
963 | 719 |
fun addar(arities, (t, (w, C))) = case assoc(tycons, t) of |
0 | 720 |
Some(n) => if n <> length w then varying_decls(t) else |
963 | 721 |
((seq o seq) ex w; ex C; |
722 |
let val ars = the (assoc(arities, t)) |
|
723 |
val ars' = add_arity subclass ars (t,(C,w)) |
|
724 |
in overwrite(arities, (t,ars')) end) |
|
256 | 725 |
| None => err_undcl_type(t); |
0 | 726 |
|
963 | 727 |
in addar end; |
0 | 728 |
|
729 |
||
963 | 730 |
(* 'close' extends the 'arities' association list after all new type |
0 | 731 |
declarations have been inserted successfully: |
732 |
for every declaration t:(Ss)C , for all classses D with C <= D: |
|
733 |
if there is no declaration t:(Ss')C' with C < C' and C' <= D |
|
963 | 734 |
then insert the declaration t:(Ss)D into 'arities' |
0 | 735 |
this means, if there exists a declaration t:(Ss)C and there is |
736 |
no declaration t:(Ss')D with C <=D then the declaration holds |
|
256 | 737 |
for all range classes more general than C *) |
738 |
||
963 | 739 |
fun close subclass arities = |
256 | 740 |
let fun check sl (l, (s, dom)) = case assoc (subclass, s) of |
621 | 741 |
Some sups => |
256 | 742 |
let fun close_sup (l, sup) = |
743 |
if exists (fn s'' => less subclass (s, s'') andalso |
|
744 |
leq subclass (s'', sup)) sl |
|
0 | 745 |
then l |
256 | 746 |
else (sup, dom)::l |
747 |
in foldl close_sup (l, sups) end |
|
0 | 748 |
| None => l; |
256 | 749 |
fun ext (s, l) = (s, foldl (check (map #1 l)) (l, l)); |
963 | 750 |
in map ext arities end; |
0 | 751 |
|
422 | 752 |
|
621 | 753 |
(* ext_tsig_arities *) |
256 | 754 |
|
621 | 755 |
fun ext_tsig_arities tsig sarities = |
416 | 756 |
let |
963 | 757 |
val TySg {classes, subclass, default, tycons, arities, abbrs} = tsig; |
758 |
val arities1 = |
|
759 |
flat (map (fn (t, ss, cs) => map (fn c => (t, (ss, c))) cs) sarities); |
|
760 |
val arities2 = foldl (coregular (classes, subclass, tycons)) |
|
761 |
(arities, min_domain subclass arities1) |
|
621 | 762 |
|> close subclass; |
416 | 763 |
in |
963 | 764 |
make_tsig (classes, subclass, default, tycons, abbrs, arities2) |
416 | 765 |
end; |
0 | 766 |
|
767 |
||
416 | 768 |
|
769 |
(*** type unification and inference ***) |
|
0 | 770 |
|
771 |
(* |
|
621 | 772 |
Input: |
773 |
- a 'raw' term which contains only dummy types and some explicit type |
|
774 |
constraints encoded as terms. |
|
775 |
- the expected type of the term. |
|
0 | 776 |
|
621 | 777 |
Output: |
778 |
- the correctly typed term |
|
779 |
- the substitution needed to unify the actual type of the term with its |
|
780 |
expected type; only the TVars in the expected type are included. |
|
0 | 781 |
|
621 | 782 |
During type inference all TVars in the term have negative index. This keeps |
783 |
them apart from normal TVars, which is essential, because at the end the |
|
784 |
type of the term is unified with the expected type, which contains normal |
|
785 |
TVars. |
|
0 | 786 |
|
621 | 787 |
1. Add initial type information to the term (attach_types). |
788 |
This freezes (freeze_vars) TVars in explicitly provided types (eg |
|
789 |
constraints or defaults) by turning them into TFrees. |
|
790 |
2. Carry out type inference, possibly introducing new negative TVars. |
|
791 |
3. Unify actual and expected type. |
|
792 |
4. Turn all (negative) TVars into unique new TFrees (freeze). |
|
793 |
5. Thaw all TVars frozen in step 1 (thaw_vars). |
|
0 | 794 |
*) |
795 |
||
796 |
(*Raised if types are not unifiable*) |
|
797 |
exception TUNIFY; |
|
798 |
||
621 | 799 |
val tyvar_count = ref ~1; |
0 | 800 |
|
801 |
fun tyinit() = (tyvar_count := ~1); |
|
802 |
||
621 | 803 |
fun new_tvar_inx () = (tyvar_count := ! tyvar_count - 1; ! tyvar_count) |
0 | 804 |
|
805 |
(* |
|
806 |
Generate new TVar. Index is < ~1 to distinguish it from TVars generated from |
|
807 |
variable names (see id_type). Name is arbitrary because index is new. |
|
808 |
*) |
|
809 |
||
256 | 810 |
fun gen_tyvar(S) = TVar(("'a", new_tvar_inx()), S); |
0 | 811 |
|
812 |
(*Occurs check: type variable occurs in type?*) |
|
813 |
fun occ v tye = |
|
256 | 814 |
let fun occ(Type(_, Ts)) = exists occ Ts |
0 | 815 |
| occ(TFree _) = false |
256 | 816 |
| occ(TVar(w, _)) = v=w orelse |
817 |
(case assoc(tye, w) of |
|
0 | 818 |
None => false |
819 |
| Some U => occ U); |
|
820 |
in occ end; |
|
821 |
||
256 | 822 |
(*Chase variable assignments in tye. |
823 |
If devar (T, tye) returns a type var then it must be unassigned.*) |
|
824 |
fun devar (T as TVar(v, _), tye) = (case assoc(tye, v) of |
|
825 |
Some U => devar (U, tye) |
|
0 | 826 |
| None => T) |
256 | 827 |
| devar (T, tye) = T; |
0 | 828 |
|
829 |
||
830 |
(* 'dom' returns for a type constructor t the list of those domains |
|
831 |
which deliver a given range class C *) |
|
832 |
||
963 | 833 |
fun dom arities t C = case assoc2 (arities, (t, C)) of |
0 | 834 |
Some(Ss) => Ss |
835 |
| None => raise TUNIFY; |
|
836 |
||
837 |
||
838 |
(* 'Dom' returns the union of all domain lists of 'dom' for a given sort S |
|
839 |
(i.e. a set of range classes ); the union is carried out elementwise |
|
840 |
for the seperate sorts in the domains *) |
|
841 |
||
963 | 842 |
fun Dom (subclass, arities) (t, S) = |
843 |
let val domlist = map (dom arities t) S; |
|
0 | 844 |
in if null domlist then [] |
256 | 845 |
else foldl (elementwise_union subclass) (hd domlist, tl domlist) |
0 | 846 |
end; |
847 |
||
848 |
||
963 | 849 |
fun W ((T, S), tsig as TySg{subclass, arities, ...}, tye) = |
256 | 850 |
let fun Wd ((T, S), tye) = W ((devar (T, tye), S), tsig, tye) |
851 |
fun Wk(T as TVar(v, S')) = |
|
852 |
if sortorder subclass (S', S) then tye |
|
853 |
else (v, gen_tyvar(union_sort subclass (S', S)))::tye |
|
854 |
| Wk(T as TFree(v, S')) = if sortorder subclass (S', S) then tye |
|
855 |
else raise TUNIFY |
|
856 |
| Wk(T as Type(f, Ts)) = |
|
857 |
if null S then tye |
|
963 | 858 |
else foldr Wd (Ts~~(Dom (subclass, arities) (f, S)) , tye) |
0 | 859 |
in Wk(T) end; |
860 |
||
861 |
||
862 |
(* Order-sorted Unification of Types (U) *) |
|
863 |
||
864 |
(* Precondition: both types are well-formed w.r.t. type constructor arities *) |
|
963 | 865 |
fun unify (tsig as TySg{subclass, arities, ...}) = |
256 | 866 |
let fun unif ((T, U), tye) = |
867 |
case (devar(T, tye), devar(U, tye)) of |
|
868 |
(T as TVar(v, S1), U as TVar(w, S2)) => |
|
0 | 869 |
if v=w then tye else |
256 | 870 |
if sortorder subclass (S1, S2) then (w, T)::tye else |
871 |
if sortorder subclass (S2, S1) then (v, U)::tye |
|
872 |
else let val nu = gen_tyvar (union_sort subclass (S1, S2)) |
|
873 |
in (v, nu)::(w, nu)::tye end |
|
874 |
| (T as TVar(v, S), U) => |
|
963 | 875 |
if occ v tye U then raise TUNIFY else W ((U,S), tsig, (v, U)::tye) |
256 | 876 |
| (U, T as TVar (v, S)) => |
963 | 877 |
if occ v tye U then raise TUNIFY else W ((U,S), tsig, (v, U)::tye) |
256 | 878 |
| (Type(a, Ts), Type(b, Us)) => |
879 |
if a<>b then raise TUNIFY else foldr unif (Ts~~Us, tye) |
|
880 |
| (T, U) => if T=U then tye else raise TUNIFY |
|
0 | 881 |
in unif end; |
882 |
||
883 |
||
450 | 884 |
(* raw_unify (ignores sorts) *) |
885 |
||
886 |
fun raw_unify (ty1, ty2) = |
|
887 |
(unify tsig0 ((rem_sorts ty1, rem_sorts ty2), []); true) |
|
888 |
handle TUNIFY => false; |
|
889 |
||
890 |
||
0 | 891 |
(*Type inference for polymorphic term*) |
892 |
fun infer tsig = |
|
256 | 893 |
let fun inf(Ts, Const (_, T), tye) = (T, tye) |
894 |
| inf(Ts, Free (_, T), tye) = (T, tye) |
|
895 |
| inf(Ts, Bound i, tye) = ((nth_elem(i, Ts) , tye) |
|
0 | 896 |
handle LIST _=> raise TYPE ("loose bound variable", [], [Bound i])) |
256 | 897 |
| inf(Ts, Var (_, T), tye) = (T, tye) |
898 |
| inf(Ts, Abs (_, T, body), tye) = |
|
899 |
let val (U, tye') = inf(T::Ts, body, tye) in (T-->U, tye') end |
|
0 | 900 |
| inf(Ts, f$u, tye) = |
256 | 901 |
let val (U, tyeU) = inf(Ts, u, tye); |
902 |
val (T, tyeT) = inf(Ts, f, tyeU); |
|
0 | 903 |
fun err s = |
904 |
raise TYPE(s, [inst_typ tyeT T, inst_typ tyeT U], [f$u]) |
|
256 | 905 |
in case T of |
906 |
Type("fun", [T1, T2]) => |
|
907 |
( (T2, unify tsig ((T1, U), tyeT)) |
|
0 | 908 |
handle TUNIFY => err"type mismatch in application" ) |
256 | 909 |
| TVar _ => |
0 | 910 |
let val T2 = gen_tyvar([]) |
911 |
in (T2, unify tsig ((T, U-->T2), tyeT)) |
|
912 |
handle TUNIFY => err"type mismatch in application" |
|
913 |
end |
|
914 |
| _ => err"rator must have function type" |
|
915 |
end |
|
916 |
in inf end; |
|
917 |
||
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
918 |
val freeze_vars = |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
919 |
map_type_tvar (fn (v, S) => TFree(Syntax.string_of_vname v, S)); |
0 | 920 |
|
921 |
(* Attach a type to a constant *) |
|
256 | 922 |
fun type_const (a, T) = Const(a, incr_tvar (new_tvar_inx()) T); |
0 | 923 |
|
924 |
(*Find type of ident. If not in table then use ident's name for tyvar |
|
925 |
to get consistent typing.*) |
|
256 | 926 |
fun new_id_type a = TVar(("'"^a, new_tvar_inx()), []); |
927 |
fun type_of_ixn(types, ixn as (a, _)) = |
|
565 | 928 |
case types ixn of Some T => freeze_vars T | None => TVar(("'"^a, ~1), []); |
929 |
||
930 |
fun constrain (term, T) = Const (Syntax.constrainC, T --> T) $ term; |
|
0 | 931 |
|
565 | 932 |
fun constrainAbs (Abs (a, _, body), T) = Abs (a, T, body) |
933 |
| constrainAbs _ = sys_error "constrainAbs"; |
|
256 | 934 |
|
0 | 935 |
|
565 | 936 |
(* attach_types *) |
937 |
||
0 | 938 |
(* |
256 | 939 |
Attach types to a term. Input is a "parse tree" containing dummy types. |
940 |
Type constraints are translated and checked for validity wrt tsig. TVars in |
|
941 |
constraints are frozen. |
|
0 | 942 |
|
256 | 943 |
The atoms in the resulting term satisfy the following spec: |
0 | 944 |
|
256 | 945 |
Const (a, T): |
946 |
T is a renamed copy of the generic type of a; renaming decreases index of |
|
947 |
all TVars by new_tvar_inx(), which is less than ~1. The index of all |
|
948 |
TVars in the generic type must be 0 for this to work! |
|
0 | 949 |
|
256 | 950 |
Free (a, T), Var (ixn, T): |
951 |
T is either the frozen default type of a or TVar (("'"^a, ~1), []) |
|
0 | 952 |
|
256 | 953 |
Abs (a, T, _): |
954 |
T is either a type constraint or TVar (("'" ^ a, i), []), where i is |
|
955 |
generated by new_tvar_inx(). Thus different abstractions can have the |
|
956 |
bound variables of the same name but different types. |
|
0 | 957 |
*) |
958 |
||
1257 | 959 |
(* FIXME consistency of sort_env / sorts (!?) *) |
256 | 960 |
|
565 | 961 |
fun attach_types (tsig, const_type, types, sorts) tm = |
256 | 962 |
let |
565 | 963 |
val sort_env = Syntax.raw_term_sorts tm; |
964 |
fun def_sort xi = if_none (sorts xi) (defaultS tsig); |
|
256 | 965 |
|
565 | 966 |
fun prepareT t = |
967 |
freeze_vars (cert_typ tsig (Syntax.typ_of_term sort_env def_sort t)); |
|
256 | 968 |
|
969 |
fun add (Const (a, _)) = |
|
565 | 970 |
(case const_type a of |
256 | 971 |
Some T => type_const (a, T) |
972 |
| None => raise_type ("No such constant: " ^ quote a) [] []) |
|
973 |
| add (Free (a, _)) = |
|
565 | 974 |
(case const_type a of |
256 | 975 |
Some T => type_const (a, T) |
976 |
| None => Free (a, type_of_ixn (types, (a, ~1)))) |
|
977 |
| add (Var (ixn, _)) = Var (ixn, type_of_ixn (types, ixn)) |
|
565 | 978 |
| add (Bound i) = Bound i |
256 | 979 |
| add (Abs (a, _, body)) = Abs (a, new_id_type a, add body) |
980 |
| add ((f as Const (a, _) $ t1) $ t2) = |
|
981 |
if a = Syntax.constrainC then |
|
982 |
constrain (add t1, prepareT t2) |
|
983 |
else if a = Syntax.constrainAbsC then |
|
984 |
constrainAbs (add t1, prepareT t2) |
|
985 |
else add f $ add t2 |
|
986 |
| add (f $ t) = add f $ add t; |
|
565 | 987 |
in add tm end; |
0 | 988 |
|
989 |
||
990 |
(* Post-Processing *) |
|
991 |
||
992 |
(*Instantiation of type variables in terms*) |
|
993 |
fun inst_types tye = map_term_types (inst_typ tye); |
|
994 |
||
995 |
(*Delete explicit constraints -- occurrences of "_constrain" *) |
|
256 | 996 |
fun unconstrain (Abs(a, T, t)) = Abs(a, T, unconstrain t) |
997 |
| unconstrain ((f as Const(a, _)) $ t) = |
|
0 | 998 |
if a=Syntax.constrainC then unconstrain t |
999 |
else unconstrain f $ unconstrain t |
|
1000 |
| unconstrain (f$t) = unconstrain f $ unconstrain t |
|
1001 |
| unconstrain (t) = t; |
|
1002 |
||
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1003 |
fun nextname(pref,c) = if c="z" then (pref^"a", "a") else (pref,chr(ord(c)+1)); |
0 | 1004 |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1005 |
fun newtvars used = |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1006 |
let fun new([],_,vmap) = vmap |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1007 |
| new(ixn::ixns,p as (pref,c),vmap) = |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1008 |
let val nm = pref ^ c |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1009 |
in if nm mem used then new(ixn::ixns,nextname p, vmap) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1010 |
else new(ixns, nextname p, (ixn,nm)::vmap) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1011 |
end |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1012 |
in new end; |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1013 |
|
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1014 |
(* |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1015 |
Turn all TVars which satisfy p into new (if freeze then TFrees else TVars). |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1016 |
Note that if t contains frozen TVars there is the possibility that a TVar is |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1017 |
turned into one of those. This is sound but not complete. |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1018 |
*) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1019 |
fun convert used freeze p t = |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1020 |
let val used = if freeze then add_term_tfree_names(t, used) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1021 |
else used union |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1022 |
(map #1 (filter_out p (add_term_tvar_ixns(t, [])))) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1023 |
val ixns = filter p (add_term_tvar_ixns(t, [])); |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1024 |
val vmap = newtvars used (ixns,("'","a"),[]); |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1025 |
fun conv(var as (ixn,S)) = case assoc(vmap,ixn) of |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1026 |
None => TVar(var) | |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1027 |
Some(a) => if freeze then TFree(a,S) else TVar((a,0),S); |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1028 |
in map_term_types (map_type_tvar conv) t end; |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1029 |
|
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1030 |
fun freeze t = convert (add_term_tfree_names(t,[])) true (K true) t; |
0 | 1031 |
|
1032 |
(* Thaw all TVars that were frozen in freeze_vars *) |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1033 |
val thaw_vars = |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1034 |
let fun thaw(f as (a, S)) = (case explode a of |
256 | 1035 |
"?"::"'"::vn => let val ((b, i), _) = Syntax.scan_varname vn |
1036 |
in TVar(("'"^b, i), S) end |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1037 |
| _ => TFree f) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1038 |
in map_type_tfree thaw end; |
0 | 1039 |
|
1040 |
||
1041 |
fun restrict tye = |
|
256 | 1042 |
let fun clean(tye1, ((a, i), T)) = |
1043 |
if i < 0 then tye1 else ((a, i), inst_typ tye T) :: tye1 |
|
1044 |
in foldl clean ([], tye) end |
|
0 | 1045 |
|
1046 |
||
1047 |
(*Infer types for term t using tables. Check that t's type and T unify *) |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1048 |
(*freeze determines if internal TVars are turned into TFrees or TVars*) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1049 |
fun infer_term (tsig, const_type, types, sorts, used, freeze, T, t) = |
565 | 1050 |
let |
1051 |
val u = attach_types (tsig, const_type, types, sorts) t; |
|
1052 |
val (U, tye) = infer tsig ([], u, []); |
|
1053 |
val uu = unconstrain u; |
|
1054 |
val tye' = unify tsig ((T, U), tye) handle TUNIFY => raise TYPE |
|
1055 |
("Term does not have expected type", [T, U], [inst_types tye uu]) |
|
1056 |
val Ttye = restrict tye' (*restriction to TVars in T*) |
|
1057 |
val all = Const("", Type("", map snd Ttye)) $ (inst_types tye' uu) |
|
1058 |
(*all is a dummy term which contains all exported TVars*) |
|
1059 |
val Const(_, Type(_, Ts)) $ u'' = |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1060 |
map_term_types thaw_vars (convert used freeze (fn (_, i) => i < 0) all) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
1061 |
(*convert all internally generated TVars into TFrees or TVars |
565 | 1062 |
and thaw all initially frozen TVars*) |
1063 |
in |
|
1064 |
(u'', (map fst Ttye) ~~ Ts) |
|
1065 |
end; |
|
0 | 1066 |
|
621 | 1067 |
fun infer_types args = (tyinit (); infer_term args); |
0 | 1068 |
|
1069 |
||
1070 |
end; |