generic-lens-1.2.0.0: Generically derive traversals, lenses and prisms.

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

Data.Generics.Sum.Constructors

Contents

Description

Derive constructor-name-based prisms generically.

Synopsis

Prisms

Running example:

>>> :set -XTypeApplications
>>> :set -XDataKinds
>>> :set -XDeriveGeneric
>>> :set -XFlexibleContexts
>>> :set -XTypeFamilies
>>> import GHC.Generics
>>> :m +Data.Generics.Internal.VL.Prism
>>> :m +Data.Generics.Product.Fields
>>> :m +Data.Function
>>> :{
data Animal a
  = Dog (Dog a)
  | Cat Name Age
  | Duck Age
  deriving (Generic, Show)
data Dog a
  = MkDog
  { name   :: Name
  , age    :: Age
  , fieldA :: a
  }
  deriving Show
type Name = String
type Age  = Int
dog, cat, duck :: Animal Int
dog = Dog (MkDog "Shep" 3 30)
cat = Cat "Mog" 5
duck = Duck 2
:}

class AsConstructor (ctor :: Symbol) s t a b | ctor s -> a, ctor t -> b where Source #

Sums that have a constructor with a given name.

Methods

_Ctor :: Prism s t a b 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, fieldA = 30})
>>> dog ^? _Ctor @"Cat"
Nothing
>>> cat ^? _Ctor @"Cat"
Just ("Mog",5)
>>> _Ctor @"Cat" # ("Garfield", 6) :: Animal Int
Cat "Garfield" 6

Type errors

>>> cat ^? _Ctor @"Turtle"
...
...
... The type Animal Int does not contain a constructor named "Turtle"
...
Instances
(ErrorUnless ctor s (HasCtorP ctor (Rep s)), GAsConstructor' ctor (Rep s) a, GAsConstructor' ctor (Rep (Indexed s)) a', GAsConstructor ctor (Rep s) (Rep t) a b, t ~ Infer s a' b, GAsConstructor' ctor (Rep (Indexed t)) b', s ~ Infer t b' a, AsConstructor0 ctor s t a b) => AsConstructor ctor s t a b Source # 
Instance details

Defined in Data.Generics.Sum.Constructors

Methods

_Ctor :: Prism s t a b Source #

AsConstructor ctor (Void1 a) (Void1 b) a b Source #

See Note [Uncluttering type signatures]

Instance details

Defined in Data.Generics.Sum.Constructors

Methods

_Ctor :: Prism (Void1 a) (Void1 b) a b Source #

class AsConstructor_ (ctor :: Symbol) s t a b where Source #

Sums that have a constructor with a given name.

The difference between HasConstructor and HasConstructor_ is similar to the one between HasField and HasField_. See HasField_.

Methods

_Ctor_ :: Prism s t a b Source #

Instances
(ErrorUnless ctor s (HasCtorP ctor (Rep s)), GAsConstructor' ctor (Rep s) a, GAsConstructor' ctor (Rep (Indexed s)) a', GAsConstructor ctor (Rep s) (Rep t) a b, GAsConstructor' ctor (Rep (Indexed t)) b', UnifyHead s t, UnifyHead t s, AsConstructor0 ctor s t a b) => AsConstructor_ ctor s t a b Source # 
Instance details

Defined in Data.Generics.Sum.Constructors

Methods

_Ctor_ :: Prism s t a b Source #

AsConstructor_ ctor (Void1 a) (Void1 b) a b Source # 
Instance details

Defined in Data.Generics.Sum.Constructors

Methods

_Ctor_ :: Prism (Void1 a) (Void1 b) a b Source #

class AsConstructor' (ctor :: Symbol) s a | ctor s -> a where Source #

Methods

_Ctor' :: Prism s s a a Source #

Instances
(Generic s, ErrorUnless ctor s (HasCtorP ctor (Rep s)), GAsConstructor' ctor (Rep s) a, Defined (Rep s) (NoGeneric s ((((Text "arising from a generic prism focusing on the " :<>: QuoteType ctor) :<>: Text " constructor of type ") :<>: QuoteType a) ': ((Text "in " :<>: QuoteType s) ': ([] :: [ErrorMessage])))) ()) => AsConstructor' ctor s a Source # 
Instance details

Defined in Data.Generics.Sum.Constructors

Methods

_Ctor' :: Prism s s a a Source #

class AsConstructor0 (ctor :: Symbol) s t a b where Source #

Sums that have a constructor with a given name.

This class gives the minimal constraints needed to define this prism. For common uses, see HasConstructor.

Methods

_Ctor0 :: Prism s t a b Source #

Instances
(Generic s, Generic t, GAsConstructor ctor (Rep s) (Rep t) a b, Defined (Rep s) (NoGeneric s ((((Text "arising from a generic prism focusing on the " :<>: QuoteType ctor) :<>: Text " constructor of type ") :<>: QuoteType a) ': ((Text "in " :<>: QuoteType s) ': ([] :: [ErrorMessage])))) ()) => AsConstructor0 ctor s t a b Source # 
Instance details

Defined in Data.Generics.Sum.Constructors

Methods

_Ctor0 :: Prism s t a b Source #