assoc-1.0.2/0000755000000000000000000000000007346545000011013 5ustar0000000000000000assoc-1.0.2/CHANGELOG.md0000644000000000000000000000015107346545000012621 0ustar0000000000000000## 1.0.2 - Add 'Swap' instances for more n-tuples ## 1.0.1 - Add `Assoc Const` and `Tagged` instances assoc-1.0.2/LICENSE0000644000000000000000000000276207346545000012027 0ustar0000000000000000Copyright (c) 2017, Oleg Grenrus All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Oleg Grenrus nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. assoc-1.0.2/assoc.cabal0000644000000000000000000000223707346545000013113 0ustar0000000000000000cabal-version: 1.12 name: assoc version: 1.0.2 license: BSD3 license-file: LICENSE synopsis: swap and assoc: Symmetric and Semigroupy Bifunctors category: Data description: Provides generalisations of @swap :: (a,b) -> (b,a)@ and @assoc :: ((a,b),c) -> (a,(b,c))@ to @Bifunctor@s supporting similar operations (e.g. @Either@, @These@). author: Oleg Grenrus maintainer: Oleg Grenrus build-type: Simple extra-source-files: CHANGELOG.md tested-with: GHC ==7.0.4 || ==7.2.2 || ==7.4.2 || ==7.6.3 || ==7.8.4 || ==7.10.3 || ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.3 || ==8.10.1 , GHCJS ==8.4 source-repository head type: git location: https://github.com/phadej/assoc.git library default-language: Haskell2010 hs-source-dirs: src build-depends: base >=4.3 && <4.15 , bifunctors >=5.5.5 && <5.6 , tagged >=0.8.6 && <0.9 exposed-modules: Data.Bifunctor.Assoc Data.Bifunctor.Swap other-extensions: TypeFamilies assoc-1.0.2/src/Data/Bifunctor/0000755000000000000000000000000007346545000014406 5ustar0000000000000000assoc-1.0.2/src/Data/Bifunctor/Assoc.hs0000644000000000000000000000667707346545000016032 0ustar0000000000000000module Data.Bifunctor.Assoc ( Assoc (..), ) where import Control.Applicative (Const (..)) import Data.Bifunctor (Bifunctor (..)) import Data.Bifunctor.Flip (Flip (..)) import Data.Bifunctor.Product (Product (..)) import Data.Bifunctor.Tannen (Tannen (..)) import Data.Tagged (Tagged (..)) -- | "Semigroup-y" 'Bifunctor's. -- -- @ -- 'assoc' . 'unassoc' = 'id' -- 'unassoc' . 'assoc' = 'id' -- 'assoc' . 'bimap' ('bimap' f g) h = 'bimap' f ('bimap' g h) . 'assoc' -- @ -- -- This library doesn't provide @Monoidal@ class, with left and right unitors. -- Are they useful in practice? -- class Bifunctor p => Assoc p where assoc :: p (p a b) c -> p a (p b c) unassoc :: p a (p b c) -> p (p a b) c instance Assoc (,) where assoc ((a, b), c) = (a, (b, c)) unassoc (a, (b, c)) = ((a, b), c) instance Assoc Either where assoc (Left (Left a)) = Left a assoc (Left (Right b)) = Right (Left b) assoc (Right c) = Right (Right c) unassoc (Left a) = Left (Left a) unassoc (Right (Left b)) = Left (Right b) unassoc (Right (Right c)) = Right c instance Assoc Const where assoc (Const (Const a)) = Const a unassoc (Const a) = Const (Const a) instance Assoc Tagged where assoc (Tagged a) = Tagged (Tagged a) unassoc (Tagged (Tagged a)) = Tagged a instance Assoc p => Assoc (Flip p) where assoc = Flip . first Flip . unassoc . second runFlip . runFlip unassoc = Flip . second Flip . assoc . first runFlip . runFlip -- $setup -- -- TODO: make proper test-suite -- -- >>> import Data.Proxy -- >>> import Test.QuickCheck -- >>> import Test.QuickCheck.Instances -- >>> import Data.Functor.Classes -- -- >>> :{ -- let assocUnassocLaw :: (Assoc p, Eq2 p) => Proxy p -> p Bool (p Int Char) -> Bool -- assocUnassocLaw _ x = liftEq2 (==) eq2 (assoc (unassoc x)) x -- :} -- -- >>> quickCheck $ assocUnassocLaw (Proxy :: Proxy (,)) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ assocUnassocLaw (Proxy :: Proxy Either) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ assocUnassocLaw (Proxy :: Proxy Tagged) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ assocUnassocLaw (Proxy :: Proxy Const) -- +++ OK, passed 100 tests. -- -- >>> :{ -- let unassocAssocLaw :: (Assoc p, Eq2 p) => Proxy p -> p (p Int Char) Bool -> Bool -- unassocAssocLaw _ x = liftEq2 eq2 (==) (unassoc (assoc x)) x -- :} -- -- >>> quickCheck $ unassocAssocLaw (Proxy :: Proxy (,)) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ unassocAssocLaw (Proxy :: Proxy Either) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ unassocAssocLaw (Proxy :: Proxy Tagged) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ unassocAssocLaw (Proxy :: Proxy Const) -- +++ OK, passed 100 tests. -- -- >>> :{ -- let bimapLaw :: (Assoc p, Eq2 p) => Proxy p -- -> Fun Int Char -> Fun Char Bool -> Fun Bool Int -- -> p (p Int Char) Bool -- -> Bool -- bimapLaw _ (Fun _ f) (Fun _ g) (Fun _ h) x = liftEq2 (==) eq2 -- (assoc . bimap (bimap f g) h $ x) -- (bimap f (bimap g h) . assoc $ x) -- :} -- -- >>> quickCheck $ bimapLaw (Proxy :: Proxy (,)) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ bimapLaw (Proxy :: Proxy Either) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ bimapLaw (Proxy :: Proxy Tagged) -- +++ OK, passed 100 tests. -- -- >>> quickCheck $ bimapLaw (Proxy :: Proxy Const) -- +++ OK, passed 100 tests. assoc-1.0.2/src/Data/Bifunctor/Swap.hs0000644000000000000000000000350607346545000015660 0ustar0000000000000000{-# LANGUAGE TypeFamilies #-} module Data.Bifunctor.Swap ( Swap (..), ) where import Data.Bifunctor (Bifunctor (..)) import Data.Bifunctor.Biff (Biff (..)) import Data.Bifunctor.Flip (Flip (..)) import Data.Bifunctor.Product (Product (..)) import Data.Bifunctor.Sum (Sum (..)) import Data.Bifunctor.Tannen (Tannen (..)) import qualified Data.Tuple -- | Symmetric 'Bifunctor's. -- -- @ -- 'swap' . 'swap' = 'id' -- @ -- -- If @p@ is a 'Bifunctor' the following property is assumed to hold: -- -- @ -- 'swap' . 'bimap' f g = 'bimap' g f . 'swap' -- @ -- -- 'Swap' isn't a subclass of 'Bifunctor', as for example -- -- >>> newtype Bipredicate a b = Bipredicate (a -> b -> Bool) -- -- is not a 'Bifunctor' but has 'Swap' instance -- -- >>> instance Swap Bipredicate where swap (Bipredicate p) = Bipredicate (flip p) -- class Swap p where swap :: p a b -> p b a instance Swap (,) where swap = Data.Tuple.swap instance Swap Either where swap (Left x) = Right x swap (Right x) = Left x instance Swap p => Swap (Flip p) where swap = Flip . swap . runFlip instance (Swap p, Swap q) => Swap (Product p q) where swap (Pair p q) = Pair (swap p) (swap q) instance (Swap p, Swap q) => Swap (Sum p q) where swap (L2 p) = L2 (swap p) swap (R2 q) = R2 (swap q) instance (Functor f, Swap p) => Swap (Tannen f p) where swap = Tannen . fmap swap . runTannen instance (f ~ g, Functor f, Swap p) => Swap (Biff p f g) where swap = Biff . swap . runBiff instance Swap ((,,) x) where swap (x,a,b) = (x,b,a) instance Swap ((,,,) x y) where swap (x,y,a,b) = (x,y,b,a) instance Swap ((,,,,) x y z) where swap (x,y,z,a,b) = (x,y,z,b,a) instance Swap ((,,,,,) x y z w) where swap (x,y,z,w,a,b) = (x,y,z,w,b,a) instance Swap ((,,,,,,) x y z w v) where swap (x,y,z,w,v,a,b) = (x,y,z,w,v,b,a)