我还在学习Haskell,需要帮助进行类型推断!
使用包SDL和Yampa
我从FRP.Yampa.reactimate
获得以下类型签名:
(Bool -> IO (DTime, Maybe a))
我希望将其用于:
myInput :: Bool -> IO (DTime, Maybe [SDL.Event])
myInput isBlocking = do
event <- SDL.pollEvent
return (1, Just [event])
...
reactimate myInit myInput myOutput mySF
但它说
Couldn't match expected type `()' against inferred type `[SDL.Event]' Expected type: IO (DTime, Maybe ()) Inferred type: IO (DTime, Maybe [SDL.Event]) In the second argument of `reactimate', namely `input' In the expression: reactimate initialize input output process
我认为Maybe a
允许我使用任何内容,甚至是SDL.Event
列表?
当类型签名实际为Maybe ()
时,为什么期望Maybe a
?
为什么它需要一个空元组,或一个不带参数的函数,或者()
应该是什么?
答案 0 :(得分:8)
完整的type signature of reactimate
是
IO a -- # myInit
-> (Bool -> IO (DTime, Maybe a)) -- # myInput
-> (Bool -> b -> IO Bool) -- # myOutput
-> SF a b -- # mySF
-> IO ()
相同的a
和b
必须匹配,这意味着如果您的myInput
类型为Bool -> IO (DTime, Maybe [SDL.Event])
,则所有其他a
也必须为{{1} }}。因此,要匹配类型,您需要确保
[SDL.Event]
BTW, myInit :: IO [SDL.Event] -- # **not** IO ().
mySF :: SF [SDL.Event] b
是unit type。