Zulip Chat Archive

Stream: Is there code for X?

Topic: Bounded int


Bjørn Kjos-Hanssen (Feb 09 2024 at 19:38):

To get computable decidability instances for statements involving bounded integers, is there a convenient way?
For example, a type FinInt b representing -b,...,b that was recognized as decidable would be good.

The following solution is too kludgy when dealing with vectors of bounded ints.

To remove noncomputable here...

noncomputable instance : Decidable (∀ x : ℤ, abs x < 7 → x = x) := by
  exact Classical.dec (∀ (x : ℤ), |x| < 7 → x = x)

... just make the statement for positive and negative numbers separately:

instance : Decidable (∀ x : ℕ, x < 7 → (-x:ℤ) = -x ∧ (x:ℤ) = (x:ℤ)) :=
  Nat.decidableBallLT 7 fun x h ↦ (-x:ℤ) = -x ∧ (x:ℤ) = (x:ℤ)

Yaël Dillies (Feb 09 2024 at 19:55):

You can use decidable_of_iff (∀ x ∈ Finset.Icc (-7 : ℤ) 7, x = x) your_proof

Kyle Miller (Feb 09 2024 at 19:58):

import Mathlib

instance {n : ℤ} {p : ℤ → Prop} [DecidablePred p] : Decidable (∀ x : ℤ, abs x < n → p x) :=
  decidable_of_iff (∀ x ∈ Finset.Ioo (-n) n, p x) <| by simp [abs_lt]

#synth Decidable (∀ x : ℤ, abs x < 7 → x = x)
-- succeeds

Bjørn Kjos-Hanssen (Feb 09 2024 at 20:00):

Nice. And it works with vectors too, like

instance : Decidable (∀ x : Vector (Finset.Icc (-7:ℤ) 7) 3,  x = x) := by exact
  Fintype.decidableForallFintype

Kyle Miller (Feb 09 2024 at 20:16):

If #synth succeeds, you do not need to write an instance for it.

Both of these succeed:

#synth Decidable (∀ x : Vector (Set.Icc (-7:ℤ) 7) 3, x = x)
#synth Decidable (∀ x : Vector (Finset.Icc (-7:ℤ) 7) 3, x = x)

(I would prefer the first.)


Last updated: Feb 28 2026 at 14:05 UTC