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

Moduleoptics-core-0.4.1.1Haskell2010

Optics.Generic

This module provides, for data types having a Generic instance, a way to focus on:

  • their named total fields via gfield,

  • their named partial fields via gafield,

  • their constructors via gconstructor,

  • their fields at a specific position via gposition,

  • their fields of a specific type via gplate.

Note: gfield and gconstructor are supported by labelOptic and can be used with a consise syntax via OverloadedLabels.

If you're looking for optics for working with a generic representation of a data type, there's GHC.Generics.Optics.

  • 5 classes

Fields

2 declarations
classclass GField (name :: Symbol) s t a b | name s -> t a b, name t -> s a b where
#

Focus on a field name of type a within a type s using its Generic instance.

Example1 expression
:{data User a  = User { name :: String         , age  :: a         }  | LazyUser { name :: String             , age  :: a             , lazy :: Bool             }  deriving (Show, Generic):}
Example1 expression
let user = User "Tom" 32 :: User Int
Example1 expression
user ^. gfield @"name""Tom"
Example1 expression
user ^. gfield @"age"32
Example1 expression
user ^. gfield @"salary"......Data constructor ‘User’ doesn't have a field named ‘salary’...In the......

Only total fields are accessible (for partial ones see gafield):

Example1 expression
user ^. gfield @"lazy"......Data constructor ‘User’ doesn't have a field named ‘lazy’...In the......

Type changing updates are supported:

Example1 expression
user & gfield @"age" .~ ()User {name = "Tom", age = ()}

Types without a Generic instance are not supported:

Example1 expression
NoG 'x' ^. gfield @"any"......Type ‘NoG’ doesn't have a Generic instance...In the......

Note: gfield is supported by labelOptic and can be used with a concise syntax via OverloadedLabels.

Example1 expression
user ^. #name"Tom"
Example1 expression
user & #age %~ (+1)User {name = "Tom", age = 33}

Methods

Instances2GField
  • GFieldContext name s t a b => GField name s t a bDefined in optics-core-0.4.1.1 · Optics.Generic
  • (a ~ Void0, b ~ Void0) => GField name Void0 Void0 a bDefined in optics-core-0.4.1.1 · Optics.Generic

    Hidden instance.

classclass GAffineField (name :: Symbol) s t a b | name s -> t a b, name t -> s a b where
#

Focus on a possibly partial field name of type a within a type s using its Generic instance.

Example1 expression
:{data Fish = Herring { name :: String }          | Tuna    { name :: String, sleeping :: Bool }  deriving Generic:}
Example2 expressions
let herring = Herring { name = "Henry" }let tuna    = Tuna { name = "Tony", sleeping = True }
Example1 expression
herring ^? gafield @"name"Just "Henry"
Example1 expression
herring ^? gafield @"sleeping"Nothing
Example1 expression
tuna ^? gafield @"sleeping"Just True

Types without a Generic instance are not supported:

Example1 expression
NoG 'x' ^? gafield @"any"......Type ‘NoG’ doesn't have a Generic instance...In the......

Note: trying to access a field that doesn't exist in any data constructor results in an error:

Example1 expression
tuna ^? gafield @"salary"......Type ‘Fish’ doesn't have a field named ‘salary’...In the......

Methods

Instances2GAffineField
  • GAFieldContext repDefined name s t a b => GAffineField name s t a bDefined in optics-core-0.4.1.1 · Optics.Generic
  • (a ~ Void0, b ~ Void0) => GAffineField name Void0 Void0 a bDefined in optics-core-0.4.1.1 · Optics.Generic

    Hidden instance.

Positions

1 declaration
classclass GPosition (n :: Nat) s t a b | n s -> t a b, n t -> s a b where
#

Focus on a field at position n of type a within a type s using its Generic instance.

Example1 expression
('a', 'b', 'c') ^. gposition @2'b'
Example1 expression
('a', 'b') & gposition @1 .~ "hi" & gposition @2 .~ "there"("hi","there")
Example1 expression
('a', 'b', 'c') ^. gposition @4......Data constructor ‘(,,)’ has 3 fields, 4th requested...In the......
Example1 expression
() ^. gposition @1......Data constructor ‘()’ has no fields, 1st requested...In the......

Types without a Generic instance are not supported:

Example1 expression
NoG 'x' ^. gposition @1......Type ‘NoG’ doesn't have a Generic instance...In the......

Note: Positions start from 1:

Example1 expression
('a', 'b') ^. gposition @0......There is no 0th position...In the......

Methods

Instances2GPosition
  • GPositionContext repDefined n s t a b => GPosition n s t a bDefined in optics-core-0.4.1.1 · Optics.Generic
  • (a ~ Void0, b ~ Void0) => GPosition name Void0 Void0 a bDefined in optics-core-0.4.1.1 · Optics.Generic

    Hidden instance.

Constructors

1 declaration
classclass GConstructor (name :: Symbol) s t a b | name s -> t a b, name t -> s a b where
#

Focus on a constructor name of a type s using its Generic instance.

Example1 expression
:{data Animal = Dog { name :: String, age :: Int }            | Cat { name :: String, purrs :: Bool }  deriving (Show, Generic):}
Example2 expressions
let dog = Dog "Sparky" 2let cat = Cat "Cuddly" True
Example1 expression
dog ^? gconstructor @"Dog"Just ("Sparky",2)
Example1 expression
dog ^? gconstructor @"Cat"Nothing
Example1 expression
cat & gconstructor @"Cat" % _2 %~ notCat {name = "Cuddly", purrs = False}
Example1 expression
dog & gconstructor @"Cat" % _1 .~ "Merry"Dog {name = "Sparky", age = 2}
Example1 expression
cat ^? gconstructor @"Parrot"......Type ‘Animal’ doesn't have a constructor named ‘Parrot’...In the......

Types without a Generic instance are not supported:

Example1 expression
NoG 'x' ^. gconstructor @"NoG"......Type ‘NoG’ doesn't have a Generic instance...In the......

Note: gconstructor is supported by labelOptic and can be used with a concise syntax via OverloadedLabels.

Example1 expression
dog ^? #_DogJust ("Sparky",2)
Example1 expression
cat & #_Cat % _1 .~ "Merry"Cat {name = "Merry", purrs = True}

Methods

Instances2GConstructor
  • GConstructorContext repDefined name s t a b => GConstructor name s t a bDefined in optics-core-0.4.1.1 · Optics.Generic
  • (a ~ Void0, b ~ Void0) => GConstructor name Void0 Void0 a bDefined in optics-core-0.4.1.1 · Optics.Generic

    Hidden instance.

Types

1 declaration
classclass GPlate a s where
#

Traverse occurrences of a type a within a type s using its Generic instance.

Example1 expression
toListOf (gplate @Char) ('h', ((), 'e', Just 'l'), "lo")"hello"

If a occurs recursively in its own definition, only outermost occurrences of a within s will be traversed:

Example1 expression
toListOf (gplate @String) ("one","two")["one","two"]

Note: types without a Generic instance in scope when GPlate class constraint is resolved will not be entered during the traversal.

Example1 expression
let noG = (NoG 'n', (Just 'i', "c"), 'e')
Example1 expression
toListOf (gplate @Char) noG"ice"
Example1 expression
deriving instance Generic NoG
Example1 expression
toListOf (gplate @Char) noG"nice"

Methods

Instances3GPlate
  • GPlate Void0 aDefined in optics-core-0.4.1.1 · Optics.Generic

    Hidden instance.

  • GPlate a Void0Defined in optics-core-0.4.1.1 · Optics.Generic

    Hidden instance.

  • GPlateContext a s => GPlate a sDefined in optics-core-0.4.1.1 · Optics.Generic