I was looking for a way to alias the constructor types of the either keyword in order to avoid swapping right and left around, so while looking for things I could do I found the PatternSynonyms language feature, which functionally does exactly what I want it to do, but while checking out how it works in practice I noticed that when pattern matching on the aliased constructors the compiler and HLint both warn me that case ac is “non-exhaustive”. Is there a way around this? is this intended or a bug in how ghc flags warnings?
example code down below
{-# LANGUAGE PatternSynonyms #-}
module Main (main) where
type Result = Either
pattern Error :: e -> Either e a
pattern Error e = Left e
pattern Ok :: a -> Either e a
pattern Ok a = Right a
newtype NotDivisibleError = NotDivisibleError (Int, Int)
tryDiv :: Int -> Int -> Result NotDivisibleError Int
tryDiv a b
| a `mod` b /= 0 = Error $ NotDivisibleError (a, b)
| otherwise = Ok $ a `div` b
main :: IO ()
main = do
let ab = 12 `tryDiv` 6
let ac = 12 `tryDiv` 5
case ab of
Left _ -> putStrLn "couldn't divide"
Right r -> print r
case ac of
Error _ -> putStrLn "couldn't divide"
Ok r -> print r