Baojun Wang | 18 Jun 02:50 2014
Picon

return with mutable object

Hi List,

Per my understanding, return x would make a new copy of the object. What if the returned object is mutable? Will this make a new (mutable) object? 

My concern is if I created a very large mutable object, does return mutable make a full copy of the original mutable data, or just copy a reference (pointer?)?

Thanks
baojun
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Baojun Wang | 18 Jun 02:53 2014
Picon

Re: return with mutable object

To make my question more clearer, will test1/test2 have noticeable performance difference?

-- mutable1.hs

import qualified Data.Vector.Mutable as MV


import Control.Monad

import Control.Monad.Primitive


a1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a1 v = do

 -- do something

 return ()


a2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a2 v = do

 -- do something else

 return ()


b1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a)

b2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a)


b1 v = do

 -- do something different

 return v


b2 v = do

 -- do something else different

 return v


test1 :: IO ()

test1 = do

 v1 <- MV.replicate 1000 0

 a1 v1

 a2 v1

 return ()


test2 :: IO ()

test2 =

 MV.replicate 1000 0 >>= b1 >>= b2 >> return () -- I'd prefer this way cause it's more haskell.





On Tue, Jun 17, 2014 at 5:50 PM, Baojun Wang <wangbj <at> gmail.com> wrote:
Hi List,

Per my understanding, return x would make a new copy of the object. What if the returned object is mutable? Will this make a new (mutable) object? 

My concern is if I created a very large mutable object, does return mutable make a full copy of the original mutable data, or just copy a reference (pointer?)?

Thanks
baojun

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Bob Ippolito | 18 Jun 03:15 2014

Re: return with mutable object

Your understanding is not correct. The monad laws would be violated If return did make a new mutable object. http://www.haskell.org/haskellwiki/Monad_laws

Side-effects typically have a `m ()` return type, so your "more Haskell" way is not idiomatic Haskell.



On Tue, Jun 17, 2014 at 7:53 PM, Baojun Wang <wangbj <at> gmail.com> wrote:
To make my question more clearer, will test1/test2 have noticeable performance difference?

-- mutable1.hs

import qualified Data.Vector.Mutable as MV


import Control.Monad

import Control.Monad.Primitive


a1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a1 v = do

 -- do something

 return ()


a2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a2 v = do

 -- do something else

 return ()


b1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a)

b2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a)


b1 v = do

 -- do something different

 return v


b2 v = do

 -- do something else different

 return v


test1 :: IO ()

test1 = do

 v1 <- MV.replicate 1000 0

 a1 v1

 a2 v1

 return ()


test2 :: IO ()

test2 =

 MV.replicate 1000 0 >>= b1 >>= b2 >> return () -- I'd prefer this way cause it's more haskell.





On Tue, Jun 17, 2014 at 5:50 PM, Baojun Wang <wangbj <at> gmail.com> wrote:
Hi List,

Per my understanding, return x would make a new copy of the object. What if the returned object is mutable? Will this make a new (mutable) object? 

My concern is if I created a very large mutable object, does return mutable make a full copy of the original mutable data, or just copy a reference (pointer?)?

Thanks
baojun


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Baojun Wang | 18 Jun 07:28 2014
Picon

Re: return with mutable object

Thanks a lot for the correction, I guess the right identity law would be violated, right? I was think about the type constructor stuff, next time should definitely think about the laws first.

On Tuesday, June 17, 2014, Bob Ippolito <bob <at> redivi.com> wrote:

Your understanding is not correct. The monad laws would be violated If return did make a new mutable object. http://www.haskell.org/haskellwiki/Monad_laws

Side-effects typically have a `m ()` return type, so your "more Haskell" way is not idiomatic Haskell.



On Tue, Jun 17, 2014 at 7:53 PM, Baojun Wang <wangbj <at> gmail.com> wrote:
To make my question more clearer, will test1/test2 have noticeable performance difference?

-- mutable1.hs

import qualified Data.Vector.Mutable as MV


import Control.Monad

import Control.Monad.Primitive


a1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a1 v = do

 -- do something

 return ()


a2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a2 v = do

 -- do something else

 return ()


b1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a)

b2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a)


b1 v = do

 -- do something different

 return v


b2 v = do

 -- do something else different

 return v


test1 :: IO ()

test1 = do

 v1 <- MV.replicate 1000 0

 a1 v1

 a2 v1

 return ()


test2 :: IO ()

test2 =

 MV.replicate 1000 0 >>= b1 >>= b2 >> return () -- I'd prefer this way cause it's more haskell.





On Tue, Jun 17, 2014 at 5:50 PM, Baojun Wang <wangbj <at> gmail.com> wrote:
Hi List,

Per my understanding, return x would make a new copy of the object. What if the returned object is mutable? Will this make a new (mutable) object? 

My concern is if I created a very large mutable object, does return mutable make a full copy of the original mutable data, or just copy a reference (pointer?)?

Thanks
baojun


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Gregory Collins | 18 Jun 08:29 2014
Picon

Re: return with mutable object

On Wed, Jun 18, 2014 at 2:53 AM, Baojun Wang <wangbj <at> gmail.com> wrote:
To make my question more clearer, will test1/test2 have noticeable performance difference?

No. The copy is by reference.

-- 
Gregory Collins <greg <at> gregorycollins.net>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane