[Haskell-beginners] Re: Re: testing and the culture of Haskell

Daniel Fischer daniel.is.fischer at web.de
Sat Jan 23 16:08:14 EST 2010


Am Samstag 23 Januar 2010 21:31:47 schrieb Adrian Adshead:
> Hi Alex,
>
> That does help, but raises a further question.
>
> If  (>>=) :: m a -> (a -> m b) -> m b
>
> Then whenever the IO monad is entered it can not be escaped,
> so in the example you gave:-
>
> f :: IO a -> Int
> f _ = 10
>
>
> while f (putStrLn "foo") is a pure function evaluating to 10,
> neither the IO Action, nor the a (from 'IO a') can be used without
> using the (>>=) or (>>). Therefore since the parameter can not
> be used in the function f without entering the IO monad, your
> function would be equivalent to :-
>
> f :: Int
> f = 10

Rather to

const 10

except with a restricted type.

>
> I have two questions to try and clarify my understanding.
>
> 1) Is it possible to have a function with an IO Action as an input
> parameter (and not just ignore the parameter) and have a non-IO
> return value?
> (ie is     f :: IO a -> b    possible without ignoring the IO a)

Excluding unsafePerformIO, only in a very weak sense.

ioseq :: IO a -> b -> b
ioseq ioa b = ioa `seq` b

doesn't quite ignore the parameter, but apart from that, I only see 
rustyBallpointTracheotomy (Kydos to Brent Yorgey for that :).

>
> 2) Is it possible to have a function which evaluates any IO Action
> within the body and have a non-IO return value?
> (ie is    f :: a -> b     possible while evaluating any IO Action
> within the body.)
>

Assuming by evaluating you mean executing and not 

(return 5 >> return 6) ~> IO (\s -> (s,6))

, no, that's not possible.

>
>
> My understanding of Haskell was that both of these situations
> would not be possible, and the return value would always be
> of the form IO b. (For these questions I am excluding use of
> unsafePerformIO)
>
> Many thanks for your time.
>
> Adrian.




More information about the Beginners mailing list