; Fieldtest cut55-2 — decision (b): consume/transform a heap value.
;
; bump rebuilds the list with each element +1. It moves the tail `t` into
; a recursive call and packs the new head into a fresh Cons. The input
; allocation is consumed (reused conceptually). This is `own` territory:
; the author writes `(own (con List))` for xs without hesitation, since
; the value is transformed and not merely read.
;
; Expected: ail check -> ok. ail run -> prints 11, 21, 31 (each +1).

(module cut55_2_consume_map

  (data List
    (ctor Nil)
    (ctor Cons (con Int) (con List)))

  (fn bump
    (doc "Own xs; return a new list with every element incremented.")
    (type
      (fn-type
        (params (own (con List)))
        (ret (own (con List)))))
    (params xs)
    (body
      (match xs
        (case (pat-ctor Nil) (term-ctor List Nil))
        (case (pat-ctor Cons h t)
          (term-ctor List Cons (app + h 1) (app bump t))))))

  (fn print_all
    (doc "Own xs; print each element on its own line, consuming the list.")
    (type (fn-type (params (own (con List))) (ret (own (con Unit))) (effects IO)))
    (params xs)
    (body
      (match xs
        (case (pat-ctor Nil) (lit-unit))
        (case (pat-ctor Cons h t)
          (seq
            (seq (app print h) (do io/print_str "\n"))
            (app print_all t))))))

  (fn main
    (type (fn-type (params) (ret (own (con Unit))) (effects IO)))
    (params)
    (body
      (let xs
        (term-ctor List Cons 10
          (term-ctor List Cons 20
            (term-ctor List Cons 30
              (term-ctor List Nil))))
        (app print_all (app bump xs))))))
