我刚刚发现了 syb 库的强大功能并试图找到它的限制。
我有everywhere
工作:
> :set -XDeriveDataTypeable
> :set -XGeneralizedNewtypeDeriving
> import Data.Generics
> newtype MyInt = MyInt Int deriving (Show, Eq, Num, Ord, Data)
> incMyInt (MyInt i) = MyInt $ 1 + i
> printMyInt (MyInt i) = putStrLn $ "MyInt = " ++ show i
> :t mkT incMyInt
mkT incMyInt :: Typeable a => a -> a
> :t mkM printMyInt
<interactive>:1:5: error:
• Couldn't match type ‘()’ with ‘MyInt’
Expected type: () -> IO ()
Actual type: MyInt -> IO ()
mkT 和 mkM 看起来很相似,我看不出 mkM 不适用于 printMyInt 的原因
> :t mkM
mkM :: (Monad m, Typeable a, Typeable b) => (b -> m b) -> a -> m a
> :t mkT
mkT :: (Typeable a, Typeable b) => (b -> b) -> a -> a
甚至pure
:t mkM pure
<interactive>:1:1: error:
• Could not deduce (Typeable b0) arising from a use of ‘mkM’
from the context: (Monad m, Typeable a)
bound by the inferred type of
it :: (Monad m, Typeable a) => a -> m a
at <interactive>:1:1
ghc 8.10.7 和 syb 0.7.2.1