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: May 02 2025 at 03:31 UTC