[Haskell-cafe] Question on proper use of Data.IORef

2012-06-22 Thread Captain Freako
Hi experts,


I fear I don't understand how to properly use *Data.IORef*.
I wrote the following code:


  1 -- Testing Data.IORef
  2 module Main where
  3
  4 import Data.IORef
  5
  6 bump :: IORef Int - IO()
  7 bump theRef = do
  8 tmp - readIORef theRef
  9 let tmp2 = tmp + 1
 10 writeIORef theRef tmp2
 11
 12 main = do
 13 let theValue = 1
 14 print theValue
 15 theValueRef - newIORef theValue
 16 bump theValueRef
 17 return theValue


and got this, in ghci:


*Main :load test2.hs
[1 of 1] Compiling Main ( test2.hs, interpreted )
Ok, modules loaded: Main.
*Main main
1
*1*


I was expecting this:


*Main :load test2.hs
[1 of 1] Compiling Main ( test2.hs, interpreted )
Ok, modules loaded: Main.
*Main main
1
*2*


Can anyone help me understand what I'm doing wrong?


Thanks!
-db
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Question on proper use of Data.IORef

2012-06-22 Thread Adam Smith
theValueRef isn't a pointer to theValue that you can use to somehow change
theValue (which is immutable).
theValueRef is a reference to a box that contains a totally separate,
mutable value.

When you use newIORef to create theValueRef, it's *copying* theValue into
the box. When you mutate theValueRef, you're mutating the value inside the
box - theValue remains unchanged.

Cheers,
Adam

On 22 June 2012 11:30, Captain Freako capn.fre...@gmail.com wrote:

 Hi experts,


 I fear I don't understand how to properly use *Data.IORef*.
 I wrote the following code:


   1 -- Testing Data.IORef
   2 module Main where
   3
   4 import Data.IORef
   5
   6 bump :: IORef Int - IO()
   7 bump theRef = do
   8 tmp - readIORef theRef
   9 let tmp2 = tmp + 1
  10 writeIORef theRef tmp2
  11
  12 main = do
  13 let theValue = 1
  14 print theValue
  15 theValueRef - newIORef theValue
  16 bump theValueRef
  17 return theValue


 and got this, in ghci:


 *Main :load test2.hs
 [1 of 1] Compiling Main ( test2.hs, interpreted )
 Ok, modules loaded: Main.
 *Main main
 1
 *1*


 I was expecting this:


 *Main :load test2.hs
 [1 of 1] Compiling Main ( test2.hs, interpreted )
 Ok, modules loaded: Main.
 *Main main
 1
 *2*


 Can anyone help me understand what I'm doing wrong?


 Thanks!
 -db

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Question on proper use of Data.IORef

2012-06-22 Thread Max Rabkin
On Fri, Jun 22, 2012 at 5:30 PM, Captain Freako capn.fre...@gmail.com wrote:
  12 main = do
  13 let theValue = 1
  14 print theValue
  15 theValueRef - newIORef theValue
  16 bump theValueRef
  17 return theValue

theValue is a plain old immutable Haskell variable. newIORef creates
an IORef whose initial value is equal to the argument; it doesn't
create a pointer to the value or something like that. Change return
theValue to readIORef theValueRef to extract the changed value in
the IORef.

--Max

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Question on proper use of Data.IORef

2012-06-22 Thread Captain Freako
Okay, I get it now. Thanks to all of you for your quick replies!

So, here's what I need to do:

   1. My Haskell code gets called by a higher level C function and asked to
   initialize itself.
   2. As part of that initialization, I'm expected to return a pointer to
   an instance of a particular data structure, which gets created/initialized
   in Haskell.
   3. I create a stable pointer to a data structure instance, using *
   newStablePtr*, and return that pointer to the calling C function.
   4. The same C function then, at a later time, calls a different function
   in my Haskell code, sending it that very same pointer, and expects that
   code to modify the data structure pointed to, in place.

So, I wrote some code that looks like this:

secondFunction :: StablePtr DataStructure - IO ()
secondFunction dsPtr = do
ds - deRefStablePtr dsPtr
theRef - newIORef ds
writeIORef theRef newDs

which, of course, didn't work, because I didn't understand the true nature
of IORef.

*So, my question is: How do I do this? That is, how do I modify, in place,
a data structure, which I originally created and made stable, using a
pointer to that structure, which is being passed back to me, by the higher
level C function that is calling me?*

Thanks, all!
-db


On Fri, Jun 22, 2012 at 8:43 AM, Max Rabkin max.rab...@gmail.com wrote:


 On Fri, Jun 22, 2012 at 5:30 PM, Captain Freako capn.fre...@gmail.com
 wrote:
   12 main = do
   13 let theValue = 1
   14 print theValue
   15 theValueRef - newIORef theValue
   16 bump theValueRef
   17 return theValue


 theValue is a plain old immutable Haskell variable. newIORef creates
 an IORef whose initial value is equal to the argument; it doesn't
 create a pointer to the value or something like that. Change return
 theValue to readIORef theValueRef to extract the changed value in
 the IORef.

 --Max


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Question on proper use of Data.IORef

2012-06-22 Thread Antoine Latter
On Fri, Jun 22, 2012 at 11:18 AM, Captain Freako capn.fre...@gmail.com wrote:
 Okay, I get it now. Thanks to all of you for your quick replies!

 So, here's what I need to do:

 My Haskell code gets called by a higher level C function and asked to
 initialize itself.
 As part of that initialization, I'm expected to return a pointer to an
 instance of a particular data structure, which gets created/initialized in
 Haskell.
 I create a stable pointer to a data structure instance, using newStablePtr,
 and return that pointer to the calling C function.
 The same C function then, at a later time, calls a different function in my
 Haskell code, sending it that very same pointer, and expects that code to
 modify the data structure pointed to, in place.

 So, I wrote some code that looks like this:

 secondFunction :: StablePtr DataStructure - IO ()
 secondFunction dsPtr = do
     ds - deRefStablePtr dsPtr
     theRef - newIORef ds
     writeIORef theRef newDs

 which, of course, didn't work, because I didn't understand the true nature
 of IORef.

 So, my question is: How do I do this? That is, how do I modify, in place, a
 data structure, which I originally created and made stable, using a pointer
 to that structure, which is being passed back to me, by the higher level C
 function that is calling me?


You could use a StablePtr (IORef DataStructure).

 Thanks, all!
 -db


 On Fri, Jun 22, 2012 at 8:43 AM, Max Rabkin max.rab...@gmail.com wrote:


 On Fri, Jun 22, 2012 at 5:30 PM, Captain Freako capn.fre...@gmail.com
 wrote:
   12 main = do
   13 let theValue = 1
   14 print theValue
   15 theValueRef - newIORef theValue
   16 bump theValueRef
   17 return theValue


 theValue is a plain old immutable Haskell variable. newIORef creates
 an IORef whose initial value is equal to the argument; it doesn't
 create a pointer to the value or something like that. Change return
 theValue to readIORef theValueRef to extract the changed value in
 the IORef.

 --Max




 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe