## return with mutable object

2014-06-18 00:50:18 GMT

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

18 Jun 02:50 2014

Baojun Wang <wangbj <at> gmail.com>

2014-06-18 00:50:18 GMT

2014-06-18 00:50:18 GMT

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

18 Jun 02:53 2014

Baojun Wang <wangbj <at> gmail.com>

2014-06-18 00:53:24 GMT

2014-06-18 00:53:24 GMT

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?)?Thanksbaojun

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

18 Jun 03:15 2014

Bob Ippolito <bob <at> redivi.com>

2014-06-18 01:15:45 GMT

2014-06-18 01:15:45 GMT

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.hsimport 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?)?Thanksbaojun

_______________________________________________

Haskell-Cafe mailing list

Haskell-Cafe <at> haskell.org

http://www.haskell.org/mailman/listinfo/haskell-cafe

18 Jun 07:28 2014

Baojun Wang <wangbj <at> gmail.com>

2014-06-18 05:28:43 GMT

2014-06-18 05:28:43 GMT

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.hsimport 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.

Hi List,

Thanksbaojun

_______________________________________________

Haskell-Cafe mailing list

Haskell-Cafe <at> haskell.org

http://www.haskell.org/mailman/listinfo/haskell-cafe

18 Jun 08:29 2014

Gregory Collins <greg <at> gregorycollins.net>

2014-06-18 06:29:27 GMT

2014-06-18 06:29:27 GMT

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>
Return

Return to gmane.comp.lang.haskell.cafe.

Advertisement

Search Archive

Language

Options

Current view:
Threads only / Showing
whole messages /
Not hiding cited text.

Change to All messages, shortened messages, or hide cited text.

Post a message

NNTP Newsgroup

Classic Gmane web interface

RSS Feed

List Information

About Gmane

Change to All messages, shortened messages, or hide cited text.

Post a message

NNTP Newsgroup

Classic Gmane web interface

RSS Feed

List Information

About Gmane