Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Equivalent of IO Monad in other functional   languages?
      (Heinrich Apfelmus)
   2. Re:  Equivalent of IO Monad in other functional   languages?
      (Heinrich Apfelmus)
   3.  testing IO code (Maurizio Vitale)
   4. Re:  Equivalent of IO Monad in other functional   languages?
      (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   5. Re:  Equivalent of IO Monad in other functional languages?
      (Frerich Raabe)
   6. Re:  Equivalent of IO Monad in other functional languages?
      (Simon Kitching)
   7. Re:  Equivalent of IO Monad in other functional languages?
      (Simon Kitching)


----------------------------------------------------------------------

Message: 1
Date: Mon, 16 Mar 2015 15:04:08 +0100
From: Heinrich Apfelmus <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Equivalent of IO Monad in other
        functional      languages?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:
> On 15 March 2015 at 23:19, <[email protected]> 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



------------------------------

Message: 2
Date: Mon, 16 Mar 2015 15:11:15 +0100
From: Heinrich Apfelmus <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Equivalent of IO Monad in other
        functional      languages?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

Karl Voelker wrote:
> On Sun, Mar 15, 2015, at 09:17 AM, Simon Kitching wrote:
>> And AIUI the Haskell compiler/runtime can postpone evaluation of any 
>> function (laziness), or reorder function calls whever it thinks this 
>> good.
> 
> It's not that it "can" but that it must. Haskell's evaluation strategy
> is a part of the language specification.
> 
> Consider this expression:
> 
> let f = 1 : f in take 5 f
> 
> You can paste this into ghci and feel confident that it's going to
> terminate. You're not at the whim of the runtime.
> 
> I think you may find these articles interesting:
> 
> https://wiki.haskell.org/Lazy_evaluation
> https://wiki.haskell.org/Non-strict_semantics

Strictly speaking, the Haskell language standard only specifies that 
Haskell has non-strict semantics, not that it needs to be evaluated 
using lazy evaluation.

Of course, the two are related. I have tried to expand on the details here:

   https://hackhands.com/non-strict-semantics-haskell


Best regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



------------------------------

Message: 3
Date: Mon, 16 Mar 2015 10:21:33 -0400
From: Maurizio Vitale <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] testing IO code
Message-ID:
        <caaelbqjmb0vkceszoawykt0pzreyocl1wcwhxt0qppqjwcu...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

suppose I have a restricted IO monad, RIO that only exposes readFile.
and then I have a monad SIO that will eventually provide a virtual file
system from a map path->content, also with a readFile function returning
SIO(String).

What is the way to write a function parseFile that can operate in both
monads so that I can use SIO for testing? should I define a third monad
CompileMonad that has instances for both RIO and SIO and then having
parseFile :: CompileMonad ast?

Thanks,

  Maurizio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150316/6aad3062/attachment-0001.html>

------------------------------

Message: 4
Date: Mon, 16 Mar 2015 19:54:41 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Equivalent of IO Monad in other
        functional      languages?
Message-ID:
        <CAJbEW8MFCdJuXArwei9oY5a6ZxAgVaKduy+tV=9xdcza6_2...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi Heinrich,

Thanks for confirming my concepts. It took me a long time pondering over
what it meant to be purely functional.
Great articles btw (+1).

On 16 March 2015 at 19:41, Heinrich Apfelmus <[email protected]>
wrote:

> Karl Voelker wrote:
>
>> On Sun, Mar 15, 2015, at 09:17 AM, Simon Kitching wrote:
>>
>>> And AIUI the Haskell compiler/runtime can postpone evaluation of any
>>> function (laziness), or reorder function calls whever it thinks this good.
>>>
>>
>> It's not that it "can" but that it must. Haskell's evaluation strategy
>> is a part of the language specification.
>>
>> Consider this expression:
>>
>> let f = 1 : f in take 5 f
>>
>> You can paste this into ghci and feel confident that it's going to
>> terminate. You're not at the whim of the runtime.
>>
>> I think you may find these articles interesting:
>>
>> https://wiki.haskell.org/Lazy_evaluation
>> https://wiki.haskell.org/Non-strict_semantics
>>
>
> Strictly speaking, the Haskell language standard only specifies that
> Haskell has non-strict semantics, not that it needs to be evaluated using
> lazy evaluation.
>
> Of course, the two are related. I have tried to expand on the details here:
>
>   https://hackhands.com/non-strict-semantics-haskell
>
>
> Best regards,
> Heinrich Apfelmus
>
> --
> http://apfelmus.nfshost.com
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150316/7330894c/attachment-0001.html>

------------------------------

Message: 5
Date: Mon, 16 Mar 2015 15:34:41 +0100
From: Frerich Raabe <[email protected]>
To: [email protected], The Haskell-Beginners Mailing
        List - Discussion of primarily beginner-level topics related to
        Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Equivalent of IO Monad in other
        functional languages?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

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, <[email protected]> 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)" <[email protected]> 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 <[email protected]> 
>> 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
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners [3]
>> 
>> --
>> 
>> Regards
>> 
>> Sumit Sahrawat
> 
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-- 
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing


------------------------------

Message: 6
Date: Mon, 16 Mar 2015 15:35:33 +0100
From: Simon Kitching <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Equivalent of IO Monad in other
        functional languages?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed

On 03/16/2015 03:04 PM, Heinrich Apfelmus wrote:
> Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:
>> On 15 March 2015 at 23:19, <[email protected]> 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 -> ()

Maybe I should have phrased my original question differently. Let me try 
again..

In an imperative language, you can write something like the following 
pseudocode:

    String transform(String name) {
        print "enter greeting:"
        greeting = read-line-from-stdin
        return greeting + " " + name
    }

 From the "outside", there is no way to tell whether the function 
"transform" performs IO, mutates global state, mutates
its parameter, or anything else. And this is just _normal_ for 
imperative-programming; if you want to write a unit-test for
a method, you need to somehow _know_ what external state it might depend 
on/affect. And compilers cannot reorder
or omit functions because any function might have side-effects.

In Haskell, the function signature clearly indicates if this kind of 
thing is happening - any function like the above will have
a return type of IO, thus clearly separating code with side-effects from 
code without side-effects.

I was wondering what other functional or partly-functional languages 
(OCaml, F#, Scheme etc) do - do they also give the
user of a function a way to tell whether the function has IO 
side-effects (reading/writing) or does the user just need to
"consult the documentation" or "read the source code"?

Thanks,
Simon


------------------------------

Message: 7
Date: Mon, 16 Mar 2015 15:45:20 +0100
From: Simon Kitching <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Equivalent of IO Monad in other
        functional languages?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed

On 03/16/2015 03:04 PM, Heinrich Apfelmus wrote:
> Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:
>> On 15 March 2015 at 23:19, <[email protected]> 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 -> ()

Doesn't "pure" correspond to "can write a unit test for"? When a 
function's return value only ever depends on its inputs, then I can
write a set of test-cases for various inputs, and assert that the 
return-value has the expected content.

A function that reads from a file, stdin, etc. cannot be tested in the 
same way; I cannot _assert_ that the returned value has specific content.

  Sumit stated earlier that IO is "a pure program that can be executed", 
which seems similar to your description above (which I admit I
don't yet 100% understand). How can I "assert" anything about this 
"program", or make a Haskell-based application 'safer' in any way by
leveraging this kind of 'purity'?

Thanks,
Simon


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 81, Issue 45
*****************************************

Reply via email to