AFTD Auto-Formalizing Theoretical Domains

explicitApproximant_torusDistance

theoremverifiedformalization

For any Boolean function f : (Fin n → Bool) → Bool on n > 0 variables and any x : Fin n → Bool, torusDistance (explicitApproximant f x - bit (f x) / 2) equals explicitError f.

Statement

theorem explicitApproximant_torusDistance {n : ℕ} (hn : 0 < n)
    (f : (Fin n → Bool) → Bool) (x : Fin n → Bool) :
    torusDistance (explicitApproximant f x - bit (f x) / 2) = explicitError f
Formalizes
arXiv:2610.19174, Listing A.14
Verified
21 Oct 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 8
  • explicitApproximantFor a Boolean function f : (Fin n → Bool) → Bool and input x : Fin n → Bool, explicitApproximant f x is (if f x then 1 else 0)/2 -…
  • torusDistanceThe distance on the one-dimensional torus R/Z, defined as the distance of a real number to the nearest integer.
  • explicitErrorThe explicit torus approximation error: 2^{-n-1} if the number of accepting inputs of f is odd, and 0 if even.
  • bitThe real embedding of a Boolean value, sending false to 0 and true to 1.
  • parityCharacterFor an implicit natural number n and a Boolean assignment x : Fin n → Bool, parityCharacter x is the real number defined by the product…
  • topCoefficientFor an implicit natural number n and a Boolean-valued function f : (Fin n → Bool) → Bool, topCoefficient f is the integer defined by the…
  • acceptingCountThe number of accepting inputs |f⁻¹(1)| of a Boolean function f.
  • fullMonomialThe full monomial x_[n] = ∏_{i=1}^n x_i evaluated at a point in {0,1}^n.

Read back from the Lean

For every natural number $n$ satisfying $0 < n$, every Boolean function $f : (\text{Fin } n \to \text{Bool}) \to \text{Bool}$, and every input vector $x : \text{Fin } n \to \text{Bool}$, the torus distance (distance to the nearest integer, $|u - \operatorname{round}(u)|$) of $\operatorname{explicitApproximant}(f, x) - \operatorname{bit}(f(x)) / 2$ equals $\operatorname{explicitError}(f)$. Here: - $\operatorname{bit}(b)$ is $1$ if $b = \text{true}$ and $0$ if $b = \text{false}$. - $\operatorname{explicitError}(f)$ is $1 / 2^{n+1}$ if the number of inputs satisfying $f$ (i.e., $\operatorname{acceptingCount}(f) = |\{y \mid f(y) = \text{true}\}|$) is odd, and $0$ if it is even. - $\operatorname{explicitApproximant}(f, x)$ is defined as $(1/2 \text{ if } f(x) \text{ else } 0) - \lfloor \operatorname{topCoefficient}(f) / 2 \rfloor \cdot \operatorname{fullMonomial}(x) - (\operatorname{topCoefficient}(f) \bmod 2) \cdot \operatorname{parityCharacter}(x) / 2^{n+1}$.

Written by a model that saw only the Lean, never the English above. If the two disagree, that is worth a challenge.

The proof, explained

The explicit approximant differs from bit(f(x)) / 2 by an integer multiple of the full monomial plus a scaled parity character. Because the full monomial takes values in {0, 1}, its contribution is an integer shift that leaves the torus distance unchanged. Since the parity character has absolute value 1 and the denominator 2^(n + 1) is at least 4, the remaining term has absolute value at most 1/4 < 1/2, so its nearest integer is 0 and its torus distance is simply (topCoefficient f % 2) / 2^(n + 1). Finally, the parity of topCoefficient f matches that of acceptingCount f, which equals explicitError f in both the odd and even cases.

  1. Every finite product of ±1 factors is 1 or -1, so the character product over all coordinates is 1 or -1.

    We prove by induction on the finite set s that the product of (if x i = true then 1 else -1) over s is 1 or -1. The empty product is 1. When inserting a new element a, the previous product is multiplied by ±1; in all four combinations of signs, the product remains 1 or -1. Specialising to the entire universe Finset.univ yields the result.

    lines 6–16

    uses Finset.induction_on, Finset.prod_insert, Finset.univ

      have key : ∀ s : Finset (Fin n),
          (∏ i ∈ s, if x i = true then (1:ℤ) else -1) = 1 ∨ (∏ i ∈ s, if x i = true then (1:ℤ) else -1) = -1 := by
        intro s
        induction s using Finset.induction_on with
        | empty => simp
        | insert a s ha ih =>
          rw [Finset.prod_insert ha]
          rcases ih with h | h <;>
            rcases (show (if x a = true then (1:ℤ) else -1) = 1 ∨ (if x a = true then (1:ℤ) else -1) = -1 from by split <;> simp) with h' | h' <;>
              rw [h', h] <;> norm_num
      simpa using key Finset.univ
  2. The top Fourier coefficient topCoefficient f and the accepting count acceptingCount f have the same parity.

    We first express the accepting count as the sum of indicator booleans ∑ x, (if f x = true then 1 else 0). Subtracting this from topCoefficient f gives a single sum over x of (∏ i, χ i(x) - 1) for accepting inputs and 0 - 0 for rejecting inputs. By the previous lemma, the character product is ±1, so on accepting inputs the summand is either 1 - 1 = 0 or -1 - 1 = -2. Since each summand is divisible by 2, their sum is divisible by 2, which means topCoefficient f and acceptingCount f are congruent modulo 2.

    lines 22–40

    uses Finset.sum_boole, Fintype.card_subtype, Finset.sum_sub_distrib, Finset.dvd_sum, if_pos, explicitApproximant_torusDistance_chi_int_pm, if_neg, Int.emod_eq_emod_iff_emod_sub_eq_zero, Int.emod_eq_zero_of_dvd

      have hsum : (∑ x : Fin n → Bool, (if f x = true then (1:ℤ) else 0)) = (acceptingCount f : ℤ) := by
        rw [Finset.sum_boole]
        rw [acceptingCount, Fintype.card_subtype]
      have hdvd : (2:ℤ) ∣ topCoefficient f - (acceptingCount f : ℤ) := by
        have hsplit : topCoefficient f - (acceptingCount f : ℤ)
            = ∑ x : Fin n → Bool,
                ((if f x = true then ∏ i, (if x i = true then (1:ℤ) else -1) else 0)
                  - (if f x = true then (1:ℤ) else 0)) := by
          rw [Finset.sum_sub_distrib, hsum]
          rfl
        rw [hsplit]
        refine Finset.dvd_sum ?_
        intro y _
        by_cases h : f y = true
        · rw [if_pos h, if_pos h]
          rcases explicitApproximant_torusDistance_chi_int_pm y with hc | hc <;> rw [hc] <;> norm_num
        · rw [if_neg h, if_neg h]
          simp
      exact (Int.emod_eq_emod_iff_emod_sub_eq_zero).mpr (Int.emod_eq_zero_of_dvd hdvd)
  3. The parity character parityCharacter x takes value 1 or -1 for every Boolean input x.

    Unfolding the definition of parityCharacter, we perform induction on subsets s ⊆ Fin n to show that the real product of ±1 factors is 1 or -1. The base case is the empty product 1, and each extension multiplies an element of {1, -1} by ±1, preserving membership in {1, -1}. Evaluating on the full set of coordinates Finset.univ gives the claim.

    lines 45–56

    uses Finset.induction_on, Finset.prod_insert, Finset.univ

      rw [parityCharacter]
      have key : ∀ s : Finset (Fin n),
          (∏ i ∈ s, (if x i = true then (1:ℝ) else -1)) = 1 ∨ (∏ i ∈ s, (if x i = true then (1:ℝ) else -1)) = -1 := by
        intro s
        induction s using Finset.induction_on with
        | empty => simp
        | insert a s ha ih =>
          rw [Finset.prod_insert ha]
          rcases ih with h | h <;>
            rcases (show (if x a = true then (1:ℝ) else -1) = 1 ∨ (if x a = true then (1:ℝ) else -1) = -1 from by split <;> simp) with h' | h' <;>
              rw [h', h] <;> norm_num
      simpa using key Finset.univ
  4. The full monomial fullMonomial x evaluates to 0 or 1 at any Boolean point x.

    If x i = true for every coordinate i, then every bit factor is 1, so the full monomial product is 1. Otherwise, there is some coordinate i where x i ≠ true, which makes bit (x i) = 0 and forces the entire product to 0.

    lines 61–73

    uses Finset.prod_eq_one, Finset.prod_eq_zero, Finset.mem_univ

      by_cases h : ∀ i, x i = true
      · right
        rw [fullMonomial]
        refine Finset.prod_eq_one ?_
        intro i _
        rw [h i]
        simp [bit]
      · left
        push Not at h
        obtain ⟨i, hi⟩ := h
        rw [fullMonomial]
        refine Finset.prod_eq_zero (Finset.mem_univ i) ?_
        simp [bit, hi]
  5. Shifting by an integer does not change the torus distance: torusDistance (u + m) = torusDistance u.

    By definition, torusDistance is |u - round u|. Because rounding commutes with integer translation (round(u + m) = round u + m), the integer m cancels inside the absolute value, leaving |u - round u|.

    lines 78–82

    uses round_add_intCast

      unfold torusDistance
      rw [round_add_intCast]
      congr 1
      push_cast
      ring
  6. The difference explicitApproximant f x - bit (f x) / 2 cancels the leading term, leaving the monomial and character terms.

    Unfolding explicitApproximant and bit, the first term is bit (f x) / 2. Subtracting bit (f x) / 2 cancels it by basic ring algebra, leaving -(topCoefficient f / 2 * fullMonomial x) - (topCoefficient f % 2 * parityCharacter x / 2^(n + 1)).

    lines 88–93

      have hsub : explicitApproximant f x - bit (f x) / 2
          = -(↑(topCoefficient f / 2) * fullMonomial x)
              - ↑(topCoefficient f % 2) * parityCharacter x / 2 ^ (n + 1) := by
        simp only [explicitApproximant, bit]
        ring
      rw [hsub]
  7. The full monomial term is an integer, so the difference is an integer shift of the character term, preserving the torus distance.

    Let r = topCoefficient f % 2 and k = topCoefficient f / 2. Since fullMonomial x is either 0 or 1, the term -k * fullMonomial x is an integer (either 0 or -k). By integer invariance of torusDistance, this integer shift can be dropped, leaving torusDistance (-r * parityCharacter x / 2^(n + 1)).

    lines 94–108

    uses explicitApproximant_torusDistance_fullMonomial_zero_or_one, explicitApproximant_torusDistance_add_int

      set r := topCoefficient f % 2 with hrdef
      set k := topCoefficient f / 2 with hkdef
      have hint : ∃ m : ℤ, (-(↑k * fullMonomial x) - ↑r * parityCharacter x / 2 ^ (n + 1))
          = (-(↑r * parityCharacter x / 2 ^ (n + 1))) + (m : ℝ) := by
        rcases explicitApproximant_torusDistance_fullMonomial_zero_or_one x with h | h
        · refine ⟨0, ?_⟩
          rw [h]
          push_cast
          ring
        · refine ⟨-k, ?_⟩
          rw [h]
          push_cast
          ring
      obtain ⟨m, hm⟩ := hint
      rw [hm, explicitApproximant_torusDistance_add_int]
  8. Since n > 0, the denominator 2^(n + 1) is at least 4 and strictly positive.

    Because n ≥ 1, we have n + 1 ≥ 2. Monotonicity of powers gives 4 = 2^2 ≤ 2^(n + 1), and positivity holds automatically.

    lines 109–113

    uses pow_le_pow_right₀

      have hpow : (4:ℝ) ≤ 2 ^ (n + 1) := by
        have h2 : 2 ≤ n + 1 := by omega
        calc (4:ℝ) = 2 ^ 2 := by norm_num
          _ ≤ 2 ^ (n + 1) := pow_le_pow_right₀ (by norm_num) h2
      have hpos : (0:ℝ) < 2 ^ (n + 1) := by positivity
  9. The absolute value |-r * parityCharacter x / 2^(n + 1)| equals r / 2^(n + 1), which is strictly less than 1/2.

    The remainder r modulo 2 is 0 or 1, so 0 ≤ r ≤ 1. The parity character has absolute value 1, so the absolute value of the expression simplifies to r / 2^(n + 1). Since r ≤ 1 and 2^(n + 1) ≥ 4, this quotient is at most 1/4, which is strictly less than 1/2.

    lines 114–124

    uses Int.emod_two_eq_zero_or_one, explicitApproximant_torusDistance_parityCharacter_pm, abs_neg, abs_div, abs_mul, abs_of_nonneg, abs_of_pos, div_lt_iff₀

      have hr01 : r = 0 ∨ r = 1 := Int.emod_two_eq_zero_or_one (topCoefficient f)
      have hrnn : (0:ℝ) ≤ (r:ℝ) := by rcases hr01 with h | h <;> rw [h] <;> norm_num
      have hrle : (r:ℝ) ≤ 1 := by rcases hr01 with h | h <;> rw [h] <;> norm_num
      have hχabs : |parityCharacter x| = 1 := by
        rcases explicitApproximant_torusDistance_parityCharacter_pm x with h | h <;> rw [h] <;> norm_num
      have habs : |-(↑r * parityCharacter x / 2 ^ (n + 1))| = (r:ℝ) / 2 ^ (n + 1) := by
        rw [abs_neg, abs_div, abs_mul, abs_of_nonneg hrnn, hχabs, abs_of_pos hpos]
        ring
      have hlt : |-(↑r * parityCharacter x / 2 ^ (n + 1))| < 1 / 2 := by
        rw [habs, div_lt_iff₀ hpos]
        nlinarith
  10. The expression rounds to 0, so its torus distance is simply its absolute value r / 2^(n + 1).

    Because the absolute value is strictly less than 1/2, the value lies in (-1/2, 1/2), whose nearest integer is 0. Unfolding torusDistance and substituting round u = 0 leaves |u - 0| = |u| = r / 2^(n + 1).

    lines 125–134

    uses round_eq_zero_iff, abs_lt, Int.cast_zero, sub_zero

      have hrnd : round (-(↑r * parityCharacter x / 2 ^ (n + 1))) = 0 := by
        rw [round_eq_zero_iff]
        rw [abs_lt] at hlt
        exact ⟨by linarith, hlt.2⟩
      have hval : torusDistance (-(↑r * parityCharacter x / 2 ^ (n + 1))) = (r:ℝ) / 2 ^ (n + 1) := by
        unfold torusDistance
        rw [hrnd]
        simp only [Int.cast_zero, sub_zero]
        exact habs
      rw [hval]
  11. The accepting count of f is odd if and only if r = 1.

    By the parity lemma proved earlier, r = topCoefficient f % 2 equals (acceptingCount f : ℤ) % 2. An integer or natural number is odd if and only if its remainder modulo 2 is 1.

    lines 135–145

    uses explicitApproximant_torusDistance_topCoefficient_emod_two, Int.natCast_emod, Int.odd_iff, Nat.odd_iff, Nat.cast_eq_one

      have hpar : r = (acceptingCount f : ℤ) % 2 := by
        rw [hrdef]
        exact explicitApproximant_torusDistance_topCoefficient_emod_two f
      have hcast : Odd ((acceptingCount f : ℤ)) ↔ Odd (acceptingCount f) := by
        have hem : ((↑(acceptingCount f) : ℤ) % 2) = (↑(acceptingCount f % 2) : ℤ) := by
          have hh := Int.natCast_emod (acceptingCount f) 2
          simpa using hh
        rw [Int.odd_iff, Nat.odd_iff, hem]
        exact Nat.cast_eq_one
      have hodd : Odd (acceptingCount f) ↔ r = 1 := by
        rw [hpar, ← hcast, Int.odd_iff]
  12. The torus distance equals explicitError f.

    Unfolding explicitError, its value is 1 / 2^(n + 1) if acceptingCount f is odd, and 0 otherwise. If acceptingCount f is odd, r = 1, so the distance is 1 / 2^(n + 1), matching the definition. If it is not odd, r must be 0 (since r is 0 or 1), yielding distance 0, which also matches.

    lines 146–154

    uses if_pos, if_neg

      unfold explicitError
      by_cases ho : Odd (acceptingCount f)
      · rw [if_pos ho, hodd.mp ho]
        push_cast
        ring
      · rw [if_neg ho]
        rcases hr01 with h | h
        · rw [h]; norm_num
        · exact absurd h (fun hh => ho (hodd.mpr hh))
Finset.induction_on
a property holds for all finite sets if it holds for the empty set and is preserved when adjoining an element not already present
Finset.prod_insert
the product of a function over an adjoined set insert a s is the value at a times the product over s, provided a ∉ s
Finset.univ
the finite set containing all elements of a finite type
Finset.sum_boole
the sum of an indicator condition over a finite set equals the cardinality of the subset satisfying the condition
Fintype.card_subtype
the cardinality of a subtype equals the cardinality of the corresponding finset of satisfying elements
Finset.sum_sub_distrib
the sum of differences over a finite set equals the difference of the sums
Finset.dvd_sum
if a divides each term in a finite sum, then a divides the sum
if_pos
an if-then-else expression evaluates to the 'then' branch when the condition is true
explicitApproximant_torusDistance_chi_int_pm
for any Boolean point x, the product of signs (if x i = true then 1 else -1) is either 1 or -1
if_neg
an if-then-else expression evaluates to the 'else' branch when the condition is false
Int.emod_eq_emod_iff_emod_sub_eq_zero
two integers have the same remainder modulo n if and only if their difference is divisible by n
Int.emod_eq_zero_of_dvd
if a divides b, then the remainder of b modulo a is zero
Finset.prod_eq_one
a finite product equals 1 if every factor equals 1
Finset.prod_eq_zero
in a monoid with zero, a finite product is 0 if any factor is 0
Finset.mem_univ
every element of a finite type belongs to its universal finset
round_add_intCast
rounding an integer shift satisfies round(x + m) = round(x) + m
explicitApproximant_torusDistance_fullMonomial_zero_or_one
the full monomial at any Boolean point is either 0 or 1
explicitApproximant_torusDistance_add_int
the distance to the nearest integer is invariant under integer translation: torusDistance(u + m) = torusDistance(u)
pow_le_pow_right₀
if 1 ≤ a and m ≤ n, then a^m ≤ a^n
Int.emod_two_eq_zero_or_one
the remainder of an integer modulo 2 is either 0 or 1
explicitApproximant_torusDistance_parityCharacter_pm
the real parity character at any Boolean point takes value 1 or -1
abs_neg
the absolute value of -a equals the absolute value of a
abs_div
the absolute value of a quotient is the quotient of the absolute values
abs_mul
the absolute value of a product is the product of the absolute values
abs_of_nonneg
the absolute value of a non-negative real number is the number itself
abs_of_pos
the absolute value of a strictly positive real number is the number itself
div_lt_iff₀
for c > 0, b / c < a is equivalent to b < a * c
round_eq_zero_iff
a real number rounds to 0 if and only if it lies in the half-open interval [-1/2, 1/2)
abs_lt
|a| < b is equivalent to -b < a and a < b
Int.cast_zero
the integer 0 cast to another ring equals 0
sub_zero
subtracting 0 from an element leaves it unchanged
explicitApproximant_torusDistance_topCoefficient_emod_two
the top Fourier coefficient and the accepting count of a Boolean function have the same parity
Int.natCast_emod
the integer cast of a natural remainder m % n equals the integer remainder of the casts
Int.odd_iff
an integer n is odd if and only if n % 2 = 1
Nat.odd_iff
a natural number n is odd if and only if n % 2 = 1
Nat.cast_eq_one
the cast of a natural number equals 1 if and only if the natural number is 1

Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.

Lean source view module on GitHub

open scoped BigOperators/-- Every product of the ±1 factors `if x i = true then 1 else -1` is `1` or `-1`. -/lemma explicitApproximant_torusDistance_chi_int_pm {n : ℕ} (x : Fin n → Bool) :    (∏ i, if x i = true then (1:ℤ) else -1) = 1 ∨ (∏ i, if x i = true then (1:ℤ) else -1) = -1 := by  have key : ∀ s : Finset (Fin n),      (∏ i ∈ s, if x i = true then (1:ℤ) else -1) = 1 ∨ (∏ i ∈ s, if x i = true then (1:ℤ) else -1) = -1 := by    intro s    induction s using Finset.induction_on with    | empty => simp    | insert a s ha ih =>      rw [Finset.prod_insert ha]      rcases ih with h | h <;>        rcases (show (if x a = true then (1:ℤ) else -1) = 1 ∨ (if x a = true then (1:ℤ) else -1) = -1 from by split <;> simp) with h' | h' <;>          rw [h', h] <;> norm_num  simpa using key Finset.univ/-- The top Fourier coefficient and the accepting count of `f` have the same parity. -/lemma explicitApproximant_torusDistance_topCoefficient_emod_two {n : ℕ}    (f : (Fin n → Bool) → Bool) :    topCoefficient f % 2 = (acceptingCount f : ℤ) % 2 := by  have hsum : (∑ x : Fin n → Bool, (if f x = true then (1:ℤ) else 0)) = (acceptingCount f : ℤ) := by    rw [Finset.sum_boole]    rw [acceptingCount, Fintype.card_subtype]  have hdvd : (2:ℤ) ∣ topCoefficient f - (acceptingCount f : ℤ) := by    have hsplit : topCoefficient f - (acceptingCount f : ℤ)        = ∑ x : Fin n → Bool,            ((if f x = true then ∏ i, (if x i = true then (1:ℤ) else -1) else 0)              - (if f x = true then (1:ℤ) else 0)) := by      rw [Finset.sum_sub_distrib, hsum]      rfl    rw [hsplit]    refine Finset.dvd_sum ?_    intro y _    by_cases h : f y = true    · rw [if_pos h, if_pos h]      rcases explicitApproximant_torusDistance_chi_int_pm y with hc | hc <;> rw [hc] <;> norm_num    · rw [if_neg h, if_neg h]      simp  exact (Int.emod_eq_emod_iff_emod_sub_eq_zero).mpr (Int.emod_eq_zero_of_dvd hdvd)/-- The parity character takes only the values `1` and `-1`. -/lemma explicitApproximant_torusDistance_parityCharacter_pm {n : ℕ} (x : Fin n → Bool) :    parityCharacter x = 1 ∨ parityCharacter x = -1 := by  rw [parityCharacter]  have key : ∀ s : Finset (Fin n),      (∏ i ∈ s, (if x i = true then (1:ℝ) else -1)) = 1 ∨ (∏ i ∈ s, (if x i = true then (1:ℝ) else -1)) = -1 := by    intro s    induction s using Finset.induction_on with    | empty => simp    | insert a s ha ih =>      rw [Finset.prod_insert ha]      rcases ih with h | h <;>        rcases (show (if x a = true then (1:ℝ) else -1) = 1 ∨ (if x a = true then (1:ℝ) else -1) = -1 from by split <;> simp) with h' | h' <;>          rw [h', h] <;> norm_num  simpa using key Finset.univ/-- The full monomial at a Boolean point is `0` or `1`. -/lemma explicitApproximant_torusDistance_fullMonomial_zero_or_one {n : ℕ} (x : Fin n → Bool) :    fullMonomial x = 0 ∨ fullMonomial x = 1 := by  by_cases h : ∀ i, x i = true  · right    rw [fullMonomial]    refine Finset.prod_eq_one ?_    intro i _    rw [h i]    simp [bit]  · left    push Not at h    obtain ⟨i, hi⟩ := h    rw [fullMonomial]    refine Finset.prod_eq_zero (Finset.mem_univ i) ?_    simp [bit, hi]/-- Shifting by an integer does not change the distance to the nearest integer. -/lemma explicitApproximant_torusDistance_add_int (u : ℝ) (m : ℤ) :    torusDistance (u + (m:ℝ)) = torusDistance u := by  unfold torusDistance  rw [round_add_intCast]  congr 1  push_cast  ring/-- Pointwise torus error of the explicit approximant at every Boolean point. -/theorem explicitApproximant_torusDistance {n : ℕ} (hn : 0 < n)    (f : (Fin n → Bool) → Bool) (x : Fin n → Bool) :    torusDistance (explicitApproximant f x - bit (f x) / 2) = explicitError f := by  have hsub : explicitApproximant f x - bit (f x) / 2      = -(↑(topCoefficient f / 2) * fullMonomial x)          - ↑(topCoefficient f % 2) * parityCharacter x / 2 ^ (n + 1) := by    simp only [explicitApproximant, bit]    ring  rw [hsub]  set r := topCoefficient f % 2 with hrdef  set k := topCoefficient f / 2 with hkdef  have hint : ∃ m : ℤ, (-(↑k * fullMonomial x) - ↑r * parityCharacter x / 2 ^ (n + 1))      = (-(↑r * parityCharacter x / 2 ^ (n + 1))) + (m : ℝ) := by    rcases explicitApproximant_torusDistance_fullMonomial_zero_or_one x with h | h    · refine ⟨0, ?_⟩      rw [h]      push_cast      ring    · refine ⟨-k, ?_⟩      rw [h]      push_cast      ring  obtain ⟨m, hm⟩ := hint  rw [hm, explicitApproximant_torusDistance_add_int]  have hpow : (4:ℝ) ≤ 2 ^ (n + 1) := by    have h2 : 2 ≤ n + 1 := by omega    calc (4:ℝ) = 2 ^ 2 := by norm_num      _ ≤ 2 ^ (n + 1) := pow_le_pow_right₀ (by norm_num) h2  have hpos : (0:ℝ) < 2 ^ (n + 1) := by positivity  have hr01 : r = 0 ∨ r = 1 := Int.emod_two_eq_zero_or_one (topCoefficient f)  have hrnn : (0:ℝ) ≤ (r:ℝ) := by rcases hr01 with h | h <;> rw [h] <;> norm_num  have hrle : (r:ℝ) ≤ 1 := by rcases hr01 with h | h <;> rw [h] <;> norm_num  have hχabs : |parityCharacter x| = 1 := by    rcases explicitApproximant_torusDistance_parityCharacter_pm x with h | h <;> rw [h] <;> norm_num  have habs : |-(↑r * parityCharacter x / 2 ^ (n + 1))| = (r:ℝ) / 2 ^ (n + 1) := by    rw [abs_neg, abs_div, abs_mul, abs_of_nonneg hrnn, hχabs, abs_of_pos hpos]    ring  have hlt : |-(↑r * parityCharacter x / 2 ^ (n + 1))| < 1 / 2 := by    rw [habs, div_lt_iff₀ hpos]    nlinarith  have hrnd : round (-(↑r * parityCharacter x / 2 ^ (n + 1))) = 0 := by    rw [round_eq_zero_iff]    rw [abs_lt] at hlt    exact ⟨by linarith, hlt.2⟩  have hval : torusDistance (-(↑r * parityCharacter x / 2 ^ (n + 1))) = (r:ℝ) / 2 ^ (n + 1) := by    unfold torusDistance    rw [hrnd]    simp only [Int.cast_zero, sub_zero]    exact habs  rw [hval]  have hpar : r = (acceptingCount f : ℤ) % 2 := by    rw [hrdef]    exact explicitApproximant_torusDistance_topCoefficient_emod_two f  have hcast : Odd ((acceptingCount f : ℤ)) ↔ Odd (acceptingCount f) := by    have hem : ((↑(acceptingCount f) : ℤ) % 2) = (↑(acceptingCount f % 2) : ℤ) := by      have hh := Int.natCast_emod (acceptingCount f) 2      simpa using hh    rw [Int.odd_iff, Nat.odd_iff, hem]    exact Nat.cast_eq_one  have hodd : Odd (acceptingCount f) ↔ r = 1 := by    rw [hpar, ← hcast, Int.odd_iff]  unfold explicitError  by_cases ho : Odd (acceptingCount f)  · rw [if_pos ho, hodd.mp ho]    push_cast    ring  · rw [if_neg ho]    rcases hr01 with h | h    · rw [h]; norm_num    · exact absurd h (fun hh => ho (hodd.mpr hh))
See it in the graphDiscuss this resultChallenge it