Zulip Chat Archive

Stream: lean4

Topic: initialize structure with IO.Ref


Adam Topaz (Dec 21 2025 at 23:09):

What's the correct way to make the following work?

structure Foo where
  ref : IO.Ref Nat

def mkFoo : IO Foo := do
  let ref : IO.Ref Nat  IO.mkRef 0
  return { ref }

initialize foo : Foo  mkFoo
/-
failed to synthesize
  Inhabited Foo
-/

For comparison, the following is just fine:

initialize foo : IO.Ref Nat  IO.mkRef 0

and the following naive things fail:

structure Foo where
  ref : IO.Ref Nat
deriving Inhabited -- fails

#synth Inhabited (IO.Ref Nat) -- fails

Robin Arnez (Dec 21 2025 at 23:11):

You need deriving Nonempty here

Adam Topaz (Dec 21 2025 at 23:12):

Ah ok.

Eric Wieser (Dec 22 2025 at 04:49):

This could probably do with a better error message; I've been tripped up by the same one

Eric Wieser (Dec 22 2025 at 04:59):

Somehow it seems to be bypassing this code

Kim Morrison (Jan 06 2026 at 23:41):

https://github.com/leanprover/lean4/pull/11919 improves the error message


Last updated: Feb 28 2026 at 14:05 UTC