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 m where
- type GenericFoldMapC m a :: Constraint
- genericFoldMapF :: GenericFoldMapC m a => a -> m
- genericFoldMapNonSum :: forall {cd} {f} asserts m a. (Generic a, Rep a ~ D1 cd f, GFoldMapNonSum m f, ApplyGCAsserts asserts f) => a -> m
- class GFoldMapNonSum m f
- genericFoldMapSum :: forall {cd} {f} opts asserts m a. (Generic a, Rep a ~ D1 cd f, GFoldMapSum opts m f, ApplyGCAsserts asserts f) => (String -> m) -> a -> m
- class GFoldMapSum (opts :: SumOpts) m f
- genericFoldMapSumConsByte :: forall m a. (Generic a, GFoldMapSumConsByte m (Rep a)) => (Word8 -> m) -> a -> m
- class GFoldMapSumConsByte m f
Documentation
class GenericFoldMap m where Source #
type GenericFoldMapC m a :: Constraint Source #
The type class that enables mapping permitted types to the monoid.
The type class should provide a function that looks like
genericFoldMapF
.
genericFoldMapF :: GenericFoldMapC m a => a -> m Source #
The "map" function in foldMap
(first argument).
Instances
GenericFoldMap Showly Source # | |
Defined in Generic.Data.Function.Example type GenericFoldMapC Showly a Source # genericFoldMapF :: GenericFoldMapC Showly a => a -> Showly Source # | |
Monoid m => GenericFoldMap (EmptyRec0 m) Source # | |
Defined in Generic.Data.Function.FoldMap.Constructor type GenericFoldMapC (EmptyRec0 m) a Source # genericFoldMapF :: GenericFoldMapC (EmptyRec0 m) a => a -> EmptyRec0 m Source # | |
GenericFoldMap (NoRec0 m) Source # |
|
Defined in Generic.Data.Function.FoldMap.Constructor type GenericFoldMapC (NoRec0 m) a Source # genericFoldMapF :: GenericFoldMapC (NoRec0 m) a => a -> NoRec0 m Source # |
genericFoldMapNonSum :: forall {cd} {f} asserts m a. (Generic a, Rep a ~ D1 cd f, GFoldMapNonSum m 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 m 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 m (V1 :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.NonSum gFoldMapNonSum :: forall (p :: k0). V1 p -> m Source # | |
GFoldMapNonSum m (l :+: r :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.NonSum gFoldMapNonSum :: forall (p :: k0). (l :+: r) p -> m Source # | |
GFoldMapC m f => GFoldMapNonSum m (C1 c f :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.NonSum gFoldMapNonSum :: forall (p :: k0). C1 c f p -> m Source # |
genericFoldMapSum :: forall {cd} {f} opts asserts m a. (Generic a, Rep a ~ D1 cd f, GFoldMapSum opts m 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) m f Source #
Instances
GFoldMapSum opts m (V1 :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k0). (String -> m) -> V1 p -> m Source # | |
GFoldMapCSum m (C1 c f) => GFoldMapSum 'AllowSingletonSum m (C1 c f :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k0). (String -> m) -> C1 c f p -> m Source # | |
GFoldMapSum 'SumOnly m (C1 c f :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k0). (String -> m) -> C1 c f p -> m Source # | |
GFoldMapCSum m (l :+: r) => GFoldMapSum opts m (l :+: r :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.Sum gFoldMapSum :: forall (p :: k0). (String -> m) -> (l :+: r) p -> m Source # |
genericFoldMapSumConsByte :: forall m a. (Generic a, GFoldMapSumConsByte m (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 m f Source #
Instances
GFoldMapSumConsByte m (V1 :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.SumConsByte gFoldMapSumConsByte :: forall (p :: k0). (Word8 -> m) -> V1 p -> m Source # | |
(FitsInByte (SumArity (l :+: r)), GFoldMapCSumCtrArityByte m 0 (l :+: r), GFoldMapCSumCtr m (l :+: r), Semigroup m) => GFoldMapSumConsByte m (l :+: r :: Type -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.SumConsByte gFoldMapSumConsByte :: forall (p :: k). (Word8 -> m) -> (l :+: r) p -> m Source # | |
GFoldMapSumConsByte m (C1 c f :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.SumConsByte gFoldMapSumConsByte :: forall (p :: k0). (Word8 -> m) -> C1 c f p -> m Source # | |
GFoldMapSumConsByte m f => GFoldMapSumConsByte m (D1 c f :: k -> Type) Source # | |
Defined in Generic.Data.Function.FoldMap.SumConsByte gFoldMapSumConsByte :: forall (p :: k0). (Word8 -> m) -> D1 c f p -> m Source # |