Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The choice of character for unidirectional patterns isn't the greatest, I'll agree.

The PatternSynonyms extension enables two new top-level declarations, both declared with "pattern".

    pattern NestedJust x = Just (Just x)
    pattern NestedNothing = Just Nothing
That's the easier syntax of a bidirectional pattern synonym. They can be used both as patterns and as expressions.

    foo :: Maybe (Maybe Int) -> Bool
    foo (NestedJust 7)  = True
    foo (NestedNothing) = False
    foo _               = False

    bar :: Maybe (Maybe String)
    bar = NestedJust "hello"
There are a couple terrible examples. Bidirectional pattern synonyms must be valid as both expressions and pattern matches, or you get a compile error

My example was using unidirectional pattern synonyms. They allow any pattern match whatsoever as the body; the expression restriction is dropped because they aren't allowed to be used in expression contexts. They are defined using <- instead of =.

    pattern MinView x m <- (minView -> Just (x, m))
That's functionally the same as the :< example above, but possibly a bit simpler to read, because the precedence of the symbols is more obvious. It defines a pattern synonym named MinView that is translated to the view pattern in parens.

It's quite possible you were conflating view pattern syntax with pattern guard syntax as the source of your confusion.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: