Theory Rat_Pair

(*  Title:      HOL/Decision_Procs/Rat_Pair.thy
    Author:     Amine Chaieb
*)

section ‹Rational numbers as pairs›

theory Rat_Pair
  imports Complex_Main
begin

type_synonym Num = "int × int"

abbreviation Num0_syn :: Num  (0N)
  where "0N  (0, 0)"

abbreviation Numi_syn :: "int  Num"  ('((_)')N)
  where "(i)N  (i, 1)"

definition isnormNum :: "Num  bool"
  where "isnormNum = (λ(a, b). if a = 0 then b = 0 else b > 0  gcd a b = 1)"

definition normNum :: "Num  Num"
  where "normNum = (λ(a,b).
    (if a = 0  b = 0 then (0, 0)
     else
      (let g = gcd a b
       in if b > 0 then (a div g, b div g) else (- (a div g), - (b div g)))))"

declare gcd_dvd1[presburger] gcd_dvd2[presburger]

lemma normNum_isnormNum [simp]: "isnormNum (normNum x)"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  consider "a = 0  b = 0" | "a  0" "b  0"
    by blast
  then show ?thesis
  proof cases
    case 1
    then show ?thesis
      by (simp add: x normNum_def isnormNum_def)
  next
    case ab: 2
    let ?g = "gcd a b"
    let ?a' = "a div ?g"
    let ?b' = "b div ?g"
    let ?g' = "gcd ?a' ?b'"
    from ab have "?g  0" by simp
    with gcd_ge_0_int[of a b] have gpos: "?g > 0" by arith
    have gdvd: "?g dvd a" "?g dvd b" by arith+
    from dvd_mult_div_cancel[OF gdvd(1)] dvd_mult_div_cancel[OF gdvd(2)] ab
    have nz': "?a'  0" "?b'  0" by - (rule notI, simp)+
    from ab have stupid: "a  0  b  0" by arith
    from div_gcd_coprime[OF stupid] have gp1: "?g' = 1"
      by simp
    from ab consider "b < 0" | "b > 0" by arith
    then show ?thesis
    proof cases
      case b: 1
      have False if b': "?b'  0"
      proof -
        from gpos have th: "?g  0" by arith
        from mult_nonneg_nonneg[OF th b'] dvd_mult_div_cancel[OF gdvd(2)]
        show ?thesis using b by arith
      qed
      then have b': "?b' < 0" by (presburger add: linorder_not_le[symmetric])
      from ab(1) nz' b b' gp1 show ?thesis
        by (simp add: x isnormNum_def normNum_def Let_def split_def)
    next
      case b: 2
      then have "?b'  0"
        by (presburger add: pos_imp_zdiv_nonneg_iff[OF gpos])
      with nz' have b': "?b' > 0" by arith
      from b b' ab(1) nz' gp1 show ?thesis
        by (simp add: x isnormNum_def normNum_def Let_def split_def)
    qed
  qed
qed

text ‹Arithmetic over Num›

definition Nadd :: "Num  Num  Num"  (infixl +N 60)
where
  "Nadd = (λ(a, b) (a', b').
    if a = 0  b = 0 then normNum (a', b')
    else if a' = 0  b' = 0 then normNum (a, b)
    else normNum (a * b' + b * a', b * b'))"

definition Nmul :: "Num  Num  Num"  (infixl *N 60)
where
  "Nmul = (λ(a, b) (a', b').
    let g = gcd (a * a') (b * b')
    in (a * a' div g, b * b' div g))"

definition Nneg :: "Num  Num" (~N)
  where "Nneg = (λ(a, b). (- a, b))"

definition Nsub :: "Num  Num  Num"  (infixl -N 60)
  where "Nsub = (λa b. a +N ~N b)"

definition Ninv :: "Num  Num"
  where "Ninv = (λ(a, b). if a < 0 then (- b, ¦a¦) else (b, a))"

definition Ndiv :: "Num  Num  Num"  (infixl ÷N 60)
  where "Ndiv = (λa b. a *N Ninv b)"

lemma Nneg_normN[simp]: "isnormNum x  isnormNum (~N x)"
  by (simp add: isnormNum_def Nneg_def split_def)

lemma Nadd_normN[simp]: "isnormNum (x +N y)"
  by (simp add: Nadd_def split_def)

lemma Nsub_normN[simp]: "isnormNum y  isnormNum (x -N y)"
  by (simp add: Nsub_def split_def)

lemma Nmul_normN[simp]:
  assumes xn: "isnormNum x"
    and yn: "isnormNum y"
  shows "isnormNum (x *N y)"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  obtain a' b' where y: "y = (a', b')" by (cases y)
  consider "a = 0" | "a' = 0" | "a  0" "a'  0" by blast
  then show ?thesis
  proof cases
    case 1
    then show ?thesis
      using xn x y by (simp add: isnormNum_def Let_def Nmul_def split_def)
  next
    case 2
    then show ?thesis
      using yn x y by (simp add: isnormNum_def Let_def Nmul_def split_def)
  next
    case aa': 3
    then have bp: "b > 0" "b' > 0"
      using xn yn x y by (simp_all add: isnormNum_def)
    from bp have "x *N y = normNum (a * a', b * b')"
      using x y aa' bp by (simp add: Nmul_def Let_def split_def normNum_def)
    then show ?thesis by simp
  qed
qed

lemma Ninv_normN[simp]: "isnormNum x  isnormNum (Ninv x)"
  by (simp add: Ninv_def isnormNum_def split_def gcd.commute split: if_split_asm)

lemma isnormNum_int[simp]: "isnormNum 0N" "isnormNum ((1::int)N)" "i  0  isnormNum (i)N"
  by (simp_all add: isnormNum_def)


text ‹Relations over Num›

definition Nlt0:: "Num  bool"  (0>N)
  where "Nlt0 = (λ(a, b). a < 0)"

definition Nle0:: "Num  bool"  (0≥N)
  where "Nle0 = (λ(a, b). a  0)"

definition Ngt0:: "Num  bool"  (0<N)
  where "Ngt0 = (λ(a, b). a > 0)"

definition Nge0:: "Num  bool"  (0≤N)
  where "Nge0 = (λ(a, b). a  0)"

definition Nlt :: "Num  Num  bool"  (infix <N 55)
  where "Nlt = (λa b. 0>N (a -N b))"

definition Nle :: "Num  Num  bool"  (infix N 55)
  where "Nle = (λa b. 0≥N (a -N b))"

definition "INum = (λ(a, b). of_int a / of_int b)"

lemma INum_int [simp]: "INum (i)N = (of_int i ::'a::field)" "INum 0N = (0::'a::field)"
  by (simp_all add: INum_def)

lemma isnormNum_unique[simp]:
  assumes na: "isnormNum x"
    and nb: "isnormNum y"
  shows "(INum x ::'a::field_char_0) = INum y  x = y"
  (is "?lhs = ?rhs")
proof
  obtain a b where x: "x = (a, b)" by (cases x)
  obtain a' b' where y: "y = (a', b')" by (cases y)
  consider "a = 0  b = 0  a' = 0  b' = 0" | "a  0" "b  0" "a'  0" "b'  0"
    by blast
  then show ?rhs if H: ?lhs
  proof cases
    case 1
    then show ?thesis
      using na nb H by (simp add: x y INum_def split_def isnormNum_def split: if_split_asm)
  next
    case 2
    with na nb have pos: "b > 0" "b' > 0"
      by (simp_all add: x y isnormNum_def)
    from H b  0 b'  0 have eq: "a * b' = a' * b"
      by (simp add: x y INum_def eq_divide_eq divide_eq_eq of_int_mult[symmetric] del: of_int_mult)
    from a  0 a'  0 na nb
    have gcd1: "gcd a b = 1" "gcd b a = 1" "gcd a' b' = 1" "gcd b' a' = 1"
      by (simp_all add: x y isnormNum_def add: gcd.commute)
    then have "coprime a b" "coprime b a" "coprime a' b'" "coprime b' a'"
      by (simp_all add: coprime_iff_gcd_eq_1)
    from eq have raw_dvd: "a dvd a' * b" "b dvd b' * a" "a' dvd a * b'" "b' dvd b * a'"
      by (algebra|simp)+
    then have eq1: "b = b'"
      using pos coprime b a coprime b' a'
      by (auto simp add: coprime_dvd_mult_left_iff intro: associated_eqI)
    with eq have "a = a'" using pos by simp
    with b = b' show ?thesis by (simp add: x y)
  qed
  show ?lhs if ?rhs
    using that by simp
qed

lemma isnormNum0[simp]: "isnormNum x  INum x = (0::'a::field_char_0)  x = 0N"
  unfolding INum_int(2)[symmetric]
  by (rule isnormNum_unique) simp_all

lemma of_int_div_aux:
  assumes "d  0"
  shows "(of_int x ::'a::field_char_0) / of_int d =
    of_int (x div d) + (of_int (x mod d)) / of_int d"
proof -
  let ?t = "of_int (x div d) * (of_int d ::'a) + of_int (x mod d)"
  let ?f = "λx. x / of_int d"
  have "x = (x div d) * d + x mod d"
    by auto
  then have eq: "of_int x = ?t"
    by (simp only: of_int_mult[symmetric] of_int_add [symmetric])
  then have "of_int x / of_int d = ?t / of_int d"
    using cong[OF refl[of ?f] eq] by simp
  then show ?thesis
    by (simp add: add_divide_distrib algebra_simps d  0)
qed

lemma of_int_div:
  fixes d :: int
  assumes "d  0" "d dvd n"
  shows "(of_int (n div d) ::'a::field_char_0) = of_int n / of_int d"
  using assms of_int_div_aux [of d n, where ?'a = 'a] by simp

lemma normNum[simp]: "INum (normNum x) = (INum x :: 'a::field_char_0)"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  consider "a = 0  b = 0" | "a  0" "b  0" by blast
  then show ?thesis
  proof cases
    case 1
    then show ?thesis
      by (simp add: x INum_def normNum_def split_def Let_def)
  next
    case ab: 2
    let ?g = "gcd a b"
    from ab have g: "?g  0"by simp
    from of_int_div[OF g, where ?'a = 'a] show ?thesis
      by (auto simp add: x INum_def normNum_def split_def Let_def)
  qed
qed

lemma INum_normNum_iff: "(INum x ::'a::field_char_0) = INum y  normNum x = normNum y"
  (is "?lhs  _")
proof -
  have "normNum x = normNum y  (INum (normNum x) :: 'a) = INum (normNum y)"
    by (simp del: normNum)
  also have " = ?lhs" by simp
  finally show ?thesis by simp
qed

lemma Nadd[simp]: "INum (x +N y) = INum x + (INum y :: 'a :: field_char_0)"
proof -
  let ?z = "0::'a"
  obtain a b where x: "x = (a, b)" by (cases x)
  obtain a' b' where y: "y = (a', b')" by (cases y)
  consider "a = 0  a'= 0  b =0  b' = 0" | "a  0" "a' 0" "b  0" "b'  0"
    by blast
  then show ?thesis
  proof cases
    case 1
    then show ?thesis
      by (auto simp: x y INum_def Nadd_def normNum_def Let_def of_int_div)
  next
    case neq: 2
    show ?thesis
    proof (cases "a * b' + b * a' = 0")
      case True
      then have "of_int (a * b' + b * a') / (of_int b * of_int b') = ?z"
        by simp
      then have "of_int b' * of_int a / (of_int b * of_int b') +
          of_int b * of_int a' / (of_int b * of_int b') = ?z"
        by (simp add: add_divide_distrib)
      then have th: "of_int a / of_int b + of_int a' / of_int b' = ?z"
        using neq by simp
      from True neq show ?thesis
        by (simp add: x y th Nadd_def normNum_def INum_def split_def)
    next
      case False
      let ?g = "gcd (a * b' + b * a') (b * b')"
      have gz: "?g  0"
        using False by simp
      show ?thesis
        using neq False gz
          of_int_div [where ?'a = 'a, OF gz gcd_dvd1 [of "a * b' + b * a'" "b * b'"]]
          of_int_div [where ?'a = 'a, OF gz gcd_dvd2 [of "a * b' + b * a'" "b * b'"]]
        by (simp add: x y Nadd_def INum_def normNum_def Let_def) (simp add: field_simps)
    qed
  qed
qed

lemma Nmul[simp]: "INum (x *N y) = INum x * (INum y:: 'a::field_char_0)"
proof -
  let ?z = "0::'a"
  obtain a b where x: "x = (a, b)" by (cases x)
  obtain a' b' where y: "y = (a', b')" by (cases y)
  consider "a = 0  a' = 0  b = 0  b' = 0" | "a  0" "a'  0" "b  0" "b'  0"
    by blast
  then show ?thesis
  proof cases
    case 1
    then show ?thesis
      by (auto simp add: x y Nmul_def INum_def)
  next
    case neq: 2
    let ?g = "gcd (a * a') (b * b')"
    have gz: "?g  0"
      using neq by simp
    from neq of_int_div [where ?'a = 'a, OF gz gcd_dvd1 [of "a * a'" "b * b'"]]
      of_int_div [where ?'a = 'a , OF gz gcd_dvd2 [of "a * a'" "b * b'"]]
    show ?thesis
      by (simp add: Nmul_def x y Let_def INum_def)
  qed
qed

lemma Nneg[simp]: "INum (~N x) = - (INum x :: 'a::field)"
  by (simp add: Nneg_def split_def INum_def)

lemma Nsub[simp]: "INum (x -N y) = INum x - (INum y:: 'a::field_char_0)"
  by (simp add: Nsub_def split_def)

lemma Ninv[simp]: "INum (Ninv x) = (1 :: 'a::field) / INum x"
  by (simp add: Ninv_def INum_def split_def)

lemma Ndiv[simp]: "INum (x ÷N y) = INum x / (INum y :: 'a::field_char_0)"
  by (simp add: Ndiv_def)

lemma Nlt0_iff[simp]:
  assumes nx: "isnormNum x"
  shows "((INum x :: 'a::{field_char_0,linordered_field}) < 0) = 0>N x"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  show ?thesis
  proof (cases "a = 0")
    case True
    then show ?thesis
      by (simp add: x Nlt0_def INum_def)
  next
    case False
    then have b: "(of_int b::'a) > 0"
      using nx by (simp add: x isnormNum_def)
    from pos_divide_less_eq[OF b, where b="of_int a" and a="0::'a"]
    show ?thesis
      by (simp add: x Nlt0_def INum_def)
  qed
qed

lemma Nle0_iff[simp]:
  assumes nx: "isnormNum x"
  shows "((INum x :: 'a::{field_char_0,linordered_field})  0) = 0≥N x"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  show ?thesis
  proof (cases "a = 0")
    case True
    then show ?thesis
      by (simp add: x Nle0_def INum_def)
  next
    case False
    then have b: "(of_int b :: 'a) > 0"
      using nx by (simp add: x isnormNum_def)
    from pos_divide_le_eq[OF b, where b="of_int a" and a="0::'a"]
    show ?thesis
      by (simp add: x Nle0_def INum_def)
  qed
qed

lemma Ngt0_iff[simp]:
  assumes nx: "isnormNum x"
  shows "((INum x :: 'a::{field_char_0,linordered_field}) > 0) = 0<N x"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  show ?thesis
  proof (cases "a = 0")
    case True
    then show ?thesis
      by (simp add: x Ngt0_def INum_def)
  next
    case False
    then have b: "(of_int b::'a) > 0"
      using nx by (simp add: x isnormNum_def)
    from pos_less_divide_eq[OF b, where b="of_int a" and a="0::'a"]
    show ?thesis
      by (simp add: x Ngt0_def INum_def)
  qed
qed

lemma Nge0_iff[simp]:
  assumes nx: "isnormNum x"
  shows "(INum x :: 'a::{field_char_0,linordered_field})  0  0≤N x"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  show ?thesis
  proof (cases "a = 0")
    case True
    then show ?thesis
      by (simp add: x Nge0_def INum_def)
  next
    case False
    then have b: "(of_int b::'a) > 0"
      using nx by (simp add: x isnormNum_def)
    from pos_le_divide_eq[OF b, where b="of_int a" and a="0::'a"]
    show ?thesis
      by (simp add: x Nge0_def INum_def)
  qed
qed

lemma Nlt_iff[simp]:
  assumes nx: "isnormNum x"
    and ny: "isnormNum y"
  shows "((INum x :: 'a::{field_char_0,linordered_field}) < INum y)  x <N y"
proof -
  let ?z = "0::'a"
  have "((INum x ::'a) < INum y)  INum (x -N y) < ?z"
    using nx ny by simp
  also have "  0>N (x -N y)"
    using Nlt0_iff[OF Nsub_normN[OF ny]] by simp
  finally show ?thesis
    by (simp add: Nlt_def)
qed

lemma Nle_iff[simp]:
  assumes nx: "isnormNum x"
    and ny: "isnormNum y"
  shows "((INum x :: 'a::{field_char_0,linordered_field})  INum y)  x N y"
proof -
  have "((INum x ::'a)  INum y)  INum (x -N y)  (0::'a)"
    using nx ny by simp
  also have "  0≥N (x -N y)"
    using Nle0_iff[OF Nsub_normN[OF ny]] by simp
  finally show ?thesis
    by (simp add: Nle_def)
qed

lemma Nadd_commute:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  shows "x +N y = y +N x"
proof -
  have n: "isnormNum (x +N y)" "isnormNum (y +N x)"
    by simp_all
  have "(INum (x +N y)::'a) = INum (y +N x)"
    by simp
  with isnormNum_unique[OF n] show ?thesis
    by simp
qed

lemma [simp]:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  shows "(0, b) +N y = normNum y"
    and "(a, 0) +N y = normNum y"
    and "x +N (0, b) = normNum x"
    and "x +N (a, 0) = normNum x"
  by (simp_all add: Nadd_def normNum_def split_def)

lemma normNum_nilpotent_aux[simp]:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  assumes nx: "isnormNum x"
  shows "normNum x = x"
proof -
  let ?a = "normNum x"
  have n: "isnormNum ?a" by simp
  have th: "INum ?a = (INum x ::'a)" by simp
  with isnormNum_unique[OF n nx] show ?thesis by simp
qed

lemma normNum_nilpotent[simp]:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  shows "normNum (normNum x) = normNum x"
  by simp

lemma normNum0[simp]: "normNum (0, b) = 0N" "normNum (a, 0) = 0N"
  by (simp_all add: normNum_def)

lemma normNum_Nadd:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  shows "normNum (x +N y) = x +N y"
  by simp

lemma Nadd_normNum1[simp]:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  shows "normNum x +N y = x +N y"
proof -
  have n: "isnormNum (normNum x +N y)" "isnormNum (x +N y)"
    by simp_all
  have "INum (normNum x +N y) = INum x + (INum y :: 'a)"
    by simp
  also have " = INum (x +N y)"
    by simp
  finally show ?thesis
    using isnormNum_unique[OF n] by simp
qed

lemma Nadd_normNum2[simp]:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  shows "x +N normNum y = x +N y"
proof -
  have n: "isnormNum (x +N normNum y)" "isnormNum (x +N y)"
    by simp_all
  have "INum (x +N normNum y) = INum x + (INum y :: 'a)"
    by simp
  also have " = INum (x +N y)"
    by simp
  finally show ?thesis
    using isnormNum_unique[OF n] by simp
qed

lemma Nadd_assoc:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  shows "x +N y +N z = x +N (y +N z)"
proof -
  have n: "isnormNum (x +N y +N z)" "isnormNum (x +N (y +N z))"
    by simp_all
  have "INum (x +N y +N z) = (INum (x +N (y +N z)) :: 'a)"
    by simp
  with isnormNum_unique[OF n] show ?thesis
    by simp
qed

lemma Nmul_commute: "isnormNum x  isnormNum y  x *N y = y *N x"
  by (simp add: Nmul_def split_def Let_def gcd.commute mult.commute)

lemma Nmul_assoc:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  assumes nx: "isnormNum x"
    and ny: "isnormNum y"
    and nz: "isnormNum z"
  shows "x *N y *N z = x *N (y *N z)"
proof -
  from nx ny nz have n: "isnormNum (x *N y *N z)" "isnormNum (x *N (y *N z))"
    by simp_all
  have "INum (x +N y +N z) = (INum (x +N (y +N z)) :: 'a)"
    by simp
  with isnormNum_unique[OF n] show ?thesis
    by simp
qed

lemma Nsub0:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  assumes x: "isnormNum x"
    and y: "isnormNum y"
  shows "x -N y = 0N  x = y"
proof -
  from isnormNum_unique[where 'a = 'a, OF Nsub_normN[OF y], where y="0N"]
  have "x -N y = 0N  INum (x -N y) = (INum 0N :: 'a)"
    by simp
  also have "  INum x = (INum y :: 'a)"
    by simp
  also have "  x = y"
    using x y by simp
  finally show ?thesis .
qed

lemma Nmul0[simp]: "c *N 0N = 0N" " 0N *N c = 0N"
  by (simp_all add: Nmul_def Let_def split_def)

lemma Nmul_eq0[simp]:
  assumes "SORT_CONSTRAINT('a::field_char_0)"
  assumes nx: "isnormNum x"
    and ny: "isnormNum y"
  shows "x*N y = 0N  x = 0N  y = 0N"
proof -
  obtain a b where x: "x = (a, b)" by (cases x)
  obtain a' b' where y: "y = (a', b')" by (cases y)
  have n0: "isnormNum 0N" by simp
  show ?thesis using nx ny
    by (metis (no_types, opaque_lifting) INum_int(2) Nmul Nmul0(1) Nmul0(2) isnormNum0 mult_eq_0_iff)
qed

lemma Nneg_Nneg[simp]: "~N (~N c) = c"
  by (simp add: Nneg_def split_def)

lemma Nmul1[simp]: "isnormNum c  (1)N *N c = c" "isnormNum c  c *N (1)N = c"
  by (simp add: Nmul_def Let_def split_def isnormNum_def, metis div_0 div_by_1 surjective_pairing)+

end