HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulegeneric-optics-2.3.0.0Haskell2010

Data.Generics.Sum.Typed

Derive constructor-field-type-based prisms generically.

  • 1 class

Prisms

1 declaration
Example6 expressions
:set -XTypeApplications:set -XDataKinds:set -XDeriveGenericimport GHC.Genericsimport Optics.Core:{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 = Stringnewtype Age  = Age Int deriving Showdog, cat, duck :: Animaldog = Dog (MkDog "Shep" (Age 3))cat = Cat "Mog" (Age 5)duck = Duck (Age 2):}
classclass AsType a s where
#

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

Methods

  • _Typed :: Prism' s a

    A prism that projects a constructor uniquely identifiable by the type of its field.

    Example3 expressions
    dog ^? _Typed @DogJust (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

    Inject by type.

    Example2 expressions
    :{dog :: Dogdog = MkDog "Fido" (Age 11)dogAsAnimal :: AnimaldogAsAnimal = injectTyped dogdogAsItself :: DogdogAsItself = injectTyped dog:}
  • projectTyped :: s -> Maybe a

    Project by type.

    Example2 expressions
    :{dogAsAnimal :: AnimaldogAsAnimal = Dog (MkDog "Fido" (Age 11))mDog :: Maybe DogmDog = projectTyped dogAsAnimal:}
Instances4AsType
  • Context a s => AsType a sDefined in generic-optics-2.3.0.0 · Data.Generics.Sum.Typed
  • AsType Void aDefined in generic-optics-2.3.0.0 · Data.Generics.Sum.Typed

    Uncluttering type signatures (see Void)

    Example1 expression
    :t _Typed @Int_Typed @Int :: AsType Int s => Prism' s Int
  • AsType a VoidDefined in generic-optics-2.3.0.0 · Data.Generics.Sum.Typed

    Uncluttering type signatures (see Void)

    Example1 expression
    :t _Typed_Typed :: AsType a s => Prism' s a
  • AsType a aDefined in generic-optics-2.3.0.0 · Data.Generics.Sum.Typed

    Every type can be treated as itself.