HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulegeneric-lens-2.2.2.0Haskell2010

Data.Generics.Sum.Subtype

Structural subtype relationships between sum types.

  • 1 class

Prisms

1 declaration

Running example:

Example6 expressions
:set -XTypeApplications:set -XDataKinds:set -XDeriveGenericimport GHC.Genericsimport Control.Lens:{data Animal  = Dog Dog  | Cat Name Age  | Duck Age  deriving (Generic, Show)data FourLeggedAnimal  = Dog4 Dog  | Cat4 Name Age  deriving (Generic, Show)data Dog = MkDog  { name :: Name  , age  :: Age  }  deriving (Generic, Show)type Name = Stringtype Age  = Intdog, cat, duck :: Animaldog = Dog (MkDog "Shep" 3)cat = Cat "Mog" 5duck = Duck 2dog4, cat4 :: FourLeggedAnimaldog4 = Dog4 (MkDog "Snowy" 4)cat4 = Cat4 "Garfield" 6:}
classclass 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).

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).

    Example1 expression
    _Sub # dog4 :: AnimalDog (MkDog {name = "Snowy", age = 4})
    Example1 expression
    cat ^? _Sub :: Maybe FourLeggedAnimalJust (Cat4 "Mog" 5)
    Example1 expression
    duck ^? _Sub :: Maybe FourLeggedAnimalNothing
  • injectSub :: sub -> sup

    Injects a subtype into a supertype (upcast).

  • projectSub :: sup -> Maybe sub

    Projects a subtype from a supertype (downcast).

Instances4AsSubtype
  • AsSubtype Void aDefined in generic-lens-2.2.2.0 · Data.Generics.Sum.Subtype

    Uncluttering type signatures (see Void)

    Example1 expression
    :t +d _Sub @Int_Sub @Int  :: (AsSubtype Int sup, Choice p, Applicative f) =>     p Int (f Int) -> p sup (f sup)
  • AsSubtype a VoidDefined in generic-lens-2.2.2.0 · Data.Generics.Sum.Subtype

    Uncluttering type signatures (see Void)

    Example1 expression
    :t +d _Sub_Sub  :: (AsSubtype sub sup, Choice p, Applicative f) =>     p sub (f sub) -> p sup (f sup)
  • AsSubtype a aDefined in generic-lens-2.2.2.0 · Data.Generics.Sum.Subtype

    Reflexive case

    Example1 expression
    _Sub # dog :: AnimalDog (MkDog {name = "Shep", age = 3})
  • Context sub sup => AsSubtype sub supDefined in generic-lens-2.2.2.0 · Data.Generics.Sum.Subtype