; 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)))))))
