輸入與Data.PSQueue不匹配

Couldn't match expected type IO t0'
with actual type PSQ.PSQ k0 p0 -> Maybe p0'


{-# LANGUAGE OverloadedStrings #-}

import System.CPUTime

import qualified Data.PSQueue as PSQ

main = do
time <- getCPUTime
let q = PSQ.singleton "msg" time
r <- PSQ.lookup "msg"
print (r)


最佳答案

You've written r <- PSQ.lookup "msg". <- is the syntax to extract a monadic value from within a block of do-notation. You should instead use let r = ..., the syntax to bind pure values.

You also forgot the queue parameter itself. This is what the error message is telling you: the right-hand side of <- has to be of type IO a for some a, but instead it's a function from a PSQ k p to the result of a lookup (Maybe p).

main :: IO ()
main = flip runStateT PSQ.empty $do time <- liftIO getCPUTime modify$ PSQ.insert "msg" time
r <- gets $PSQ.lookup "msg" liftIO . print$ r