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:  'Simple' function (Mike Houghton)
   2. Re:  'Simple' function (Marcin Mrotek)
   3. Re:  'Simple' function (Yannis Juglaret)
   4. Re:  'Simple' function (Rein Henrichs)
   5. Re:  'Simple' function (Rein Henrichs)


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

Message: 1
Date: Wed, 10 Jun 2015 18:50:04 +0100
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] 'Simple' function
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Thanks for all the replies! 
It?s become a little clearer. However?
(again this is naive begginer stuff.. )
if the signature is

asString :: IO String -> String

why is this not a pure function? The IO string has already been  supplied - 
maybe via keyboard input - and so
for the same IO String the function will always return the  same value. Surely 
this behaviour is different to a monadic function that
reads the keyboard and its  output (rather than the input) could be different. 
ie if I give asString an input of   IO ?myString? then it will always return 
?myString? every time I invoke it with IO ?myString?

Many thanks

Mike




> On 10 Jun 2015, at 18:20, Imants Cekusins <[email protected]> wrote:
> 
> Mike, if you are trying to run a "hello world" program in ghci, here
> are 2 working functions.
> 
> -- #1 : all it does is prompts for input and sends the value back to IO
> 
> module Text where
> 
> ioStr :: IO()
> ioStr = do
>  putStrLn "enter anything"
>  str <- getLine
>  putStrLn str
> 
> 
> -- #2 this program prepends the string you pass to it as an arg with "Hello"
> 
> str2str:: String -> String
> str2str s = "Hello " ++ s
> 
> 
> -- how to run:
> -- #1 : ioStr
> -- #2 : str2str "some text"
> 
> hope this helps
> 
> 
> On 10 June 2015 at 19:08, aldiyen <[email protected]> wrote:
>> And just as a note, you can't really ever get the value inside the IO monad 
>> out. IO is not pure / non-deterministic, since it depends on something 
>> outside the program, and there's no way to "make it pure", as it were. You 
>> have to do all your operations on that String within the context of an IO
>> 
>> -aldiyen
>> 
>> 
>> 
>>> On Jun 10, 2015, at 12:47, Steven Williams <[email protected]> 
>>> wrote:
>>> 
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>> 
>>> Here is return's type signature:
>>> 
>>> return :: Monad m => a -> m a
>>> 
>>> What you are doing with the do notation can also be expressed as ioStr
>>>>> = (\str -> return str).
>>> 
>>> do notation and bind both require you to have a value that has the
>>> same monad as before.
>>> 
>>> Steven Williams
>>> My PGP Key: http://pgp.mit.edu/pks/lookup?op=get&search=0xCACA6C74669A54
>>> FA
>>> 
>>>> On 10/06/15 12:35, Mike Houghton wrote:
>>>> Hi,
>>>> 
>>>> I?ve been tryimg to write a function  with signature
>>>> 
>>>> asString :: IO String -> String
>>>> 
>>>> 
>>>> Does someone please have the patience to explain to me what the
>>>> compiler error messages really mean for these two attempts and
>>>> exactly what I?m doing (!!!) If I *do not* give this function any
>>>> type signature then it works i.e..
>>>> 
>>>> asString ioStr = do str <- ioStr return $ str
>>>> 
>>>> and the compiler tells me its signature is
>>>> 
>>>> asString :: forall (m :: * -> *) b. Monad m => m b -> m b
>>>> 
>>>> which, at this stage of my Haskell progress, is just pure Voodoo.
>>>> Why isn?t it?s signature  asString :: IO String -> String ?
>>>> 
>>>> 
>>>> Another naive attempt is asString ioStr = str where str <- ioStr
>>>> 
>>>> and then compiler says parse error on input ?<-?
>>>> 
>>>> 
>>>> Many Thanks
>>>> 
>>>> Mike
>>>> 
>>>> _______________________________________________ Beginners mailing
>>>> list [email protected]
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>> -----BEGIN PGP SIGNATURE-----
>>> Version: GnuPG v2
>>> 
>>> iQIcBAEBAgAGBQJVeGo5AAoJEMrKbHRmmlT6N8UP/i/tAhDtyHiG3sgH3e5xAqyt
>>> JAsyX2JaBQVjERRVaJQy1+Pg9hNdGBCrVljxY0BH5B8np956bnuIEyZKtSc2i2Jc
>>> HM0lBesyzCYqw29QxAyFFno07iXQllocZaHUIgC4AoNYO5zNGSPYcNaB4O5SYoKl
>>> 83Cjz97BHgAHkvHpsLDLOpizOkP+CsXwi8s/KRKoidLkbQpmv9SpqiFvmm9u+UK1
>>> emZF/4veFE4Ay3AvIsxMpn7M5hVoKgat1xyGX02IrenvkOL69IIYc+4OvzK49Lxg
>>> e8jrAehJDMh+U7zN+qVCY1ZyJbJF+uGawFC+XoswOdAra+Q23te77RKkligkmN7s
>>> ACut72hwTejZN/sIaORqZXuy+HUY1LjlJnlz0RCdG1CLkr3EaKG5ZCX3E2N8RnxL
>>> 1CKtEdtFJGDeBcIBh5my/7IC22loTpVhBhPU2DPo+iOP2sRsUs0nllbqbjGfGpuE
>>> m37dR/tfq9FKwqYS5RUuAcZ8fWuPdojmO2WvI4thHBGJhsRK4gqhAI4MnKLHBEoL
>>> xfyHSaoFif/jC7peF/+ZPjKSsIpCJU+R/tDUBM9u22o3IVeTs1sWGZXM7J32tlGc
>>> K/MTF/F3phcxwSCqb99WBHhXOIkKSgp47gx1INgDZFug/CgjUI1Sl4jvZ5j/45D5
>>> +RlHcYv+qp4J8nI59pFW
>>> =Vunc
>>> -----END PGP SIGNATURE-----
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



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

Message: 2
Date: Wed, 10 Jun 2015 20:15:41 +0200
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] 'Simple' function
Message-ID:
        <CAJcfPz=5PDhqQKUPh8QYF7rbkTzyNYCpzer=u8vs9mjnhxq...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hello,

The key word here is "if I give asSting an input of IO "myString". Any
function that returns and IO "myString" (this is really a misnomer, an
(IO String) is a black box that stores the string obtained from an
external source, but in an implementation-dependent,
not-visible-to-an-user way) can do an arbitrary interaction with the
real world to obtain the value. It can look at the keyborad input,
read a file, connect to some networked resource, etc. That's why, this
is not a pure function because every time you call it, it may return a
different string. Or simply put, there's no legal way to write a
function with that signature (okay, you can make it happen by using
the unsafePerformIO function, but beware, it's called "unsafe" for a
reason. It can cause a whole lot of seemingly "magic", unexpected
behavior. In general, don't use it unless you really know it won't
bite you) I don't know how to explain it more thoroughly. If you want
to get deeper into Haskell, it could be said that ANY Haskell function
or value is "pure" - for example an (IO String) value is always the
same, well, (IO String) value, but the String "inside" may differ
(just like a (Maybe String) or [String] is a wholly different beast
than a mere String)

If you want to connect pure functions to impure function do it the
other way around - promote pure functions to IO (or any other monad).
For example, you can use any pure function when using do notation
easily. You can also compose any function of type (a -> b) with return
to create a function (a -> m b). IO, being  a monad, also implements
many type classes from here: https://wiki.haskell.org/Typeclassopedia
so you can turn a (a -> b) function into a (IO a -> IO b) function
with just a fmap or <$>, for example.

Best regards,
Marcin Mrotek


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

Message: 3
Date: Wed, 10 Jun 2015 20:22:20 +0200
From: Yannis Juglaret <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] 'Simple' function
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Assuming

  asString :: IO String -> String

we have

  getLine :: IO String

  asString getLine :: String

Yet

  asString getLine

could be "Hello" the first time you use it, then "Hi" the second time
you use it. Same argument, different result, so this is not a pure
function.

- -- Yannis

On 10/06/2015 19:50, Mike Houghton wrote:
> Thanks for all the replies! It?s become a little clearer. However? 
> (again this is naive begginer stuff.. ) if the signature is
> 
> asString :: IO String -> String
> 
> why is this not a pure function? The IO string has already been
> supplied - maybe via keyboard input - and so for the same IO String
> the function will always return the  same value. Surely this
> behaviour is different to a monadic function that reads the
> keyboard and its  output (rather than the input) could be
> different. ie if I give asString an input of   IO ?myString? then
> it will always return ?myString? every time I invoke it with IO
> ?myString?
> 
> Many thanks
> 
> Mike
> 
> 
> 
> 
>> On 10 Jun 2015, at 18:20, Imants Cekusins <[email protected]>
>> wrote:
>> 
>> Mike, if you are trying to run a "hello world" program in ghci,
>> here are 2 working functions.
>> 
>> -- #1 : all it does is prompts for input and sends the value back
>> to IO
>> 
>> module Text where
>> 
>> ioStr :: IO() ioStr = do putStrLn "enter anything" str <-
>> getLine putStrLn str
>> 
>> 
>> -- #2 this program prepends the string you pass to it as an arg
>> with "Hello"
>> 
>> str2str:: String -> String str2str s = "Hello " ++ s
>> 
>> 
>> -- how to run: -- #1 : ioStr -- #2 : str2str "some text"
>> 
>> hope this helps
>> 
>> 
>> On 10 June 2015 at 19:08, aldiyen <[email protected]> wrote:
>>> And just as a note, you can't really ever get the value inside
>>> the IO monad out. IO is not pure / non-deterministic, since it
>>> depends on something outside the program, and there's no way to
>>> "make it pure", as it were. You have to do all your operations
>>> on that String within the context of an IO
>>> 
>>> -aldiyen
>>> 
>>> 
>>> 
>>>> On Jun 10, 2015, at 12:47, Steven Williams
>>>> <[email protected]> wrote:
>>>> 
> Here is return's type signature:
> 
> return :: Monad m => a -> m a
> 
> What you are doing with the do notation can also be expressed as
> ioStr
>>>>>>> = (\str -> return str).
> 
> do notation and bind both require you to have a value that has the 
> same monad as before.
> 
> Steven Williams My PGP Key:
> http://pgp.mit.edu/pks/lookup?op=get&search=0xCACA6C74669A54 FA
> 
>>>>>> On 10/06/15 12:35, Mike Houghton wrote: Hi,
>>>>>> 
>>>>>> I?ve been tryimg to write a function  with signature
>>>>>> 
>>>>>> asString :: IO String -> String
>>>>>> 
>>>>>> 
>>>>>> Does someone please have the patience to explain to me
>>>>>> what the compiler error messages really mean for these
>>>>>> two attempts and exactly what I?m doing (!!!) If I *do
>>>>>> not* give this function any type signature then it works
>>>>>> i.e..
>>>>>> 
>>>>>> asString ioStr = do str <- ioStr return $ str
>>>>>> 
>>>>>> and the compiler tells me its signature is
>>>>>> 
>>>>>> asString :: forall (m :: * -> *) b. Monad m => m b -> m
>>>>>> b
>>>>>> 
>>>>>> which, at this stage of my Haskell progress, is just pure
>>>>>> Voodoo. Why isn?t it?s signature  asString :: IO String
>>>>>> -> String ?
>>>>>> 
>>>>>> 
>>>>>> Another naive attempt is asString ioStr = str where str
>>>>>> <- ioStr
>>>>>> 
>>>>>> and then compiler says parse error on input ?<-?
>>>>>> 
>>>>>> 
>>>>>> Many Thanks
>>>>>> 
>>>>>> Mike
>>>>>> 
>>>>>> _______________________________________________ Beginners
>>>>>> mailing list [email protected] 
>>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>
>>>>>> 
_______________________________________________
>>>> Beginners mailing list [email protected] 
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>> _______________________________________________ Beginners
>>> mailing list [email protected] 
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________ Beginners mailing
>> list [email protected] 
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 
> _______________________________________________ Beginners mailing
> list [email protected] 
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 

- -- 
Yannis JUGLARET


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

Message: 4
Date: Wed, 10 Jun 2015 18:46:32 +0000
From: Rein Henrichs <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] 'Simple' function
Message-ID:
        <CAJp6G8wjEG2SVUVbu9+ABbz2c3oKq6OjGw4x3zVTc7VPz0i=q...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Jun 10, 2015 at 10:50 AM Mike Houghton <[email protected]>
wrote:

> The IO string has already been  supplied - maybe via keyboard input -
>

A value of type IO String has been provided, but a value of type IO String *is
not a string.* In much the same way that `ls` is not a list of files, but
rather a recipe for retrieving a list of files, a value of type IO String
is a not a String but a recipe for performing IO that will retrieve a
String once it is executed. It is important to remember is that *evaluating
an IO action does not execute it*. Only the runtime system can do that, and
the only IO action the runtime system executes is main (which can, of
course, be composed of many other IO actions by using, e.g., the Monad
interface).

You can't write a function of type IO String -> String that "retrieves" the
string because there is no string to retrieve. There is only a recipe that
must be *executed* and that execution must stay within the IO context.

(It is possible to write this function using unsafePerformIO precisely
because unsafePerformIO instructs the runtime to ignore its usual safety
mechanisms (that ensure that IO actions can be used with in a safe, pure,
and referentially transparent way) and force the execution of the IO action
as part of its evalulation and, as the name suggests, this is unsafe for a
number of reasons and should only be used when the programmer is willing to
take on the obligation to prove that they are using it in safe way.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150610/a4efb8a7/attachment-0001.html>

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

Message: 5
Date: Wed, 10 Jun 2015 18:55:22 +0000
From: Rein Henrichs <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] 'Simple' function
Message-ID:
        <CAJp6G8wr9iC5BgdQduKyt=mwmbdsqizug_yecf9h+_1nmt5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Jun 10, 2015 at 11:15 AM Marcin Mrotek <[email protected]>
wrote:

> That's why, this is not a pure function because every time you call it, it
> may return a different string.
>

This is a common source of confusion. A value of type IO a for some a is
not an impure function because it is not a function. Its *evaluation* is
completely pure and referentially transparent: every time you evaluate
`getLine`, you get the same IO String value. The only observable difference
is under execution, but *we don't expect execution to be pure*: we only
expect evaluation to be pure.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150610/bc7bc991/attachment.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 84, Issue 18
*****************************************

Reply via email to