[Haskell-cafe] return with mutable object

Bob Ippolito bob at redivi.com
Wed Jun 18 01:15:45 UTC 2014


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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140617/25b553eb/attachment.html>


More information about the Haskell-Cafe mailing list