This
newtype ( Show a , Show b , Show c ) => T a b c = T Int
t :: T a b c -> a -> b -> c -> String
t ( T x ) a b c = show a ++ show b ++ show c
gives me an error:
No instance for (Show c)
arising from a use of `show'
In the second argument of `(++)', namely `show c'
In the second argument of `(++)', namely `show b ++ show c'
In the expression: show a ++ show b ++ show c
But this
newtype ( Show a , Show b , Show c ) => T a b c = T Int
t :: ( Show a , Show b , Show c ) => T a b c -> a -> b -> c -> String
t ( T x ) a b c = show a ++ show b ++ show c
compiles.
Why?
In the first case, doesn't "T a b c" already imply that "( Show a , Show b , Show c )"? Why is it necessary to explicitly specify the constraint?
No, putting a context on a data (newtype) definition never did quite what one might expect. It only changes the type of the constructor when constructing values, nothing new happens when pattern matching. It's a basically useless feature and it has been removed in the latest version of Haskell.
What you expect is quite reasonable, but it's not what data type contexts do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With