213 in hnorm t handle SAME => t end; |
213 in hnorm t handle SAME => t end; |
214 |
214 |
215 |
215 |
216 (*Eta-contract a term (fully)*) |
216 (*Eta-contract a term (fully)*) |
217 |
217 |
|
218 local |
|
219 |
|
220 fun decr lev (Bound i) = if i >= lev then Bound (i - 1) else raise SAME |
|
221 | decr lev (Abs (a, T, body)) = Abs (a, T, decr (lev + 1) body) |
|
222 | decr lev (t $ u) = (decr lev t $ decrh lev u handle SAME => t $ decr lev u) |
|
223 | decr _ _ = raise SAME |
|
224 and decrh lev t = (decr lev t handle SAME => t); |
|
225 |
|
226 fun eta (Abs (a, T, body)) = |
|
227 ((case eta body of |
|
228 body' as (f $ Bound 0) => |
|
229 if loose_bvar1 (f, 0) then Abs (a, T, body') |
|
230 else decrh 0 f |
|
231 | body' => Abs (a, T, body')) handle SAME => |
|
232 (case body of |
|
233 f $ Bound 0 => |
|
234 if loose_bvar1 (f, 0) then raise SAME |
|
235 else decrh 0 f |
|
236 | _ => raise SAME)) |
|
237 | eta (t $ u) = (eta t $ etah u handle SAME => t $ eta u) |
|
238 | eta _ = raise SAME |
|
239 and etah t = (eta t handle SAME => t); |
|
240 |
|
241 fun has_abs (Abs _) = true |
|
242 | has_abs (t $ u) = has_abs t orelse has_abs u |
|
243 | has_abs _ = false; |
|
244 |
|
245 in |
|
246 |
218 fun eta_contract t = |
247 fun eta_contract t = |
219 let |
248 if has_abs t then etah t else t; |
220 fun decr lev (Bound i) = if i >= lev then Bound (i - 1) else raise SAME |
|
221 | decr lev (Abs (a, T, body)) = Abs (a, T, decr (lev + 1) body) |
|
222 | decr lev (t $ u) = (decr lev t $ decrh lev u handle SAME => t $ decr lev u) |
|
223 | decr _ _ = raise SAME |
|
224 and decrh lev t = (decr lev t handle SAME => t); |
|
225 |
|
226 fun eta (Abs (a, T, body)) = |
|
227 ((case eta body of |
|
228 body' as (f $ Bound 0) => |
|
229 if loose_bvar1 (f, 0) then Abs (a, T, body') |
|
230 else decrh 0 f |
|
231 | body' => Abs (a, T, body')) handle SAME => |
|
232 (case body of |
|
233 f $ Bound 0 => |
|
234 if loose_bvar1 (f, 0) then raise SAME |
|
235 else decrh 0 f |
|
236 | _ => raise SAME)) |
|
237 | eta (t $ u) = (eta t $ etah u handle SAME => t $ eta u) |
|
238 | eta _ = raise SAME |
|
239 and etah t = (eta t handle SAME => t); |
|
240 in etah t end; |
|
241 |
249 |
242 val beta_eta_contract = eta_contract o beta_norm; |
250 val beta_eta_contract = eta_contract o beta_norm; |
|
251 |
|
252 end; |
243 |
253 |
244 |
254 |
245 (*finds type of term without checking that combinations are consistent |
255 (*finds type of term without checking that combinations are consistent |
246 Ts holds types of bound variables*) |
256 Ts holds types of bound variables*) |
247 fun fastype (Envir {iTs, ...}) = |
257 fun fastype (Envir {iTs, ...}) = |