; Axis: running numeric accumulator over a bounded range.
;
; Imperative instinct (the shape `mut` would invite):
;   s = 0
;   for i in 1..=n: s = s + i*i
;   return s
;
; Surviving-surface shape: a `loop` with two binders (i counter,
; acc accumulator) and a `recur` in the tail of the else branch.
; sum_sq(10) = 1+4+9+16+25+36+49+64+81+100 = 385.
; Expected stdout: 385

(module remove-mut_1_sum_of_squares

  (fn sum_sq
    (doc "Sum of i*i for i in 1..=n via loop/recur. Two binders: i (counter), acc (running total).")
    (type (fn-type (params (own (con Int))) (ret (own (con Int)))))
    (params n)
    (body
      (loop (i (con Int) 1) (acc (con Int) 0)
        (if (app gt i n)
            acc
            (recur (app + i 1) (app + acc (app * i i)))))))

  (fn main
    (type (fn-type (params) (ret (own (con Unit))) (effects IO)))
    (params)
    (body
      (app print (app sum_sq 10)))))
