church-pair (empty) → 0.1.0.0
raw patch · 5 files changed
+160/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- CHANGELOG.md +5/−0
- Data/Church/Pair.hs +97/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- church-pair.cabal +26/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for church-pair++## 0.1.0.0++* First version. Released on an unsuspecting world.
+ Data/Church/Pair.hs view
@@ -0,0 +1,97 @@+{-# LANGUAGE Rank2Types #-}+{-# LANGUAGE Safe #-}++-----------------------------------------------------------------------------+-- |+-- Module : Data.Church.Pair+-- Copyright : Matthew Harm Bekkema 2017+-- License : BSD3+-- Maintainer : mbekkema97@gmail.com+-- Stability : experimental+-- Portability : portable+--+-- The church encoded pair type and associated operations.+--+-----------------------------------------------------------------------------++module Data.Church.Pair+( Pair(Pair)+, mkPair+, pair+) where++import Data.Bifunctor (Bifunctor (bimap), second)+import Data.Semigroup (Semigroup ((<>), stimes))+import Data.Functor.Classes (Eq2 (liftEq2), Eq1 (liftEq), Ord2 (liftCompare2),+ Ord1 (liftCompare), eq2, compare2)+++-- | The church encoded pair+newtype Pair a b = Pair { runPair :: forall r. (a -> b -> r) -> r }++instance Bifunctor Pair where+ bimap fx fy (Pair p) = Pair $ \r -> p $ \x y -> r (fx x) (fy y)++instance Functor (Pair a) where+ fmap = second++instance Eq2 Pair where+ liftEq2 f1 f2 (Pair px) (Pair py) = px $ \x1 x2 -> py $ \y1 y2 ->+ f1 x1 y1 && f2 x2 y2++instance Ord2 Pair where+ liftCompare2 f1 f2 (Pair px) (Pair py) = px $ \x1 x2 -> py $ \y1 y2 ->+ case f1 x1 y1 of+ EQ -> f2 x2 y2+ oo -> oo++instance Eq a => Eq1 (Pair a) where+ liftEq = liftEq2 (==)++instance Ord a => Ord1 (Pair a) where+ liftCompare = liftCompare2 compare++instance (Eq a, Eq b) => Eq (Pair a b) where+ (==) = eq2++instance (Ord a, Ord b) => Ord (Pair a b) where+ compare = compare2++instance (Semigroup a, Semigroup b) => Semigroup (Pair a b) where+ (<>) = liftMappend2 (<>) (<>)+ stimes n (Pair p) = Pair $ \r -> p $ \x y -> r (stimes n x) (stimes n y)++instance (Monoid a, Monoid b) => Monoid (Pair a b) where+ mempty = mkPair mempty mempty+ mappend = liftMappend2 mappend mappend++instance Monoid a => Applicative (Pair a) where+ pure = mkPair mempty+ Pair pf <*> Pair px = Pair $ \r -> pf $ \u f -> px $ \v x ->+ r (mappend u v) (f x)++instance Foldable (Pair a) where+ foldr f z (Pair p) = p $ \_ y -> f y z+ foldMap f (Pair p) = p $ \_ y -> f y++instance Traversable (Pair a) where+ traverse f (Pair p) = p $ \x y -> mkPair x <$> f y++instance Monoid a => Monad (Pair a) where+ return = pure+ (Pair p) >>= k = Pair $ \r -> p $ \u a -> runPair (k a) $ r . mappend u+++liftMappend2 :: (a -> a -> a)+ -> (b -> b -> b)+ -> (Pair a b -> Pair a b -> Pair a b)+liftMappend2 f1 f2 (Pair px) (Pair py) = Pair $ \r -> px $ \x1 x2 -> py $ \y1 y2 ->+ r (f1 x1 y1) (f2 x2 y2)++-- | Case analysis on `Pair`+pair :: (a -> b -> r) -> Pair a b -> r+pair = flip runPair++-- | Construct a `Pair` from two values+mkPair :: a -> b -> Pair a b+mkPair x y = Pair $ \r -> r x y
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2017, Matthew Harm Bekkema++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 Matthew Harm Bekkema 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ church-pair.cabal view
@@ -0,0 +1,26 @@+name: church-pair+version: 0.1.0.0+license: BSD3+license-file: LICENSE+author: Matthew Harm Bekkema+maintainer: mbekkema97@gmail.com+synopsis: Church encoded pair+category: Data+description:+ This package contains a Church encoded 'Pair' type along with appropriate+ instances for typeclasses in 'base'.+cabal-version: >=1.10+build-type: Simple++extra-source-files: CHANGELOG.md+++library+ default-language: Haskell98++ other-extensions: Rank2Types+ Safe++ build-depends: base >= 4.9 && < 5+ exposed-modules: Data.Church.Pair+ ghc-options: -Wall