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-optics-2.2.1.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.2.1.0 · Data.Generics.Product.Subtype
  • Subtype Void aDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Subtype

    See Note [Uncluttering type signatures] >>> :t super Int super Int :: Subtype Int sub => Lens sub sub Int Int

  • Subtype a VoidDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Subtype

    See Note [Uncluttering type signatures] >>> :t super super :: Subtype sup sub => Lens sub sub sup sup

  • Subtype a aDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Subtype