; Iter 14f stress fixture for the Boehm conservative GC integration.
; Builds a List Int of length 50 by recursive Cons construction,
; sums it, prints the sum (1275 = 50*51/2).
;
; Bool branching uses the canonical `if` form (Decision 7, restored
; in Iter 14g): `(if (== n 0) Nil (Cons n (build (- n 1))))`.

(module gc_stress

  (data List (vars a)
    (doc "Polymorphic singly-linked list (re-declared locally).")
    (ctor Nil)
    (ctor Cons a (con List a)))

  (fn build
    (doc "Build [n, n-1, ..., 1] :: List Int via Cons recursion.")
    (type
      (fn-type
        (params (con Int))
        (ret (con List (con Int)))))
    (params n)
    (body
      (if (app == n 0)
        (term-ctor List Nil)
        (term-ctor List Cons
          n
          (app build (app - n 1))))))

  (fn sum_list
    (doc "Recursively sum a List Int.")
    (type
      (fn-type
        (params (con List (con Int)))
        (ret (con Int))))
    (params xs)
    (body
      (match xs
        (case (pat-ctor Nil) 0)
        (case (pat-ctor Cons h t)
          (app + h (app sum_list t))))))

  (fn main
    (doc "Build [50..1], sum, print 1275.")
    (type (fn-type (params) (ret (con Unit)) (effects IO)))
    (params)
    (body
      (do io/print_int (app sum_list (app build 50))))))
