Files
AILang/examples/local_rec_as_value.ailx
T
Brummel ca507c9f52 Iter 16b.5: LetRec name as value (in_term only) via eta-Lam wrapper
Drops the direct-call-only restriction on the LetRec name when it
appears in `in_term`. No new ABI: the lift wraps the in-term in
`(let f (lam P (app f$lr_N P CAPTURES)) in_term')` so codegen's
existing Iter-8b closure-pair machinery handles it. Body-position
name-as-value still rejected (deferred — eta-of-self is harder).

- desugar.rs / lift.rs: split find_non_callee_use into body
  (panic) and in_term (wrap).
- examples/local_rec_as_value.{ailx,ail.json}: no-capture path
  (factorial passed to apply5 → 120).
- examples/local_rec_as_value_capture.{ailx,ail.json}: capture
  path; lifted fn has captures, eta-Lam picks them up via 8b.
- e2e + desugar tests: 113 → 116 (+3).

Codegen Lam machinery handled the wrapped form on the first try.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 22:27:21 +02:00

34 lines
1.3 KiB
Plaintext

; Iter 16b.5 — LetRec name as value (in_term, no capture).
; A recursive `factorial` is bound by `(let-rec ...)` inside `main`,
; then in the in-clause it is passed as a VALUE to a higher-order
; combinator `apply5` (which expects `Fn(Int) -> Int`). Without
; 16b.5, the desugar pass would panic with the "name-as-value not
; supported" message. With 16b.5 it wraps the in-term in
; `(let factorial (lam ...) ...)` whose lam eta-expands the lifted
; top-level fn — so `factorial` reaches `apply5` as a closure-pair
; value just like any other fn-value (Iter 7/8b ABI).
;
; Expected stdout: 120 (= apply5 factorial = factorial 5).
(module local_rec_as_value
(fn apply5
(doc "Higher-order: applies its fn argument to 5.")
(type (fn-type (params (fn-type (params (con Int)) (ret (con Int)))) (ret (con Int))))
(params f)
(body (app f 5)))
(fn main
(doc "Iter 16b.5: pass a let-rec'd factorial as a value to apply5. Expected stdout: 120.")
(type (fn-type (params) (ret (con Unit)) (effects IO)))
(params)
(body
(let-rec factorial
(params n)
(type (fn-type (params (con Int)) (ret (con Int))))
(body
(if (app <= n 1)
1
(app * n (app factorial (app - n 1)))))
(in (do io/print_int (app apply5 factorial)))))))