Zulip Chat Archive

Stream: new members

Topic: How to create instance of Decidable?


MrQubo (Apr 29 2025 at 20:02):

I cannot grasp how to create instances of Decidable. Can someone help me for example write this instance?

import Mathlib

variable {α : Type}

def funPos (f : α  Nat) : Prop :=  a : α, f a > 0

instance instDecidable_funPos [FinEnum α] : DecidablePred (@funPos α)
  := λ f 
  let b := (FinEnum.toList α).all (λ a  f a > 0)
  _

I know how to compute the result as Bool. But how to make instance of Decidable from this?

Matthew Jasper (Apr 29 2025 at 20:15):

instance instDecidable_funPos [FinEnum α] : DecidablePred (@funPos α)
  := λ f 
  if h : (FinEnum.toList α).all (λ a  f a > 0) then
    .isTrue (by simpa [funPos] using h)
  else
    .isFalse (by simpa [funPos] using h)

Johannes Tantow (Apr 29 2025 at 20:16):

import Mathlib

variable {α : Type}

def funPos (f : α  Nat) : Prop :=  a : α, f a > 0

instance instDecidable_funPos [FinEnum α] : DecidablePred (@funPos α)
  := fun f =>
    decidable_of_bool
    ((FinEnum.toList α).all (λ a  f a > 0)) (by simp [funPos])

Eric Wieser (Apr 29 2025 at 20:50):

If you replace def with abbrev then optimistically you can use inferInstance as the whole proof

MrQubo (Apr 29 2025 at 21:26):

Eric Wieser said:

If you replace def with abbrev then optimistically you can use inferInstance as the whole proof

You're right, this works:

import Mathlib

variable {α : Type}

abbrev funPos (f : α  Nat) : Prop :=  a : α, f a > 0

instance instDecidable_funPos [FinEnum α] : DecidablePred (@funPos α) := inferInstance

But what is abbrev?

Kevin Buzzard (Apr 29 2025 at 21:27):

a def which Lean (and specifically here, the typeclass system) sees through easily (a reducible inline def)

MrQubo (Apr 29 2025 at 21:28):

So, in this case, typeclass system can replace funPos with it's definition?

Hm, so it seems like, there is somewhere Decidable instance for \forall?

Kevin Buzzard (Apr 29 2025 at 21:29):

You can use #synth to see which instance it's finding.

MrQubo (Apr 29 2025 at 21:37):

Kevin Buzzard said:

You can use #synth to see which instance it's finding.

Thanks! It's λ _ ↦ Fintype.decidableForallFintype.


Last updated: May 02 2025 at 03:31 UTC