author | wenzelm |
Thu, 16 Jul 2009 17:03:11 +0200 | |
changeset 32016 | 597b3b69b8d8 |
parent 31980 | c7c1d545007e |
child 32020 | 9abf5d66606e |
permissions | -rw-r--r-- |
20513 | 1 |
(* Title: Pure/term_subst.ML |
2 |
Author: Makarius |
|
3 |
||
32016 | 4 |
Efficient type/term substitution. |
20513 | 5 |
*) |
6 |
||
7 |
signature TERM_SUBST = |
|
8 |
sig |
|
32016 | 9 |
val map_atypsT_same: typ Same.operation -> typ Same.operation |
10 |
val map_types_same: typ Same.operation -> term Same.operation |
|
11 |
val map_aterms_same: term Same.operation -> term Same.operation |
|
31980 | 12 |
val map_atypsT_option: (typ -> typ option) -> typ -> typ option |
13 |
val map_atyps_option: (typ -> typ option) -> term -> term option |
|
14 |
val map_types_option: (typ -> typ option) -> term -> term option |
|
15 |
val map_aterms_option: (term -> term option) -> term -> term option |
|
20513 | 16 |
val generalize: string list * string list -> int -> term -> term |
17 |
val generalizeT: string list -> int -> typ -> typ |
|
18 |
val generalize_option: string list * string list -> int -> term -> term option |
|
19 |
val generalizeT_option: string list -> int -> typ -> typ option |
|
20 |
val instantiateT_maxidx: ((indexname * sort) * (typ * int)) list -> typ -> int -> typ * int |
|
21 |
val instantiate_maxidx: |
|
22 |
((indexname * sort) * (typ * int)) list * ((indexname * typ) * (term * int)) list -> |
|
23 |
term -> int -> term * int |
|
24 |
val instantiate: ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> |
|
25 |
term -> term |
|
26 |
val instantiateT: ((indexname * sort) * typ) list -> typ -> typ |
|
27 |
val instantiate_option: ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> |
|
28 |
term -> term option |
|
29 |
val instantiateT_option: ((indexname * sort) * typ) list -> typ -> typ option |
|
30 |
val zero_var_indexes: term -> term |
|
21607 | 31 |
val zero_var_indexes_inst: term list -> |
20513 | 32 |
((indexname * sort) * typ) list * ((indexname * typ) * term) list |
33 |
end; |
|
34 |
||
31977 | 35 |
structure Term_Subst: TERM_SUBST = |
20513 | 36 |
struct |
37 |
||
31980 | 38 |
(* generic mapping *) |
39 |
||
40 |
fun map_atypsT_same f = |
|
41 |
let |
|
42 |
fun typ (Type (a, Ts)) = Type (a, typs Ts) |
|
43 |
| typ T = f T |
|
32016 | 44 |
and typs (T :: Ts) = (typ T :: Same.commit typs Ts handle Same.SAME => T :: typs Ts) |
45 |
| typs [] = raise Same.SAME; |
|
31980 | 46 |
in typ end; |
47 |
||
48 |
fun map_types_same f = |
|
49 |
let |
|
50 |
fun term (Const (a, T)) = Const (a, f T) |
|
51 |
| term (Free (a, T)) = Free (a, f T) |
|
52 |
| term (Var (v, T)) = Var (v, f T) |
|
32016 | 53 |
| term (Bound _) = raise Same.SAME |
31980 | 54 |
| term (Abs (x, T, t)) = |
32016 | 55 |
(Abs (x, f T, Same.commit term t) |
56 |
handle Same.SAME => Abs (x, T, term t)) |
|
57 |
| term (t $ u) = (term t $ Same.commit term u handle Same.SAME => t $ term u); |
|
31980 | 58 |
in term end; |
59 |
||
60 |
fun map_aterms_same f = |
|
61 |
let |
|
62 |
fun term (Abs (x, T, t)) = Abs (x, T, term t) |
|
32016 | 63 |
| term (t $ u) = (term t $ Same.commit term u handle Same.SAME => t $ term u) |
31980 | 64 |
| term a = f a; |
65 |
in term end; |
|
66 |
||
32016 | 67 |
val map_atypsT_option = Same.capture o map_atypsT_same o Same.function; |
68 |
val map_atyps_option = Same.capture o map_types_same o map_atypsT_same o Same.function; |
|
69 |
val map_types_option = Same.capture o map_types_same o Same.function; |
|
70 |
val map_aterms_option = Same.capture o map_aterms_same o Same.function; |
|
31980 | 71 |
|
72 |
||
20513 | 73 |
(* generalization of fixed variables *) |
74 |
||
75 |
local |
|
76 |
||
32016 | 77 |
fun generalizeT_same [] _ _ = raise Same.SAME |
20513 | 78 |
| generalizeT_same tfrees idx ty = |
79 |
let |
|
80 |
fun gen_typ (Type (a, Ts)) = Type (a, gen_typs Ts) |
|
81 |
| gen_typ (TFree (a, S)) = |
|
82 |
if member (op =) tfrees a then TVar ((a, idx), S) |
|
32016 | 83 |
else raise Same.SAME |
84 |
| gen_typ _ = raise Same.SAME |
|
20513 | 85 |
and gen_typs (T :: Ts) = |
32016 | 86 |
(gen_typ T :: Same.commit gen_typs Ts |
87 |
handle Same.SAME => T :: gen_typs Ts) |
|
88 |
| gen_typs [] = raise Same.SAME; |
|
20513 | 89 |
in gen_typ ty end; |
90 |
||
32016 | 91 |
fun generalize_same ([], []) _ _ = raise Same.SAME |
20513 | 92 |
| generalize_same (tfrees, frees) idx tm = |
93 |
let |
|
94 |
val genT = generalizeT_same tfrees idx; |
|
95 |
fun gen (Free (x, T)) = |
|
96 |
if member (op =) frees x then |
|
32016 | 97 |
Var (Name.clean_index (x, idx), Same.commit genT T) |
20513 | 98 |
else Free (x, genT T) |
99 |
| gen (Var (xi, T)) = Var (xi, genT T) |
|
100 |
| gen (Const (c, T)) = Const (c, genT T) |
|
32016 | 101 |
| gen (Bound _) = raise Same.SAME |
20513 | 102 |
| gen (Abs (x, T, t)) = |
32016 | 103 |
(Abs (x, genT T, Same.commit gen t) |
104 |
handle Same.SAME => Abs (x, T, gen t)) |
|
105 |
| gen (t $ u) = (gen t $ Same.commit gen u handle Same.SAME => t $ gen u); |
|
20513 | 106 |
in gen tm end; |
107 |
||
108 |
in |
|
109 |
||
32016 | 110 |
fun generalize names i tm = generalize_same names i tm handle Same.SAME => tm; |
111 |
fun generalizeT names i ty = generalizeT_same names i ty handle Same.SAME => ty; |
|
20513 | 112 |
|
32016 | 113 |
fun generalize_option names i tm = SOME (generalize_same names i tm) handle Same.SAME => NONE; |
114 |
fun generalizeT_option names i ty = SOME (generalizeT_same names i ty) handle Same.SAME => NONE; |
|
20513 | 115 |
|
116 |
end; |
|
117 |
||
118 |
||
119 |
(* instantiation of schematic variables (types before terms) -- recomputes maxidx *) |
|
120 |
||
121 |
local |
|
122 |
||
123 |
fun no_index (x, y) = (x, (y, ~1)); |
|
124 |
fun no_indexes1 inst = map no_index inst; |
|
125 |
fun no_indexes2 (inst1, inst2) = (map no_index inst1, map no_index inst2); |
|
126 |
||
21184 | 127 |
fun instantiateT_same maxidx instT ty = |
20513 | 128 |
let |
21184 | 129 |
fun maxify i = if i > ! maxidx then maxidx := i else (); |
130 |
||
20513 | 131 |
fun subst_typ (Type (a, Ts)) = Type (a, subst_typs Ts) |
132 |
| subst_typ (TVar ((a, i), S)) = |
|
133 |
(case AList.lookup Term.eq_tvar instT ((a, i), S) of |
|
134 |
SOME (T, j) => (maxify j; T) |
|
32016 | 135 |
| NONE => (maxify i; raise Same.SAME)) |
136 |
| subst_typ _ = raise Same.SAME |
|
20513 | 137 |
and subst_typs (T :: Ts) = |
32016 | 138 |
(subst_typ T :: Same.commit subst_typs Ts |
139 |
handle Same.SAME => T :: subst_typs Ts) |
|
140 |
| subst_typs [] = raise Same.SAME; |
|
20513 | 141 |
in subst_typ ty end; |
142 |
||
21184 | 143 |
fun instantiate_same maxidx (instT, inst) tm = |
20513 | 144 |
let |
21184 | 145 |
fun maxify i = if i > ! maxidx then maxidx := i else (); |
146 |
||
147 |
val substT = instantiateT_same maxidx instT; |
|
20513 | 148 |
fun subst (Const (c, T)) = Const (c, substT T) |
149 |
| subst (Free (x, T)) = Free (x, substT T) |
|
150 |
| subst (Var ((x, i), T)) = |
|
32016 | 151 |
let val (T', same) = (substT T, false) handle Same.SAME => (T, true) in |
20513 | 152 |
(case AList.lookup Term.eq_var inst ((x, i), T') of |
153 |
SOME (t, j) => (maxify j; t) |
|
32016 | 154 |
| NONE => (maxify i; if same then raise Same.SAME else Var ((x, i), T'))) |
20513 | 155 |
end |
32016 | 156 |
| subst (Bound _) = raise Same.SAME |
20513 | 157 |
| subst (Abs (x, T, t)) = |
32016 | 158 |
(Abs (x, substT T, Same.commit subst t) |
159 |
handle Same.SAME => Abs (x, T, subst t)) |
|
160 |
| subst (t $ u) = (subst t $ Same.commit subst u handle Same.SAME => t $ subst u); |
|
20513 | 161 |
in subst tm end; |
162 |
||
163 |
in |
|
164 |
||
165 |
fun instantiateT_maxidx instT ty i = |
|
21184 | 166 |
let val maxidx = ref i |
32016 | 167 |
in (instantiateT_same maxidx instT ty handle Same.SAME => ty, ! maxidx) end; |
20513 | 168 |
|
169 |
fun instantiate_maxidx insts tm i = |
|
21184 | 170 |
let val maxidx = ref i |
32016 | 171 |
in (instantiate_same maxidx insts tm handle Same.SAME => tm, ! maxidx) end; |
20513 | 172 |
|
21315 | 173 |
fun instantiateT [] ty = ty |
174 |
| instantiateT instT ty = |
|
32016 | 175 |
(instantiateT_same (ref ~1) (no_indexes1 instT) ty handle Same.SAME => ty); |
21184 | 176 |
|
21315 | 177 |
fun instantiate ([], []) tm = tm |
178 |
| instantiate insts tm = |
|
32016 | 179 |
(instantiate_same (ref ~1) (no_indexes2 insts) tm handle Same.SAME => tm); |
20513 | 180 |
|
21315 | 181 |
fun instantiateT_option [] _ = NONE |
182 |
| instantiateT_option instT ty = |
|
32016 | 183 |
(SOME (instantiateT_same (ref ~1) (no_indexes1 instT) ty) handle Same.SAME => NONE); |
20513 | 184 |
|
21315 | 185 |
fun instantiate_option ([], []) _ = NONE |
186 |
| instantiate_option insts tm = |
|
32016 | 187 |
(SOME (instantiate_same (ref ~1) (no_indexes2 insts) tm) handle Same.SAME => NONE); |
20513 | 188 |
|
189 |
end; |
|
190 |
||
191 |
||
192 |
(* zero var indexes *) |
|
193 |
||
194 |
fun zero_var_inst vars = |
|
195 |
fold (fn v as ((x, i), X) => fn (inst, used) => |
|
196 |
let |
|
197 |
val ([x'], used') = Name.variants [if Name.is_bound x then "u" else x] used; |
|
198 |
in if x = x' andalso i = 0 then (inst, used') else ((v, ((x', 0), X)) :: inst, used') end) |
|
199 |
vars ([], Name.context) |> #1; |
|
200 |
||
21607 | 201 |
fun zero_var_indexes_inst ts = |
20513 | 202 |
let |
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
21607
diff
changeset
|
203 |
val tvars = sort TermOrd.tvar_ord (fold Term.add_tvars ts []); |
21607 | 204 |
val instT = map (apsnd TVar) (zero_var_inst tvars); |
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
21607
diff
changeset
|
205 |
val vars = sort TermOrd.var_ord (map (apsnd (instantiateT instT)) (fold Term.add_vars ts [])); |
21607 | 206 |
val inst = map (apsnd Var) (zero_var_inst vars); |
20513 | 207 |
in (instT, inst) end; |
208 |
||
21607 | 209 |
fun zero_var_indexes t = instantiate (zero_var_indexes_inst [t]) t; |
20513 | 210 |
|
211 |
end; |