Saravanan
Saravanan

Reputation: 911

Ask of ReaderT is not deducing the Monad type correctly

I'm trying to write code with ReaderT in purescript. But my compiler isn't deducting the type correctly

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

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

The error thrown is

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

Upvotes: 2

Views: 102

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476679

The reason this does not work is because tell has type tell :: (MonadTell w m, Monad m) => w -> m Unit. You use lift :: (MonadTrans t, Monad m) => m a -> t m a, but this will not "return type" a, so the type of your line' is:

--                                           Unit 🖟  🖟
type Doc' = ReaderT Level (Writer (Array String)) Unit

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

Upvotes: 5

Related Questions