type frac = { num : int; den : int }

let rec gcd a b = if b = 0 then a else gcd b (a mod b)

let make n d =
  let g = gcd (abs n) (abs d) in
  if d < 0 then { num = -n / g; den = -d / g }
  else { num = n / g; den = d / g }

let add x y =
  make (x.num * y.den + y.num * x.den) (x.den * y.den)

let mul x y = make (x.num * y.num) (x.den * y.den)

;;

let r = add (make 1 2) (make 1 3) in
let s = mul (make 2 3) (make 3 4) in
let t = add r s in
t.num + t.den
