4 ms·
I haven't had a look at the extension definition itself, but I find this really hard to read. Can you or someone else give an explanation of what's going on? It
by axman6 13y ago
I haven't had a look at the extension definition itself, but I find this really hard to read. Can you or someone else give an explanation of what's going on? It seems to me that the brackets are all wrong, I would've expected
pattern (x :< m <- minView) -> Just (x,m)
but that's just basically view patterns/pattern guards. Basically I can sort of see what's going on here, but only in this specific case; I can't see how one uses this elsewhere.
- chowells 13y agoThe 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.