2010-05-24 42 views
3

我仍在學習Haskell,需要類型推理的幫助!類型簽名「也許一個」不喜歡「只是[事件]」

使用包SDL和揚帕 我從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允許我使用任何東西,前夕n a SDL.Event list? 爲什麼期望Maybe()當型簽名實際上是Maybe a? 爲什麼它需要一個空的元組,或者一個沒有參數的函數,或者()應該是什麼?

回答

8

type signature of reactimate

IO a        -- # myInit 
-> (Bool -> IO (DTime, Maybe a)) -- # myInput 
-> (Bool -> b -> IO Bool)   -- # myOutput 
-> SF a b       -- # mySF 
-> IO() 

同樣ab必須匹配,這意味着如果你的myInput有類型Bool -> IO (DTime, Maybe [SDL.Event]),則所有其他a也必須是[SDL.Event]。因此,相匹配的類型,你需要確保

myInit :: IO [SDL.Event]  -- # **not** IO(). 
mySF :: SF [SDL.Event] b 

BTW,()unit type

+0

最好把它稱爲*單位類型,至少在這種情況下。 – Martijn 2010-05-24 11:49:23