Zulip Chat Archive

Stream: lean4

Topic: changing reducibility for type class inference


Rob Lewis (Jan 06 2024 at 19:39):

We're facing an issue where semireducible List functions in the library are blocking type class inference, similar to this MWE:

@[reducible]
def myLength : List Nat  Nat
| [] => 0
| _::t => 1 + myLength t

#check (1 : Fin 2) -- works
#check  (1 :  Fin (myLength [3, 4])) -- works
#check  (1 :  Fin (List.length [3, 4])) -- fails

In Lean 3 I would have locally changed the reducibility of List.length but Lean 4 doesn't allow this. Is there a canonical workaround, outside of redefining the relevant functions with @[reducible]?

Adam Topaz (Jan 07 2024 at 17:53):

In this case you could introduce something like the following (possibly inline in the problematic decl):

instance {i} : OfNat (Fin (List.length (x :: xs))) i :=
  show OfNat (Fin (Nat.succ _)) _ from inferInstance

Adam Topaz (Jan 07 2024 at 17:54):

Of course that doesn't answer the actual question.

Eric Wieser (Jan 07 2024 at 18:38):

For this specific case you can also fix things with:

instance : NeZero (List.length <| .cons x xs) := by simp

(with import Mathlib.Data.Fin.Basic)

Rob Lewis (Jan 07 2024 at 19:35):

Thanks. Unfortunately I'm not sure either of these translate to our actual use case :sad: We have a large class of these problems, some too long to reasonably unfold by hand, and they show up in definitions theorem statements that we expect users to write.

Eric Wieser (Jan 07 2024 at 21:33):

Does no_index help at all here?

Rob Lewis (Jan 07 2024 at 21:58):

I can't actually find any documentation about no_index, but if I understand correctly what it does, then no. (The Nat in this mwe isn't relevant.)

Mario Carneiro (Jan 09 2024 at 05:36):

This came up in iris-lean and it was addressed by just copy pasting all the list defs as reducible

Jannis Limperg (Jan 09 2024 at 10:26):

Is it even allowed to make recursive definitions reducible? I think Gabriel once told me that Lean assumes only nonrecursive definitions are reducible. The question came up in a discussion about discrimination trees, so maybe discrimination tree indexing relies on this invariant for correctness.

Mario Carneiro (Jan 09 2024 at 14:01):

you can put @[reducible] def on a recursive def, but not abbrev IIRC

Gabriel Ebner (Jan 10 2024 at 18:55):

Yes, iirc the discrimination tree module eagerly unfolds reducible definitions. So if you declare an instance for NeZero (a :: b).lengthReducible, then it wouldn't apply to NeZero (a :: b :: c).lengthReducible because the two are reduced to NeZero a.lengthReducible.succ and NeZero c.lengthReducible.succ.succ as a preprocessing step.


Last updated: May 02 2025 at 03:31 UTC