Zulip Chat Archive

Stream: general

Topic: typeclasses and binding order


Dean Young (Mar 03 2023 at 22:36):

Supposing I have different instances of a type class, how do I tell Lean to always try matching one before the other?

Arthur Paulino (Mar 03 2023 at 22:38):

In Lean 4 (maybe in Lean 3 as well) you can define (priority := low/high)

Dean Young (Mar 03 2023 at 22:40):

can you link me to an example?

Arthur Paulino (Mar 03 2023 at 22:41):

From Init/SizeOf.lean:

class SizeOf (α : Sort u) where
  sizeOf : α  Nat

export SizeOf (sizeOf)

protected def default.sizeOf (α : Sort u) : α  Nat
  | _ => 0

instance (priority := low) (α : Sort u) : SizeOf α where
  sizeOf := default.sizeOf α

Eric Wieser (Mar 03 2023 at 22:42):

This is generally not a good way to use typeclasses. Mathlib tends to use priority as a form of optimization, not to actually change behavior

Dean Young (Mar 05 2023 at 22:01):

ok I'll keep the binding order to the notation then


Last updated: Dec 20 2023 at 11:08 UTC