packages feed

representable-functors 3.0.1 → 3.1

raw patch · 6 files changed

+128/−133 lines, 6 files

Files

CHANGELOG.markdown view
@@ -1,3 +1,8 @@+3.1+---+* Required Distributive as a superclass+* Renamed `Data.Functor.Corepresentable` to `Data.Functor.Contravariant.Representable` to finally clean up this long-standing abuse of terminology.+ 3.0.1 ----- * Removed intra-package dependencies
LICENSE view
@@ -1,4 +1,4 @@-Copyright 2011 Edward Kmett+Copyright 2011-2013 Edward Kmett  All rights reserved. 
representable-functors.cabal view
@@ -1,6 +1,6 @@ name:          representable-functors category:      Monads, Functors, Data Structures-version:       3.0.1+version:       3.1 license:       BSD3 cabal-version: >= 1.6 license-file:  LICENSE@@ -9,7 +9,7 @@ stability:     provisional homepage:      http://github.com/ekmett/representable-functors/ bug-reports:   http://github.com/ekmett/representable-functors/issues-copyright:     Copyright (C) 2011 Edward A. Kmett+copyright:     Copyright (C) 2011-2013 Edward A. Kmett synopsis:      Representable functors description:   Representable functors build-type:    Simple@@ -56,7 +56,7 @@     transformers         >= 0.2 && < 0.4    exposed-modules:-    Data.Functor.Corepresentable+    Data.Functor.Contravariant.Representable     Data.Functor.Representable     Control.Monad.Representable.Reader     Control.Monad.Representable.State
+ src/Data/Functor/Contravariant/Representable.hs view
@@ -0,0 +1,113 @@+{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances #-}+{-# OPTIONS_GHC -fenable-rewrite-rules #-}+----------------------------------------------------------------------+-- |+-- Copyright   :  (c) Edward Kmett 2011-2013+-- License     :  BSD3+--+-- Maintainer  :  ekmett@gmail.com+-- Stability   :  experimental+--+-- Representable contravariant endofunctors over the category of Haskell+-- types are isomorphic to @(_ -> r)@ and resemble mappings to a+-- fixed range.+----------------------------------------------------------------------+module Data.Functor.Contravariant.Representable+  (+  -- * Values+    Value+  -- * Contravariant Keyed+  , Valued(..)+  -- * Contravariant Indexed+  , Coindexed(..)+  -- * Representable Contravariant Functors+  , Representable(..)+  -- * Default definitions+  , contramapDefault+  , contramapWithValueDefault+  ) where++import Control.Monad.Reader+import Data.Functor.Contravariant+import Data.Functor.Product+import Data.Functor.Coproduct+import Prelude hiding (lookup)++type family Value (f :: * -> *)++-- | Dual to 'Keyed'.+class Contravariant f => Valued f where+  contramapWithValue :: (b -> Either a (Value f)) -> f a -> f b++-- | Dual to 'Indexed'.+class Coindexed f where+  coindex :: f a -> a -> Value f++-- | A 'Contravariant' functor @f@ is 'Representable' if 'contrarep' and 'coindex' witness an isomorphism to @(_ -> Value f)@.+class (Coindexed f, Valued f) => Representable f where+  -- | > contramap f (contrarep g) = contrarep (g . f)+  contrarep :: (a -> Value f) -> f a++{-# RULES+"contrarep/coindex" forall t. contrarep (coindex t) = t+ #-}++-- * Default definitions++contramapDefault :: Representable f => (a -> b) -> f b -> f a+contramapDefault f = contrarep . (. f) . coindex++contramapWithValueDefault :: Representable f => (b -> Either a (Value f)) -> f a -> f b+contramapWithValueDefault f p = contrarep $ either (coindex p) id . f++-- * Dual arrows++type instance Value (Op r) = r++instance Valued (Op r) where+  contramapWithValue = contramapWithValueDefault++instance Coindexed (Op r) where+  coindex = getOp++instance Representable (Op r) where+  contrarep = Op++-- * Predicates++type instance Value Predicate = Bool++instance Valued Predicate where+  contramapWithValue = contramapWithValueDefault++instance Coindexed Predicate where+  coindex = getPredicate++instance Representable Predicate where+  contrarep = Predicate++-- * Products++type instance Value (Product f g) = (Value f, Value g)++instance (Valued f, Valued g) => Valued (Product f g) where+  -- contramapWithValue :: (b -> Either a (Value f)) -> f a -> f b+  contramapWithValue h (Pair f g) = Pair+      (contramapWithValue (fmap fst . h) f)+      (contramapWithValue (fmap snd . h) g)+      -- (contramapWithValue (either id snd . h) g)+      -- (either g snd . h)++instance (Coindexed f, Coindexed g) => Coindexed (Product f g) where+  coindex (Pair f g) a = (coindex f a, coindex g a)++instance (Representable f, Representable g) => Representable (Product f g) where+  contrarep f = Pair (contrarep (fst . f)) (contrarep (snd . f))++-- * Coproducts++type instance Value (Coproduct f g) = Either (Value f) (Value g)++instance (Coindexed f, Coindexed g) => Coindexed (Coproduct f g) where+  coindex (Coproduct (Left f)) a  = Left  $ coindex f a+  coindex (Coproduct (Right g)) a = Right $ coindex g a
− src/Data/Functor/Corepresentable.hs
@@ -1,122 +0,0 @@-{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances #-}-{-# OPTIONS_GHC -fenable-rewrite-rules #-}--------------------------------------------------------------------------- |--- Module      :  Data.Functor.Corepresentable--- Copyright   :  (c) Edward Kmett 2011--- License     :  BSD3--- --- Maintainer  :  ekmett@gmail.com--- Stability   :  experimental--- --- Representable contravariant endofunctors over the category of Haskell --- types are isomorphic to @(_ -> r)@ and resemble mappings to a--- fixed range.-------------------------------------------------------------------------module Data.Functor.Corepresentable-  ( -  -- * Values-    Value-  -- * Contravariant Keyed-  , Valued(..)-  -- * Contravariant Indexed-  , Coindexed(..)-  -- * Representable Contravariant Functors-  , Corepresentable(..)-  -- * Default definitions-  , contramapDefault-  , contramapWithValueDefault-  ) where--import Control.Monad.Reader-import Data.Functor.Contravariant-import Data.Functor.Product-import Data.Functor.Coproduct-import Prelude hiding (lookup)--type family Value (f :: * -> *)---- | Dual to 'Keyed'.-class Contravariant f => Valued f where-  contramapWithValue :: (b -> Either a (Value f)) -> f a -> f b---- | Dual to 'Indexed'.-class Coindexed f where-  coindex :: f a -> a -> Value f---- | A 'Functor' @f@ is 'Corepresentable' if 'corep' and 'coindex' witness an isomorphism to @(_ -> Value f)@.------ > tabulate . index = id--- > index . tabulate = id--- > tabulate . return f = return f--class (Coindexed f, Valued f) => Corepresentable f where-  -- | > contramap f (corep g) = corep (g . f)-  corep :: (a -> Value f) -> f a--{-# RULES-"corep/coindex" forall t. corep (coindex t) = t- #-}---- * Default definitions--contramapDefault :: Corepresentable f => (a -> b) -> f b -> f a-contramapDefault f = corep . (. f) . coindex --contramapWithValueDefault :: Corepresentable f => (b -> Either a (Value f)) -> f a -> f b-contramapWithValueDefault f p = corep $ either (coindex p) id . f---- * Dual arrows--type instance Value (Op r) = r--instance Valued (Op r) where-  contramapWithValue = contramapWithValueDefault--instance Coindexed (Op r) where-  coindex = getOp--instance Corepresentable (Op r) where-  corep = Op---- * Predicates--type instance Value Predicate = Bool--instance Valued Predicate where-  contramapWithValue = contramapWithValueDefault--instance Coindexed Predicate where-  coindex = getPredicate--instance Corepresentable Predicate where-  corep = Predicate---- * Products--type instance Value (Product f g) = (Value f, Value g)--instance (Valued f, Valued g) => Valued (Product f g) where-  -- contramapWithValue :: (b -> Either a (Value f)) -> f a -> f b-  contramapWithValue h (Pair f g) = Pair -      (contramapWithValue (fmap fst . h) f)-      (contramapWithValue (fmap snd . h) g)-      -- (contramapWithValue (either id snd . h) g)-      -- (either g snd . h)--instance (Coindexed f, Coindexed g) => Coindexed (Product f g) where-  coindex (Pair f g) a = (coindex f a, coindex g a)--instance (Corepresentable f, Corepresentable g) => Corepresentable (Product f g) where-  corep f = Pair (corep (fst . f)) (corep (snd . f))----- * Coproducts--type instance Value (Coproduct f g) = Either (Value f) (Value g)--instance (Coindexed f, Coindexed g) => Coindexed (Coproduct f g) where-  coindex (Coproduct (Left f)) a  = Left  $ coindex f a -  coindex (Coproduct (Right g)) a = Right $ coindex g a
src/Data/Functor/Representable.hs view
@@ -7,14 +7,13 @@ {-# OPTIONS_GHC -fenable-rewrite-rules #-} ---------------------------------------------------------------------- -- |--- Module      :  Data.Functor.Representable--- Copyright   :  (c) Edward Kmett 2011+-- Copyright   :  (c) Edward Kmett 2011-2013 -- License     :  BSD3--- +-- -- Maintainer  :  ekmett@gmail.com -- Stability   :  experimental--- --- Representable endofunctors over the category of Haskell types are +--+-- Representable endofunctors over the category of Haskell types are -- isomorphic to the reader monad and so inherit a very large number -- of properties for free. ----------------------------------------------------------------------@@ -79,7 +78,7 @@ -- > index . tabulate = id -- > tabulate . return f = return f -class (Functor f, Indexable f) => Representable f where+class (Distributive f, Indexable f) => Representable f where   -- | > fmap f . tabulate = tabulate . fmap f   tabulate :: (Key f -> a) -> f a @@ -157,7 +156,7 @@   tabulate = id  instance Representable m => Representable (ReaderT e m) where-  tabulate = ReaderT . fmap tabulate . curry +  tabulate = ReaderT . fmap tabulate . curry  instance (Representable f, Representable g) => Representable (Compose f g) where   tabulate = Compose . tabulate . fmap tabulate . curry