Skip to content
Discussions/App Development/What's the precendence of `@`?Forum ↗

What's the precendence of `@`?

App Development4 posts304 views7 likesLast activity Aug 2020
BE
bernhardOP
Aug 2020

What’s the precendence of @ in pattern matching?

foo@bar::[] seems to be (foo@bar)::[], not the foo@[bar] equivalent foo@(bar::[]) that I was expecting.

LU
Luciano
Aug 2020

It might be because :: binds lower than function application? Like you said foo@[bar] would work, but that’s kind of like foo@((::) bar ()).

I’m sure someone will have a definitive answer … :thinking:

CO
cocreature
Aug 2020

The definitive answer here is the section on pattern matching in the Haskell 2010 standard.

But nobody likes reading standards so let me try a more readable summary:

  • var@pat binds var to pat if pat has arity 0 so takes no arguments (e.g., a variable or True)
  • Something like var @ Some x is a syntax error, you have to parenthesize it and use var @ (Some x) so this is always unambiguous.
  • For infix patterns like :: or `C` for some constructor C, the infix pattern has the lowest precedence so
    foo@bar::[] parses as (foo@bar)::[] just like Some x::[] parses as (Some x)::[].
BE
bernhard
Aug 2020

Thanks for that summary. Having now had a look the standard, I have to say it’s not just a matter of not reading it. I’m not sure I could have derived your summary from it.

← Back to Discussions