@@ -20,7 +20,7 @@ module Data.Primitive.Array (
20
20
Array (.. ), MutableArray (.. ),
21
21
22
22
newArray , readArray , writeArray , indexArray , indexArrayM ,
23
- freezeArray , thawArray , runArray , runArrays , runArraysHetOf , runArraysHetOfThen ,
23
+ freezeArray , thawArray , runArray , runArrays , runArraysOf , runArraysHetOf ,
24
24
unsafeFreezeArray , unsafeThawArray , sameMutableArray ,
25
25
copyArray , copyMutableArray ,
26
26
cloneArray , cloneMutableArray ,
@@ -29,7 +29,6 @@ module Data.Primitive.Array (
29
29
unsafeTraverseArray
30
30
) where
31
31
32
- import Data.Functor.Compose
33
32
import Control.Monad.Primitive
34
33
35
34
import GHC.Base ( Int (.. ) )
@@ -805,16 +804,49 @@ instance (Typeable s, Typeable a) => Data (MutableArray s a) where
805
804
-- | Create any number of arrays of the same type within an arbitrary
806
805
-- 'Traversable' context. This will often be useful with traversables
807
806
-- like @(c,)@, @'Either' e@, @'Compose' (c,) ('Either' e)@, and
808
- -- @'Compose' ('Either' e) (c,)@. For a more general version, see
809
- -- 'runArraysHetOf'.
807
+ -- @'Compose' ('Either' e) (c,)@. To use an arbitrary traversal
808
+ -- function, see 'runArraysOf'. To create arrays of varying types,
809
+ -- see 'runArraysHetOf'.
810
810
runArrays
811
811
:: Traversable t
812
812
=> (forall s . ST s (t (MutableArray s a )))
813
813
-> t (Array a )
814
814
runArrays m = runST $ m >>= traverse unsafeFreezeArray
815
815
816
+ -- | Just like 'runArrays', but takes an arbitrary (potentially
817
+ -- type-changing) traversal function instead of requiring a 'Traversable'
818
+ -- constraint. To produce arrays of varying types, use 'runArraysHetOf'.
819
+ runArraysOf
820
+ :: (forall s1 s2 .
821
+ (MutableArray s1 a -> ST s2 (Array a )) -> t (MutableArray s1 a ) -> ST s2 (u (Array a )))
822
+ -> (forall s . ST s (t (MutableArray s a )))
823
+ -> u (Array a )
824
+ runArraysOf trav m = runST $ m >>= trav unsafeFreezeArray
825
+
826
+ {-
827
+ I initially thought we'd need a function like
828
+
829
+ runArraysOfThen
830
+ :: (forall s1 s2.
831
+ (MutableArray s1 a -> Compose (ST s2) q r) -> t (MutableArray s1 a) -> Compose (ST s2) q (u r))
832
+ -> (Array a -> q r)
833
+ -> (forall s. ST s (t (MutableArray s a)))
834
+ -> q (u r)
835
+
836
+ to allow users to traverse over the generated arrays. But because 'runArraysOf'
837
+ allows the traversal function to know that it is producing values of type
838
+ @Array a@, one could just write
839
+
840
+ runArraysOfThen trav post m = getConst $
841
+ runArraysOf (\f -> coerce . getCompose . (trav (Compose . fmap post . f))) m
842
+
843
+ Perhaps such a function *should* be added for convenience, but it's
844
+ clearly not necessary.
845
+ -}
846
+
816
847
-- | Create arbitrarily many arrays that may have different types.
817
- -- For a simpler but less general version, see 'runArrays'.
848
+ -- For a simpler but less general version, see 'runArrays' or
849
+ -- 'runArraysOf'.
818
850
--
819
851
-- === __Examples__
820
852
--
@@ -848,27 +880,27 @@ runArrays m = runST $ m >>= traverse unsafeFreezeArray
848
880
--
849
881
-- traversePair :: Applicative h => (forall x. f x -> h (g x)) -> Pair ab f -> h (Pair ab g)
850
882
-- traversePair f (Pair (xs, ys)) = liftA2 (\x y -> Pair (x,y)) (f xs) (f ys)
883
+ --
884
+ -- ==== Produce a container of arrays and traverse over them
885
+ --
886
+ -- @
887
+ -- runArraysHetOfThen
888
+ -- :: (forall s1 s2.
889
+ -- ((forall x. MutableArray s1 x -> Compose (ST s2) q (r x)) -> t (MutableArray s1) -> Compose (ST s2) q (u r)))
890
+ -- -- ^ A rank-2 traversal
891
+ -- -> (forall x. Array x -> q (r x))
892
+ -- -- ^ A function to traverse over the container of 'Array's
893
+ -- -> (forall s. ST s (t (MutableArray s)))
894
+ -- -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
895
+ -- -> q (u r)
896
+ -- runArraysHetOfThen trav post m = getConst $
897
+ -- runArraysHetOf (\f -> coerce . getCompose . trav (Compose . fmap post . f)) m
851
898
-- @
852
899
runArraysHetOf
853
- :: (forall h f g .
854
- (Applicative h => (forall x . f x -> h ( g x )) -> t f -> h (u g )))
900
+ :: (forall s1 s2 .
901
+ ((forall x . MutableArray s1 x -> ST s2 ( Array x )) -> t ( MutableArray s1 ) -> ST s2 (u Array )))
855
902
-- ^ A rank-2 traversal
856
903
-> (forall s . ST s (t (MutableArray s )))
857
904
-- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
858
905
-> u Array
859
906
runArraysHetOf trav m = runST $ m >>= trav unsafeFreezeArray
860
-
861
- -- | Similar to 'runArraysHetOf', but takes a function to traverse over the
862
- -- generated 'Array's as it freezes them.
863
- runArraysHetOfThen
864
- :: Applicative q
865
- => (forall h f g .
866
- (Applicative h => (forall x . f x -> h (g x )) -> t f -> h (u g )))
867
- -- ^ A rank-2 traversal
868
- -> (forall x . Array x -> q (r x ))
869
- -- ^ A function to traverse over the container of 'Array's
870
- -> (forall s . ST s (t (MutableArray s )))
871
- -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
872
- -> q (u r )
873
- runArraysHetOfThen trav post m =
874
- runST $ m >>= getCompose . trav (Compose . fmap post . unsafeFreezeArray)
0 commit comments