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-data-1.1.0.2Haskell2010

Generic.Data.Internal.Microsurgery

Surgeries that are just coerce.

Warning

This is an internal module: it is not subject to any versioning policy, breaking changes can happen at any time.

If something here seems useful, please report it or create a pull request to export it from an external module.

  • 20 types
  • 1 class
  • 11 values

Surgery

6 declarations
familytype family GSurgery s (f :: k -> Type) :: k -> Type
#

Apply a microsurgery represented by a symbol s (declared as a dummy data type) to a generic representation f.

Instances9GSurgery, …

Derecordify

4 declarations
datadata Derecordify
#

Forget that a type was declared using record syntax.

data Foo = Bar { baz :: Zap }

-- becomes --

data Foo = Bar Zap

Concretely, set the last field of MetaCons to False and forget field names.

This is a defunctionalized symbol, applied using GSurgery or Surgery.

Instances1GSurgery
familytype family GDerecordify (f :: k -> Type) :: k -> Type
#
Instances7GDerecordify, …

Type aging ("denewtypify")

3 declarations
datadata Typeage
#

Forget that a type is a newtype. (The pun is that "aging" a type makes it no longer "new".)

newtype Foo = Bar Baz

-- becomes --

data Foo = Bar Baz

This is a defunctionalized symbol, applied using GSurgery or Surgery.

Instances1GSurgery

Renaming

8 declarations
datadata RenameFields rnm
#

Rename fields using the function rnm given as a parameter.

data Foo = Bar { baz :: Zap }

-- becomes, renaming "baz" to "bag" --

data Foo = Bar { bag :: Zap }

This is a defunctionalized symbol, applied using GSurgery or Surgery.

Instances1GSurgery
familytype family GRenameFields rnm (f :: k -> Type) :: k -> Type
#
Instances7GRenameFields, …
datadata RenameConstrs rnm
#

Rename constructors using the function rnm given as a parameter.

data Foo = Bar { baz :: Zap }

-- becomes, renaming "Bar" to "Car" --

data Foo = Car { baz :: Zap }

This is a defunctionalized symbol, applied using GSurgery or Surgery.

Instances1GSurgery
familytype family GRenameConstrs rnm (f :: k -> Type) :: k -> Type
#
Instances5GRenameConstrs

Defining symbol functions

familytype family (@@) f (s :: Symbol) :: Symbol
#

f @@ s is the application of a type-level function symbolized by f to a s :: Symbol.

A function FooToBar can be defined as follows:

data FooToBar
type instance FooToBar @@ "foo" = "bar"
Instances4@@
  • type (@@) SError s = TypeError ('Text "Invalid name: " ':<>: 'ShowType s)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type (@@) SId s = sDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type (@@) (SConst z) _s = zDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type (@@) (SRename xs f) s = SRename' xs f sDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
datadata SId
#

Identity function Symbol -> Symbol.

Instances1@@
  • type (@@) SId s = sDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
datadata SError
#

Empty function (compile-time error when applied).

Instances1@@
datadata SConst (s :: Symbol)
#

Constant function.

Instances1@@
  • type (@@) (SConst z) _s = zDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
datadata SRename (xs :: [(Symbol, Symbol)]) f
#

Define a function for a fixed set of strings, and fall back to f for the others.

Instances1@@
  • type (@@) (SRename xs f) s = SRename' xs f sDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery

Other

13 declarations
classclass UnifyRep (f :: k -> Type) (g :: k -> Type)
#

Unify the "spines" of two generic representations (the "spine" is everything except the field types).

Instances6UnifyRep
  • g' ~ U1 => UnifyRep U1 g'Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • g' ~ V1 => UnifyRep V1 g'Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • (g' ~ (g1 :*: g2), UnifyRep f1 g1, UnifyRep f2 g2) => UnifyRep (f1 :*: f2) g'Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • (g' ~ (g1 :+: g2), UnifyRep f1 g1, UnifyRep f2 g2) => UnifyRep (f1 :+: f2) g'Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • g' ~ K1 i b => UnifyRep (K1 i a) g'Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • (g' ~ M1 s c g, UnifyRep f g) => UnifyRep (M1 s c f) g'Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
valueonData
  1. :: (UnifyRep r s, UnifyRep s r)
  2. => p (Data r x) (Data s y)
  3. -> p (Data r x) (Data s y)
#
onData :: _ => (Data r x -> Data s y) -> (Data r x -> Data s y)  -- possible specialization

Can be used with generic-lens for type-changing field updates with field_ (and possibly other generic optics).

A specialization of the identity function to be used to fix types of functions on Data, unifying the "spines" of input and output generic representations (the "spine" is everything except field types, which may thus change).

datadata OnFields (f :: Type -> Type)
#

Apply a type constructor f to every field type of a generic representation r.

data Color = RGB
  { r :: Int
  , g :: Int
  , b :: Int }

-- becomes --

data Color f = RGB
  { r :: f Int
  , g :: f Int
  , b :: f Int }

This is a defunctionalized symbol, applied using GSurgery or Surgery.

Instances1GSurgery
familytype family GOnFields (f :: Type -> Type) (g :: k -> Type) :: k -> Type
#
Instances6GOnFields
  • type GOnFields f U1 = U1Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type GOnFields f V1 = V1Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type GOnFields f (K1 i a) = K1 i (f a)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type GOnFields f (M1 s m r) = M1 s m (GOnFields f r)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type GOnFields f (r :*: s) = GOnFields f r :*: GOnFields f sDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type GOnFields f (r :+: s) = GOnFields f r :+: GOnFields f sDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
datadata OnField (s :: Symbol) (f :: Type -> Type)
#

Apply a type constructor f to the field named s in a generic record r.

data Vec a = Vec
  { len :: Int
  , contents :: [a] }

-- with (OnField "len" Sum) becomes --

data Vec a = Vec
  { len :: Sum Int
  , contents :: [a] }

This is a defunctionalized symbol, applied using GSurgery or Surgery. See also the synonym (%~).

Instances1GSurgery
  • type GSurgery (OnField s f) g = GOnField s f gDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
familytype family GOnField (x :: Symbol) (f :: Type -> Type) (g :: k -> Type) :: k -> Type where
#

Equations

typetype (%~) = OnField
#

Infix name for OnField. To be used with Surgeries or Cat.

Examples

Transform one Int field into Sum Int for deriving Monoid:

data Vec a = Vec
  { len :: Int
  , contents :: [a] }
  deriving Generic
  deriving (Eq, Show) via Generically (Vec a)
  deriving (Semigroup, Monoid) via ProductSurgeries '["len" %~ Sum] (Vec a)

Wrap unshowable fields in Generic.Data.Opaque for deriving Show:

data Unshowable = Unshowable
  { fun :: Int -> Int
  , io :: IO Bool
  , int :: Int }
  deriving Generic
  deriving Show via Surgeries '["fun" %~ Generic.Data.Opaque, "io" %~ Generic.Data.Opaque] Unshowable

-- show (Unshowable id (pure True) 42) = "Unshowable _ _ 42"
datadata Cat (ss :: [Type])
#

Compose surgeries together.

Instances2GSurgery
  • type GSurgery (Cat '[]) g = gDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
  • type GSurgery (Cat (s ': ss)) g = GSurgery s (GSurgery (Cat ss) g)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery
datadata CopyRep a
#

Change the generic representation to that of another type a.

Instances1GSurgery
  • type GSurgery (CopyRep a) _1 = Rep aDefined in generic-data-1.1.0.2 · Generic.Data.Internal.Microsurgery