HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulevinyl-0.14.3Haskell2010

Data.Vinyl.Functor

  • 7 types
  • 1 value
  • Packagevinyl-0.14.3
  • Exports8
  • LanguageHaskell2010
  • LicenceMIT
  • SourceFunctor.hs

Introduction

8 declarations

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.

newtypenewtype Identity a
#

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

Constructors

Instances14Monad, Functor, Applicative, Foldable, Traversable, IsoHKD, …
datadata Thunk a
#

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

Constructors

Instances6Monad, Functor, Applicative, Foldable, Traversable, Show
newtypenewtype Lift (op :: l -> l' -> Type) (f :: k -> l) (g :: k -> l') (x :: k)
#

Constructors

Instances5IsoHKD, Functor, Applicative, HKD
newtypenewtype ElField (t :: (Symbol, Type))
#

A value with a phantom Symbol label. It is not a Haskell Functor, but it is used in many of the same places a Functor is used in vinyl.

Morally: newtype ElField (s, t) = Field t But GHC doesn't allow that

Constructors

Instances19Eq, Floating, Fractional, Num, Ord, Real, …
newtypenewtype Compose (f :: l -> Type) (g :: k -> l) (x :: k)
#

Constructors

Instances12IsoHKD, Functor, Applicative, Foldable, Traversable, Show, …
valueonCompose :: (f (g a) -> h (k2 a)) -> (:.) f g a -> (:.) h k2 a
#

Apply a function to a value whose type is the application of the Compose type constructor. This works under the Compose newtype wrapper.

newtypenewtype Const a (b :: k)
#

Constructors

Instances8Functor, Foldable, Traversable, Eq, Show, Generic, …

Discussion

0 declarations

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:

Example1 expression
:{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:

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

And we can gather all of the effects with rtraverse:

Example3 expressions
let r2 = rtraverse (fmap Identity) myRec2:t r2r2 :: Maybe (Rec Identity '[Int, Bool, Char])r2Nothing

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

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

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

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

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

Example2 expressions
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:

Example1 expression
Identity "hello""hello"

But, when using Identity from "base":

Example2 expressions
import qualified Data.Functor.Identity as BaseBase.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.