Skip to content

Commit

Permalink
initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Feb 16, 2011
0 parents commit 33213f6
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
_darcs
dist
102 changes: 102 additions & 0 deletions Data/Copointed.hs
@@ -0,0 +1,102 @@
module Data.Copointed where

import Control.Comonad
import Data.Default
import Data.Functor.Identity
import Data.Functor.Compose
import Data.Functor.Coproduct
import Data.Tree
import Data.Monoid as Monoid
import Data.Semigroup as Semigroup
import Control.Monad.Trans.Identity
import qualified Control.Monad.Trans.Writer.Lazy as Lazy
import qualified Control.Monad.Trans.Writer.Strict as Strict
import qualified Control.Comonad.Trans.Discont.Lazy as Lazy
import qualified Control.Comonad.Trans.Discont.Memo as Memo
import qualified Control.Comonad.Trans.Discont.Strict as Strict
import qualified Control.Comonad.Trans.Env.Lazy as Lazy
import qualified Control.Comonad.Trans.Env.Strict as Strict
import qualified Control.Comonad.Trans.Store.Lazy as Lazy
import qualified Control.Comonad.Trans.Store.Memo as Memo
import qualified Control.Comonad.Trans.Store.Strict as Strict

-- | 'Copointed' does not require a 'Functor', as the only relationship
-- between 'copoint' and 'fmap' is given by a free theorem.

class Copointed p where
copoint :: p a -> a

instance Copointed Identity where
copoint = runIdentity

instance Default m => Copointed ((->)m) where
copoint f = f def

instance Copointed ((,) a) where
copoint = snd

instance Copointed ((,,) a b) where
copoint (_,_,a) = a

instance Copointed ((,,,) a b c) where
copoint (_,_,_,a) = a

instance Copointed Tree where
copoint = rootLabel

instance (Copointed p, Copointed q) => Copointed (Compose p q) where
copoint = copoint . copoint . getCompose

instance (Copointed p, Copointed q) => Copointed (Coproduct p q) where
copoint = coproduct copoint copoint

instance Copointed m => Copointed (IdentityT m) where
copoint = copoint . runIdentityT

instance Copointed m => Copointed (Lazy.WriterT w m) where
copoint = fst . copoint . Lazy.runWriterT

instance Copointed m => Copointed (Strict.WriterT w m) where
copoint = fst . copoint . Strict.runWriterT

instance Copointed Dual where
copoint = getDual

instance Copointed Sum where
copoint = getSum

instance Copointed Semigroup.First where
copoint = Semigroup.getFirst

instance Copointed Semigroup.Last where
copoint = Semigroup.getLast

instance Copointed Semigroup.Max where
copoint = Semigroup.getMax

instance Copointed Semigroup.Min where
copoint = Semigroup.getMin

instance Copointed (Lazy.DiscontT s w) where
copoint (Lazy.DiscontT f w) = f w

instance Copointed (Strict.DiscontT s w) where
copoint (Strict.DiscontT f w) = f w

instance Copointed (Memo.DiscontT s w) where
copoint = extract

instance Copointed w => Copointed (Lazy.EnvT e w) where
copoint = copoint . Lazy.lowerEnvT

instance Copointed w => Copointed (Strict.EnvT e w) where
copoint = copoint . Strict.lowerEnvT

instance Copointed w => Copointed (Lazy.StoreT s w) where
copoint (Lazy.StoreT wf s) = copoint wf s

instance Copointed w => Copointed (Strict.StoreT s w) where
copoint (Strict.StoreT wf s) = copoint wf s

instance Copointed w => Copointed (Memo.StoreT s w) where
copoint = copoint . Memo.lowerStoreT
155 changes: 155 additions & 0 deletions Data/Pointed.hs
@@ -0,0 +1,155 @@
module Data.Pointed where

import Control.Arrow
import Control.Applicative
import Control.Concurrent.STM
import Data.Default
import Data.Monoid as Monoid
import Data.Semigroup as Semigroup
import Data.Functor.Identity
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Tree (Tree(..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Functor.Constant
import qualified Data.Functor.Product as Functor
import Data.Functor.Compose
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Error
import Control.Monad.Trans.List
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Identity
import qualified Control.Monad.Trans.RWS.Lazy as Lazy
import qualified Control.Monad.Trans.RWS.Strict as Strict
import qualified Control.Monad.Trans.Writer.Lazy as Lazy
import qualified Control.Monad.Trans.Writer.Strict as Strict
import qualified Control.Monad.Trans.State.Lazy as Lazy
import qualified Control.Monad.Trans.State.Strict as Strict
import Data.Semigroupoid.Static

-- | 'Pointed' does not require a 'Functor', as the only relationship
-- between 'point' and 'fmap' is given by a free theorem.

class Pointed p where
point :: a -> p a

instance Pointed [] where
point a = [a]

instance Pointed Maybe where
point = Just

instance Pointed (Either a) where
point = Right

instance Pointed IO where
point = return

instance Pointed STM where
point = return

instance Pointed Tree where
point a = Node a []

instance Pointed ZipList where
point = pure

instance Pointed Identity where
point = Identity

instance Pointed ((->)e) where
point = const

instance Default e => Pointed ((,)e) where
point = (,) def

instance Monad m => Pointed (WrappedMonad m) where
point = WrapMonad . return

instance Default m => Pointed (Const m) where
point _ = Const def

instance Arrow a => Pointed (WrappedArrow a b) where
point = pure

instance Pointed Dual where
point = Dual

instance Pointed Endo where
point = Endo . const

instance Pointed Sum where
point = Sum

instance Pointed Monoid.Product where
point = Monoid.Product

instance Pointed Monoid.First where
point = Monoid.First . Just

instance Pointed Monoid.Last where
point = Monoid.Last . Just

instance Pointed Semigroup.First where
point = Semigroup.First

instance Pointed Semigroup.Last where
point = Semigroup.Last

instance Pointed Semigroup.Max where
point = Semigroup.Max

instance Pointed Semigroup.Min where
point = Semigroup.Min

instance Pointed Seq where
point = Seq.singleton

instance Pointed Set where
point = Set.singleton

instance (Pointed p, Pointed q) => Pointed (Compose p q) where
point = Compose . point . point

instance (Pointed p, Pointed q) => Pointed (Functor.Product p q) where
point a = Functor.Pair (point a) (point a)

instance Default m => Pointed (Constant m) where
point _ = Constant def

instance Pointed (ContT r m) where
point a = ContT ($ a)

instance Pointed m => Pointed (ErrorT e m) where
point = ErrorT . point . Right

instance Pointed m => Pointed (IdentityT m) where
point = IdentityT . point

instance Pointed m => Pointed (ListT m) where
point = ListT . point . point

instance Pointed m => Pointed (MaybeT m) where
point = MaybeT . point . point

instance (Default w, Pointed m) => Pointed (Lazy.RWST r w s m) where
point a = Lazy.RWST $ \_ s -> point (a, s, def)

instance (Default w, Pointed m) => Pointed (Strict.RWST r w s m) where
point a = Strict.RWST $ \_ s -> point (a, s, def)

instance (Default w, Pointed m) => Pointed (Lazy.WriterT w m) where
point a = Lazy.WriterT $ point (a, def)

instance (Default w, Pointed m) => Pointed (Strict.WriterT w m) where
point a = Strict.WriterT $ point (a, def)

instance Pointed m => Pointed (Lazy.StateT s m) where
point a = Lazy.StateT $ \s -> point (a, s)

instance Pointed m => Pointed (Strict.StateT s m) where
point a = Strict.StateT $ \s -> point (a, s)

instance Pointed m => Pointed (Static m a) where
point = Static . point . const
30 changes: 30 additions & 0 deletions LICENSE
@@ -0,0 +1,30 @@
Copyright 2008-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.
7 changes: 7 additions & 0 deletions Setup.lhs
@@ -0,0 +1,7 @@
#!/usr/bin/runhaskell
> module Main (main) where

> import Distribution.Simple

> main :: IO ()
> main = defaultMain
36 changes: 36 additions & 0 deletions pointed.cabal
@@ -0,0 +1,36 @@
name: pointed
category: 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/pointed/
copyright: Copyright (C) 2008-2011 Edward A. Kmett
synopsis: Pointed and Copointed
description: Pointed and Copointed
build-type: Simple

source-repository head
type: git
location: git://github.com/ekmett/pointed.git

library
build-depends:
base >= 4 && < 4.4,
transformers >= 0.2.0 && < 0.3,
containers >= 0.4.0.0 && < 0.5,
data-default >= 0.2.0.1 && < 0.3,
comonad >= 1.0.1 && < 1.1,
comonad-transformers >= 1.5.2.1 && < 1.6,
semigroups >= 0.3.4 && < 0.4,
semigroupoids >= 1.1.1 && < 1.2,
stm >= 2.1.2.1 && < 2.2

exposed-modules:
Data.Pointed
Data.Copointed

ghc-options: -Wall

0 comments on commit 33213f6

Please sign in to comment.