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