2012-06-25 23 views
2

我有以下幾點:讀者單子並輸入變量

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) 

,並得到有關讀者單子的錯誤:

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) } 

我對讀者單子不夠默契還沒有確定如何解決這個問題。

[編輯] 添加類型簽名適用於涉及類型變量的部分,但會爲其餘部分創建錯誤。例如。

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

你可以寫'getTMap =問>> =回報。 cwmap'。 – JJJ

回答

5

你只需要提供一個類型簽名:

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

適用於引用的問題,但導致我陷入其餘的麻煩。編輯了我的問題。 –

+0

(a)當某人提供了答案(b)您沒有提供代碼的許多相關部分時,改變問題是一種糟糕的形式,例如什麼是'TPSQ'? (c)錯誤信息告訴你你的代碼有什麼問題 - 它期待一個'TVar a',但你給它一個'Reader(Qcfg b)TPSQ'。 –

+0

謝謝。在我上面的問題是,它需要讀取'let q = runReader getTPsq qcfg'。 –