; Fieldtest fixture — Axis 2: user-ADT Eq instance, hand-written.
;
; Rational numbers compared by cross-multiplication:
;   a/b == c/d  iff  a*d == b*c
; (assumes positive denominators; the constructor enforces it.)
;
; The interesting bit for the milestone is the *nested* equality call
; inside the user instance body: the inner `(app eq (app * a d) (app * b c))`
; dispatches to prelude.eq__Int (the primitive instance, intercept-
; lowered to `icmp eq i64`), while the outer `(app eq r1 r2)` from main
; dispatches to this module's own `eq__Rational`. End-to-end the chain
; is: user-instance method → prelude class method → primitive intercept.
;
; Expected stdout (one per line):
;   true     ; 1/2 == 2/4
;   false    ; 1/2 == 1/3
;   true     ; 3/4 == 6/8
;   false    ; 5/6 == 4/5

(module eqord_2_rational_eq

  (data Rational
    (doc "Numerator + (positive) denominator. No normalisation.")
    (ctor MkRational (con Int) (con Int)))

  (instance
    (class prelude.Eq)
    (type (con Rational))
    (doc "Cross-multiplication equality. Inner eq dispatches to prelude.eq__Int.")
    (method eq
      (body
        (lam
          (params (typed r1 (con Rational)) (typed r2 (con Rational)))
          (ret (con Bool))
          (body
            (match r1
              (case (pat-ctor MkRational a b)
                (match r2
                  (case (pat-ctor MkRational c d)
                    (app eq (app * a d) (app * b c)))))))))))

  (fn main
    (type (fn-type (params) (ret (own (con Unit))) (effects IO)))
    (params)
    (body
      (let r_1_2 (term-ctor Rational MkRational 1 2)
        (let r_2_4 (term-ctor Rational MkRational 2 4)
          (let r_1_3 (term-ctor Rational MkRational 1 3)
            (let r_3_4 (term-ctor Rational MkRational 3 4)
              (let r_6_8 (term-ctor Rational MkRational 6 8)
                (let r_5_6 (term-ctor Rational MkRational 5 6)
                  (let r_4_5 (term-ctor Rational MkRational 4 5)
                    (seq (app print (app eq r_1_2 r_2_4))
                    (seq (app print (app eq r_1_2 r_1_3))
                    (seq (app print (app eq r_3_4 r_6_8))
                         (app print (app eq r_5_6 r_4_5)))))))))))))))
