[Haskell-beginners] Re: Re: testing and the culture of Haskell
Maciej Piechotka
uzytkownik2 at gmail.com
Thu Jan 21 03:39:11 EST 2010
On Wed, 2010-01-20 at 18:13 -0500, Isaac Dupree wrote:
> Heinrich Apfelmus wrote:
> > However, in a sense, one can interpret the pure in purely functional as
> > a property of the type constructor (->) , i.e. a function of type (a ->
> > b) is guaranteed to not have side effects. In this light, ML and Scheme
> > are lacking a very important type: they only have functions with side
> > effects.
>
> > Or more precisely, their type system does not distinguish
> > between functions with and without side effects.
>
> that's not actually more precise! Haskell doesn't entirely do that
> either. consider:
> f :: Int -> IO Int
> f x = return (x + 1)
> No side effects! But other ->IO typed values do yield side-effects.
>
> It is exactly as if ML and Scheme contain only the type of (->) composed
> with IO. (Well, actually Scheme is dynamically typed so it's a bit
> silly to say so..) (and Scheme allows functions with zero arguments,
> which are different from its non-side-effecting values.. not sure about ML)
>
> -Isaac
Hmm. Is it possible to distinguish them?
Consider:
f :: Integer -> IO Integer
f x = if cond
then putStrLn "Hello World!" >> return x
else return (x + 1)
where cond = ...
As far as I remember it is impossible to determine in general if cond
will ever be true[1]. Hence it is unknown in general if f is pure or
have side-effects.
Generally IO marks that function have side effects so there should be no
point in testing if it has none (although it may be worth to check if
only the specific side effects occurred). So it rather (forgetting about
unsafe* functions): whenever v is not a IO it has no effects[2].
Regards
[1] However as Int is bounded it is possible for it
[2] I am not sure how correct it is but I think about a -> IO b as
function from a to some sub-program returning b with haskell program
being combination of programs rather then a unpure function from a to b.
More information about the Beginners
mailing list