; Fieldtest — canonical-type-names, axis 4: canonical happy path.
;
; Reduce a list of Int to a 'sign profile': for each element, print
; -1, 0, or +1 according to its compare-to-zero result. Exercises:
;   - cross-module reference to prelude.Ordering at use site
;   - pattern match against prelude.Ordering's three constructors LT/EQ/GT
;   - returning a freshly chosen Int from each arm
;   - simple recursion over a local List ADT
;
; Expected stdout (one per line):
;   -1   ; signum -3
;    0   ; signum  0
;    1   ; signum  7
;   -1   ; signum -1
;    1   ; signum  2

(module ct_1_ordering_signum

  (data IntList
    (doc "Local Int-list ADT; nothing fancy.")
    (ctor INil)
    (ctor ICons (con Int) (con IntList)))

  (fn signum
    (doc "Map an Int to -1/0/+1 via prelude.compare. Returns the sign as an Int.")
    (type (fn-type (params (own (con Int))) (ret (own (con Int)))))
    (params n)
    (body
      (match (app compare n 0)
        (case (pat-ctor LT) (app - 0 1))
        (case (pat-ctor EQ) 0)
        (case (pat-ctor GT) 1))))

  (fn drain
    (doc "Print signum of every element of xs, in list order.")
    (type (fn-type (params (own (con IntList))) (ret (own (con Unit))) (effects IO)))
    (params xs)
    (body
      (match xs
        (case (pat-ctor INil) (lit-unit))
        (case (pat-ctor ICons h t)
          (seq (app print (app signum h))
               (tail-app drain t))))))

  (fn main
    (type (fn-type (params) (ret (own (con Unit))) (effects IO)))
    (params)
    (body
      (app drain
        (term-ctor IntList ICons (app - 0 3)
        (term-ctor IntList ICons 0
        (term-ctor IntList ICons 7
        (term-ctor IntList ICons (app - 0 1)
        (term-ctor IntList ICons 2
        (term-ctor IntList INil))))))))))
