[Haskell-beginners] Equivalent of IO Monad in other functional languages?

Heinrich Apfelmus apfelmus at quantentunnel.de
Mon Mar 16 14:04:08 UTC 2015


Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:
> On 15 March 2015 at 23:19, <amindfv at gmail.com> wrote:
> 
>> From that perspective isn't every language pure? Haskell's still got
>> "randomIO" and "print <=< readMVar"
> 
> Haskell is impure only when you use the unsafe* functions (atleast that's
> how I understand purity).
> 
> My understanding is that a language is impure if it allows one to write
> arbitrary IO within a function and still give it a proper (mathematical)
> function type. In other words impurity arises only if you can unwrap the IO
> monad (which is what the unsafe functions do).
> 
> The two examples you give above are pure under such a perspective, but I
> might be wrong.

You're right, indeed. A language is pure if supplying a value `x` of 
type `A` to the a function

     A -> B

will always returns the same result, no matter how often or in which 
order this function is called. This is true for both

     randomIO             :: Random a => IO a
     (readMVar >=> print) :: Show a   => MVar a -> IO ()

because they return an IO action. This action will always be the same 
given the same arguments.

The language would be impure if these functions had the types

     randomIO             :: Random a => a
     (readMVar >=> print) :: Show a   => MVar a -> ()


Best regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list