vinyl-plus-0.1.1: Vinyl records utilities

Safe HaskellNone
LanguageHaskell2010

Data.Vinyl.Optic.Tagged.Proxy.Identity

Contents

Description

This module provides accessors and modifiers for labeled records. As the module name indicates, the functions provided by this module are designed to work with records under the following conditions:

  1. The TaggedFunctor is parameterized by Identity.
  2. The VisibleTypeApplication extension is unavailable.

Synopsis

Functions

lens :: forall k g rs i v proxy. (Functor g, IxElem k rs i v) => proxy k -> (v -> g v) -> Rec (TaggedFunctor Identity) rs -> g (Rec (TaggedFunctor Identity) rs) Source

get :: forall k rs i v proxy. IxElem k rs i v => proxy k -> Rec (TaggedFunctor Identity) rs -> v Source

set :: forall k rs i v proxy. IxElem k rs i v => proxy k -> v -> Rec (TaggedFunctor Identity) rs -> Rec (TaggedFunctor Identity) rs Source

modify :: forall k rs i v proxy. IxElem k rs i v => proxy k -> (v -> v) -> Rec (TaggedFunctor Identity) rs -> Rec (TaggedFunctor Identity) rs Source

Tutorial

Here is a explanation of how the functions in this module can be used. First we will create a record:

>>> import Data.Tagged.Functor
>>> import Data.Functor.Identity
>>> import Data.Proxy
>>> import Data.Vinyl.Core
>>> :{
let person = tagIdentity (Proxy :: Proxy "age") (44 :: Int)
          :& tagIdentity (Proxy :: Proxy "name") ("Alexa" :: String)
          :& tagIdentity (Proxy :: Proxy "alive") True
          :& RNil
:}

Notice that the type of person is inferred and fully monomorphic:

>>> :t person
person
  :: Rec
       (TaggedFunctor Identity)
       '['("age", Int), '("name", String), '("alive", Bool)]

The Identity wrappers are cumbersome to deal with. This module provides extra functions (suffixed with Id) that get eliminate some of the boilerplate. With these functions, the above becomes:

>>> get (Proxy :: Proxy "name") person
"Alexa"
>>> let deceased2 = set (Proxy :: Proxy "alive") False person
>>> get (Proxy :: Proxy "alive") deceased2
False
>>> let older2 = modify (Proxy :: Proxy "age") (+12) person
>>> get (Proxy :: Proxy "age") older2
56