contravariant (empty) → 0.1.0
raw patch · 4 files changed
+133/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- Data/Functor/Contravariant.hs +73/−0
- LICENSE +30/−0
- Setup.lhs +7/−0
- contravariant.cabal +23/−0
+ Data/Functor/Contravariant.hs view
@@ -0,0 +1,73 @@+-----------------------------------------------------------------------------+-- |+-- Module : Data.Functor.Contravariant+-- Copyright : (C) 2007-2011 Edward Kmett+-- License : BSD-style (see the file LICENSE)+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : provisional+-- Portability : portable+--+-- 'Contravariant' functors, sometimes referred to colloquially as @Cofunctor@,+-- even though the dual of a 'Functor' is just a 'Functor'. As with 'Functor'+-- the definition of 'Contravariant'' for a given ADT is unambiguous.+----------------------------------------------------------------------------++module Data.Functor.Contravariant ( + -- * Contravariant Functors+ Contravariant(..)+ + -- * Predicates+ , Predicate(..)++ -- * Comparisons+ , Comparison(..)+ , defaultComparison++ -- * Equivalence Relations+ , Equivalence(..)+ , defaultEquivalence+ ) where++-- | Any instance should be subject to the following laws:+--+-- > contramap id = id+-- > contramap f . contramap g = contramap (g . f)+--+-- Note, that the second law follows from the free theorem of the type of +-- 'contramap' and the first law, so you+-- need only check that the former condition holds.++class Contravariant f where+ contramap :: (a -> b) -> f b -> f a ++newtype Predicate a = Predicate { getPredicate :: a -> Bool } +-- | A 'Predicate' is a 'Contravariant' 'Functor', because 'contramap' can +-- apply its function argument to the input of the predicate.+instance Contravariant Predicate where+ contramap f g = Predicate $ getPredicate g . f++-- | Defines a total ordering on a type as per 'compare'+newtype Comparison a = Comparison { getComparison :: a -> a -> Ordering } ++-- | A 'Comparison' is a 'Contravariant' 'Functor', because 'contramap' can +-- apply its function argument to each input to each input to the +-- comparison function.+instance Contravariant Comparison where+ contramap f g = Comparison $ \a b -> getComparison g (f a) (f b)++-- | Compare using 'compare'+defaultComparison :: Ord a => Comparison a+defaultComparison = Comparison compare++-- | Define an equivalence relation +newtype Equivalence a = Equivalence { getEquivalence :: a -> a -> Bool } +-- | Equivalence relations are 'Contravariant', because you can +-- apply the contramapped function to each input to the equivalence +-- relation.+instance Contravariant Equivalence where+ contramap f g = Equivalence $ \a b -> getEquivalence g (f a) (f b)++-- | Check for equivalence with '=='+defaultEquivalence :: Eq a => Equivalence a+defaultEquivalence = Equivalence (==)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright 2007-2011 Edward Kmett++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. Neither the name of the author nor the names of his contributors+ may 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 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.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/runhaskell+> module Main (main) where++> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ contravariant.cabal view
@@ -0,0 +1,23 @@+name: contravariant+category: Control, Data+version: 0.1.0+license: BSD3+cabal-version: >= 1.6+license-file: LICENSE+author: Edward A. Kmett+maintainer: Edward A. Kmett <ekmett@gmail.com>+stability: provisional+homepage: http://github.com/ekmett/contravariant/+copyright: Copyright (C) 2007-2011 Edward A. Kmett+synopsis: Haskell 98 contravariant functors+description: Haskell 98 contravariant functors+build-type: Simple++source-repository head+ type: git+ location: git://github.com/ekmett/contravariant.git++library+ build-depends: base < 4.5+ exposed-modules: Data.Functor.Contravariant+ ghc-options: -Wall