Zulip Chat Archive

Stream: new members

Topic: Can't display a number wrapped in a structure


Mike Shulman (Nov 17 2022 at 04:37):

The following code:

structure wrap := (t : Type)
def wrapN : wrap := { t :=  }
def x : wrapN.t := (0 : )
#eval x

complains that result type does not have an instance of type class 'has_repr', dumping internal representation and prints #0. Is there a way to make this print something nicer, short of reimplementing has_repr by hand for the structure wrapN?

Eric Wieser (Nov 17 2022 at 09:35):

Reimplementing has_repr by hand is about one line of work:

instance : has_repr wrapN.t := nat.has_repr

Eric Wieser (Nov 17 2022 at 09:36):

attribute [reducible] wrapN is also enough to make this work

Mike Shulman (Nov 17 2022 at 16:38):

But I have to do it for every instance of wrap?

Eric Wieser (Nov 17 2022 at 16:43):

This feels like an #xy problem

Alex J. Best (Nov 17 2022 at 16:43):

What do you mean by instance of wrap (instance is a keyword) are you varying wrapN or the class?

Eric Wieser (Nov 17 2022 at 16:43):

Also your title is misleading; your code example isn't a number wrapped in a struct, this is a type wrapped in a struct which typeclass resolution can't see though.

Eric Wieser (Nov 17 2022 at 16:45):

A "number wrapped in a struct" would be

structure wrap (t : Type) := (val : t)
def x : wrap   := wrap.mk 0

Last updated: Dec 20 2023 at 11:08 UTC