Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MonadFix instance for boxed vectors #312

Merged
merged 9 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Data/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ import Control.DeepSeq ( NFData(rnf)
)

import Control.Monad ( MonadPlus(..), liftM, ap )
import Control.Monad.ST ( ST )
import Control.Monad.ST ( ST, runST )
import Control.Monad.Primitive
import qualified Control.Monad.Fail as Fail

import Control.Monad.Fix ( MonadFix (mfix) )
import Control.Monad.Zip
import Data.Function ( fix )

import Prelude hiding ( length, null,
replicate, (++), concat,
Expand Down Expand Up @@ -381,6 +382,24 @@ instance MonadZip Vector where
{-# INLINE munzip #-}
munzip = unzip

instance MonadFix Vector where
-- We take care to dispose of v0 as soon as possible.
-- We also avoid setting up the result vector to refer to
-- itself. These measures should prevent memory leaks.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I no longer know what I meant by "We also avoid setting up the result vector to refer to itself." Perhaps We should just change the above to

-- To prevent memory leaks, we take care to dispose of v0 as soon as possible (see headM docs).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I don't understand what does it mean either

-- It's perfectly safe to use non-monadic indexing within
-- each element, as the result of indexing will be demanded
-- as soon as the vector is produced.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean about this is that the use of (!) in line 399 is okay: it can't cause a vector to be kept alive by a thunk. Why? Because we don't create that vector until we need the value of the result of that indexing operation. This is as opposed to the headM we need to get h, where we'd otherwise keep v0 alive in the first element of the result vector until that first element is forced by someone else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I'll integrate that explanation into comment tomorrow. Right now it's somewhat cryptic

{-# INLINE mfix #-}
mfix f
| null v0 = empty
| otherwise = runST $ do
h <- headM v0
return $ cons h $
generate (lv0 - 1) $
\i -> fix (\a -> f a ! (i + 1))
where
v0 = fix (f . head)
!lv0 = length v0

instance Applicative.Applicative Vector where
{-# INLINE pure #-}
Expand Down
17 changes: 17 additions & 0 deletions tests/Tests/Vector/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Tests.Vector.UnitTests (tests) where
import Control.Applicative as Applicative
import Control.Exception
import Control.Monad.Primitive
import Control.Monad.Fix (mfix)
import qualified Data.Vector as Vector
import Data.Int
import Data.Word
import Data.Typeable
Expand Down Expand Up @@ -80,6 +82,9 @@ tests =
, testCase "Unboxed" $ testTakeOutOfMemory Unboxed.take
]
]
, testGroup "Data.Vector"
[ testCase "MonadFix" checkMonadFix
]
]

testsSliceOutOfBounds ::
Expand Down Expand Up @@ -157,3 +162,15 @@ _f :: (Generic.Vector v a, Generic.Vector w a, PrimMonad f)
=> Generic.Mutable v (PrimState f) a -> f (w a)
_f v = Generic.convert `fmap` Generic.unsafeFreeze v
#endif
checkMonadFix :: Assertion
checkMonadFix = assertBool "checkMonadFix" $
Vector.toList fewV == fewL &&
Vector.toList none == []
where
facty _ 0 = 1; facty f n = n * f (n - 1)
fewV :: Vector.Vector Int
fewV = fmap ($ 12) $ mfix (\i -> Vector.fromList [facty i, facty (+1), facty (+2)])
fewL :: [Int]
fewL = fmap ($ 12) $ mfix (\i -> [facty i, facty (+1), facty (+2)])
none :: Vector.Vector Int
none = mfix (const Vector.empty)