11{-# LANGUAGE CPP #-}
22{-# LANGUAGE DeriveFunctor #-}
3+ {-# LANGUAGE DeriveFoldable #-}
4+ {-# LANGUAGE DeriveTraversable #-}
35{-# LANGUAGE FlexibleInstances #-}
46{-# LANGUAGE FunctionalDependencies #-}
57{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@@ -14,13 +16,13 @@ module Data.Semialign.Internal where
1416import Prelude
1517 (Bool (.. ), Either (.. ), Eq (.. ), Functor (fmap ), Int , Maybe (.. ),
1618 Monad (.. ), Ord (.. ), Ordering (.. ), String , error , flip , fst , id ,
17- maybe , snd , uncurry , ($) , (++) , (.) )
19+ maybe , snd , uncurry , ($) , (++) , (.) , Traversable , Foldable )
1820
1921import qualified Prelude as Prelude
2022
2123import Control.Applicative (ZipList (.. ), pure , (<$>) )
2224import Data.Bifunctor (Bifunctor (.. ))
23- import Data.Biapplicative (Biapplicative ( .. ), traverseBia )
25+ import Data.Biapplicative (traverseBia )
2426import Data.Functor.Compose (Compose (.. ))
2527import Data.Functor.Identity (Identity (.. ))
2628import Data.Functor.Product (Product (.. ))
@@ -76,6 +78,8 @@ import Data.IntMap (IntMap)
7678import qualified Data.IntMap as IntMap
7779#endif
7880
81+ import Data.Semialign.Internal.Tuples (SBPair (.. ), LBPair (.. ))
82+
7983import Data.These
8084import Data.These.Combinators
8185
@@ -579,16 +583,57 @@ instance (Ord k) => Align (Map k) where
579583instance Ord k => Unalign (Map k ) where
580584 unalign xs = (Map. mapMaybe justHere xs, Map. mapMaybe justThere xs)
581585
582- -- A copy of (,) with a stricter bimap.
583- newtype SBPair a b = SBPair { unSBPair :: (a , b ) }
584-
585- instance Bifunctor SBPair where
586- bimap f g (SBPair (a, b)) = SBPair (f a, g b)
587-
588- instance Biapplicative SBPair where
589- bipure a b = SBPair (a, b)
590- biliftA2 f g (SBPair (a, b)) (SBPair (c, d)) =
591- SBPair (f a c, g b d)
586+ newtype UnzipStrictSpineStrictPairs t a =
587+ UnzipStrictSpineStrictPairs { getUnzipStrictSpineStrictPairs :: t a }
588+ deriving (Functor , Foldable , Traversable , Semialign , Align , Zip )
589+
590+ instance (Zip t , Traversable t ) => Unzip (UnzipStrictSpineStrictPairs t ) where
591+ unzipWith = unzipWithStrictSpineStrictPairs
592+
593+ newtype UnzipStrictSpineLazyPairs t a =
594+ UnzipStrictSpineLazyPairs { getUnzipStrictSpineLazyPairs :: t a }
595+ deriving (Functor , Foldable , Traversable , Semialign , Align , Zip )
596+
597+ instance (Zip t , Traversable t ) => Unzip (UnzipStrictSpineLazyPairs t ) where
598+ unzipWith = unzipWithStrictSpineLazyPairs
599+ unzip = unzipStrictSpineLazyPairs
600+
601+ newtype UnzipLazySpineLazyPairs t a =
602+ UnzipLazySpineLazyPairs { getUnzipLazySpineLazyPairs :: t a }
603+ deriving (Functor , Foldable , Traversable , Semialign , Align , Zip )
604+
605+ instance (Zip t , Traversable t ) => Unzip (UnzipLazySpineLazyPairs t ) where
606+ unzipWith = unzipWithLazySpineLazyPairs
607+
608+ unzipWithStrictSpineStrictPairs :: Traversable t
609+ => (c -> (a , b )) -> t c -> (t a , t b )
610+ unzipWithStrictSpineStrictPairs f = unSBPair . traverseBia (SBPair . f)
611+
612+ unzipWithStrictSpineLazyPairs :: Traversable t
613+ => (c -> (a , b )) -> t c -> (t a , t b )
614+ unzipWithStrictSpineLazyPairs f = unSBPair . traverseBia (SBPair . foo)
615+ where
616+ foo c = let
617+ {-# NOINLINE fc #-}
618+ {-# NOINLINE a #-}
619+ {-# NOINLINE b #-}
620+ fc = f c
621+ (a, b) = fc
622+ in (a, b)
623+
624+ unzipStrictSpineLazyPairs :: Traversable t
625+ => t (a , b ) -> (t a , t b )
626+ unzipStrictSpineLazyPairs = unSBPair . traverseBia (SBPair . foo)
627+ where
628+ foo ab = let
629+ {-# NOINLINE a #-}
630+ {-# NOINLINE b #-}
631+ (a, b) = ab
632+ in (a, b)
633+
634+ unzipWithLazySpineLazyPairs :: Traversable t
635+ => (c -> (a , b )) -> t c -> (t a , t b )
636+ unzipWithLazySpineLazyPairs f = unLBPair . traverseBia (LBPair . f)
592637
593638instance Ord k => Unzip (Map k ) where unzip = unzipDefault
594639
0 commit comments