//
// implementation of some rational number operations
//

staload "rational.sats"

assume rat_t =
  '(int(*numer*), int(*denom*))
// numer: any integer; denom: any positive integer
// gcd (numer, denom) = 1

extern fun gcd (x: int, y: int): int

implement rat_make_int_int (p, q) =
  if q >= 0 then let
    val r = gcd (p, q)
  in
    '(p / r, q / r)
  end else begin
    rat_make_int_int (~p, ~q)
  end
// end of [rat_make_int_int]

implement add_rat_rat (x, y) = let
  val p1 = x.0
  val q1 = x.1
  val p2 = y.0
  val q2 = y.1
in
  rat_make_int_int (p1 * q2 + p2 * q1, q1 * q2)
end (* end of [add_rat_rat] *)

(* ****** ****** *)

(* end of [rational.dats] *)