(module rc_drop_iterative_long_list
  (data IntList
    (ctor INil)
    (ctor ICons (con Int) (con IntList))
    (drop-iterative))
  (fn cons_n_acc
    (doc "Tail-recursive list builder. acc-prepended.")
    (type (fn-type (params (own (con Int)) (own (con IntList))) (ret (own (con IntList)))))
    (params n acc)
    (body (if (app eq n 0) acc (tail-app cons_n_acc (app - n 1) (term-ctor IntList ICons n acc)))))
  (fn cons_n
    (doc "Build [1, 2, ..., n] :: IntList. Order is reverse of build but immaterial for head/sum.")
    (type (fn-type (params (own (con Int))) (ret (own (con IntList)))))
    (params n)
    (body (app cons_n_acc n (term-ctor IntList INil))))
  (fn head_or_zero
    (doc "Take ownership of an IntList; return its head if ICons, else 0. The (own ...) signature signals 18d.4 to emit an Own-param drop at fn return — which under the (drop-iterative) annotation is the iterative-worklist body, freeing the entire chain via the heap-allocated worklist instead of recursive cascade.")
    (suppress (code "over-strict-mode") (because "Iter 18e test: forces (drop-iterative) cascade via Own-param drop at fn return"))
    (type (fn-type (params (own (con IntList))) (ret (own (con Int)))))
    (params xs)
    (body (match xs
      (case (pat-ctor INil) 0)
      (case (pat-ctor ICons h t) h))))
  (fn main
    (type (fn-type (params) (ret (own (con Unit))) (effects IO)))
    (params)
    (body (app print (app head_or_zero (app cons_n 1000000))))))
