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