generic-lens-0.4.1.0: 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.Sum

Contents

Description

Magic sum 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

Prisms

class AsAny sel a s | s sel k -> a where Source #

Sums that have generic prisms.

Methods

_As :: Prism' s a Source #

A prism that projects a sum as identified by some selector. Currently supported selectors are constructor names and unique types. Compatible with the lens package's Prism type.

>>> dog ^? _As @"Dog"
Just (MkDog {name = "Shep", age = 3})
>>> dog ^? _As @Dog
Just (MkDog {name = "Shep", age = 3})
>>> dog ^? _As @"Cat"
Nothing
>>> cat ^? _As @(Name, Age)
Just ("Mog",5)
>>> cat ^? _As @"Cat"
Just ("Mog",5)
>>> _As @"Cat" # ("Garfield", 6) :: Animal
Cat ("Garfield",6)
>>> duck ^? _As @Age
Just 2

Instances

AsConstructor ctor a s => AsAny Symbol ctor a s Source # 

Methods

_As :: Prism' s s Source #

AsType a s => AsAny * a a s Source # 

Methods

_As :: Prism' s s Source #

class AsConstructor ctor a s | s ctor -> a where Source #

Sums that have a constructor with a given name.

Methods

_Ctor :: Prism' s a Source #

A prism that projects a named constructor from a sum. Compatible with the lens package's Prism type.

>>> dog ^? _Ctor @"Dog"
Just (MkDog {name = "Shep", age = 3})
>>> dog ^? _Ctor @"Cat"
Nothing
>>> cat ^? _Ctor @"Cat"
Just ("Mog", 5)
>>> _Ctor @"Cat" # ("Garfield", 6) :: Animal
Cat ("Garfield", 6)

Instances

(Generic s, ErrorUnless ctor s (HasCtorP ctor (Rep s)), GAsConstructor ctor (Rep s) a) => AsConstructor ctor a s Source # 

Methods

_Ctor :: Prism' s a Source #

class AsSubtype sub sup where Source #

Structural subtyping between sums. A sum Sub is a subtype of another sum Sup if a value of Sub can be given (modulo naming of constructors) whenever a value of Sup is expected. In the running example for instance, FourLeggedAnimal is a subtype of Animal since a value of the former can be given as a value of the latter (renaming Dog4 to Dog and Cat4 to Cat).

Minimal complete definition

injectSub, projectSub

Methods

_Sub :: Prism' sup sub Source #

A prism that captures structural subtyping. Allows a substructure to be injected (upcast) into a superstructure or a superstructure to be downcast into a substructure (which may fail).

>>> _Sub # dog4 :: Animal
Dog (MkDog {name = "Snowy", age = 4})
>>> cat ^? _Sub :: Maybe FourLeggedAnimal
Just (Cat4 "Mog" 5)
>>> duck ^? _Sub :: Maybe FourLeggedAnimal
Nothing

injectSub :: sub -> sup Source #

Injects a subtype into a supertype (upcast).

projectSub :: sup -> Either sup sub Source #

Projects a subtype from a supertype (downcast).

Instances

(Generic sub, Generic sup, GAsSubtype (Rep sub) (Rep sup)) => AsSubtype sub sup Source # 

Methods

_Sub :: Prism' sup sub Source #

injectSub :: sub -> sup Source #

projectSub :: sup -> Either sup sub Source #

class AsType a s where Source #

Sums that have a constructor with a field of the given type.

Minimal complete definition

injectTyped, projectTyped

Methods

_Typed :: Prism' s a Source #

A prism that projects a constructor uniquely identifiable by the type of its field. Compatible with the lens package's Prism type.

>>> dog ^? _Typed @Dog
Just (MkDog {name = "Shep", age = 3})
>>> dog ^? _Typed @Age
Nothing
>>> cat ^? _Typed @(Name, Age)
Just ("Mog",5)
>>> duck ^? _Typed @Age
Just 2

injectTyped :: a -> s Source #

Inject by type.

projectTyped :: s -> Maybe a Source #

Project by type.

Instances

(Generic s, ErrorUnlessOne a s (CountPartialType a (Rep s)), GAsType (Rep s) a) => AsType a s Source # 

Methods

_Typed :: Prism' s a Source #

injectTyped :: a -> s Source #

projectTyped :: s -> Maybe a Source #