Why can't I define this pattern synonym?
pattern Double a = (a,a)
error:
• Conflicting definitions for ‘a’
Bound at: <interactive>:1:21
<interactive>:1:23
• In a pattern synonym declaration
This could only work by equality-comparing the two elements of the tuple, but patterns don't just automatically spawn Eq
constraints for this purpose.
You can get this functionality by using a view pattern in the definition:
{-# LANGUAGE PatternSynonyms, ViewPatterns #-}
fromRedunTuple :: Eq a => (a,a) -> Maybe a
fromRedunTuple (x,y)
| x==y = Just x
| otherwise = Nothing
pattern Double a <- (fromRedunTuple -> Just a)
where Double a = (a,a)