252 fun replace_lambda Us Ts t (cx as (defs, ctxt)) = |
252 fun replace_lambda Us Ts t (cx as (defs, ctxt)) = |
253 let |
253 let |
254 val T = Term.fastype_of1 (Us @ Ts, t) |
254 val T = Term.fastype_of1 (Us @ Ts, t) |
255 val lev = length Us |
255 val lev = length Us |
256 val bs = sort int_ord (Term.add_loose_bnos (t, lev, [])) |
256 val bs = sort int_ord (Term.add_loose_bnos (t, lev, [])) |
257 val bss = map_index (fn (i, j) => (j, Integer.add lev i)) bs |
257 val bss = map_index (fn (i, j) => (j + lev, i + lev)) bs |
258 val norm = perhaps (AList.lookup (op =) bss) |
258 val norm = perhaps (AList.lookup (op =) bss) |
259 val t' = Term.map_aterms (fn Bound i => Bound (norm i) | t => t) t |
259 val t' = Term.map_aterms (fn Bound i => Bound (norm i) | t => t) t |
260 val Ts' = map (nth Ts) bs |
260 val Ts' = map (nth Ts) bs |
261 |
261 |
262 fun mk_abs U u = Abs (Name.uu, U, u) |
262 fun mk_abs U u = Abs (Name.uu, U, u) |
263 val abs_rhs = fold mk_abs Ts' (fold mk_abs Us t') |
263 val abs_rhs = fold mk_abs Ts' (fold mk_abs Us t') |
|
264 |
|
265 fun app f = Term.list_comb (f, map Bound bs) |
264 in |
266 in |
265 (case Termtab.lookup defs abs_rhs of |
267 (case Termtab.lookup defs abs_rhs of |
266 SOME (f, _) => (Term.list_comb (f, map Bound bs), cx) |
268 SOME (f, _) => (app f, cx) |
267 | NONE => |
269 | NONE => |
268 let |
270 let |
269 val (n, ctxt') = |
271 val (n, ctxt') = |
270 yield_singleton Variable.variant_fixes Name.uu ctxt |
272 yield_singleton Variable.variant_fixes Name.uu ctxt |
271 val f = Free (n, rev Ts' ---> (rev Us ---> T)) |
273 val f = Free (n, rev Ts' ---> (rev Us ---> T)) |
273 val lhs = |
275 val lhs = |
274 f |
276 f |
275 |> fold_rev (mk_bapp o snd) bss |
277 |> fold_rev (mk_bapp o snd) bss |
276 |> fold_rev mk_bapp (0 upto (length Us - 1)) |
278 |> fold_rev mk_bapp (0 upto (length Us - 1)) |
277 val def = mk_def (Us @ Ts') T lhs t' |
279 val def = mk_def (Us @ Ts') T lhs t' |
278 in (f, (Termtab.update (abs_rhs, (f, def)) defs, ctxt')) end) |
280 in (app f, (Termtab.update (abs_rhs, (f, def)) defs, ctxt')) end) |
279 end |
281 end |
280 |
282 |
281 fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t |
283 fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t |
282 | dest_abs Ts t = (Ts, t) |
284 | dest_abs Ts t = (Ts, t) |
283 |
285 |