semialign 1.1.0.1 → 1.2
raw patch · 4 files changed
+157/−16 lines, 4 filesdep +indexed-traversabledep +indexed-traversable-instancesdep +voiddep ~basedep ~thesedep ~transformersPVP ok
version bump matches the API change (PVP)
Dependencies added: indexed-traversable, indexed-traversable-instances, void
Dependency ranges changed: base, these, transformers, transformers-compat
API changes (from Hackage documentation)
+ Data.Semialign.Indexed: class (ZipWithIndex i f, Repeat f) => RepeatWithIndex i f | f -> i
+ Data.Semialign.Indexed: class (FunctorWithIndex i f, Semialign f) => SemialignWithIndex i f | f -> i
+ Data.Semialign.Indexed: class (SemialignWithIndex i f, Zip f) => ZipWithIndex i f | f -> i
+ Data.Semialign.Indexed: ialignWith :: SemialignWithIndex i f => (i -> These a b -> c) -> f a -> f b -> f c
+ Data.Semialign.Indexed: irepeat :: RepeatWithIndex i f => (i -> a) -> f a
+ Data.Semialign.Indexed: izipWith :: ZipWithIndex i f => (i -> a -> b -> c) -> f a -> f b -> f c
Files
- CHANGELOG.md +7/−0
- semialign.cabal +19/−11
- src/Data/Semialign/Indexed.hs +9/−0
- src/Data/Semialign/Internal.hs +122/−5
CHANGELOG.md view
@@ -1,3 +1,10 @@+# 1.2++- Migrate `SemialignWithIndex` and `ZipWithIndex` to this package,+ using `FunctorWithIndex` from `indexed-traversable`.+- Add `RepeatWithIndex` type-class.+- Poly-kinded instances (notably `Tagged`)+ # 1.1.0.1 - Drop `base-compat` dependency
semialign.cabal view
@@ -1,10 +1,10 @@ cabal-version: >=1.10 name: semialign-version: 1.1.0.1+version: 1.2 synopsis: Align and Zip type-classes from the common Semialign ancestor. -homepage: https://github.com/isomorphism/these+homepage: https://github.com/haskellari/these license: BSD3 license-file: LICENSE author: C. McCann, Oleg Grenrus@@ -29,13 +29,15 @@ || ==8.2.2 || ==8.4.4 || ==8.6.5- || ==8.8.3- || ==8.10.1+ || ==8.8.4+ || ==8.10.4+ || ==9.0.1 , GHCJS ==8.4 source-repository head type: git- location: https://github.com/isomorphism/these.git+ location: https://github.com/haskellari/these.git+ subdir: semialign flag semigroupoids description: Build with semigroupoids dependency@@ -54,25 +56,28 @@ Data.Align Data.Crosswalk Data.Semialign+ Data.Semialign.Indexed Data.Zip other-modules: Data.Semialign.Internal -- ghc boot libs build-depends:- base >=4.5.1.0 && <4.15+ base >=4.5.1.0 && <4.16 , containers >=0.4.2.1 && <0.7 , transformers >=0.3.0.0 && <0.6 -- These- build-depends: these >=1 && <1.2+ build-depends: these >=1.1.1.1 && <1.2 -- other dependencies build-depends:- hashable >=1.2.7.0 && <1.4- , tagged >=0.8.6 && <0.9- , unordered-containers >=0.2.8.0 && <0.3- , vector >=0.12.0.2 && <0.13+ hashable >=1.2.7.0 && <1.4+ , indexed-traversable >=0.1.1 && <0.2+ , indexed-traversable-instances >=0.1 && <0.2+ , tagged >=0.8.6 && <0.9+ , unordered-containers >=0.2.8.0 && <0.3+ , vector >=0.12.0.2 && <0.13 -- base shims if !impl(ghc >=8.2)@@ -87,6 +92,9 @@ -- Ensure Data.Functor.Classes is always available if impl(ghc >=7.10) build-depends: transformers >=0.4.2.0++ if !impl(ghc >=7.10)+ build-depends: void >=0.7.3 && <0.8 if impl(ghc <7.5) build-depends: ghc-prim
+ src/Data/Semialign/Indexed.hs view
@@ -0,0 +1,9 @@+{-# LANGUAGE Safe #-}+-- | Zipping and aligning of indexed functors.+module Data.Semialign.Indexed (+ SemialignWithIndex (..),+ ZipWithIndex (..),+ RepeatWithIndex (..),+) where++import Data.Semialign.Internal
src/Data/Semialign/Internal.hs view
@@ -1,18 +1,23 @@ {-# LANGUAGE CPP #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE Trustworthy #-}+{-# LANGUAGE UndecidableInstances #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE PolyKinds #-}+#endif module Data.Semialign.Internal where import Prelude- (Bool (..), Eq (..), Functor (fmap), Maybe (..), Monad (..), Ord (..),- Ordering (..), String, error, flip, fst, id, maybe, snd, uncurry, ($),- (++), (.))+ (Bool (..), Either (..), Eq (..), Functor (fmap), Int, Maybe (..),+ Monad (..), Ord (..), Ordering (..), String, error, flip, fst, id,+ maybe, snd, uncurry, ($), (++), (.)) import qualified Prelude as Prelude -import Control.Applicative (ZipList (..), pure, (<$>))-import Data.Monoid (Monoid (..))+import Control.Applicative (ZipList (..), pure, (<$>)) import Data.Bifunctor (Bifunctor (..)) import Data.Functor.Compose (Compose (..)) import Data.Functor.Identity (Identity (..))@@ -21,13 +26,18 @@ import Data.HashMap.Strict (HashMap) import Data.List.NonEmpty (NonEmpty (..)) import Data.Maybe (catMaybes)+import Data.Monoid (Monoid (..)) import Data.Proxy (Proxy (..)) import Data.Semigroup (Option (..), Semigroup (..)) import Data.Sequence (Seq) import Data.Tagged (Tagged (..)) import Data.Vector.Fusion.Stream.Monadic (Step (..), Stream (..)) import Data.Vector.Generic (Vector, empty, stream, unstream)+import Data.Void (Void) +import Data.Functor.WithIndex (FunctorWithIndex (imap))+import Data.Functor.WithIndex.Instances ()+ import qualified Data.HashMap.Strict as HM import qualified Data.List.NonEmpty as NE import qualified Data.Sequence as Seq@@ -330,6 +340,32 @@ unzipDefault :: Functor f => f (a, b) -> (f a, f b) unzipDefault x = (fst <$> x, snd <$> x) +-- | Indexed version of 'Semialign'.+--+-- @since 1.2+class (FunctorWithIndex i f, Semialign f) => SemialignWithIndex i f | f -> i where+ -- | Analogous to 'alignWith', but also provides an index.+ ialignWith :: (i -> These a b -> c) -> f a -> f b -> f c+ ialignWith f a b = imap f (align a b)++-- | Indexed version of 'Zip'.+--+-- @since 1.2+class (SemialignWithIndex i f, Zip f) => ZipWithIndex i f | f -> i where+ -- | Analogous to 'zipWith', but also provides an index.+ izipWith :: (i -> a -> b -> c) -> f a -> f b -> f c+ izipWith f a b = imap (uncurry . f) (zip a b)++-- | Indexed version of 'Repeat'.+--+-- @since 1.2+class (ZipWithIndex i f, Repeat f) => RepeatWithIndex i f | f -> i where+ -- | Analogous to 'repeat', but also provides an index.+ --+ -- This should be the same as 'tabulate' for representable functors.+ irepeat :: (i -> a) -> f a+ irepeat f = imap (\i f' -> f' i) (repeat f)+ ------------------------------------------------------------------------------- -- base -------------------------------------------------------------------------------@@ -344,6 +380,13 @@ instance Repeat ((->) e) where repeat = pure +instance SemialignWithIndex e ((->) e) where+ ialignWith h f g x = h x (These (f x) (g x))+instance ZipWithIndex e ((->) e) where+ izipWith h f g x = h x (f x) (g x)+instance RepeatWithIndex e ((->) e) where+ irepeat = id+ instance Semialign Maybe where align Nothing Nothing = Nothing align (Just a) Nothing = Just (This a)@@ -370,6 +413,9 @@ instance Align Maybe where nil = Nothing +instance SemialignWithIndex () Maybe+instance ZipWithIndex () Maybe+instance RepeatWithIndex () Maybe instance Semialign [] where align xs [] = This <$> xs@@ -389,6 +435,9 @@ instance Unzip [] where unzip = Prelude.unzip +instance SemialignWithIndex Int []+instance ZipWithIndex Int []+instance RepeatWithIndex Int [] -- | @'zipWith' = 'liftA2'@ . instance Semialign ZipList where@@ -407,6 +456,10 @@ unzip (ZipList xs) = (ZipList ys, ZipList zs) where (ys, zs) = unzip xs +instance SemialignWithIndex Int ZipList+instance ZipWithIndex Int ZipList+instance RepeatWithIndex Int ZipList+ ------------------------------------------------------------------------------- -- semigroups -------------------------------------------------------------------------------@@ -424,6 +477,10 @@ instance Unzip NonEmpty where unzip = NE.unzip +instance SemialignWithIndex Int NonEmpty+instance ZipWithIndex Int NonEmpty+instance RepeatWithIndex Int NonEmpty+ deriving instance Semialign Option deriving instance Align Option deriving instance Unalign Option@@ -431,6 +488,12 @@ deriving instance Repeat Option deriving instance Unzip Option +{-+deriving instance SemialignWithIndex () Option+deriving instance ZipWithIndex () Option+deriving instance RepeatWithIndex () Option+-}+ ------------------------------------------------------------------------------- -- containers: ListLike -------------------------------------------------------------------------------@@ -473,6 +536,9 @@ zip = Seq.zip zipWith = Seq.zipWith +instance SemialignWithIndex Int Seq+instance ZipWithIndex Int Seq+ instance Semialign T.Tree where align (T.Node x xs) (T.Node y ys) = T.Node (These x y) (alignWith (these (fmap This) (fmap That) align) xs ys) @@ -540,6 +606,13 @@ instance Zip IntMap where zipWith = IntMap.intersectionWith +instance SemialignWithIndex Int IntMap+instance ZipWithIndex Int IntMap where+ izipWith = IntMap.intersectionWithKey+instance Ord k => SemialignWithIndex k (Map k) where+instance Ord k => ZipWithIndex k (Map k) where+ izipWith = Map.intersectionWithKey+ ------------------------------------------------------------------------------- -- transformers -------------------------------------------------------------------------------@@ -556,6 +629,9 @@ instance Unzip Identity where unzip (Identity ~(a, b)) = (Identity a, Identity b) +instance SemialignWithIndex () Identity+instance ZipWithIndex () Identity+instance RepeatWithIndex () Identity instance (Semialign f, Semialign g) => Semialign (Product f g) where align (Pair a b) (Pair c d) = Pair (align a c) (align b d)@@ -581,7 +657,20 @@ ~(al, ar) = unzip a ~(bl, br) = unzip b +instance (SemialignWithIndex i f, SemialignWithIndex j g) => SemialignWithIndex (Either i j) (Product f g) where+ ialignWith f (Pair fa ga) (Pair fb gb) = Pair fc gc where+ fc = ialignWith (f . Left) fa fb+ gc = ialignWith (f . Right) ga gb +instance (ZipWithIndex i f, ZipWithIndex j g) => ZipWithIndex (Either i j) (Product f g) where+ izipWith f (Pair fa ga) (Pair fb gb) = Pair fc gc where+ fc = izipWith (f . Left) fa fb+ gc = izipWith (f . Right) ga gb++instance (RepeatWithIndex i f, RepeatWithIndex j g) => RepeatWithIndex (Either i j) (Product f g) where+ irepeat f = Pair (irepeat (f . Left)) (irepeat (f . Right))++ instance (Semialign f, Semialign g) => Semialign (Compose f g) where alignWith f (Compose x) (Compose y) = Compose (alignWith g x y) where g (This ga) = fmap (f . This) ga@@ -607,6 +696,19 @@ -- unalignWith f (Compose x) = (Compose y, Compose z) where -- ~(y, z) = unalignWith (uncurry These . unalignWith f) x +instance (SemialignWithIndex i f, SemialignWithIndex j g) => SemialignWithIndex (i, j) (Compose f g) where+ ialignWith f (Compose fga) (Compose fgb) = Compose $ ialignWith g fga fgb where+ g i (This ga) = imap (\j -> f (i, j) . This) ga+ g i (That gb) = imap (\j -> f (i, j) . That) gb+ g i (These ga gb) = ialignWith (\j -> f (i, j)) ga gb++instance (ZipWithIndex i f, ZipWithIndex j g) => ZipWithIndex (i, j) (Compose f g) where+ izipWith f (Compose fga) (Compose fgb) = Compose fgc where+ fgc = izipWith (\i -> izipWith (\j -> f (i, j))) fga fgb++instance (RepeatWithIndex i f, RepeatWithIndex j g) => RepeatWithIndex (i, j) (Compose f g) where+ irepeat f = Compose (irepeat (\i -> irepeat (\j -> f (i, j))))+ ------------------------------------------------------------------------------- -- vector -------------------------------------------------------------------------------@@ -673,6 +775,10 @@ => (These a b -> c) -> v a -> v b -> v c alignVectorWith f x y = unstream $ alignWith f (stream x) (stream y) +instance SemialignWithIndex Int V.Vector where+instance ZipWithIndex Int V.Vector where+ izipWith = V.izipWith+ ------------------------------------------------------------------------------- -- unordered-containers -------------------------------------------------------------------------------@@ -693,6 +799,10 @@ instance (Eq k, Hashable k) => Unalign (HashMap k) where unalign xs = (HM.mapMaybe justHere xs, HM.mapMaybe justThere xs) +instance (Eq k, Hashable k) => SemialignWithIndex k (HashMap k) where+instance (Eq k, Hashable k) => ZipWithIndex k (HashMap k) where+ izipWith = HM.intersectionWithKey+ ------------------------------------------------------------------------------- -- tagged -------------------------------------------------------------------------------@@ -709,6 +819,9 @@ instance Unzip (Tagged b) where unzip (Tagged ~(a, b)) = (Tagged a, Tagged b) +instance SemialignWithIndex () (Tagged b)+instance ZipWithIndex () (Tagged b)+instance RepeatWithIndex () (Tagged b) instance Semialign Proxy where alignWith _ _ _ = Proxy@@ -729,6 +842,10 @@ instance Unzip Proxy where unzip _ = (Proxy, Proxy)++instance SemialignWithIndex Void Proxy+instance ZipWithIndex Void Proxy+instance RepeatWithIndex Void Proxy ------------------------------------------------------------------------------- -- combinators