Zulip Chat Archive

Stream: lean4

Topic: monadic bind ignoring type annotation


Jakob von Raumer (Feb 25 2025 at 10:19):

Is it intended if I write something like

def foo : Nat := Id.run <| do
  let x : Int × Int  do
    pure ((5 : Nat), (6 : Nat))
  pure x.1

the annotation for x just gets ignored? (In that no cast is applied) I would have expected the above snippet to fail like

def bar : Nat := Id.run <| do
  let x : Int × Int :=
    ((5 : Nat), (6 : Nat))
  pure x.1

does

Jakob von Raumer (Feb 25 2025 at 10:22):

(I mean, there's no coercion from Nat × Nat to Int × Int, so I'd expect the error to be at the second line, not at the fourth line)

Jakob von Raumer (Feb 25 2025 at 15:53):

Maybe slightly unrelated but also on the topic of type ascriptions on monadic lets: Why is it a syntax error if I attempt a let pattern on a monadic let:

def foo : Id Unit := do
  let (x, y) : Nat × Nat  do
    pure (3, 4)
  pure ()

Jakob von Raumer (Feb 25 2025 at 15:54):

This works if I expand it to

def foo : Id Unit := do
  let (x, y) : Nat × Nat :=  do
    pure (3, 4)
  pure ()

Eric Wieser (Feb 25 2025 at 16:20):

lean4#3126:

set_option autoImplicit false

example : IO Unit := do
  let x : IAmIgnored  do pure 1
  return

Jakob von Raumer (Feb 25 2025 at 19:05):

Do you think the matching issue counts as a separate bug?

Eric Wieser (Feb 25 2025 at 20:13):

Yes, probably worth filing a separate bug

Eric Wieser (Feb 25 2025 at 20:13):

This works fine too:

  let ((x, y) : Nat × Nat)  do
    pure (3, 4)

Jakob von Raumer (Feb 26 2025 at 07:52):

(deleted)

Jakob von Raumer (Feb 26 2025 at 08:06):

But somehow this only works without the brackets:

instance : CoeT (BitVec n) x (BitVec m) where
  coe := x.setWidth m

def foo (x : Nat) : Id Unit := do
  let ((z, b) : ((BitVec x) × Bool)) :=
    match x with
    | 16 => (0#16, false)
    | n => (0#n, false)
  pure ()

I guess because putting brackets in a let binder performs a casting after elaborating the body?

Jakob von Raumer (Feb 26 2025 at 08:11):

(This probably isn't a bug, just shows that having brackets there changes the elaboration, sometimes in unfavourable ways)


Last updated: May 02 2025 at 03:31 UTC