HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulegeneric-lens-2.2.2.0Haskell2010

Data.Generics.Sum.Constructors

Derive constructor-name-based prisms generically.

  • 4 classes

Prisms

4 declarations

Running example:

Example10 expressions
:set -XTypeApplications:set -XDataKinds:set -XDeriveGeneric:set -XFlexibleContexts:set -XTypeFamiliesimport GHC.Genericsimport Control.Lens: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 Showtype Name = Stringtype Age  = Intdog, cat, duck :: Animal Intdog = Dog (MkDog "Shep" 3 30)cat = Cat "Mog" 5duck = Duck 2:}
classclass AsConstructor (ctor :: Symbol) s t a b | ctor s -> a, ctor t -> b where
#

Sums that have a constructor with a given name.

Methods

  • _Ctor :: Prism s t a b

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

    Example1 expression
    dog ^? _Ctor @"Dog"Just (MkDog {name = "Shep", age = 3, fieldA = 30})
    Example1 expression
    dog ^? _Ctor @"Cat"Nothing
    Example1 expression
    cat ^? _Ctor @"Cat"Just ("Mog",5)
    Example1 expression
    _Ctor @"Cat" # ("Garfield", 6) :: Animal IntCat "Garfield" 6
    Type errors
    Example1 expression
    cat ^? _Ctor @"Turtle"......... The type Animal Int does not contain a constructor named "Turtle"...
Instances2AsConstructor
  • (Context ctor s t a b, AsConstructor0 ctor s t a b) => AsConstructor ctor s t a bDefined in generic-lens-2.2.2.0 · Data.Generics.Sum.Constructors
  • AsConstructor ctor (Void1 a) (Void1 b) a bDefined in generic-lens-2.2.2.0 · Data.Generics.Sum.Constructors

    Uncluttering type signatures (see Void)

    Example1 expression
    :t +d _Ctor_Ctor  :: (AsConstructor ctor s t a b, Choice p, Applicative f) =>     p a (f b) -> p s (f t)
classclass AsConstructor_ (ctor :: Symbol) s t a b where
#

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

Instances2AsConstructor_
classclass AsConstructor0 (ctor :: Symbol) s t a b where
#

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

Instances1AsConstructor0
  • Context0 ctor s t a b => AsConstructor0 ctor s t a bDefined in generic-lens-2.2.2.0 · Data.Generics.Sum.Constructors