comonad 0.7.0 → 0.9.0
raw patch · 3 files changed
+143/−49 lines, 3 filesdep +semigroupsPVP ok
version bump matches the API change (PVP)
Dependencies added: semigroups
API changes (from Hackage documentation)
- Control.Comonad: (<<=) :: Comonad w => (w a -> b) -> w a -> w b
- Control.Comonad: (=<=) :: Comonad w => (w b -> c) -> (w a -> b) -> w a -> c
- Control.Comonad: (=>=) :: Comonad w => (w a -> b) -> (w b -> c) -> w a -> c
- Control.Comonad: (=>>) :: Comonad w => w a -> (w a -> b) -> w b
- Control.Comonad: duplicate :: Comonad w => w a -> w (w a)
- Control.Comonad: extend :: Comonad w => (w a -> b) -> w a -> w b
- Control.Comonad: instance Monoid m => Comonad ((->) m)
+ Control.Comonad: instance (Semigroup m, Monoid m) => Comonad ((->) m)
+ Data.Functor.Extend: (<<=) :: Extend w => (w a -> b) -> w a -> w b
+ Data.Functor.Extend: (=<=) :: Extend w => (w b -> c) -> (w a -> b) -> w a -> c
+ Data.Functor.Extend: (=>=) :: Extend w => (w a -> b) -> (w b -> c) -> w a -> c
+ Data.Functor.Extend: (=>>) :: Extend w => w a -> (w a -> b) -> w b
+ Data.Functor.Extend: class Functor w => Extend w
+ Data.Functor.Extend: duplicate :: Extend w => w a -> w (w a)
+ Data.Functor.Extend: extend :: Extend w => (w a -> b) -> w a -> w b
+ Data.Functor.Extend: instance Extend ((,) e)
+ Data.Functor.Extend: instance Extend (Either a)
+ Data.Functor.Extend: instance Extend Identity
+ Data.Functor.Extend: instance Extend Maybe
+ Data.Functor.Extend: instance Extend []
+ Data.Functor.Extend: instance Extend w => Extend (IdentityT w)
+ Data.Functor.Extend: instance Semigroup m => Extend ((->) m)
- Control.Comonad: class Functor w => Comonad w
+ Control.Comonad: class Extend w => Comonad w
Files
- Control/Comonad.hs +5/−47
- Data/Functor/Extend.hs +134/−0
- comonad.cabal +4/−2
Control/Comonad.hs view
@@ -14,16 +14,12 @@ -- * Comonads -- $definition Comonad(..)- , (=>>) -- :: Comonad w => w a -> (w a -> b) -> w b- , (<<=) -- :: Comonad w => (w a -> b) -> w a -> w b , liftW -- :: Comonad w => (a -> b) -> w a -> w b , wfix -- :: Comonad w => w (w a -> a) -> a -- * Cokleisli Arrows , Cokleisli(..)- -- ** Cokleisli composition- , (=>=) -- :: Comonad w => (w a -> b) -> (w b -> c) -> w a -> c- , (=<=) -- :: Comonad w => (w b -> c) -> (w a -> b) -> w a -> c+ , module Data.Functor.Extend ) where import Prelude hiding (id, (.))@@ -32,29 +28,18 @@ import Control.Category import Control.Monad.Trans.Identity import Data.Functor.Identity+import Data.Functor.Extend import Data.Monoid import Data.Typeable--infixl 1 =>> -infixr 1 <<=, =<=, =>= +import Data.Semigroup {- | $definition -} -class Functor w => Comonad w where+class Extend w => Comonad w where -- | -- > extract . fmap f = f . extract extract :: w a -> a- -- | - -- > duplicate = extend id- -- > fmap (fmap f) . duplicate = duplicate . fmap f- duplicate :: w a -> w (w a)- -- |- -- > extend f = fmap f . duplicate- extend :: (w a -> b) -> w a -> w b - extend f = fmap f . duplicate- duplicate = extend id- -- | A suitable default definition for 'fmap' for a 'Comonad'. -- Promotes a function to a comonad. --@@ -63,26 +48,6 @@ liftW f = extend (f . extract) {-# INLINE liftW #-} --- | 'extend' with the arguments swapped. Dual to '>>=' for a 'Monad'.-(=>>) :: Comonad w => w a -> (w a -> b) -> w b-(=>>) = flip extend-{-# INLINE (=>>) #-}---- | 'extend' in operator form -(<<=) :: Comonad w => (w a -> b) -> w a -> w b-(<<=) = extend-{-# INLINE (<<=) #-}---- | Right-to-left Cokleisli composition -(=<=) :: Comonad w => (w b -> c) -> (w a -> b) -> w a -> c-f =<= g = f . extend g-{-# INLINE (=<=) #-}---- | Left-to-right Cokleisli composition-(=>=) :: Comonad w => (w a -> b) -> (w b -> c) -> w a -> c-f =>= g = g . extend f -{-# INLINE (=>=) #-}- -- | Comonadic fixed point wfix :: Comonad w => w (w a -> a) -> a wfix w = extract w (extend wfix w)@@ -99,11 +64,9 @@ instance Comonad ((,)e) where extract = snd- duplicate ~(e,a) = (e,(e,a)) -instance Monoid m => Comonad ((->)m) where+instance (Semigroup m, Monoid m) => Comonad ((->)m) where extract f = f mempty- duplicate f m = f . mappend m -- * Comonads for types from 'transformers'. --@@ -112,14 +75,11 @@ -- TODO: Petition to move Data.Functor.Identity into base instance Comonad Identity where extract = runIdentity- extend f = Identity . f - duplicate = Identity -- Provided to avoid an orphan instance. Not proposed to standardize. -- If Comonad moved to base, consider moving instance into transformers? instance Comonad w => Comonad (IdentityT w) where extract = extract . runIdentityT- extend f (IdentityT m) = IdentityT (extend (f . IdentityT) m) -- | The 'Cokleisli' 'Arrow's of a given 'Comonad' newtype Cokleisli w a b = Cokleisli { runCokleisli :: w a -> b }@@ -163,7 +123,6 @@ return = Cokleisli . const Cokleisli k >>= f = Cokleisli $ \w -> runCokleisli (f (k w)) w - {- $definition There are two ways to define a comonad:@@ -207,4 +166,3 @@ the definition of 'liftW' respectively. -}-
+ Data/Functor/Extend.hs view
@@ -0,0 +1,134 @@+-----------------------------------------------------------------------------+-- |+-- Module : Data.Functor.Extend+-- Copyright : (C) 2011 Edward Kmett+-- License : BSD-style (see the file LICENSE)+--+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : provisional+-- Portability : portable+--+----------------------------------------------------------------------------+module Data.Functor.Extend+ ( -- * $definition+ Extend(..)+ , (=>>) -- :: Extend w => w a -> (w a -> b) -> w b+ , (<<=) -- :: Extend w => (w a -> b) -> w a -> w b+ , (=>=) -- :: Extend w => (w a -> b) -> (w b -> c) -> w a -> c+ , (=<=) -- :: Extend w => (w b -> c) -> (w a -> b) -> w a -> c+ ) where++import Prelude hiding (id, (.))+import Control.Category+import Control.Monad.Trans.Identity+import Data.Functor.Identity+import Data.Semigroup+import Data.List (tails)++infixl 1 =>> +infixr 1 <<=, =<=, =>= ++class Functor w => Extend w where+ -- | + -- > duplicate = extend id+ -- > fmap (fmap f) . duplicate = duplicate . fmap f+ duplicate :: w a -> w (w a)+ -- |+ -- > extend f = fmap f . duplicate+ extend :: (w a -> b) -> w a -> w b++ extend f = fmap f . duplicate+ duplicate = extend id++-- | 'extend' with the arguments swapped. Dual to '>>=' for a 'Monad'.+(=>>) :: Extend w => w a -> (w a -> b) -> w b+(=>>) = flip extend+{-# INLINE (=>>) #-}++-- | 'extend' in operator form +(<<=) :: Extend w => (w a -> b) -> w a -> w b+(<<=) = extend+{-# INLINE (<<=) #-}++-- | Right-to-left Cokleisli composition +(=<=) :: Extend w => (w b -> c) -> (w a -> b) -> w a -> c+f =<= g = f . extend g+{-# INLINE (=<=) #-}++-- | Left-to-right Cokleisli composition+(=>=) :: Extend w => (w a -> b) -> (w b -> c) -> w a -> c+f =>= g = g . extend f +{-# INLINE (=>=) #-}++-- * Extends for Prelude types:+--+-- Instances: While Data.Functor.Extend.Instances would be symmetric+-- to the definition of Control.Monad.Instances in base, the reason+-- the latter exists is because of Haskell 98 specifying the types+-- @'Either' a@, @((,)m)@ and @((->)e)@ and the class Monad without+-- having the foresight to require or allow instances between them.+--+-- Here Haskell 98 says nothing about Extend, so we can include the+-- instances directly avoiding the wart of orphan instances.++instance Extend [] where+ duplicate = tails++instance Extend Maybe where+ duplicate Nothing = Nothing+ duplicate j = Just j++instance Extend (Either a) where+ duplicate (Left a) = Left a+ duplicate r = Right r++instance Extend ((,)e) where+ duplicate p = (fst p, p)++instance Semigroup m => Extend ((->)m) where+ duplicate f m = f . (<>) m++-- I can't fix the world+-- instance (Monoid m, Extend n) => Extend (ReaderT m n) +-- duplicate f m = f . mappend m++-- * Extends for types from 'transformers'.+--+-- This isn't really a transformer, so i have no compunction about including the instance here.+--+-- TODO: Petition to move Data.Functor.Identity into base+instance Extend Identity where+ duplicate = Identity++-- Provided to avoid an orphan instance. Not proposed to standardize. +-- If Extend moved to base, consider moving instance into transformers?+instance Extend w => Extend (IdentityT w) where+ extend f (IdentityT m) = IdentityT (extend (f . IdentityT) m)++{- $definition++There are two ways to define an 'Extend' instance:++I. Provide definitions for 'extend'+satisfying this law:++> extend f . extend g = extend (f . extend g)++II. Alternately, you may choose to provide definitions for 'duplicate' +satisfying this laws:++> duplicate . duplicate = fmap duplicate . duplicate++These are both equivalent to the statement that (=>=) is associative++> (f =>= g) =>= h = f =>= (g =>= h)++You may of course, choose to define both 'duplicate' /and/ 'extend'. +In that case you must also satisfy these laws:++> extend f = fmap f . duplicate+> duplicate = extend id++These are the default definitions of 'extend' and 'duplicate'.++-}
comonad.cabal view
@@ -1,6 +1,6 @@ name: comonad category: Control, Comonads-version: 0.7.0+version: 0.9.0 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE@@ -20,9 +20,11 @@ library build-depends: base >= 4 && < 4.4,- transformers >= 0.2.0 && < 0.3+ transformers >= 0.2.0 && < 0.3,+ semigroups >= 0.3.4 && < 0.5 exposed-modules: Control.Comonad+ Data.Functor.Extend ghc-options: -Wall