I am using a graphics library in Haskell called Threepenny-GUI . In this library, the main function returns a UI monad object. This causes me a big headache when I try to unpack IO values โโinto local variables. I get errors complaining about different types of monad.
Here is an example of my problem. This is a slightly modified version of the standard core function, as shown in the 3penny-GUI code example:
main :: IO () main = startGUI defaultConfig setup setup :: Window -> UI () setup w = do labelsAndValues <- shuffle [1..10] shuffle :: [Int] -> IO [Int] shuffle [] = return [] shuffle xs = do randomPosition <- getStdRandom (randomR (0, length xs - 1)) let (left, (a:right)) = splitAt randomPosition xs fmap (a:) (shuffle (left ++ right))
Pay attention to the fifth line:
labelsAndValues <- shuffle [1..10]
It returns the following error:
Couldn't match type 'IO' with 'UI' Expected type: UI [Int] Actual type: IO [Int] In a stmt of a 'do' block: labelsAndValues <- shuffle [1 .. 10]
As for my question, how can I unpack the IO function using the standard arrow notation ( <- ), and continue to have these variables as IO () , not UI () , so I can easily pass them to other functions.
Currently, the only solution I found was to use liftIO , but this leads to conversion to the UI monad type, while I actually want to continue using the IO type.
source share