Reader Monad和类型变量

时间:2012-06-25 22:29:40

标签: haskell

我有以下内容:

type KEY = (IPv4, Integer)
type TPSQ = TVar (PSQ.PSQ KEY POSIXTime)
type TMap a = TVar (Map.Map KEY [a])

data Qcfg a = Qcfg { qthresh :: Int, tdelay :: Rational, cwpsq :: TPSQ, cwmap :: TMap a
, cwchan :: TChan String }

getTMap = do
   c <- ask
   return (cwmap c)

并收到有关Reader Monad的错误:

No instance for (MonadReader (Qcfg a2) m2)
      arising from a use of `ask'
    Possible fix:
      add an instance declaration for (MonadReader (Qcfg a2) m2)
    In a stmt of a 'do' expression: c <- ask
    In the expression:
      do { c <- ask;
           return (cwmap c) }
    In an equation for `getTMap':
        getTMap
          = do { c <- ask;
                 return (cwmap c) }

我还没有充分了解Reader Monad尚未确定如何解决这个问题。

[编辑] 添加类型签名适用于涉及类型变量的部分,但会为所有其他部分创建错误。 E.g。

getTMap :: Reader (Qcfg a) (TMap a)
getTMap = do
   c <- ask
   return (cwmap c)

getTPsq :: Reader (Qcfg a) TPSQ
getTPsq = do
   c <- ask
   return (cwpsq c)
...
let q = getTPsq 
 qT <- atomically $ readTVar q

结果

   Couldn't match expected type `TVar a0'
                with actual type `ReaderT
                                    (Qcfg a1) Data.Functor.Identity.Identity TPSQ'
    Expected type: TVar a0
      Actual type: Reader (Qcfg a1) TPSQ
    In the first argument of `readTVar', namely `q'
    In the second argument of `($)', namely `readTVar q'

1 个答案:

答案 0 :(得分:5)

您只需要提供类型签名:

getTMap :: Reader (Qcfg a) (TMap a)
getTMap = do
  c <- ask
  return (cwmap c)
相关问题