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

GHC 10.0.0.20260917 · lts/ghc-10.0.x · fc4e598 · 2026-10-11

Modulegeneric-optics-2.3.0.0Haskell2010

Data.Generics.Product.Subtype

Structural subtype relationships between product types.

  • 1 class

Lenses

1 declaration

Running example:

Example7 expressions
:set -XTypeApplications:set -XDataKinds:set -XDeriveGeneric:set -XDuplicateRecordFieldsimport GHC.Genericsimport Optics.Core:{data Human = Human  { name    :: String  , age     :: Int  , address :: String  }  deriving (Generic, Show)data Animal = Animal  { name    :: String  , age     :: Int  }  deriving (Generic, Show)human :: Humanhuman = Human "Tunyasz" 50 "London":}
classclass Subtype sup sub where
#

Structural subtype relationship

sub is a (structural) subtype of sup, if its fields are a subset of those of sup.

Methods

  • super :: Lens sub sub sup sup

    Structural subtype lens. Given a subtype relationship sub :< sup, we can focus on the sub structure of sup.

    Example1 expression
    human ^. super @AnimalAnimal {name = "Tunyasz", age = 50}
    Example1 expression
    set (super @Animal) (Animal "dog" 10) humanHuman {name = "dog", age = 10, address = "London"}
  • upcast :: sub -> sup

    Cast the more specific subtype to the more general supertype

    Example1 expression
    upcast human :: AnimalAnimal {name = "Tunyasz", age = 50}
    Example1 expression
    upcast (upcast human :: Animal) :: Human...... The type 'Animal' is not a subtype of 'Human'.... The following fields are missing from 'Animal':... address...
  • smash :: sup -> sub -> sub

    Plug a smaller structure into a larger one

    Example1 expression
    smash (Animal "dog" 10) humanHuman {name = "dog", age = 10, address = "London"}
Instances4Subtype
  • Context a b => Subtype b aDefined in generic-optics-2.3.0.0 · Data.Generics.Product.Subtype
  • Subtype Void aDefined in generic-optics-2.3.0.0 · Data.Generics.Product.Subtype

    Uncluttering type signatures (see Void)

    Example1 expression
    :t super @Intsuper @Int :: Subtype Int sub => Lens sub sub Int Int
  • Subtype a VoidDefined in generic-optics-2.3.0.0 · Data.Generics.Product.Subtype

    Uncluttering type signatures (see Void)

    Example1 expression
    :t supersuper :: Subtype sup sub => Lens sub sub sup sup
  • Subtype a aDefined in generic-optics-2.3.0.0 · Data.Generics.Product.Subtype