Zulip Chat Archive

Stream: lean4

Topic: dependent `guard` syntax?


Asei Inoue (Jan 05 2025 at 20:27):

While it is convenient to use the guard function within a monadic context, how can we assign names to the propositions being guarded, as in the following example?

def bar (x : Nat) (_ : x % 2 == 0) := x

def foo (i : Nat) (arr : Array Nat) : Option Nat := do
  let x  arr.get? i
  if h : x % 2 == 0 then
    some (bar x h)
  else
    none

Eric Wieser (Jan 06 2025 at 09:11):

I found myself needing this the other day, and used

let h <- if h : P then PLift.up h else failure

Eric Wieser (Jan 06 2025 at 09:12):

(in my case I actually used throwError not failure)

Eric Wieser (Jan 06 2025 at 09:12):

Ideally we'd have unless h : P do failure as syntax for this

Edward van de Meent (Jan 06 2025 at 11:48):

i seem to recall someone working on this/something along these lines?

Edward van de Meent (Jan 06 2025 at 11:48):

i feel like there might be a lean issue/pr for it

Edward van de Meent (Jan 06 2025 at 11:56):

unfortunately, i can't find it

Joachim Breitner (Jan 06 2025 at 12:42):

Do you mean https://github.com/leanprover/lean4/issues/5598?

Edward van de Meent (Jan 06 2025 at 13:12):

that might be it, although i also remember this involving if without else... that might just be wishful thinking on my part though :upside_down:

Damiano Testa (Jan 06 2025 at 13:36):

Edward, maybe you were thinking of this or this?

Edward van de Meent (Jan 06 2025 at 13:54):

yes, that second one is it!

Eric Wieser (Jan 06 2025 at 14:41):

I put a snippet you can copy/paste on lean4#5598


Last updated: May 02 2025 at 03:31 UTC