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