vinyl-0.8.1.1: 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 # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

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

Applicative Identity Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

pure :: a -> Identity a #

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

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

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

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

Foldable Identity Source # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

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

Ord a => Ord (Identity a) Source # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

show :: Identity a -> String #

showList :: [Identity a] -> ShowS #

Storable a => Storable (Identity a) Source # 
Instance details

Defined in Data.Vinyl.Functor

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) # 
Instance details

Defined in Data.Vinyl.CoRec

Methods

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

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

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

Defined in Data.Vinyl.CoRec

data Thunk a Source #

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

Constructors

Thunk 

Fields

Instances
Monad Thunk Source # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

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

Applicative Thunk Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

pure :: a -> Thunk a #

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

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

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

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

Foldable Thunk Source # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

show :: Thunk a -> String #

showList :: [Thunk a] -> ShowS #

newtype Lift (op :: l -> l' -> *) (f :: k -> l) (g :: k -> l') (x :: k) Source #

Constructors

Lift 

Fields

Instances
(Functor f, Functor g) => Functor (Lift Either f g) Source # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

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

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

(*>) :: 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 :: l -> *) (g :: k -> l) (x :: k) Source #

Constructors

Compose 

Fields

Instances
(Functor f, Functor g) => Functor (Compose f g) Source # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

pure :: a -> Compose f g a #

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

liftA2 :: (a -> b -> c) -> Compose f g a -> Compose f g b -> Compose f g c #

(*>) :: 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 # 
Instance details

Defined in Data.Vinyl.Functor

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 # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

sequenceA :: Applicative f0 => Compose f g (f0 a) -> f0 (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 f g x) Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

sizeOf :: Compose f g x -> Int #

alignment :: Compose f g x -> Int #

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

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

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

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

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

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

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

newtype Const (a :: *) (b :: k) Source #

Constructors

Const 

Fields

Instances
Functor (Const a :: * -> *) Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

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

Foldable (Const a :: * -> *) Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

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

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

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

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

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

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

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

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

null :: Const a a0 -> Bool #

length :: Const a a0 -> Int #

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

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

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

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

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

Traversable (Const a :: * -> *) Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

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

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

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

Show a => Show (Const a b) Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

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

show :: Const a b -> String #

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

Storable a => Storable (Const a b) Source # 
Instance details

Defined in Data.Vinyl.Functor

Methods

sizeOf :: Const a b -> Int #

alignment :: Const a b -> Int #

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

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

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

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

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

poke :: Ptr (Const a b) -> Const 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.