[Haskell-beginners] Equivalent of IO Monad in other functional languages?
Frerich Raabe
raabe at froglogic.com
Mon Mar 16 14:34:41 UTC 2015
Hi,
I also used to share this "Every function in Haskell is pure, as long as it
does not use any of the unsafe* functions" view but by now I'm wondering
whether that's really a useful definition. It may be true that
putStrLn :: String -> IO ()
is pure in the sense that it only yields a 'recipe' for printing the given
string instead of actually printing it. However, I think it's important to
realize that putStrLn *could* actually yield a poisoned recipe, something
which not *only* prints a string but which also plays a sound at 4AM. And the
types won't let you know whether this is the case.
Hence, even though putStrLn may be pure in the classical 'same input yields
same output' sense, I wondr whether it's more useful to consider putStrLn to
be impure on the grounds that a value of type 'IO a' means "anything can
happen, and whatever happens doesn't necessarily only depend on the
arguments".
On 2015-03-15 18:57, Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:
> 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.
>
> 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"
>>
>> Tom
>>
>> El Mar 15, 2015, a las 13:15, "Sumit Sahrawat, Maths & Computing, IIT
>> (BHU)" <sumit.sahrawat.apm13 at iitbhu.ac.in> escribió:
>>
>> Hello Simon,
>>
>> If you changed your perspective, you would realize that all functions in
>> haskell are pure.
>> A function is pure if it returns the same output if given the same input.
>> Every monadic function (including functions returning IO) is also
>> pure.
>> For example,
>>
>> putStrLn :: String -> IO ()
>> -- A function that takes a string, and returns an impure computation
>> -- which, when executed will print the given String.
>>
>> For any string, putStrLn applied to that same string always describes the
>> same impure computation, thus the function is actually pure.
>> I am not familiar with any other functional language, but there are not
>> many purely functional ones out there [1].
>> I guess the impure ones get around this issue by giving in to impurity, but
>> I'm not sure.
>>
>> I'll be interested in hearing more about the other languages too.
>>
>> [1] :
>> http://en.wikipedia.org/wiki/List_of_programming_languages_by_type#Pure [1]
>>
>> On 15 March 2015 at 22:25, Marcin Mrotek <marcin.jan.mrotek at gmail.com>
>> wrote:
>> Hello,
>>
>> F* uses a somewhat similar approach: https://fstar-lang.org/tutorial/ [2]
>> (section 2, Types and Effects)
>>
>> Best regards,
>> Marcin Mrotek
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners [3]
>>
>> --
>>
>> Regards
>>
>> Sumit Sahrawat
>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners [3]
>
> --
>
> Regards
>
> Sumit Sahrawat
>
> Links:
> ------
> [1] http://en.wikipedia.org/wiki/List_of_programming_languages_by_type#Pure
> [2] https://fstar-lang.org/tutorial/
> [3] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
--
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing
More information about the Beginners
mailing list