Why can\'t I do this:
import Data.Char
getBool = do
c <- getChar
if c == \'t\'
then IO True
else IO False
instead of using
You can use IO
instead of return
. But not such easy. And you also need to import some inner modules.
Let's look at source of Control.Monad
:
instance Monad IO where
{-# INLINE return #-}
{-# INLINE (>>) #-}
{-# INLINE (>>=) #-}
m >> k = m >>= \ _ -> k
return = returnIO
(>>=) = bindIO
fail s = failIO s
returnIO :: a -> IO a
returnIO x = IO $ \ s -> (# s, x #)
But even to use IO
instead of return
, you need to import GHC.Types(IO(..))
:
newtype IO a = IO (State# RealWorld -> (# State# RealWorld, a #))
After this, you can write IO $ \ s -> (# s, True #)
(IO
is a State) instead of return True
:
Solution:
{-# LANGUAGE UnboxedTuples #-} -- for unboxed tuples (# a, b #)
{-# LANGUAGE TupleSections #-} -- then (,b) == \a -> (a, b)
import GHC.Types (IO (..))
import Data.Char
getBool = do
c <- getChar
if c == 't'
then IO (# , True #)
else IO (# , False #)