; NEGATIVE FIXTURE. The LLM-natural mistake: inside the loop body
; the author builds a closure that closes over the loop binder
; `acc` (a `\delta. acc + delta` adder) and applies it. Loop
; binders are alloca-resident and may not be captured.
; Expected: `ail check` exits 1 with code loop-binder-captured-by-lambda.
(module loop_recur_3e_binder_captured
  (fn apply_int
    (type (fn-type (params (own (fn-type (params (own (con Int))) (ret (own (con Int))))) (own (con Int))) (ret (own (con Int)))))
    (params f x)
    (body (app f x)))
  (fn sum_to
    (type (fn-type (params (own (con Int))) (ret (own (con Int)))))
    (params n)
    (body
      (loop (acc (con Int) 0) (i (con Int) 1)
        (if (app gt i n)
            acc
            (recur (app apply_int (lam (params (typed d (con Int))) (ret (con Int)) (body (app + acc d))) i)
                   (app + i 1))))))
  (fn main
    (type (fn-type (params) (ret (own (con Unit))) (effects IO)))
    (params)
    (body (app print (app sum_to 10)))))
