ReaderT 的 Ask 未正确推导 Monad 类型

Sar*_*nan 2 haskell functional-programming purescript

我正在尝试用纯脚本中的 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"] 
Run Code Online (Sandbox Code Playgroud)

抛出的错误是

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
Run Code Online (Sandbox Code Playgroud)

为什么询问不能自动扣除类型...我对 FP 很陌生,我是否做错了什么?为什么它说不能将单元类型与字符串匹配。我没有在任何地方使用 Unit

Wil*_*sem 5

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

--                                           Unit