author | wenzelm |
Thu, 30 Aug 2007 15:04:42 +0200 | |
changeset 24484 | 013b98b57b86 |
parent 24275 | bbc3dab6d4fe |
child 24485 | 687bbb686ef9 |
permissions | -rw-r--r-- |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/type_infer.ML |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
3 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
4 |
|
22698 | 5 |
Simple type inference. |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
6 |
*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
7 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
8 |
signature TYPE_INFER = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
9 |
sig |
8087 | 10 |
val anyT: sort -> typ |
11 |
val logicT: typ |
|
8611 | 12 |
val polymorphicT: typ -> typ |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
13 |
val constrain: term -> typ -> term |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
14 |
val param: int -> string * sort -> typ |
22771 | 15 |
val paramify_vars: typ -> typ |
18339 | 16 |
val paramify_dummies: typ -> int -> typ * int |
22698 | 17 |
val appl_error: Pretty.pp -> string -> term -> typ -> term -> typ -> string list |
24275 | 18 |
val infer_types: Pretty.pp -> Type.tsig -> Type.mode -> |
22698 | 19 |
(string -> typ option) -> (indexname -> typ option) -> |
20 |
Name.context -> bool -> (term * typ) list -> (term * typ) list * (indexname * typ) list |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
21 |
end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
22 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
23 |
structure TypeInfer: TYPE_INFER = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
24 |
struct |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
25 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
26 |
|
22698 | 27 |
(** type parameters and constraints **) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
28 |
|
22698 | 29 |
fun anyT S = TFree ("'_dummy_", S); |
30 |
val logicT = anyT []; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
31 |
|
22698 | 32 |
(*indicate polymorphic Vars*) |
33 |
fun polymorphicT T = Type ("_polymorphic_", [T]); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
34 |
|
22698 | 35 |
fun constrain t T = |
36 |
if T = dummyT then t |
|
37 |
else Const ("_type_constraint_", T --> T) $ t; |
|
38 |
||
39 |
||
40 |
(* user parameters *) |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
41 |
|
22698 | 42 |
fun is_param_default (x, _) = size x > 0 andalso ord x = ord "?"; |
43 |
fun param i (x, S) = TVar (("?" ^ x, i), S); |
|
44 |
||
22771 | 45 |
val paramify_vars = Term.map_atyps (fn TVar ((x, i), S) => param i (x, S) | T => T); |
46 |
||
22698 | 47 |
val paramify_dummies = |
48 |
let |
|
49 |
fun dummy S maxidx = (param (maxidx + 1) ("'dummy", S), maxidx + 1); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
50 |
|
22698 | 51 |
fun paramify (TFree ("'_dummy_", S)) maxidx = dummy S maxidx |
52 |
| paramify (Type ("dummy", _)) maxidx = dummy [] maxidx |
|
53 |
| paramify (Type (a, Ts)) maxidx = |
|
54 |
let val (Ts', maxidx') = fold_map paramify Ts maxidx |
|
55 |
in (Type (a, Ts'), maxidx') end |
|
56 |
| paramify T maxidx = (T, maxidx); |
|
57 |
in paramify end; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
58 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
59 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
60 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
61 |
(** pretyps and preterms **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
62 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
63 |
(*links to parameters may get instantiated, anything else is rigid*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
64 |
datatype pretyp = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
65 |
PType of string * pretyp list | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
66 |
PTFree of string * sort | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
67 |
PTVar of indexname * sort | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
68 |
Param of sort | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
69 |
Link of pretyp ref; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
70 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
71 |
datatype preterm = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
72 |
PConst of string * pretyp | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
73 |
PFree of string * pretyp | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
74 |
PVar of indexname * pretyp | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
75 |
PBound of int | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
76 |
PAbs of string * pretyp * preterm | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
77 |
PAppl of preterm * preterm | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
78 |
Constraint of preterm * pretyp; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
79 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
80 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
81 |
(* utils *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
82 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
83 |
val mk_param = Link o ref o Param; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
84 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
85 |
fun deref (T as Link (ref (Param _))) = T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
86 |
| deref (Link (ref T)) = deref T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
87 |
| deref T = T; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
88 |
|
16195 | 89 |
fun fold_pretyps f (PConst (_, T)) x = f T x |
90 |
| fold_pretyps f (PFree (_, T)) x = f T x |
|
91 |
| fold_pretyps f (PVar (_, T)) x = f T x |
|
92 |
| fold_pretyps _ (PBound _) x = x |
|
93 |
| fold_pretyps f (PAbs (_, T, t)) x = fold_pretyps f t (f T x) |
|
94 |
| fold_pretyps f (PAppl (t, u)) x = fold_pretyps f u (fold_pretyps f t x) |
|
95 |
| fold_pretyps f (Constraint (t, T)) x = f T (fold_pretyps f t x); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
96 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
97 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
98 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
99 |
(** raw typs/terms to pretyps/preterms **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
100 |
|
20668 | 101 |
(* pretyp_of *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
102 |
|
20668 | 103 |
fun pretyp_of is_param typ params = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
104 |
let |
20668 | 105 |
val params' = fold_atyps |
106 |
(fn TVar (xi as (x, _), S) => |
|
107 |
(fn ps => |
|
20735 | 108 |
if is_param xi andalso not (Vartab.defined ps xi) |
109 |
then Vartab.update (xi, mk_param S) ps else ps) |
|
20668 | 110 |
| _ => I) typ params; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
111 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
112 |
fun pre_of (TVar (v as (xi, _))) = |
20735 | 113 |
(case Vartab.lookup params' xi of |
15531 | 114 |
NONE => PTVar v |
115 |
| SOME p => p) |
|
8087 | 116 |
| pre_of (TFree ("'_dummy_", S)) = mk_param S |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
117 |
| pre_of (TFree v) = PTFree v |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
118 |
| pre_of (T as Type (a, Ts)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
119 |
if T = dummyT then mk_param [] |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
120 |
else PType (a, map pre_of Ts); |
20668 | 121 |
in (pre_of typ, params') end; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
122 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
123 |
|
20668 | 124 |
(* preterm_of *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
125 |
|
20668 | 126 |
fun preterm_of const_type is_param tm (vparams, params) = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
127 |
let |
20668 | 128 |
fun add_vparm xi ps = |
20735 | 129 |
if not (Vartab.defined ps xi) then |
130 |
Vartab.update (xi, mk_param []) ps |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
131 |
else ps; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
132 |
|
20668 | 133 |
val vparams' = fold_aterms |
134 |
(fn Var (_, Type ("_polymorphic_", _)) => I |
|
135 |
| Var (xi, _) => add_vparm xi |
|
136 |
| Free (x, _) => add_vparm (x, ~1) |
|
137 |
| _ => I) |
|
138 |
tm vparams; |
|
20735 | 139 |
fun var_param xi = the (Vartab.lookup vparams' xi); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
140 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
141 |
val preT_of = pretyp_of is_param; |
20735 | 142 |
fun polyT_of T = fst (pretyp_of (K true) T Vartab.empty); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
143 |
|
22698 | 144 |
fun constraint T t ps = |
20668 | 145 |
if T = dummyT then (t, ps) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
146 |
else |
20668 | 147 |
let val (T', ps') = preT_of T ps |
148 |
in (Constraint (t, T'), ps') end; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
149 |
|
20668 | 150 |
fun pre_of (Const (c, T)) ps = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
151 |
(case const_type c of |
22698 | 152 |
SOME U => constraint T (PConst (c, polyT_of U)) ps |
15531 | 153 |
| NONE => raise TYPE ("No such constant: " ^ quote c, [], [])) |
20735 | 154 |
| pre_of (Var (xi, Type ("_polymorphic_", [T]))) ps = (PVar (xi, polyT_of T), ps) |
22698 | 155 |
| pre_of (Var (xi, T)) ps = constraint T (PVar (xi, var_param xi)) ps |
156 |
| pre_of (Free (x, T)) ps = constraint T (PFree (x, var_param (x, ~1))) ps |
|
20668 | 157 |
| pre_of (Const ("_type_constraint_", Type ("fun", [T, _])) $ t) ps = |
22698 | 158 |
pre_of t ps |-> constraint T |
20668 | 159 |
| pre_of (Bound i) ps = (PBound i, ps) |
160 |
| pre_of (Abs (x, T, t)) ps = |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
161 |
let |
20668 | 162 |
val (T', ps') = preT_of T ps; |
163 |
val (t', ps'') = pre_of t ps'; |
|
164 |
in (PAbs (x, T', t'), ps'') end |
|
165 |
| pre_of (t $ u) ps = |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
166 |
let |
20668 | 167 |
val (t', ps') = pre_of t ps; |
168 |
val (u', ps'') = pre_of u ps'; |
|
169 |
in (PAppl (t', u'), ps'') end; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
170 |
|
20668 | 171 |
val (tm', params') = pre_of tm params; |
172 |
in (tm', (vparams', params')) end; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
173 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
174 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
175 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
176 |
(** pretyps/terms to typs/terms **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
177 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
178 |
(* add_parms *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
179 |
|
16195 | 180 |
fun add_parmsT (PType (_, Ts)) rs = fold add_parmsT Ts rs |
20854 | 181 |
| add_parmsT (Link (r as ref (Param _))) rs = insert (op =) r rs |
16195 | 182 |
| add_parmsT (Link (ref T)) rs = add_parmsT T rs |
183 |
| add_parmsT _ rs = rs; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
184 |
|
16195 | 185 |
val add_parms = fold_pretyps add_parmsT; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
186 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
187 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
188 |
(* add_names *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
189 |
|
20161 | 190 |
fun add_namesT (PType (_, Ts)) = fold add_namesT Ts |
191 |
| add_namesT (PTFree (x, _)) = Name.declare x |
|
192 |
| add_namesT (PTVar ((x, _), _)) = Name.declare x |
|
193 |
| add_namesT (Link (ref T)) = add_namesT T |
|
194 |
| add_namesT (Param _) = I; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
195 |
|
16195 | 196 |
val add_names = fold_pretyps add_namesT; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
197 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
198 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
199 |
(* simple_typ/term_of *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
200 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
201 |
(*deref links, fail on params*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
202 |
fun simple_typ_of (PType (a, Ts)) = Type (a, map simple_typ_of Ts) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
203 |
| simple_typ_of (PTFree v) = TFree v |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
204 |
| simple_typ_of (PTVar v) = TVar v |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
205 |
| simple_typ_of (Link (ref T)) = simple_typ_of T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
206 |
| simple_typ_of (Param _) = sys_error "simple_typ_of: illegal Param"; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
207 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
208 |
(*convert types, drop constraints*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
209 |
fun simple_term_of (PConst (c, T)) = Const (c, simple_typ_of T) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
210 |
| simple_term_of (PFree (x, T)) = Free (x, simple_typ_of T) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
211 |
| simple_term_of (PVar (xi, T)) = Var (xi, simple_typ_of T) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
212 |
| simple_term_of (PBound i) = Bound i |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
213 |
| simple_term_of (PAbs (x, T, t)) = Abs (x, simple_typ_of T, simple_term_of t) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
214 |
| simple_term_of (PAppl (t, u)) = simple_term_of t $ simple_term_of u |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
215 |
| simple_term_of (Constraint (t, _)) = simple_term_of t; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
216 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
217 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
218 |
(* typs_terms_of *) (*DESTRUCTIVE*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
219 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
220 |
fun typs_terms_of used mk_var prfx (Ts, ts) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
221 |
let |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
222 |
fun elim (r as ref (Param S), x) = r := mk_var (x, S) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
223 |
| elim _ = (); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
224 |
|
16195 | 225 |
val used' = fold add_names ts (fold add_namesT Ts used); |
226 |
val parms = rev (fold add_parms ts (fold add_parmsT Ts [])); |
|
20161 | 227 |
val names = Name.invents used' (prfx ^ "'a") (length parms); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
228 |
in |
16195 | 229 |
ListPair.app elim (parms, names); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
230 |
(map simple_typ_of Ts, map simple_term_of ts) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
231 |
end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
232 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
233 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
234 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
235 |
(** order-sorted unification of types **) (*DESTRUCTIVE*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
236 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
237 |
exception NO_UNIFIER of string; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
238 |
|
19465 | 239 |
fun unify pp tsig = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
240 |
let |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
241 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
242 |
(* adjust sorts of parameters *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
243 |
|
19465 | 244 |
fun not_of_sort x S' S = |
14828 | 245 |
"Variable " ^ x ^ "::" ^ Pretty.string_of_sort pp S' ^ " not of sort " ^ |
246 |
Pretty.string_of_sort pp S; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
247 |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
248 |
fun meet (_, []) = () |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
249 |
| meet (Link (r as (ref (Param S'))), S) = |
19465 | 250 |
if Type.subsort tsig (S', S) then () |
251 |
else r := mk_param (Type.inter_sort tsig (S', S)) |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
252 |
| meet (Link (ref T), S) = meet (T, S) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
253 |
| meet (PType (a, Ts), S) = |
19465 | 254 |
ListPair.app meet (Ts, Type.arity_sorts pp tsig a S |
255 |
handle ERROR msg => raise NO_UNIFIER msg) |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
256 |
| meet (PTFree (x, S'), S) = |
19465 | 257 |
if Type.subsort tsig (S', S) then () |
258 |
else raise NO_UNIFIER (not_of_sort x S' S) |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
259 |
| meet (PTVar (xi, S'), S) = |
19465 | 260 |
if Type.subsort tsig (S', S) then () |
22678 | 261 |
else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S) |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
262 |
| meet (Param _, _) = sys_error "meet"; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
263 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
264 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
265 |
(* occurs check and assigment *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
266 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
267 |
fun occurs_check r (Link (r' as ref T)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
268 |
if r = r' then raise NO_UNIFIER "Occurs check!" |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
269 |
else occurs_check r T |
15570 | 270 |
| occurs_check r (PType (_, Ts)) = List.app (occurs_check r) Ts |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
271 |
| occurs_check _ _ = (); |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
272 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
273 |
fun assign r T S = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
274 |
(case deref T of |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
275 |
T' as Link (r' as ref (Param _)) => |
8087 | 276 |
if r = r' then () else (meet (T', S); r := T') |
277 |
| T' => (occurs_check r T'; meet (T', S); r := T')); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
278 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
279 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
280 |
(* unification *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
281 |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
282 |
fun unif (Link (r as ref (Param S)), T) = assign r T S |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
283 |
| unif (T, Link (r as ref (Param S))) = assign r T S |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
284 |
| unif (Link (ref T), U) = unif (T, U) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
285 |
| unif (T, Link (ref U)) = unif (T, U) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
286 |
| unif (PType (a, Ts), PType (b, Us)) = |
2979 | 287 |
if a <> b then |
14828 | 288 |
raise NO_UNIFIER ("Clash of types " ^ quote a ^ " and " ^ quote b) |
16195 | 289 |
else ListPair.app unif (Ts, Us) |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
290 |
| unif (T, U) = if T = U then () else raise NO_UNIFIER ""; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
291 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
292 |
in unif end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
293 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
294 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
295 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
296 |
(** type inference **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
297 |
|
22698 | 298 |
(* appl_error *) |
299 |
||
14828 | 300 |
fun appl_error pp why t T u U = |
8087 | 301 |
["Type error in application: " ^ why, |
302 |
"", |
|
303 |
Pretty.string_of (Pretty.block |
|
14828 | 304 |
[Pretty.str "Operator:", Pretty.brk 2, Pretty.term pp t, |
305 |
Pretty.str " ::", Pretty.brk 1, Pretty.typ pp T]), |
|
8087 | 306 |
Pretty.string_of (Pretty.block |
14828 | 307 |
[Pretty.str "Operand:", Pretty.brk 3, Pretty.term pp u, |
308 |
Pretty.str " ::", Pretty.brk 1, Pretty.typ pp U]), |
|
8087 | 309 |
""]; |
310 |
||
5635 | 311 |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
312 |
(* infer *) (*DESTRUCTIVE*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
313 |
|
19465 | 314 |
fun infer pp tsig = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
315 |
let |
2979 | 316 |
(* errors *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
317 |
|
2979 | 318 |
fun unif_failed msg = |
14828 | 319 |
"Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n"; |
2979 | 320 |
|
321 |
fun prep_output bs ts Ts = |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
322 |
let |
20161 | 323 |
val (Ts_bTs', ts') = typs_terms_of Name.context PTFree "??" (Ts @ map snd bs, ts); |
19012 | 324 |
val (Ts', Ts'') = chop (length Ts) Ts_bTs'; |
13629 | 325 |
val xs = map Free (map fst bs ~~ Ts''); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
326 |
val ts'' = map (fn t => subst_bounds (xs, t)) ts'; |
2979 | 327 |
in (ts'', Ts') end; |
328 |
||
329 |
fun err_loose i = |
|
3784 | 330 |
raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], []); |
2979 | 331 |
|
3510 | 332 |
fun err_appl msg bs t T u U = |
2979 | 333 |
let |
3510 | 334 |
val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U]; |
335 |
val why = |
|
336 |
(case T' of |
|
14828 | 337 |
Type ("fun", _) => "Incompatible operand type" |
338 |
| _ => "Operator not of function type"); |
|
339 |
val text = unif_failed msg ^ cat_lines (appl_error pp why t' T' u' U'); |
|
3784 | 340 |
in raise TYPE (text, [T', U'], [t', u']) end; |
2979 | 341 |
|
342 |
fun err_constraint msg bs t T U = |
|
343 |
let |
|
344 |
val ([t'], [T', U']) = prep_output bs [t] [T, U]; |
|
345 |
val text = cat_lines |
|
346 |
[unif_failed msg, |
|
5635 | 347 |
"Cannot meet type constraint:", "", |
14828 | 348 |
Pretty.string_of (Pretty.block |
349 |
[Pretty.str "Term:", Pretty.brk 2, Pretty.term pp t', |
|
350 |
Pretty.str " ::", Pretty.brk 1, Pretty.typ pp T']), |
|
351 |
Pretty.string_of (Pretty.block |
|
352 |
[Pretty.str "Type:", Pretty.brk 2, Pretty.typ pp U']), ""]; |
|
3784 | 353 |
in raise TYPE (text, [T', U'], [t']) end; |
2979 | 354 |
|
355 |
||
356 |
(* main *) |
|
357 |
||
19465 | 358 |
val unif = unify pp tsig; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
359 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
360 |
fun inf _ (PConst (_, T)) = T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
361 |
| inf _ (PFree (_, T)) = T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
362 |
| inf _ (PVar (_, T)) = T |
15570 | 363 |
| inf bs (PBound i) = snd (List.nth (bs, i) handle Subscript => err_loose i) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
364 |
| inf bs (PAbs (x, T, t)) = PType ("fun", [T, inf ((x, T) :: bs) t]) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
365 |
| inf bs (PAppl (t, u)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
366 |
let |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
367 |
val T = inf bs t; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
368 |
val U = inf bs u; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
369 |
val V = mk_param []; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
370 |
val U_to_V = PType ("fun", [U, V]); |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
371 |
val _ = unif (U_to_V, T) handle NO_UNIFIER msg => err_appl msg bs t T u U; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
372 |
in V end |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
373 |
| inf bs (Constraint (t, U)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
374 |
let val T = inf bs t in |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
375 |
unif (T, U) handle NO_UNIFIER msg => err_constraint msg bs t T U; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
376 |
T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
377 |
end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
378 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
379 |
in inf [] end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
380 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
381 |
|
22698 | 382 |
(* infer_types *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
383 |
|
24275 | 384 |
fun infer_types pp tsig mode const_type var_type used freeze args = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
385 |
let |
22698 | 386 |
(*certify types*) |
24275 | 387 |
fun certT T = Type.cert_typ_mode mode tsig T; |
22698 | 388 |
val (raw_ts, raw_Ts) = split_list args; |
389 |
val ts = map (Term.map_types certT) raw_ts; |
|
390 |
val Ts = map certT raw_Ts; |
|
391 |
||
392 |
(*constrain vars*) |
|
393 |
val get_type = the_default dummyT o var_type; |
|
394 |
val constrain_vars = Term.map_aterms |
|
395 |
(fn Free (x, T) => constrain (Free (x, get_type (x, ~1))) T |
|
396 |
| Var (xi, T) => constrain (Var (xi, get_type xi)) T |
|
397 |
| t => t); |
|
398 |
||
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
399 |
(*convert to preterms/typs*) |
20735 | 400 |
val (Ts', Tps) = fold_map (pretyp_of (K true)) Ts Vartab.empty; |
22698 | 401 |
val (ts', (vps, ps)) = |
402 |
fold_map (preterm_of const_type is_param_default o constrain_vars) ts (Vartab.empty, Tps); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
403 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
404 |
(*run type inference*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
405 |
val tTs' = ListPair.map Constraint (ts', Ts'); |
19465 | 406 |
val _ = List.app (fn t => (infer pp tsig t; ())) tTs'; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
407 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
408 |
(*convert back to terms/typs*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
409 |
val mk_var = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
410 |
if freeze then PTFree |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
411 |
else (fn (x, S) => PTVar ((x, 0), S)); |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
412 |
val (final_Ts, final_ts) = typs_terms_of used mk_var "" (Ts', ts'); |
22771 | 413 |
|
414 |
(*collect result unifier*) |
|
415 |
val redundant = fn (xi, TVar (yi, _)) => xi = yi | _ => false; |
|
416 |
val env = filter_out redundant (map (apsnd simple_typ_of) (Vartab.dest Tps)); |
|
417 |
||
418 |
in (final_ts ~~ final_Ts, env) end; |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
419 |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
420 |
end; |