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