generic-lens-2.2.2.0: Generically derive traversals, lenses and prisms.

Copyright | (C) 2020 Csongor Kiss
License | BSD3
Maintainer | Csongor Kiss <kiss.csongor.kiss@gmail.com>
Stability | experimental
Portability | non-portable
Safe Haskell | None
Language | Haskell2010

Data.Generics.Product.Subtype

Contents

- Lenses

Description

Structural subtype relationships between product types.

Synopsis

- class Subtype sup sub where
  - super :: Lens sub sub sup sup
  - upcast :: sub -> sup
  - smash :: sup -> sub -> sub

# Lenses

## Running example:

    >>> :set -XTypeApplications
    >>> :set -XDataKinds
    >>> :set -XDeriveGeneric
    >>> :set -XDuplicateRecordFields
    >>> import GHC.Generics
    >>> :m +Data.Generics.Internal.VL.Lens
    >>> :{
    data Human = Human
      { name    :: String
      , age     :: Int
      , address :: String
      }
      deriving (Generic, Show)
    data Animal = Animal
      { name    :: String
      , age     :: Int
      }
      deriving (Generic, Show)
    human :: Human
    human = Human "Tunyasz" 50 "London"
    :}

class Subtype sup sub where Source #

Structural subtype relationship

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

Minimal complete definition

super | smash, upcast

Methods

super :: Lens sub sub sup sup Source #

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

    >>> human ^. super @Animal
    Animal {name = "Tunyasz", age = 50}

    >>> set (super @Animal) (Animal "dog" 10) human
    Human {name = "dog", age = 10, address = "London"}

upcast :: sub -> sup Source #

Cast the more specific subtype to the more general supertype

    >>> upcast human :: Animal
    Animal {name = "Tunyasz", age = 50}

    >>> 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 Source #

Plug a smaller structure into a larger one

    >>> smash (Animal "dog" 10) human
    Human {name = "dog", age = 10, address = "London"}

#### Instances

Instances details

Subtype Void a Source #

Uncluttering type signatures (see `Void`)

    >>> :t +d super @Int
    super @Int
      :: (Subtype Int sub, Functor f) => (Int -> f Int) -> sub -> f sub

Instance details Defined in Data.Generics.Product.Subtype Methods super :: Lens a a Void Void Source # upcast :: a -> Void Source # smash :: Void -> a -> a Source #

Subtype a Void Source #

Uncluttering type signatures (see `Void`)

    >>> :t +d super
    super
      :: (Subtype sup sub, Functor f) => (sup -> f sup) -> sub -> f sub

Instance details Defined in Data.Generics.Product.Subtype Methods super :: Lens Void Void a a Source # upcast :: Void -> a Source # smash :: a -> Void -> Void Source #
Subtype a a Source # |
Instance details Defined in Data.Generics.Product.Subtype Methods super :: Lens a a a a Source # upcast :: a -> a Source # smash :: a -> a -> a Source #
Context a b => Subtype b a Source # |
Instance details Defined in Data.Generics.Product.Subtype Methods super :: Lens a a b b Source # upcast :: a -> b Source # smash :: b -> a -> a Source #