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