generic-lens-0.4.0.1: Generic data-structure operations exposed as lenses.

Copyright(C) 2017 Csongor Kiss
LicenseBSD3
MaintainerCsongor Kiss <kiss.csongor.kiss@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellSafe
LanguageHaskell2010

Data.Generics.Product

Contents

Description

Magic product operations using Generics

These classes need not be instantiated manually, as GHC can automatically prove valid instances via Generics. Only the Generic class needs to be derived (see examples).

Synopsis

Lenses

class HasAny (sel :: k) a s | s sel k -> a where #

Records that have generic lenses.

Minimal complete definition

the

Methods

the :: Lens' s a #

A lens that focuses on a part of a product as identified by some selector. Currently supported selectors are field names, positions and unique types. Compatible with the lens package's Lens type.

>>> human ^. the @Int
50
>>> human ^. the @"name"
"Tunyasz"
>>> human ^. the @3
"London"

Instances

HasPosition i a s => HasAny Nat i a s # 

Methods

the :: Lens' s s #

HasField field a s => HasAny Symbol field a s # 

Methods

the :: Lens' s s #

HasType a s => HasAny * a a s # 

Methods

the :: Lens' s s #

class HasField (field :: Symbol) a s | s field -> a where #

Records that have a field with a given name.

Minimal complete definition

field | setField, getField

Methods

field :: Lens' s a #

A lens that focuses on a field with a given name. Compatible with the lens package's Lens type.

>>> human ^. field @"age"
50
>>> human & field @"name" .~ "Tamas"
Human {name = "Tamas", age = 50, address = "London"}

getField :: s -> a #

Get field

>>> getField @"name" human
"Tunyasz"

setField :: a -> s -> s #

Set field

>>> setField @"age" (setField @"name" "Tamas" human) 30
Human {name = "Tamas", age = 30, address = "London"}

Instances

(Generic s, ErrorUnless field s (HasTotalFieldP * field (Rep s)), GHasField field (Rep s) a) => HasField field a s # 

Methods

field :: Lens' s a #

getField :: s -> a #

setField :: a -> s -> s #

class HasPosition (i :: Nat) a s | s i -> a where #

Records that have a field at a given position.

Minimal complete definition

position | setPosition, getPosition

Methods

position :: Lens' s a #

A lens that focuses on a field at a given position. Compatible with the lens package's Lens type.

>>> human ^. position @1
"Tunyasz"
>>> human & position @2 .~ "Berlin"
Human {name = "Tunyasz", age = 50, address = "Berlin"}

getPosition :: s -> a #

Get positional field

>>> getPosition @1 human
"Tunyasz"

setPosition :: a -> s -> s #

Set positional field

>>> setPosition @2 (setField @1 "Tamas" human) 30
Human "Tamas" 30 "London"

Instances

(Generic s, ErrorUnless i s ((&&) ((<?) 0 i) ((<=?) i (Size * (Rep s)))), GHasPosition 1 i (Rep s) a) => HasPosition i a s # 

Methods

position :: Lens' s a #

getPosition :: s -> a #

setPosition :: a -> s -> s #

class HasType a s where #

Records that have a field with a unique type.

Minimal complete definition

typed | setTyped, getTyped

Methods

typed :: Lens' s a #

A lens that focuses on a field with a unique type in its parent type. Compatible with the lens package's Lens type.

>>> human ^. typed @Int
50

getTyped :: s -> a #

Get field at type.

setTyped :: a -> s -> s #

Set field at type.

Instances

(Generic s, ErrorUnlessOne a s (CountTotalType * a (Rep s)), GHasType (Rep s) a) => HasType a s # 

Methods

typed :: Lens' s a #

getTyped :: s -> a #

setTyped :: a -> s -> s #

Subtype relationships

class Subtype sup sub where #

Structural subtype relationship

sub is a (structural) subtype of sup, if its fields are a subset of those of sup.

Minimal complete definition

super | smash, upcast

Methods

super :: Lens' sub sup #

Structural subtype lens. Given a subtype relationship sub :< sup, we can focus on the sub structure of sup.

>>> human ^. super @Animal
Animal {name = "Tunyasz", age = 50}
>>> set (super @Animal) (Animal "dog" 10) human
Human {name = "dog", age = 10, address = "London"}

upcast :: sub -> sup #

Cast the more specific subtype to the more general supertype

>>> upcast human :: Animal
Animal {name = "Tunyasz", age = 50}

smash :: sup -> sub -> sub #

Plug a smaller structure into a larger one

>>> smash (Animal "dog" 10) human
Human {name = "dog", age = 10, address = "London"}

Instances

(GSmash * (Rep a) (Rep b), GUpcast (Rep a) (Rep b), Generic a, Generic b) => Subtype b a # 

Methods

super :: Lens' a b #

upcast :: a -> b #

smash :: b -> a -> a #