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.Sum.Subtype

Contents

Description

Structural subtype relationships between sum types.

Synopsis

Prisms

class AsSubtype sub sup where #

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 #

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 #

Injects a subtype into a supertype (upcast).

projectSub :: sup -> Either sup sub #

Projects a subtype from a supertype (downcast).

Instances

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

Methods

_Sub :: Prism' sup sub #

injectSub :: sub -> sup #

projectSub :: sup -> Either sup sub #