e5f9828a04
Inner LetRec capturing outer LetRec's params now lifts cleanly via
the existing post-order traversal — outer's params are KnownType
in inner's outer-scope, so 16b.2's fast-path handles them. Inner
LetRec capturing outer's NAME remains rejected (closure-of-self
in body, deferred separately).
Plus hardening: lift.rs's deferred path tracks the outer LetRec
name in a parallel `enclosing_letrec_names` set so the symmetric
inner-captures-outer-name rejection fires there too — without it
a silent lift produced a runtime arity mismatch.
- examples/nested_let_rec.{ailx,ail.json}: outer counts down via
inner that captures the outer's threshold param.
- desugar.rs: clarified panic message for outer-name capture.
- lift.rs: enclosing_letrec_names tracking.
- e2e + desugar tests: 120 → 122 (+2).
End-of-16b series: feature-complete except for closure-of-self
in body and closure-with-polymorphism, both queued separately.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
54 lines
1.8 KiB
Plaintext
54 lines
1.8 KiB
Plaintext
; Iter 16b.7 — nested LetRec mutual capture (params, not name).
|
|
;
|
|
; `nested_sum(n)` returns the sum 1 + 2 + ... + n by combining two
|
|
; recursive helpers. The OUTER let-rec `outer(i)` iterates i over
|
|
; 1..=n; for each i it calls the INNER let-rec `inner(j)` to count
|
|
; how many integers are in 1..=i (= i). The total is then
|
|
; 1 + 2 + ... + n = n*(n+1)/2.
|
|
;
|
|
; The shape exercises:
|
|
; - outer captures `n` from `nested_sum`'s fn-params (16b.2 case).
|
|
; - inner captures `i` from outer LetRec's PARAMS (16b.7 case
|
|
; under iter — outer LetRec's params are KnownType in body
|
|
; scope, so the inner lift sees a resolvable capture type).
|
|
; - inner does NOT capture outer's NAME (still rejected in 16b.7).
|
|
;
|
|
; Expected stdout (one per line):
|
|
; nested_sum(1) = 1
|
|
; nested_sum(3) = 6
|
|
; nested_sum(5) = 15
|
|
|
|
(module nested_let_rec
|
|
|
|
(fn nested_sum
|
|
(doc "Sum 1 + 2 + ... + n by nested LetRec helpers; inner captures outer's param i.")
|
|
(type (fn-type (params (con Int)) (ret (con Int))))
|
|
(params n)
|
|
(body
|
|
(let-rec outer
|
|
(params i)
|
|
(type (fn-type (params (con Int)) (ret (con Int))))
|
|
(body
|
|
(if (app > i n)
|
|
0
|
|
(app +
|
|
(let-rec inner
|
|
(params j)
|
|
(type (fn-type (params (con Int)) (ret (con Int))))
|
|
(body
|
|
(if (app > j i)
|
|
0
|
|
(app + 1 (app inner (app + j 1)))))
|
|
(in (app inner 1)))
|
|
(app outer (app + i 1)))))
|
|
(in (app outer 1)))))
|
|
|
|
(fn main
|
|
(doc "Drive nested_sum at 1, 3, 5. Expected (per line): 1, 6, 15.")
|
|
(type (fn-type (params) (ret (con Unit)) (effects IO)))
|
|
(params)
|
|
(body
|
|
(seq (do io/print_int (app nested_sum 1))
|
|
(seq (do io/print_int (app nested_sum 3))
|
|
(do io/print_int (app nested_sum 5)))))))
|