Zulip Chat Archive

Stream: lean4

Topic: Pattern match actions


J. O. (Feb 03 2022 at 01:59):

How do i pattern match on actions, since the manual says that it is TODO

Mario Carneiro (Feb 03 2022 at 02:55):

You are going to have to give more context. What do you mean by "actions"?

Mario Carneiro (Feb 03 2022 at 02:55):

do you have a #mwe with an example of what you want to write?

Chris B (Feb 03 2022 at 03:17):

The do notation section of the manual introduces "action" as a term of art. They define it as function application when the return type is m a for some monad m.

Mario Carneiro (Feb 03 2022 at 03:19):

I suspected that might be the meaning, but it would be weird to pattern match on actions in most cases

Mario Carneiro (Feb 03 2022 at 03:19):

is this talking about lean 3's do some x <- m | return false, ...?

Chris B (Feb 03 2022 at 03:19):

I think the meaning is this deal:

match (<- thing) with
| _ => ...

when thing is of type m α.

Chris B (Feb 03 2022 at 03:20):

Oh yeah there's that too.

Mario Carneiro (Feb 03 2022 at 03:20):

For the version you just wrote I don't see what extra sugar is needed

Mario Carneiro (Feb 03 2022 at 03:22):

lean 4 does have if let some x <- m then ... else return false already, so I guess this is just about the let-else pattern

Chris B (Feb 03 2022 at 03:23):

You should put those in the manual :eyes:

Chris B (Feb 03 2022 at 04:18):

I gave it a shot. https://github.com/leanprover/lean4/pull/990

Marc Huisinga (Feb 03 2022 at 10:40):

Mario Carneiro said:

is this talking about lean 3's do some x <- m | return false, ...?

This works in Lean 4 too:

def bar : Option Nat := some 0

def foo : Bool := Id.run do
  let some x  bar | return false
  return x = 0

Last updated: Dec 20 2023 at 11:08 UTC