Zulip Chat Archive

Stream: new members

Topic: Mutable variables with dependent types


Evgeny Kurnevsky (Aug 28 2024 at 15:44):

Hi. Is it possible to assign mutable variables with dependent types inside a do notation? Here is an example what I'm trying to do:

def f := Id.run do
  let mut x₁, x₂, prop: Σ' x y: Nat, x = y := 1, 1, rfl
  repeat
    x₁, x₂, prop := x₁.succ, x₂.succ, Nat.succ_inj'.mpr prop
    if x₁ > 5 then
      break
  return x₁

It doesn't compile because of some type mismatch. Maybe I just miss some part of lean syntax? :)

It works if I assign them into a single variable like:

def f := Id.run do
  let mut x: Σ' x y: Nat, x = y := 1, 1, rfl
  repeat
    x := x.fst.succ, x.snd.fst.succ, Nat.succ_inj'.mpr x.snd.snd
    if x.fst > 5 then
      break
  return x.fst

It's less readable.

Chris Bailey (Aug 28 2024 at 17:18):

Will you settle for this:

def f := Id.run do
  let mut psig@⟨x₁, x₂, prop: Σ' x y: Nat, x = y := 1, 1, rfl
  repeat
    psig := x₁.succ, x₂.succ, Nat.succ_inj'.mpr prop
    if x₁ > 5 then
      break
  return x₁

Chris Bailey (Aug 28 2024 at 17:31):

Oh boy, that makes a really subtle change to the evaluation.

Evgeny Kurnevsky (Aug 28 2024 at 17:34):

Yeah, unfortunately it doesn't change x₁ outside the loop, otherwise it would look good :)

Evgeny Kurnevsky (Aug 28 2024 at 17:46):

And if I change it to

def f := Id.run do
  let mut psig@x₁, x₂, prop⟩: Σ' x y: Nat, x = y := 1, 1, rfl
  repeat
    psig := x₁.succ, x₂.succ, Nat.succ_inj'.mpr prop
    if x₁ > 5 then
      break
  return psig.fst

it just gets stuck in the loop :)

Chris Bailey (Aug 28 2024 at 17:52):

Yeah that behavior of the @- binding is rough. I would almost prefer that use of pattern bindings to be prohibited if it's going to create a second copy of the underlying value behind the scenes.

Eric Wieser (Aug 28 2024 at 22:07):

I think I saw a proposal somewhere to force pattern bindings to be spelt with have instead


Last updated: May 02 2025 at 03:31 UTC