vinyl-0.6.0: Extensible Records

Safe HaskellNone
LanguageHaskell2010

Data.Vinyl.Functor

Contents

Synopsis

Introduction

This module provides functors and functor compositions that can be used as the interpretation function for a Rec. For a more full discussion of this, scroll down to the bottom.

newtype Identity a Source #

This is identical to the Identity from Data.Functor.Identity in "base" except for its Show instance.

Constructors

Identity 

Fields

Instances

Monad Identity Source # 

Methods

(>>=) :: Identity a -> (a -> Identity b) -> Identity b #

(>>) :: Identity a -> Identity b -> Identity b #

return :: a -> Identity a #

fail :: String -> Identity a #

Functor Identity Source # 

Methods

fmap :: (a -> b) -> Identity a -> Identity b #

(<$) :: a -> Identity b -> Identity a #

Applicative Identity Source # 

Methods

pure :: a -> Identity a #

(<*>) :: Identity (a -> b) -> Identity a -> Identity b #

(*>) :: Identity a -> Identity b -> Identity b #

(<*) :: Identity a -> Identity b -> Identity a #

Foldable Identity Source # 

Methods

fold :: Monoid m => Identity m -> m #

foldMap :: Monoid m => (a -> m) -> Identity a -> m #

foldr :: (a -> b -> b) -> b -> Identity a -> b #

foldr' :: (a -> b -> b) -> b -> Identity a -> b #

foldl :: (b -> a -> b) -> b -> Identity a -> b #

foldl' :: (b -> a -> b) -> b -> Identity a -> b #

foldr1 :: (a -> a -> a) -> Identity a -> a #

foldl1 :: (a -> a -> a) -> Identity a -> a #

toList :: Identity a -> [a] #

null :: Identity a -> Bool #

length :: Identity a -> Int #

elem :: Eq a => a -> Identity a -> Bool #

maximum :: Ord a => Identity a -> a #

minimum :: Ord a => Identity a -> a #

sum :: Num a => Identity a -> a #

product :: Num a => Identity a -> a #

Traversable Identity Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Identity a -> f (Identity b) #

sequenceA :: Applicative f => Identity (f a) -> f (Identity a) #

mapM :: Monad m => (a -> m b) -> Identity a -> m (Identity b) #

sequence :: Monad m => Identity (m a) -> m (Identity a) #

Eq a => Eq (Identity a) Source # 

Methods

(==) :: Identity a -> Identity a -> Bool #

(/=) :: Identity a -> Identity a -> Bool #

Ord a => Ord (Identity a) Source # 

Methods

compare :: Identity a -> Identity a -> Ordering #

(<) :: Identity a -> Identity a -> Bool #

(<=) :: Identity a -> Identity a -> Bool #

(>) :: Identity a -> Identity a -> Bool #

(>=) :: Identity a -> Identity a -> Bool #

max :: Identity a -> Identity a -> Identity a #

min :: Identity a -> Identity a -> Identity a #

Show a => Show (Identity a) Source # 

Methods

showsPrec :: Int -> Identity a -> ShowS #

show :: Identity a -> String #

showList :: [Identity a] -> ShowS #

Storable a => Storable (Identity a) Source # 

Methods

sizeOf :: Identity a -> Int #

alignment :: Identity a -> Int #

peekElemOff :: Ptr (Identity a) -> Int -> IO (Identity a) #

pokeElemOff :: Ptr (Identity a) -> Int -> Identity a -> IO () #

peekByteOff :: Ptr b -> Int -> IO (Identity a) #

pokeByteOff :: Ptr b -> Int -> Identity a -> IO () #

peek :: Ptr (Identity a) -> IO (Identity a) #

poke :: Ptr (Identity a) -> Identity a -> IO () #

(RecAll * Maybe ts Eq, RecApplicative * ts) => Eq (CoRec * Identity ts) # 

Methods

(==) :: CoRec * Identity ts -> CoRec * Identity ts -> Bool #

(/=) :: CoRec * Identity ts -> CoRec * Identity ts -> Bool #

(AllConstrained * Show ts, RecApplicative * ts) => Show (CoRec * Identity ts) # 

data Thunk a Source #

Used this instead of Identity to make a record lazy in its fields.

Constructors

Thunk 

Fields

Instances

Monad Thunk Source # 

Methods

(>>=) :: Thunk a -> (a -> Thunk b) -> Thunk b #

(>>) :: Thunk a -> Thunk b -> Thunk b #

return :: a -> Thunk a #

fail :: String -> Thunk a #

Functor Thunk Source # 

Methods

fmap :: (a -> b) -> Thunk a -> Thunk b #

(<$) :: a -> Thunk b -> Thunk a #

Applicative Thunk Source # 

Methods

pure :: a -> Thunk a #

(<*>) :: Thunk (a -> b) -> Thunk a -> Thunk b #

(*>) :: Thunk a -> Thunk b -> Thunk b #

(<*) :: Thunk a -> Thunk b -> Thunk a #

Foldable Thunk Source # 

Methods

fold :: Monoid m => Thunk m -> m #

foldMap :: Monoid m => (a -> m) -> Thunk a -> m #

foldr :: (a -> b -> b) -> b -> Thunk a -> b #

foldr' :: (a -> b -> b) -> b -> Thunk a -> b #

foldl :: (b -> a -> b) -> b -> Thunk a -> b #

foldl' :: (b -> a -> b) -> b -> Thunk a -> b #

foldr1 :: (a -> a -> a) -> Thunk a -> a #

foldl1 :: (a -> a -> a) -> Thunk a -> a #

toList :: Thunk a -> [a] #

null :: Thunk a -> Bool #

length :: Thunk a -> Int #

elem :: Eq a => a -> Thunk a -> Bool #

maximum :: Ord a => Thunk a -> a #

minimum :: Ord a => Thunk a -> a #

sum :: Num a => Thunk a -> a #

product :: Num a => Thunk a -> a #

Traversable Thunk Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Thunk a -> f (Thunk b) #

sequenceA :: Applicative f => Thunk (f a) -> f (Thunk a) #

mapM :: Monad m => (a -> m b) -> Thunk a -> m (Thunk b) #

sequence :: Monad m => Thunk (m a) -> m (Thunk a) #

Show a => Show (Thunk a) Source # 

Methods

showsPrec :: Int -> Thunk a -> ShowS #

show :: Thunk a -> String #

showList :: [Thunk a] -> ShowS #

newtype Lift op f g x Source #

Constructors

Lift 

Fields

Instances

(Functor f, Functor g) => Functor (Lift * * * Either f g) Source # 

Methods

fmap :: (a -> b) -> Lift * * * Either f g a -> Lift * * * Either f g b #

(<$) :: a -> Lift * * * Either f g b -> Lift * * * Either f g a #

(Functor f, Functor g) => Functor (Lift * * * (,) f g) Source # 

Methods

fmap :: (a -> b) -> Lift * * * (,) f g a -> Lift * * * (,) f g b #

(<$) :: a -> Lift * * * (,) f g b -> Lift * * * (,) f g a #

(Applicative f, Applicative g) => Applicative (Lift * * * (,) f g) Source # 

Methods

pure :: a -> Lift * * * (,) f g a #

(<*>) :: Lift * * * (,) f g (a -> b) -> Lift * * * (,) f g a -> Lift * * * (,) f g b #

(*>) :: Lift * * * (,) f g a -> Lift * * * (,) f g b -> Lift * * * (,) f g b #

(<*) :: Lift * * * (,) f g a -> Lift * * * (,) f g b -> Lift * * * (,) f g a #

newtype Compose f g x Source #

Constructors

Compose 

Fields

Instances

(Functor f, Functor g) => Functor (Compose * * f g) Source # 

Methods

fmap :: (a -> b) -> Compose * * f g a -> Compose * * f g b #

(<$) :: a -> Compose * * f g b -> Compose * * f g a #

(Applicative f, Applicative g) => Applicative (Compose * * f g) Source # 

Methods

pure :: a -> Compose * * f g a #

(<*>) :: Compose * * f g (a -> b) -> Compose * * f g a -> Compose * * f g b #

(*>) :: Compose * * f g a -> Compose * * f g b -> Compose * * f g b #

(<*) :: Compose * * f g a -> Compose * * f g b -> Compose * * f g a #

(Foldable f, Foldable g) => Foldable (Compose * * f g) Source # 

Methods

fold :: Monoid m => Compose * * f g m -> m #

foldMap :: Monoid m => (a -> m) -> Compose * * f g a -> m #

foldr :: (a -> b -> b) -> b -> Compose * * f g a -> b #

foldr' :: (a -> b -> b) -> b -> Compose * * f g a -> b #

foldl :: (b -> a -> b) -> b -> Compose * * f g a -> b #

foldl' :: (b -> a -> b) -> b -> Compose * * f g a -> b #

foldr1 :: (a -> a -> a) -> Compose * * f g a -> a #

foldl1 :: (a -> a -> a) -> Compose * * f g a -> a #

toList :: Compose * * f g a -> [a] #

null :: Compose * * f g a -> Bool #

length :: Compose * * f g a -> Int #

elem :: Eq a => a -> Compose * * f g a -> Bool #

maximum :: Ord a => Compose * * f g a -> a #

minimum :: Ord a => Compose * * f g a -> a #

sum :: Num a => Compose * * f g a -> a #

product :: Num a => Compose * * f g a -> a #

(Traversable f, Traversable g) => Traversable (Compose * * f g) Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Compose * * f g a -> f (Compose * * f g b) #

sequenceA :: Applicative f => Compose * * f g (f a) -> f (Compose * * f g a) #

mapM :: Monad m => (a -> m b) -> Compose * * f g a -> m (Compose * * f g b) #

sequence :: Monad m => Compose * * f g (m a) -> m (Compose * * f g a) #

Storable (f (g x)) => Storable (Compose k l f g x) Source # 

Methods

sizeOf :: Compose k l f g x -> Int #

alignment :: Compose k l f g x -> Int #

peekElemOff :: Ptr (Compose k l f g x) -> Int -> IO (Compose k l f g x) #

pokeElemOff :: Ptr (Compose k l f g x) -> Int -> Compose k l f g x -> IO () #

peekByteOff :: Ptr b -> Int -> IO (Compose k l f g x) #

pokeByteOff :: Ptr b -> Int -> Compose k l f g x -> IO () #

peek :: Ptr (Compose k l f g x) -> IO (Compose k l f g x) #

poke :: Ptr (Compose k l f g x) -> Compose k l f g x -> IO () #

type (:.) f g = Compose f g infixr 9 Source #

newtype Const a b Source #

Constructors

Const 

Fields

Instances

Functor (Const * a) Source # 

Methods

fmap :: (a -> b) -> Const * a a -> Const * a b #

(<$) :: a -> Const * a b -> Const * a a #

Foldable (Const * a) Source # 

Methods

fold :: Monoid m => Const * a m -> m #

foldMap :: Monoid m => (a -> m) -> Const * a a -> m #

foldr :: (a -> b -> b) -> b -> Const * a a -> b #

foldr' :: (a -> b -> b) -> b -> Const * a a -> b #

foldl :: (b -> a -> b) -> b -> Const * a a -> b #

foldl' :: (b -> a -> b) -> b -> Const * a a -> b #

foldr1 :: (a -> a -> a) -> Const * a a -> a #

foldl1 :: (a -> a -> a) -> Const * a a -> a #

toList :: Const * a a -> [a] #

null :: Const * a a -> Bool #

length :: Const * a a -> Int #

elem :: Eq a => a -> Const * a a -> Bool #

maximum :: Ord a => Const * a a -> a #

minimum :: Ord a => Const * a a -> a #

sum :: Num a => Const * a a -> a #

product :: Num a => Const * a a -> a #

Traversable (Const * a) Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Const * a a -> f (Const * a b) #

sequenceA :: Applicative f => Const * a (f a) -> f (Const * a a) #

mapM :: Monad m => (a -> m b) -> Const * a a -> m (Const * a b) #

sequence :: Monad m => Const * a (m a) -> m (Const * a a) #

Show a => Show (Const k a b) Source # 

Methods

showsPrec :: Int -> Const k a b -> ShowS #

show :: Const k a b -> String #

showList :: [Const k a b] -> ShowS #

Storable a => Storable (Const k a b) Source # 

Methods

sizeOf :: Const k a b -> Int #

alignment :: Const k a b -> Int #

peekElemOff :: Ptr (Const k a b) -> Int -> IO (Const k a b) #

pokeElemOff :: Ptr (Const k a b) -> Int -> Const k a b -> IO () #

peekByteOff :: Ptr b -> Int -> IO (Const k a b) #

pokeByteOff :: Ptr b -> Int -> Const k a b -> IO () #

peek :: Ptr (Const k a b) -> IO (Const k a b) #

poke :: Ptr (Const k a b) -> Const k a b -> IO () #

Discussion

Example

The data types in this module are used to build interpretation fuctions for a Rec. To build a Rec that is simply a heterogeneous list, use Identity:

>>> :{
let myRec1 :: Rec Identity '[Int,Bool,Char]
    myRec1 = Identity 4 :& Identity True :& Identity 'c' :& RNil
:}

For a record in which the fields are optional, you could alternatively write:

>>> :{
let myRec2 :: Rec Maybe '[Int,Bool,Char]
    myRec2 = Just 4 :& Nothing :& Nothing :& RNil
:}

And we can gather all of the effects with rtraverse:

>>> let r2 = rtraverse (fmap Identity) myRec2
>>> :t r2
r2 :: Maybe (Rec Identity '[Int, Bool, Char])
>>> r2
Nothing

If the fields only exist once an environment is provided, you can build the record as follows:

>>> :{
let myRec3 :: Rec ((->) Int) '[Int,Bool,Char]
    myRec3 = (+5) :& (const True) :& (head . show) :& RNil
:}

And again, we can collect these effects with "rtraverse":

>>> (rtraverse (fmap Identity) myRec3) 8
{13, True, '8'}

If you want the composition of these two effects, you can use Compose:

>>> import Data.Char (chr)
>>> :{
let safeDiv a b = if b == 0 then Nothing else Just (div a b)
    safeChr i = if i >= 32 && i <= 126 then Just (chr i) else Nothing
    myRec4 :: Rec (Compose ((->) Int) Maybe) '[Int,Char]
    myRec4 = (Compose $ safeDiv 42) :& (Compose safeChr) :& RNil
:}

Ecosystem

Of the five data types provided by this modules, three can be found in others places: Identity, Compose, and Const. They are included with "vinyl" to help keep the dependency list small. The differences will be discussed here.

The Data.Functor.Identity module was originally provided by "transformers". When GHC 7.10 was released, it was moved into "base-4.8". The Identity data type provided by that module is well recognized across the haskell ecosystem and has typeclass instances for lots of common typeclasses. The significant difference between it and the copy of it provided here is that this one has a different Show instance. This is illustrated below:

>>> Identity "hello"
"hello"

But, when using Identity from "base":

>>> import qualified Data.Functor.Identity as Base
>>> Base.Identity "hello"
Identity "hello"

This Show instance makes records look nicer in GHCi. Feel free to use Data.Functor.Identity if you do not need the prettier output or if you need the many additional typeclass instances that are provided for the standard Identity.

The story with Compose and Const is much more simple. These also exist in "transformers", although Const is named Constant there. Prior to the release of "transformers-0.5", they were not polykinded, making them unusable for certain universes. However, in "transformers-0.5" and forward, they have been made polykinded. This means that they are just as usable with Rec as the vinyl equivalents but with many more typeclass instances such as Ord and Show.