[Haskell-cafe] flipped IO sequence

Dougal Stanton dougal at dougalstanton.net
Wed Oct 1 06:39:43 EDT 2008


2008/10/1 Cetin Sert <cetin.sert at gmail.com>:
> warn :: String → IO Int
> warn = return 1 << putStrLn            -- causes an error
>   -- = \msg → return 1 << putStrLn msg -- works just fine
>   -- = \msg → putStrLn msg >> return 1 -- works just fine
>
> (<<) :: Monad m ⇒ m b → m a → m b
> b << a = a >>= \_ → b
>
> Why do I get this compile-time error?? How can one define << ?
>
> cetin at linux-d312:~/lab/test/qths/p> ghc -fglasgow-exts -O2 -o d64x --make
> demo2.hs system.hs
> [1 of 2] Compiling Netman.System    ( system.hs, system.o )
>
> system.hs:23:14:
>     No instance for (Num (IO Int))
>       arising from the literal `1' at system.hs:23:14
>     Possible fix: add an instance declaration for (Num (IO Int))
>     In the first argument of `return', namely `1'
>     In the first argument of `(<<)', namely `return 1'
>     In the expression: return 1 << putStrLn
>

This works for me (type signature added so GHCi doesn't choke)

Prelude> let (<<) = flip (>>) :: IO b -> IO a -> IO b

And thus:

Prelude> return 1 << putStrLn "yo"
yo
1
Prelude>

You might be having problems with the point-free code:

Prelude> let warn' = return 1 << putStrLn

<interactive>:1:24:
    Couldn't match expected type `IO a'
           against inferred type `String -> IO ()'
    In the second argument of `(<<)', namely `putStrLn'
    In the expression: return 1 << putStrLn
    In the definition of `warn'': warn' = return 1 << putStrLn

Adding in variable names straightens that out for me:

Prelude> let warn s = return 1 << putStrLn s
Prelude> warn "help"
help
1
Prelude>


Cheers,


D


More information about the Haskell-Cafe mailing list