Zulip Chat Archive

Stream: lean4

Topic: Confusing error with computed field


François G. Dorais (Jun 04 2024 at 18:43):

This snippet

inductive RankedList (α : Type _) where
  | nil : RankedList α
  | cons : α  RankedList α  RankedList α
with
  @[computed_field] rank : RankedList α  Nat
  | .nil => 0
  | .cons _ l => l.rank + 1

gives me this error (v4.8.0-rc2)

application type mismatch
  RankedList.rank α
argument
  α
has type
  Type u_1 : Type (u_1 + 1)
but is expected to have type
  RankedList ?α : Type ?u.1293

Apparently computed fields don't work for inductives with parameters or indices? If that's the case, the error message could be more useful.

Number Eighteen (Jun 07 2024 at 21:29):

I don'tknow what a computed feld is; can you lnk to any documentation?

François G. Dorais (Jun 07 2024 at 22:43):

It's in core: Lean.Elab.ComputedFields, documentation is there.

Kyle Miller (Jun 07 2024 at 22:57):

It looks like you can get it to work:

inductive RankedList (α : Type _) where
  | nil : RankedList α
  | cons : α  RankedList α  RankedList α
with
  @[computed_field] rank : (α : Type _)  RankedList α  Nat
  | _, .nil => 0
  | _, .cons _ l => l.rank + 1

Eric Wieser (Jun 07 2024 at 23:07):

Does this mean that the computed field can be specialized to certain types?

François G. Dorais (Jun 07 2024 at 23:38):

I didn't think about that variation. Thanks! Sadly, this fails:

inductive RankedList (α : Type _) where
  | nil : RankedList α
  | cons : α  RankedList α  RankedList α
with
  @[computed_field] rank : {α : Type _}  RankedList α  Nat
  | _, .nil => 0
  | _, .cons _ l => l.rank + 1

I think we're homing in on the core issue!


Last updated: May 02 2025 at 03:31 UTC