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