gpt4 book ai didi

haskell - ReaderT 的 Ask 未正确推导 Monad 类型

转载 作者:行者123 更新时间:2023-12-02 01:46:05 24 4
gpt4 key购买 nike

我正在尝试用纯脚本中的 ReaderT 编写代码。但我的编译器没有正确推断类型

type Doc' = ReaderT Level (Writer (Array String)) String

line' :: String -> Doc'
line' input = do
space <- ask -- error line
lift $ tell $ [(power " " space) <> input <> "\n"]

抛出的错误是

Could not match type

Unit

with type

String

while trying to match type t0 t1
with type ReaderT @Type Int (WriterT (Array String) Identity) String
while checking that expression (bind ask) (\space ->
(apply lift) ((...) [ ...
]
)
)
has type ReaderT @Type Int (WriterT (Array String) Identity) String
in value declaration line'

where t0 is an unknown type
t1 is an unknown type

Why the ask can't deduct the type automatically... I'm very new to FP,am I doing something fundamentally wrong?Why its saying cant match type of Unit with String. I'm not using Unit anywhere

最佳答案

这不起作用的原因是 tell 的类型为 tell :: (MonadTell w m, Monad m) => w -> m Unit 。您使用lift :: (MonadTrans t, Monad m) => m a -> t m a ,但这不会“返回类型”a,因此您的line'的类型是:

--                                           Unit 🖟  🖟
type Doc' = ReaderT Level (Writer (Array String)) <strong>Unit</strong>

line' :: String -> Doc'
line' input = do
space <- ask
lift $ tell $ [(power " " space) <> input <> "\n"]

关于haskell - ReaderT 的 Ask 未正确推导 Monad 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70866802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com