简体   繁体   English

阅读器Monad和类型变量

[英]Reader Monad and type variable

I have the following: 我有以下几点:

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)

and get an error concerning the Reader Monad: 并收到有关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) }

I have not enough understanding about the Reader Monad yet to determine how to fix this best. 我对Reader Monad的了解还不够多,因此无法确定最佳解决方法。

[Edit] Adding a type signature works for the parts that involve the type variable but creates an error for all the rest. [编辑]添加类型签名适用于涉及类型变量的部分,但其余所有部分均会产生错误。 Eg 例如

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

Results in 结果是

   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'

You just need to provide a type signature: 您只需要提供类型签名:

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

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

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