Skip to content

Commit 53d247b

Browse files
Shimuuartreeowl
andauthored
Add MonadFix instance for boxed vectors (#312)
It's #179 with merged into latest master and documentation tweaks Originally PR authored by David Feurer I *believe* this is equivalent to the instance for `[]`. Writing QuickCheck properties for `mfix` seems pretty tricky, so I just added a small unit test. Co-authored-by: David Feuer <[email protected]>
1 parent e350a52 commit 53d247b

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Data/Vector.hs

+26-2
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ import Control.DeepSeq ( NFData(rnf)
177177
)
178178

179179
import Control.Monad ( MonadPlus(..), liftM, ap )
180-
import Control.Monad.ST ( ST )
180+
import Control.Monad.ST ( ST, runST )
181181
import Control.Monad.Primitive
182182
import qualified Control.Monad.Fail as Fail
183-
183+
import Control.Monad.Fix ( MonadFix (mfix) )
184184
import Control.Monad.Zip
185+
import Data.Function ( fix )
185186

186187
import Prelude hiding ( length, null,
187188
replicate, (++), concat,
@@ -382,6 +383,29 @@ instance MonadZip Vector where
382383
{-# INLINE munzip #-}
383384
munzip = unzip
384385

386+
-- | Instance has same semantics as one for lists
387+
--
388+
-- @since 0.13.0.0
389+
instance MonadFix Vector where
390+
-- We take care to dispose of v0 as soon as possible (see headM docs).
391+
--
392+
-- It's perfectly safe to use non-monadic indexing within generate
393+
-- call since intermediate vector won't be created until result's
394+
-- value is demanded.
395+
{-# INLINE mfix #-}
396+
mfix f
397+
| null v0 = empty
398+
-- We take first element of resulting vector from v0 and create
399+
-- rest using generate. Note that cons should fuse with generate
400+
| otherwise = runST $ do
401+
h <- headM v0
402+
return $ cons h $
403+
generate (lv0 - 1) $
404+
\i -> fix (\a -> f a ! (i + 1))
405+
where
406+
-- Used to calculate size of resulting vector
407+
v0 = fix (f . head)
408+
!lv0 = length v0
385409

386410
instance Applicative.Applicative Vector where
387411
{-# INLINE pure #-}

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changes in NEXT_VERSION
22

3+
* Added `MonadFix` instance for boxed vectors
34
* New functions: `unfoldrExactN` and `unfoldrExactNM`
45
* `mkType` from `Data.Vector.Generic` is deprecated in favor of
56
`Data.Data.mkNoRepType`

tests/Tests/Vector/UnitTests.hs

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module Tests.Vector.UnitTests (tests) where
66
import Control.Applicative as Applicative
77
import Control.Exception
88
import Control.Monad.Primitive
9+
import Control.Monad.Fix (mfix)
10+
import qualified Data.Vector as Vector
911
import Data.Int
1012
import Data.Word
1113
import Data.Typeable
@@ -80,6 +82,9 @@ tests =
8082
, testCase "Unboxed" $ testTakeOutOfMemory Unboxed.take
8183
]
8284
]
85+
, testGroup "Data.Vector"
86+
[ testCase "MonadFix" checkMonadFix
87+
]
8388
]
8489

8590
testsSliceOutOfBounds ::
@@ -157,3 +162,15 @@ _f :: (Generic.Vector v a, Generic.Vector w a, PrimMonad f)
157162
=> Generic.Mutable v (PrimState f) a -> f (w a)
158163
_f v = Generic.convert `fmap` Generic.unsafeFreeze v
159164
#endif
165+
checkMonadFix :: Assertion
166+
checkMonadFix = assertBool "checkMonadFix" $
167+
Vector.toList fewV == fewL &&
168+
Vector.toList none == []
169+
where
170+
facty _ 0 = 1; facty f n = n * f (n - 1)
171+
fewV :: Vector.Vector Int
172+
fewV = fmap ($ 12) $ mfix (\i -> Vector.fromList [facty i, facty (+1), facty (+2)])
173+
fewL :: [Int]
174+
fewL = fmap ($ 12) $ mfix (\i -> [facty i, facty (+1), facty (+2)])
175+
none :: Vector.Vector Int
176+
none = mfix (const Vector.empty)

0 commit comments

Comments
 (0)