Portability | portable |
---|---|

Stability | experimental |

Maintainer | Reiner Pope <reiner.pope@gmail.com> |

Common pattern combinators.

- var :: Pattern (a :*: Nil) a
- __ :: Pat0 a
- (/\) :: Pat2 a a a
- (\/) :: Pattern as a -> Pattern as a -> Pattern as a
- view :: (a -> b) -> Pat1 b a
- tryView :: (a -> Maybe b) -> Pat1 b a
- is :: (a -> Bool) -> Pat0 a
- cst :: Eq a => a -> Pat0 a
- elim :: Clause a r -> a -> r
- mmatch :: Monad m => m a -> Clause a (m b) -> m b
- mk0 :: (a -> Maybe ()) -> Pat0 a
- mk1 :: (a -> Maybe b) -> Pat1 b a
- mk2 :: (a -> Maybe (b, c)) -> Pat2 b c a
- mk3 :: (a -> Maybe (b, c, d)) -> Pat3 b c d a
- mk4 :: (a -> Maybe (b, c, d, e)) -> Pat4 b c d e a
- mk5 :: (a -> Maybe (b, c, d, e, f)) -> Pat5 b c d e f a
- tup0 :: Pat0 ()
- tup1 :: Pat1 a a
- tup2 :: Pat2 a b (a, b)
- tup3 :: Pat3 a b c (a, b, c)
- tup4 :: Pat4 a b c d (a, b, c, d)
- tup5 :: Pat5 a b c d e (a, b, c, d, e)
- left :: Pat1 a (Either a b)
- right :: Pat1 b (Either a b)
- nil :: Pat0 [a]
- cons :: Pat2 a [a] [a]

# Basic patterns

var :: Pattern (a :*: Nil) aSource

"Variable patterns": always succeeds, and binds the value to a variable.

"And patterns". Succeeds only if both patterns succeed.

`(/\) = ``mk2`

(a -> (a,a))

(\/) :: Pattern as a -> Pattern as a -> Pattern as aSource

"Or patterns". If first pattern fails, then tries the second.

view :: (a -> b) -> Pat1 b aSource

"View patterns": do some computation, then pattern match on the result.

# Non-binding patterns

cst :: Eq a => a -> Pat0 aSource

"Constant patterns": tests for equality to the given constant.
`cst x = is (==x)`

# Anonymous matching

elim :: Clause a r -> a -> rSource

`elim = flip ``match`

Useful for anonymous matching (or for building eliminators, like `either`

). For example:

either withLeft withRight = elim $ left var ->> withLeft <|> right var ->> withRight

# Monadic matching

mmatch :: Monad m => m a -> Clause a (m b) -> m bSource

`mmatch m p = m >>= ``elim`

p

Useful for applicative-looking monadic pattern matching, as in

ex7 :: IO () ex7 = mmatch getLine $ cst "" ->> return () <|> var ->> putStrLn . ("You said " ++)

# Smart constructors for patterns

These build patterns from a selector function.