简体   繁体   English

如何使用 syb mkM

[英]How to use syb mkM

I just discovered power of syb library and trying to find its limits.我刚刚发现了 syb 库的强大功能并试图找到它的极限。

I've got everywhere working: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 and mkM look similar and I don't see the reason why mkM is not working for printMyInt 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

even pure甚至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 and syb 0.7.2.1 ghc 8.10.7 和 syb 0.7.2.1

printMyInt has type MyInt -> IO () . printMyInt类型为printMyInt MyInt -> IO ()

mkM expects an argument of type b -> mb . mkM需要类型为b -> mb的参数。

There is a mismatch because MyInt is not () .存在不匹配,因为MyInt不是()

You can write你可以写

mkM (\x -> printMyInt x >> pure x)

mkM pure is ambiguous. mkM pure是模棱两可的。 mkM :: (Typeable a, Typeable b) => (b -> mb) -> a -> ma has two type parameters, a and b , and b is undetermined in mkM pure . mkM :: (Typeable a, Typeable b) => (b -> mb) -> a -> ma有两个类型参数abbmkM pure是不确定的。 It would not be a problem if b were not constrained, but here there is a Typeable b constraint that cannot be solved without fixing b .如果b不受约束,这将不是问题,但是这里有一个Typeable b约束,如果不修复b就无法解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM