uniform-pair (empty) → 0.1.1
raw patch · 4 files changed
+126/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- COPYING +25/−0
- Setup.lhs +3/−0
- src/Data/UniformPair.hs +72/−0
- uniform-pair.cabal +26/−0
+ COPYING view
@@ -0,0 +1,25 @@+Copyright (c) 2012 Conal Elliott+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:+1. Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+2. 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.+3. The names of the authors may not be used to endorse or promote products+ derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ src/Data/UniformPair.hs view
@@ -0,0 +1,72 @@+{-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable #-}+{-# OPTIONS_GHC -Wall #-}++----------------------------------------------------------------------+-- |+-- Module : Data.UniformPair+-- Copyright : (c) 2013 Tabula, Inc.+-- +-- Maintainer : conal@conal.net+-- Stability : experimental+-- +-- Uniform pairs. Because these pairs memoize functions (from Bool)--i.e.,+-- they're representable functors--these instances provided are fully determined+-- by the corresponding instances for functions, thanks to the type class+-- morphism principle.+----------------------------------------------------------------------++module Data.UniformPair (Pair(..), fstP,sndP, firstP, secondP, compareSwap) where++import Data.Monoid (Monoid(..),(<>))+import Data.Functor ((<$>))+import Data.Foldable (Foldable(..))+import Data.Traversable (Traversable(..))+import Control.Applicative (Applicative(..)) -- ,liftA2++infix 1 :#++-- | Uniform pairs+data Pair a = a :# a deriving (Show, Functor, Foldable,Traversable)++-- instance Traversable Pair where sequenceA (u :# v) = (:#) <$> u <*> v++fstP :: Pair a -> a+fstP (a :# _) = a++sndP :: Pair a -> a+sndP (_ :# b) = b++firstP, secondP :: (a -> a) -> (Pair a -> Pair a)+firstP f (a :# b) = f a :# b+secondP g (a :# b) = a :# g b++-- unzipP :: Functor f => f (Pair a) -> Pair (f a)+-- unzipP ps = (fstP <$> ps) :# (sndP <$> ps)+-- unzipP = liftA2 (:#) (fmap fstP) (fmap sndP)++instance Monoid a => Monoid (Pair a) where+ mempty = mempty :# mempty+ (a :# b) `mappend` (c :# d) = (a <> c) :# (b <> d) -- exchange++instance Applicative Pair where+ pure a = a :# a+ (f :# g) <*> (a :# b) = f a :# g b++instance Monad Pair where+ return = pure+ m >>= f = joinP (f <$> m)++joinP :: Pair (Pair a) -> Pair a+joinP ((a :# _) :# (_ :# d)) = a :# d++-- so+--+-- (a :# b) >>= f = (c :# d)+-- where+-- (c :# _) = f a+-- (_ :# d) = f b++-- Compare and swap+compareSwap :: Ord a => Pair a -> Pair a+compareSwap (a :# b) | a <= b = a :# b+ | otherwise = b :# a
+ uniform-pair.cabal view
@@ -0,0 +1,26 @@+Name: uniform-pair+Version: 0.1.1+Cabal-Version: >= 1.6+Synopsis: Uniform pairs with class instances+Category: Data+Description:+ Uniform pairs with class instances+Author: Conal Elliott+Maintainer: conal@conal.net+Copyright: (c) 2013 by Conal Elliott+License: BSD3+License-File: COPYING+Stability: experimental+build-type: Simple++source-repository head+ type: git+ location: git://github.com/conal/uniform-pair.git++Library+ hs-Source-Dirs: src+ Extensions:+ Build-Depends: base<5+ Exposed-Modules: + Data.UniformPair+ ghc-options: -Wall