Version 3 (modified by 12 years ago) (diff) | ,
---|

# Pattern Guards

**Ticket:** #56

See ExtensionDescriptionHowto for information on how to write these extension descriptions. Please add any new extensions to the list of HaskellExtensions.

## Brief Explanation

The syntax for guards is changed to

gd→`|`

qual_{1}`,`

…`,`

qual_{n}

(Haskell 98 allows only a single, Boolean, guard.)
In pattern guards of the form *p* `<-`

*e*, the two sides must have the same type, and if the value if *e* does not match *p*, the guards fails.

## References

- Case Expressions and Formal Semantics of Pattern Matching in the Haskell 98 Report
- Simon's original proposal, 1997.
- Pattern guards in the GHC User's Guide
- Pattern Guards and Transformational Patterns, Martin Erwig and Simon Peyton Jones, Haskell Workshop, 2000.

## Pros

- avoids awkward
`case`

constructs in certain situations - a relatively small and orthogonal extension
- the required changes to the Report are clearly laid out in the above paper.

## Cons

- the re-use of monadic binding notation from list comprehensions and
`do`

-notation for pure matching is confusing, but the more rational`=`

would create parsing difficulties, e.g.f p | x = y = z