-- WadrayVerification/Aux.lean

import CorelibVerification.Aux.ZMod

import Aegis.Aux.Bool

import Aegis.Aux.ZMod.DivMod

open Sierra

def Wad : Type := UInt128

namespace Wad

def WAD_SCALE : ℕ := 1000000000000000000

variable (w w' : Wad)

protected def toZMod : UInt128 := w

protected def toRat : ℚ := w.toZMod.val / WAD_SCALE

end Wad

def Ray : Type := UInt128

namespace Ray

variable (r r' : Ray)

protected def toZMod : UInt128 := r

end Ray

def SignedWad := UInt128 × (Unit ⊕ Unit)

namespace SignedWad

variable (w w₁ w₂ : SignedWad)

def toRat : ℚ := if w.2 then -(Wad.toRat w.1) else Wad.toRat w.1

-- Repository definitions used:
content: ...

-- Repository lemmas used:
content: theorem List.getElem_append_left{α : Type u_1} {l₁ l₂ : List α} {i : Nat} (hn : i < l₁.length) :\n(l₁ ++ l₂)[i] = l₁[i]

-- Local definitions used:
content: def Wad : Type := UInt128

content: def WAD_SCALE : ℕ := 1000000000000000000

content: protected def toZMod : UInt128 := w

content: protected def toRat : ℚ := w.toZMod.val / WAD_SCALE

content: def Ray : Type := UInt128

content: protected def toZMod : UInt128 := r

content: def SignedWad := UInt128 × (Unit ⊕ Unit)

content: def toRat : ℚ := if w.2 then -(Wad.toRat w.1) else Wad.toRat w.1

-- Local lemmas used:
content: theorem ZMod.cast_rat_nonneg [NeZero n] (a : ZMod n) : 0 ≤ (a.cast : ℚ)

content: theorem WAD_SCALE_rat_pos : 0 < (WAD_SCALE : ℚ)

content: theorem toRat_lt_toRat_of_val_lt_val (h : @ZMod.val U128_MOD w < @ZMod.val U128_MOD w') :
    w.toRat < w'.toRat

content: theorem toRat_injective : Function.Injective Wad.toRat

content: theorem toRat_nonneg : 0 ≤ w.toRat

content: @[simp]
theorem toRat_zero : (0 : Wad).toRat = 0

-- Target theorem and proof:
theorem val_eq_of_toRat_eq : w₁.toRat = w₂.toRat → w₁.1 = w₂.1
:= by
  rcases w₁ with ⟨w₁, s₁⟩
  rcases w₂ with ⟨w₂, s₂⟩
  intro h
  cases s₁ <;> cases s₂
  · have := Wad.toRat_injective h
    cases this
    rfl
  · simp only [toRat, SierraBool_toBool_inl, ite_false, SierraBool_toBool_inr, ite_true] at *
    have h' : Wad.toRat w₁ = 0 := by
      apply le_antisymm _ _
      · rw [h, Left.neg_nonpos_iff]
        apply Wad.toRat_nonneg
      · apply Wad.toRat_nonneg
    rw [h', zero_eq_neg, ← Wad.toRat_zero] at h
    have := Wad.toRat_injective h; cases this
    rw [← Wad.toRat_zero] at h'
    have := Wad.toRat_injective h'; cases this
    rfl
  · simp only [toRat, SierraBool_toBool_inr, ite_true, SierraBool_toBool_inl, ite_false] at *
    have h' : Wad.toRat w₂ = 0 := by
      apply le_antisymm _ _
      · rw [← h, Left.neg_nonpos_iff]
        apply Wad.toRat_nonneg
      · apply Wad.toRat_nonneg
    rw [h', eq_comm, zero_eq_neg, ← Wad.toRat_zero] at h
    have := Wad.toRat_injective h; cases this
    rw [← Wad.toRat_zero] at h'
    have := Wad.toRat_injective h'; cases this
    rfl
  · simp only [toRat, SierraBool_toBool_inr, ite_true, neg_inj] at h
    have := Wad.toRat_injective h
    cases this
    rfl