Zulip Chat Archive
Stream: new members
Topic: LE Prop vs Bool
Burkhardt Renz (Feb 17 2025 at 13:18):
I' am confused:
#check 2 ≤ 3 -- Prop
#eval [1, 3, 5].filter (· ≤ 3) -- [1, 3]
but
def myle [LE α] : α → α → Bool :=
λ x y => x ≤ y
is a type mismatch
type mismatch
x ≤ y
has type
Prop : Type
but is expected to have type
Bool : Type
I thought that LE is Decidable and therefore Prop is automatically coerced to Bool if needed??
Eric Wieser (Feb 17 2025 at 13:20):
Burkhardt Renz said:
I thought that LE is Decidable
In general it's not. Your first example only shows it is decidable on Nat
.
suhr (Feb 17 2025 at 13:22):
def myle [LE α][DecidableLE α] : α → α → Bool :=
λ x y => x ≤ y
Burkhardt Renz (Feb 17 2025 at 13:27):
Which type class should I use, when I want to compare Nats, Ints, Strings and so on?
With
def myle [LE α] [DecidableLE α] : α → α → Bool :=
λ x y => x ≤ y
I get
invalid binder annotation, type is not a class instance
?m.5185
Ruben Van de Velde (Feb 17 2025 at 13:27):
Try turning on autoImplicit errors
Eric Wieser (Feb 17 2025 at 13:34):
(set_option autoImplicit false
)
Burkhardt Renz (Feb 17 2025 at 14:10):
I didn't manage to define the function using typeclass LE
My solution now:
def myle {α : Type} [Ord α] : α → α → Bool :=
λ x y => if Ordering.isLE (compare x y) then true else false
What do you think about that approach?
Aaron Liu (Feb 17 2025 at 14:14):
It's fine, but you won't be able to compare Float
s.
#synth Ord Float
Ruben Van de Velde (Feb 17 2025 at 14:14):
What is your goal?
Burkhardt Renz (Feb 17 2025 at 14:16):
My goal is a version of Quicksort that works for all reasonable types, not just Int or Nat
I have a version of Quicksort for Int together with a proof of termination.
Kevin Buzzard (Feb 17 2025 at 14:18):
Then why not just assume DecidableLE
?
Aaron Liu (Feb 17 2025 at 14:20):
That's docs#DecidableLE
Aaron Liu (Feb 17 2025 at 14:22):
You can also try writing decide
explicitly
Burkhardt Renz (Feb 17 2025 at 14:25):
`DecidableLe':
def myle [DecidableLe α] : α → α → Bool :=
λ x y => x ≤ y
error:
invalid binder annotation, type is not a class instance
?m.6720
Aaron Liu (Feb 17 2025 at 14:27):
Your code from above is working fine for me. Can you try putting set_option autoImplicit false
at the top of the file, right after all the imports?
Burkhardt Renz said:
With
def myle [LE α] [DecidableLE α] : α → α → Bool := λ x y => x ≤ y
I get
invalid binder annotation, type is not a class instance ?m.5185
Kevin Buzzard (Feb 17 2025 at 14:28):
Sorry, DecidableLE
not DecidableLe
. You've run into the autoImplicit footgun.
Eric Wieser (Feb 17 2025 at 14:32):
Eric Wieser said:
(
set_option autoImplicit false
)
Please add this line to your code that we suggested earlier
Burkhardt Renz (Feb 17 2025 at 14:37):
I tried with
set_option autoImplicit false
but I get the same error message
I use Lean Version 4.15.0
In Lean 4 Web there is no error message. I try to update Lean.
Eric Wieser (Feb 17 2025 at 14:41):
Yes, your lean version is probably the issue
Eric Wieser (Feb 17 2025 at 14:41):
Burkhardt Renz said:
but I get the same error message
Are you sure? I would expect a different error message
Kevin Buzzard (Feb 17 2025 at 14:47):
@Burkhardt Renz can you please post a #mwe so we are all on the same page?
Aaron Liu (Feb 17 2025 at 14:51):
It looks like DecidableLE
was only added 2 months ago. You can use DecidableRel (LE.le : α → α → Prop)
instead.
Burkhardt Renz (Feb 17 2025 at 14:53):
Sorry for the trouble, and thanks for your help
In the future I will check first on Lean 4 Web!
I updated Lean to Lake version 5.0.0-93d4ae6 (Lean version 4.17.0-rc1)
and th problem is gone. I even don't need the autoImplicit option:
def myle {α : Type} [LE α] [DecidableLE α] : α → α → Bool :=
λ x y => x ≤ y
#eval myle 2 3 -- true
#eval myle 'a' 'z' -- true
#eval myle "aa" "aaa" -- true
#eval myle "b" "aaa" -- false
#eval myle 5.0 4.2 --.false
`` `
Kevin Buzzard (Feb 17 2025 at 15:08):
The autoImplicit option is just to prevent people from getting confusing errors if they make typos :-)
Last updated: May 02 2025 at 03:31 UTC