Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
foldMap
for generic data types.
foldMap
can be considered a two-step process:
Applying this to generic data types:
Field mappings are handled using a per-monoid type class. You need a monoid m
with an associated type class which has a function a -> m
. Write a
GenericFoldMap
instance for your monoid which points to your type class. If a
field type doesn't have a matching instance, the generic instance emits a type
error.
Sum types (with multiple constructors) are handled by (<>)
-ing the constructor
with its contents (in that order). You must provide a String -> m
function for
mapping constructor names. If you need custom sum type handling, you may write
your own and still leverage the individual constructor generics.
This function can provide generic support for simple fold-y operations like serialization.
Synopsis
- class GenericFoldMap tag where
- type GenericFoldMapM tag :: Type
- type GenericFoldMapC tag a :: Constraint
- genericFoldMapF :: GenericFoldMapC tag a => a -> GenericFoldMapM tag
- genericFoldMapNonSum :: forall {cd} {f} asserts tag {m} a. (Generic a, Rep a ~ D1 cd f, m ~ GenericFoldMapM tag, GFoldMapNonSum tag f, ApplyGCAsserts asserts f) => a -> m
- class GFoldMapNonSum tag f
- genericFoldMapSum :: forall {cd} {f} opts asserts tag {m} a. (Generic a, Rep a ~ D1 cd f, m ~ GenericFoldMapM tag, GFoldMapSum opts tag f, ApplyGCAsserts asserts f) => (String -> m) -> a -> m
- class GFoldMapSum (opts :: SumOpts) tag f
- genericFoldMapSumConsByte :: forall tag {m} a. (m ~ GenericFoldMapM tag, Generic a, GFoldMapSumConsByte tag (Rep a)) => (Word8 -> m) -> a -> m
- class GFoldMapSumConsByte tag f
Documentation
class GenericFoldMap tag where Source #
Implementation enumeration type class for generic foldMap
.
The type variable is uninstantiated, used purely as a tag.
Avoid orphan instances by defining custom empty types to use here. See the binrep library on Hackage for an example.
type GenericFoldMapM tag :: Type Source #
type GenericFoldMapC tag a :: Constraint Source #
The type class providing the map function in foldMap
for permitted
types.
genericFoldMapF :: GenericFoldMapC tag a => a -> GenericFoldMapM tag Source #
The map function in foldMap
(first argument).
Instances
GenericFoldMap Showly Source # | |
Defined in Generic.Data.Function.Example type GenericFoldMapM Showly Source # type GenericFoldMapC Showly a Source # genericFoldMapF :: GenericFoldMapC Showly a => a -> GenericFoldMapM Showly Source # | |
Monoid m => GenericFoldMap (EmptyRec0 m :: Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Constructor type GenericFoldMapM (EmptyRec0 m) Source # type GenericFoldMapC (EmptyRec0 m) a Source # genericFoldMapF :: GenericFoldMapC (EmptyRec0 m) a => a -> GenericFoldMapM (EmptyRec0 m) Source # | |
GenericFoldMap (NoRec0 m :: Type) Source # |
|
Defined in Generic.Data.Function.FoldMap.Constructor type GenericFoldMapM (NoRec0 m) Source # type GenericFoldMapC (NoRec0 m) a Source # genericFoldMapF :: GenericFoldMapC (NoRec0 m) a => a -> GenericFoldMapM (NoRec0 m) Source # |
genericFoldMapNonSum :: forall {cd} {f} asserts tag {m} a. (Generic a, Rep a ~ D1 cd f, m ~ GenericFoldMapM tag, GFoldMapNonSum tag f, ApplyGCAsserts asserts f) => a -> m Source #
Generic foldMap
over a term of non-sum data type a
.
a
must have exactly one constructor.
class GFoldMapNonSum tag f Source #
foldMap
over generic product data types.
Take a generic representation, map each field in the data type to a Monoid
,
and combine the results with (<>
).
Instances
GFoldMapNonSum (tag :: k1) (V1 :: k2 -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.NonSum gFoldMapNonSum :: forall (p :: k10). V1 p -> GenericFoldMapM tag Source # | |
GFoldMapNonSum (tag :: k1) (l :+: r :: k2 -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.NonSum gFoldMapNonSum :: forall (p :: k10). (l :+: r) p -> GenericFoldMapM tag Source # | |
GFoldMapC tag f => GFoldMapNonSum (tag :: k1) (C1 c f :: k2 -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.NonSum gFoldMapNonSum :: forall (p :: k10). C1 c f p -> GenericFoldMapM tag Source # |
genericFoldMapSum :: forall {cd} {f} opts asserts tag {m} a. (Generic a, Rep a ~ D1 cd f, m ~ GenericFoldMapM tag, GFoldMapSum opts tag f, ApplyGCAsserts asserts f) => (String -> m) -> a -> m Source #
Generic foldMap
over a term of sum data type a
.
You must provide a function for mapping constructor names to monoidal values.
This is the most generic option, but depending on your string manipulation may be slower.
class GFoldMapSum (opts :: SumOpts) tag f Source #
Instances
GFoldMapSum opts (tag :: k1) (V1 :: k2 -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k10). (String -> GenericFoldMapM tag) -> V1 p -> GenericFoldMapM tag Source # | |
GFoldMapCSum tag (C1 c f) => GFoldMapSum 'AllowSingletonSum (tag :: k1) (C1 c f :: k2 -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k10). (String -> GenericFoldMapM tag) -> C1 c f p -> GenericFoldMapM tag Source # | |
GFoldMapSum 'SumOnly (tag :: k1) (C1 c f :: k2 -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k10). (String -> GenericFoldMapM tag) -> C1 c f p -> GenericFoldMapM tag Source # | |
GFoldMapCSum tag (l :+: r) => GFoldMapSum opts (tag :: k1) (l :+: r :: k2 -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k10). (String -> GenericFoldMapM tag) -> (l :+: r) p -> GenericFoldMapM tag Source # |
genericFoldMapSumConsByte :: forall tag {m} a. (m ~ GenericFoldMapM tag, Generic a, GFoldMapSumConsByte tag (Rep a)) => (Word8 -> m) -> a -> m Source #
Generic foldMap
over a term of sum data type a
where constructors are
mapped to their index (distance from first/leftmost constructor)
a
must have at least two constructors.
You must provide a function for mapping bytes to monoidal values.
This should be fairly fast, but sadly I think it's slower than the generics in store and binary/cereal libraries.
class GFoldMapSumConsByte tag f Source #