generic-lens
Copyright(C) 2020 Csongor Kiss
LicenseBSD3
MaintainerCsongor Kiss <kiss.csongor.kiss@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Generics.Sum.Typed

Contents

Description

Derive constructor-field-type-based prisms generically.

Synopsis

Prisms

>>> :set -XTypeApplications
>>> :set -XDataKinds
>>> :set -XDeriveGeneric
>>> import GHC.Generics
>>> import Control.Lens
>>> :{
data Animal
  = Dog Dog
  | Cat Name Age
  | Duck Age
  | Turtle Age
  deriving (Generic, Show)
data Dog
  = MkDog
  { name :: Name
  , age  :: Age
  }
  deriving (Generic, Show)
type Name = String
newtype Age  = Age Int deriving Show
dog, cat, duck :: Animal
dog = Dog (MkDog "Shep" (Age 3))
cat = Cat "Mog" (Age 5)
duck = Duck (Age 2)
>>> :}

class AsType a s where Source #

Types that can represent another type, either by being a sum with a constructor containing that type, or by actually being that type.

Minimal complete definition

injectTyped, projectTyped | _Typed

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 = Age 3})
>>> cat ^? _Typed @(Name, Age)
Just ("Mog",Age 5)
>>> dog ^? _Typed @Age
...
...
... The type Animal contains multiple constructors whose fields are of type Age.
... The choice of constructor is thus ambiguous, could be any of:
... Duck
... Turtle
...

injectTyped :: a -> s Source #

Inject by type.

>>> :{
dog :: Dog
dog = MkDog "Fido" (Age 11)
dogAsAnimal :: Animal
dogAsAnimal = injectTyped dog
dogAsItself :: Dog
dogAsItself = injectTyped dog
>>> :}

projectTyped :: s -> Maybe a Source #

Project by type.

>>> :{
dogAsAnimal :: Animal
dogAsAnimal = Dog (MkDog "Fido" (Age 11))
mDog :: Maybe Dog
mDog = projectTyped dogAsAnimal
>>> :}

Instances

Instances details
AsType Void a Source #

Uncluttering type signatures (see Void)

>>> :t +d _Typed @Int
_Typed @Int
  :: (AsType Int s, Choice p, Applicative f) =>
     p Int (f Int) -> p s (f s)
Instance details

Defined in Data.Generics.Sum.Typed

Methods

_Typed :: Prism' a Void Source #

injectTyped :: Void -> a Source #

projectTyped :: a -> Maybe Void Source #

AsType a Void Source #

Uncluttering type signatures (see Void)

>>> :t +d _Typed
_Typed
  :: (AsType a s, Choice p, Applicative f) => p a (f a) -> p s (f s)
Instance details

Defined in Data.Generics.Sum.Typed

Methods

_Typed :: Prism' Void a Source #

injectTyped :: a -> Void Source #

projectTyped :: Void -> Maybe a Source #

AsType a a Source #

Every type can be treated as itself.

Instance details

Defined in Data.Generics.Sum.Typed

Methods

_Typed :: Prism' a a Source #

injectTyped :: a -> a Source #

projectTyped :: a -> Maybe a Source #

Context a s => AsType a s Source # 
Instance details

Defined in Data.Generics.Sum.Typed

Methods

_Typed :: Prism' s a Source #

injectTyped :: a -> s Source #

projectTyped :: s -> Maybe a Source #