File: Constant.hs

package info (click to toggle)
haskell-transformers 0.3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 208 kB
  • sloc: haskell: 1,451; makefile: 3
file content (35 lines) | stat: -rw-r--r-- 929 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- |
-- Module      :  Data.Functor.Constant
-- Copyright   :  (c) Ross Paterson 2010
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  ross@soi.city.ac.uk
-- Stability   :  experimental
-- Portability :  portable
--
-- The constant functor.

module Data.Functor.Constant (
    Constant(..),
   ) where

import Control.Applicative
import Data.Foldable (Foldable(foldMap))
import Data.Monoid (Monoid(..))
import Data.Traversable (Traversable(traverse))

-- | Constant functor.
newtype Constant a b = Constant { getConstant :: a }

instance Functor (Constant a) where
    fmap f (Constant x) = Constant x

instance Foldable (Constant a) where
    foldMap f (Constant x) = mempty

instance Traversable (Constant a) where
    traverse f (Constant x) = pure (Constant x)

instance (Monoid a) => Applicative (Constant a) where
    pure _ = Constant mempty
    Constant x <*> Constant y = Constant (x `mappend` y)