Iter 16b.3: post-typecheck lift for LetRec with Let-bound captures
Adds path-2 from the 16b.2 planning entry. Desugar now defers
LetRecs whose captures include Term::Let-bound names; a new
ailang-check::lift_letrecs pass runs after typecheck and uses the
elaborated env to resolve capture types. ailang-check learns a real
Term::LetRec typing rule in synth and verify_tail_positions.
- desugar: Term::LetRec arm gains a defer-arm; helpers promoted to
pub for reuse by the lift pass; find_non_callee_use moved before
classification.
- ailang-check::synth/verify_tail_positions: real LetRec rules
(effect-subset, locals install, recursive name in body+in_term).
- ailang-check::lift.rs (new, 720 LOC): post-typecheck lift with
post-order traversal, env-walk for capture-type resolution,
fast-path skip when no LetRec is present.
- ail::main.rs: build path now does load → check → desugar →
lift_letrecs → codegen.
- examples/local_rec_let_capture.{ailx,ail.json}: new fixture
capturing a let-bound `threshold` in a recursive helper.
- e2e + check unit tests: 106 → 110 (+4).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"defs":[{"body":{"body":{"body":{"cond":{"args":[{"name":"i","t":"var"},{"name":"n","t":"var"}],"fn":{"name":">","t":"var"},"t":"app"},"else":{"cond":{"args":[{"name":"i","t":"var"},{"name":"threshold","t":"var"}],"fn":{"name":"<","t":"var"},"t":"app"},"else":{"args":[{"args":[{"name":"i","t":"var"},{"lit":{"kind":"int","value":1},"t":"lit"}],"fn":{"name":"+","t":"var"},"t":"app"}],"fn":{"name":"loop","t":"var"},"t":"app"},"t":"if","then":{"args":[{"lit":{"kind":"int","value":1},"t":"lit"},{"args":[{"args":[{"name":"i","t":"var"},{"lit":{"kind":"int","value":1},"t":"lit"}],"fn":{"name":"+","t":"var"},"t":"app"}],"fn":{"name":"loop","t":"var"},"t":"app"}],"fn":{"name":"+","t":"var"},"t":"app"}},"t":"if","then":{"lit":{"kind":"int","value":0},"t":"lit"}},"in":{"args":[{"lit":{"kind":"int","value":1},"t":"lit"}],"fn":{"name":"loop","t":"var"},"t":"app"},"name":"loop","params":["i"],"t":"letrec","type":{"effects":[],"k":"fn","params":[{"k":"con","name":"Int"}],"ret":{"k":"con","name":"Int"}}},"name":"threshold","t":"let","value":{"args":[{"lit":{"kind":"int","value":5},"t":"lit"},{"lit":{"kind":"int","value":5},"t":"lit"}],"fn":{"name":"+","t":"var"},"t":"app"}},"doc":"Count i in 1..=n with i < threshold, where threshold = 5+5.","kind":"fn","name":"count_below","params":["n"],"type":{"effects":[],"k":"fn","params":[{"k":"con","name":"Int"}],"ret":{"k":"con","name":"Int"}}},{"body":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":0},"t":"lit"}],"fn":{"name":"count_below","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":5},"t":"lit"}],"fn":{"name":"count_below","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"args":[{"args":[{"lit":{"kind":"int","value":15},"t":"lit"}],"fn":{"name":"count_below","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"t":"seq"},"t":"seq"},"doc":"Drive count_below at 0, 5, 15. Expected (per line): 0, 5, 9.","kind":"fn","name":"main","params":[],"type":{"effects":["IO"],"k":"fn","params":[],"ret":{"k":"con","name":"Unit"}}}],"imports":[],"name":"local_rec_let_capture","schema":"ailang/v0"}
|
||||
@@ -0,0 +1,48 @@
|
||||
; Iter 16b.3 — LetRec captures of `Term::Let`-bound names.
|
||||
; `count_below(n)` returns how many integers in 1..=n are strictly
|
||||
; less than a `threshold` computed locally from `n`. The recursive
|
||||
; helper `loop` captures the let-bound name `threshold` from the
|
||||
; enclosing scope; its type is only known after typecheck (the
|
||||
; let-value is `(app + 5 5)` — an `Int` expression, but the desugar
|
||||
; pass cannot resolve that without inference).
|
||||
;
|
||||
; The 16b.2 desugar pass cannot lift this LetRec (capture is Let-bound,
|
||||
; not fn-param). Instead the desugar pass leaves the LetRec in place;
|
||||
; the post-typecheck `lift_letrecs` pass in `ailang-check` resolves
|
||||
; `threshold`'s type as `Int` from the typechecker's env and produces
|
||||
; a synthetic top-level fn `loop$lr_0(i: Int, n: Int, threshold: Int)
|
||||
; -> Int`, rewriting every `(app loop ARGS)` to `(app loop$lr_0 ARGS n
|
||||
; threshold)`.
|
||||
;
|
||||
; Expected stdout (one per line):
|
||||
; count_below(0) = 0 (empty range; loop's first call has i > n)
|
||||
; count_below(5) = 5 (1..=5; threshold = 10; all 5 are < 10)
|
||||
; count_below(15) = 9 (1..=15; threshold = 10; 1..=9 are < 10)
|
||||
|
||||
(module local_rec_let_capture
|
||||
|
||||
(fn count_below
|
||||
(doc "Count i in 1..=n with i < threshold, where threshold = 5+5.")
|
||||
(type (fn-type (params (con Int)) (ret (con Int))))
|
||||
(params n)
|
||||
(body
|
||||
(let threshold (app + 5 5)
|
||||
(let-rec loop
|
||||
(params i)
|
||||
(type (fn-type (params (con Int)) (ret (con Int))))
|
||||
(body
|
||||
(if (app > i n)
|
||||
0
|
||||
(if (app < i threshold)
|
||||
(app + 1 (app loop (app + i 1)))
|
||||
(app loop (app + i 1)))))
|
||||
(in (app loop 1))))))
|
||||
|
||||
(fn main
|
||||
(doc "Drive count_below at 0, 5, 15. Expected (per line): 0, 5, 9.")
|
||||
(type (fn-type (params) (ret (con Unit)) (effects IO)))
|
||||
(params)
|
||||
(body
|
||||
(seq (do io/print_int (app count_below 0))
|
||||
(seq (do io/print_int (app count_below 5))
|
||||
(do io/print_int (app count_below 15)))))))
|
||||
Reference in New Issue
Block a user