Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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. Using IORef (Courtney Robinson)
2. Re: Using IORef (Brandon Allbery)
3. Re: Using IORef (Courtney Robinson)
4. Re: Using IORef (Brandon Allbery)
5. Re: Using IORef (Courtney Robinson)
----------------------------------------------------------------------
Message: 1
Date: Tue, 7 Jan 2014 02:33:02 +0000
From: Courtney Robinson <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Using IORef
Message-ID:
<cago6xjy3omzotpwu_o9hoxd6olga5gj_u4zpsjndkqfmivb...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
I've found this example.
counter :: IO Int
counter = unsafePerformIO $ newIORef 0
getUnique :: IO Int
getUnique = atomicModifyIORef counter $ \x -> let y = x + 1 in (y, y)
Which I ran, and I know works in as much as calling getUnique multiple
times results in an incremented value.
I have two things I'm failing to see about it.
1) Why doesn't counter return a newIORef with value 0, every time getUnique
is called there by producing a value of 1 no matter how often getUnique is
called? Is this this because it returns an IORef so counter just becomes
bound to the value it first returns and it's not evaluated again and the
IORef is just returned, or something else?
2) Why does the lambda return a tuple and why does the example return the
same value in both positions i.e. (y,y)? The only thing I could think of is
because the name has "atomic" that the values in the tuple is being used
for some sort of compare and swap,CAS.
P.S I didn't find
http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-IORef.html
enlightening
on the matter and I know that example is not great because
the atomicModifyIORef is lazy so could lead to stackoverflow as that doc.
page says...
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140107/9a7da011/attachment-0001.html>
------------------------------
Message: 2
Date: Mon, 6 Jan 2014 22:03:48 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using IORef
Message-ID:
<cakfcl4uagnexynxfz9qw2yijnawsqfb3o4_wmtvp5mcqxax...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Mon, Jan 6, 2014 at 9:33 PM, Courtney Robinson <[email protected]>wrote:
> I've found this example.
>
An example of something you should not do; it is very fragile. Most
notably, if the compiler decides to inline counter then you no longer have
a single IORef. (It will work in ghci/runhaskell, which does not have an
optimizer.)
I have two things I'm failing to see about it.
>
> 1) Why doesn't counter return a newIORef with value 0, every time
> getUnique is called there by producing a value of 1 no matter how often
> getUnique is called? Is this this because it returns an IORef so counter
> just becomes bound to the value it first returns and it's not evaluated
> again and the IORef is just returned, or something else?
>
Anything given a name and no parameters (and not polymorphic; see the
monomorphism restriction) is potentially shared. The unsafePerformIO would
be run only once and its result remembered and shared between all uses ---
if you're lucky.
The compiler is allowed to do anything it wants as long as it would have
the same behavior; but because you hid an effect behind unsafePerformIO,
thereby telling the compiler that it is a pure value that it is free to
remember or recompute as it sees fit, the compiler's idea of what
constitutes "the same behavior" is likely to be different from yours.
2) Why does the lambda return a tuple and why does the example return the
> same value in both positions i.e. (y,y)? The only thing I could think of is
> because the name has "atomic" that the values in the tuple is being used
> for some sort of compare and swap,CAS.
>
One value is the return value of atomicModifyIORef; the other is the new
value to be stored. You could think of this as the Haskell version of the
preincrement/postincrement operators in some languages.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140106/a68345b0/attachment-0001.html>
------------------------------
Message: 3
Date: Tue, 7 Jan 2014 03:15:10 +0000
From: Courtney Robinson <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using IORef
Message-ID:
<CAGo6xJY7HSUsNvr0CWYt1=S=5rbrm3ncemgnm+u1azmkvwn...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery <[email protected]> wrote:
> behind
Oh I see, thanks for the info. really helpful.
It brings about another question now.
How is newIORef meant to be used so that I only have a single IORef?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140107/51559370/attachment-0001.html>
------------------------------
Message: 4
Date: Mon, 6 Jan 2014 22:21:17 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using IORef
Message-ID:
<cakfcl4w63stk9fr1ud1tly_nkbvte6mwq7ulhe4ttduljp4...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Mon, Jan 6, 2014 at 10:15 PM, Courtney Robinson <[email protected]>wrote:
>
> On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery <[email protected]>wrote:
>
>> behind
>
>
> Oh I see, thanks for the info. really helpful.
> It brings about another question now.
>
> How is newIORef meant to be used so that I only have a single IORef?
>
The Haskell way is to carry such things implicitly in a State or Reader
monad; since the IORef itself doesn't change, and you need the IO monad
around anyway to actually use it, you would use a ReaderT IORef IO and then
use ask >>= liftIO . readIORef to get the value and similar to write or
modify it. (Commonly one uses a type or newtype+newtype deriving to make
one's own monad combining them.)
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140106/6efe35b5/attachment-0001.html>
------------------------------
Message: 5
Date: Tue, 7 Jan 2014 03:31:58 +0000
From: Courtney Robinson <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using IORef
Message-ID:
<cago6xjbxg2cde5o8-caoaehz94vojuapzugdd+efwg1j_km...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Cool. I think I get so i'll have a go.
Thanks again
On Tue, Jan 7, 2014 at 3:21 AM, Brandon Allbery <[email protected]> wrote:
> On Mon, Jan 6, 2014 at 10:15 PM, Courtney Robinson <[email protected]>wrote:
>>
>> On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery <[email protected]>wrote:
>>
>>> behind
>>
>>
>> Oh I see, thanks for the info. really helpful.
>> It brings about another question now.
>>
>> How is newIORef meant to be used so that I only have a single IORef?
>>
>
> The Haskell way is to carry such things implicitly in a State or Reader
> monad; since the IORef itself doesn't change, and you need the IO monad
> around anyway to actually use it, you would use a ReaderT IORef IO and then
> use ask >>= liftIO . readIORef to get the value and similar to write or
> modify it. (Commonly one uses a type or newtype+newtype deriving to make
> one's own monad combining them.)
>
> --
> brandon s allbery kf8nh sine nomine
> associates
> [email protected]
> [email protected]
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
Courtney Robinson
[email protected]
http://crlog.info
07535691628 (No private #s)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140107/5ed65aee/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 67, Issue 10
*****************************************