-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Familiar functions lifted to generic data types
--
-- Please see README.md.
@package generic-data-functions
@version 0.2.0
-- | Functions "lifted" (roughly) to generic Haskell data types.
--
-- Haskell data types have a fair amount of structure to them:
--
--
-- - Multiple constructors (sums)
-- - Multiple fields (products)
-- - Constructor names must be unique
-- - Fields are ordered left-to-right (or top-to-bottom)
--
--
-- We leverage this structure to provide parameterized generic functions,
-- where the user only handles the base case (individual fields). Such
-- generics are very relevant for simplistic usages like boring type
-- folds and serializing tasks. No need to bash out 50 lines of arcane
-- type algebra -- just write a single instance and you're golden.
--
-- Sum types introduce choice, which brings an extra layer of complexity.
-- For this reason, most functions provide a sum type version and a
-- non-sum type version. Sum type generic functions will require a bit
-- more information, like some extra definitions or instances. Using the
-- wrong one will result in a clear type error.
module Generic.Data.Function
module Generic.Data.Function.Common
data SumOpts
-- | Only "proper" sum types are permitted, no singletons.
SumOnly :: SumOpts
-- | Treat a single constructor as a sum type still.
AllowSingletonSum :: SumOpts
-- | Handy generics utils.
module Generic.Data.Function.Util.Generic
-- | datatypeName without the value (only used as a proxy). Lets us
-- push our undefineds into one place.
datatypeName' :: forall d. Datatype d => String
-- | conName without the value (only used as a proxy). Lets us push
-- our undefineds into one place.
conName' :: forall c. Constructor c => String
-- | selName without the value (only used as a proxy). Lets us push
-- our undefineds into one place.
selName' :: forall s. Selector s => String
-- | Get the record name for a selector if present.
--
-- On the type level, a 'Maybe Symbol' is stored for record names. But
-- the reification is done using fromMaybe "". So we have to
-- inspect the resulting string to determine whether the field uses
-- record syntax or not. (Silly.)
selName'' :: forall s. Selector s => Maybe String
-- | Handy typenat utils.
module Generic.Data.Function.Util.TypeNats
natVal'' :: forall n. KnownNat n => Natural
natValInt :: forall n. KnownNat n => Int
-- | Wrapper types for using with DerivingVia.
module Generic.Data.Function.Via
-- | Wrapper for using to derive instances via generics. Emit type error on
-- Rec0 base case i.e. any non-empty constructor.
newtype NoRec0 a
NoRec0 :: a -> NoRec0 a
[unNoRec0] :: NoRec0 a -> a
type ENoRec0 = 'Text "Cannot use generic function on NoRec0-wrapped type containing fields"
-- | Wrapper for using to derive instances via generics. Do nothing for
-- Rec0 base case i.e. every constructor field.
--
-- "nothing" probably means mempty, but *may* be another
-- unit-like.
--
-- TODO This might not be useful. It's not "special" like NoRec0,
-- it's basically tied to Monoid. So it's useful for
-- foldMap, but kind of arbitrary when applied to traverse.
newtype EmptyRec0 a
EmptyRec0 :: a -> EmptyRec0 a
[unEmptyRec0] :: EmptyRec0 a -> a
instance GHC.Base.Monad Generic.Data.Function.Via.NoRec0
instance GHC.Base.Applicative Generic.Data.Function.Via.NoRec0
instance GHC.Base.Functor Generic.Data.Function.Via.NoRec0
instance GHC.Show.Show a => GHC.Show.Show (Generic.Data.Function.Via.NoRec0 a)
instance GHC.Generics.Generic (Generic.Data.Function.Via.NoRec0 a)
instance GHC.Base.Monad Generic.Data.Function.Via.EmptyRec0
instance GHC.Base.Applicative Generic.Data.Function.Via.EmptyRec0
instance GHC.Base.Functor Generic.Data.Function.Via.EmptyRec0
instance GHC.Show.Show a => GHC.Show.Show (Generic.Data.Function.Via.EmptyRec0 a)
instance GHC.Generics.Generic (Generic.Data.Function.Via.EmptyRec0 a)
module Generic.Data.Function.Traverse.Constructor
data A a
A :: a -> Sum Int -> () -> A a
-- | Applicative functors that can be generically traversed.
class GenericTraverse f where {
-- | The type class providing (applicative) actions for permitted types.
type GenericTraverseC f a :: Constraint;
}
-- | The action in traverse (first argument).
--
-- We include data type metadata because this function is useful for
-- monadic parsers, which can record it in error messages. (We don't do
-- it for foldMap because it's pure.)
genericTraverseAction :: (GenericTraverse f, GenericTraverseC f a) => String -> String -> Maybe String -> Natural -> f a
class GTraverseC cd cc (si :: Natural) f f'
gTraverseC :: GTraverseC cd cc si f f' => f (f' p)
type family ProdArity (f :: Type -> Type) :: Natural
instance GHC.Show.Show a => GHC.Show.Show (Generic.Data.Function.Traverse.Constructor.A a)
instance GHC.Generics.Generic (Generic.Data.Function.Traverse.Constructor.A a)
instance forall k1 k2 (f :: Type -> Type) (cd :: k1) (cc :: k2) (si :: GHC.Num.Natural.Natural) (l :: Type -> Type) (r :: Type -> Type). (GHC.Base.Applicative f, Generic.Data.Function.Traverse.Constructor.GTraverseC cd cc si f l, Generic.Data.Function.Traverse.Constructor.GTraverseC cd cc (si GHC.TypeNats.+ Generic.Data.Function.Traverse.Constructor.ProdArity r) f r) => Generic.Data.Function.Traverse.Constructor.GTraverseC cd cc si f (l GHC.Generics.:*: r)
instance forall k1 k2 k3 (f :: Type -> Type) a (si :: GHC.TypeNats.Nat) (cs :: GHC.Generics.Meta) (cc :: k1) (cd :: k2). (Generic.Data.Function.Traverse.Constructor.GenericTraverse f, Generic.Data.Function.Traverse.Constructor.GenericTraverseC f a, GHC.Base.Functor f, GHC.TypeNats.KnownNat si, GHC.Generics.Selector cs, GHC.Generics.Constructor cc, GHC.Generics.Datatype cd) => Generic.Data.Function.Traverse.Constructor.GTraverseC cd cc si f (GHC.Generics.S1 cs (GHC.Generics.Rec0 a))
instance forall k1 k2 k3 (f :: Type -> Type) (cd :: k1) (cc :: k2). GHC.Base.Applicative f => Generic.Data.Function.Traverse.Constructor.GTraverseC cd cc 0 f GHC.Generics.U1
instance Generic.Data.Function.Traverse.Constructor.GenericTraverse Generic.Data.Function.Via.NoRec0
instance Generic.Data.Function.Traverse.Constructor.GenericTraverse Generic.Data.Function.Via.EmptyRec0
module Generic.Data.Function.Traverse.NonSum
class GTraverseNonSum (cd :: Meta) f f'
gTraverseNonSum :: GTraverseNonSum cd f f' => f (f' p)
instance forall k (cd :: GHC.Generics.Meta) (f :: Type -> Type) (l :: k -> Type) (r :: k -> Type). Generic.Data.Function.Traverse.NonSum.GTraverseNonSum cd f (l GHC.Generics.:+: r)
instance forall k (f :: Type -> Type) (cd :: GHC.Generics.Meta) (cc :: GHC.Generics.Meta) (f' :: k -> Type). (GHC.Base.Functor f, Generic.Data.Function.Traverse.Constructor.GTraverseC cd cc 0 f f') => Generic.Data.Function.Traverse.NonSum.GTraverseNonSum cd f (GHC.Generics.C1 cc f')
instance Generic.Data.Function.Traverse.NonSum.GTraverseNonSum cd f GHC.Generics.V1
module Generic.Data.Function.FoldMap.Constructor
-- | Monoids that can be generically foldMapped to.
class GenericFoldMap m where {
-- | The type class that enables mapping permitted types to the monoid.
--
-- The type class should provide a function that looks like
-- genericFoldMapF.
type GenericFoldMapC m a :: Constraint;
}
-- | The "map" function in foldMap (first argument).
genericFoldMapF :: (GenericFoldMap m, GenericFoldMapC m a) => a -> m
-- | foldMap on individual constructors (products).
class GFoldMapC m f
gFoldMapC :: GFoldMapC m f => f p -> m
instance forall k m (l :: k -> Type) (r :: k -> Type). (GHC.Base.Semigroup m, Generic.Data.Function.FoldMap.Constructor.GFoldMapC m l, Generic.Data.Function.FoldMap.Constructor.GFoldMapC m r) => Generic.Data.Function.FoldMap.Constructor.GFoldMapC m (l GHC.Generics.:*: r)
instance (Generic.Data.Function.FoldMap.Constructor.GenericFoldMap m, Generic.Data.Function.FoldMap.Constructor.GenericFoldMapC m a) => Generic.Data.Function.FoldMap.Constructor.GFoldMapC m (GHC.Generics.S1 c (GHC.Generics.Rec0 a))
instance GHC.Base.Monoid m => Generic.Data.Function.FoldMap.Constructor.GFoldMapC m GHC.Generics.U1
instance Generic.Data.Function.FoldMap.Constructor.GenericFoldMap (Generic.Data.Function.Via.NoRec0 m)
instance GHC.Base.Monoid m => Generic.Data.Function.FoldMap.Constructor.GenericFoldMap (Generic.Data.Function.Via.EmptyRec0 m)
-- | foldMap for sum types, where constructors are encoded by index
-- (distance from first/leftmost constructor) in a single byte, which is
-- prepended to their contents.
--
-- TODO. Clumsy and limited. And yet, still handy enough I think.
module Generic.Data.Function.FoldMap.SumConsByte
class GFoldMapSumConsByte m f
gFoldMapSumConsByte :: GFoldMapSumConsByte m f => (Word8 -> m) -> f p -> m
-- | Sum type handler handling constructors only. Useful if you handle
-- constructor prefixes elsewhere.
class GFoldMapCSumCtr m f
gFoldMapCSumCtr :: GFoldMapCSumCtr m f => f p -> m
class GFoldMapCSumCtrArityByte m (arity :: Natural) f
gFoldMapCSumCtrArityByte :: GFoldMapCSumCtrArityByte m arity f => (Word8 -> m) -> f p -> m
type family SumArity (a :: Type -> Type) :: Natural
type FitsInByte n = FitsInByteResult (n <=? 255)
type family FitsInByteResult (b :: Bool) :: Constraint
type family TypeErrorMessage (a :: Symbol) :: Constraint
instance (Generic.Data.Function.FoldMap.SumConsByte.FitsInByte (Generic.Data.Function.FoldMap.SumConsByte.SumArity (l GHC.Generics.:+: r)), Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtrArityByte m 0 (l GHC.Generics.:+: r), Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtr m (l GHC.Generics.:+: r), GHC.Base.Semigroup m) => Generic.Data.Function.FoldMap.SumConsByte.GFoldMapSumConsByte m (l GHC.Generics.:+: r)
instance (Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtrArityByte m arity l, Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtrArityByte m (arity GHC.TypeNats.+ Generic.Data.Function.FoldMap.SumConsByte.SumArity l) r) => Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtrArityByte m arity (l GHC.Generics.:+: r)
instance forall k (arity :: GHC.TypeNats.Nat) m (c :: GHC.Generics.Meta) (f :: k -> Type). GHC.TypeNats.KnownNat arity => Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtrArityByte m arity (GHC.Generics.C1 c f)
instance forall k m (l :: k -> Type) (r :: k -> Type). (Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtr m l, Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtr m r) => Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtr m (l GHC.Generics.:+: r)
instance forall k m (f :: k -> Type) (c :: GHC.Generics.Meta). Generic.Data.Function.FoldMap.Constructor.GFoldMapC m f => Generic.Data.Function.FoldMap.SumConsByte.GFoldMapCSumCtr m (GHC.Generics.C1 c f)
instance forall k m (f :: k -> Type) (c :: GHC.Generics.Meta). Generic.Data.Function.FoldMap.SumConsByte.GFoldMapSumConsByte m f => Generic.Data.Function.FoldMap.SumConsByte.GFoldMapSumConsByte m (GHC.Generics.D1 c f)
instance forall k m (c :: GHC.Generics.Meta) (f :: k -> Type). Generic.Data.Function.FoldMap.SumConsByte.GFoldMapSumConsByte m (GHC.Generics.C1 c f)
instance Generic.Data.Function.FoldMap.SumConsByte.GFoldMapSumConsByte m GHC.Generics.V1
-- | Common descriptions for common generic data representation errors.
-- Type level (compile time) and term level (runtime).
--
-- TODO: if this package ever expands, these deserve plenty of attention,
-- like generic-optics has.
--
-- Runtime errors are a bit meatier because it's easy to do so, and I
-- don't want people to see them more than once (really you should use
-- the typing support).
module Generic.Data.Rep.Error
wrapE :: String -> String -> String
-- | Common type error string for when you attempt to use a generic
-- instance at an empty data type (e.g. Void, V1).
type ENoEmpty = 'Text "Requested generic instance disallows empty data type"
eNoEmpty :: String
-- | Common type error string for when GHC is asked to derive a non-sum
-- instance, but the data type in question turns out to be a sum data
-- type.
--
-- No need to add the data type name here, since GHC's context includes
-- the surrounding instance declaration.
type EUnexpectedSum = 'Text "Cannot derive non-sum generic instance for sum data type"
eNoSum :: String
-- | Common type error string for when GHC is asked to derive a sum
-- instance, but the data type in question turns out to be a non-sum data
-- type.
--
-- No need to add the data type name here, since GHC's context includes
-- the surrounding instance declaration.
type EUnexpectedNonSum = 'Text "Refusing to derive sum generic instance for non-sum data type"
eNeedSum :: String
-- | Assertions on precise generic data representation.
--
-- I like being real picky with my generic code, disallowing misuse at
-- the type level. However, this makes it less flexible overall, and is a
-- large chunk of the code I have to write over and over again.
--
-- I mainly care about sanity checks, along the lines of "if the generic
-- representations looks like this, type error out". So they don't make
-- any term-level changes.
--
-- So, instead of hiding these in generic type class instances, I put
-- them in type family equations. Now we can turn these checks on and off
-- -- and when they're on, you have to carry around that fact in your
-- types. Fantastic!
--
-- Checks are formed as Constraints, where a failure triggers a
-- TypeError and a success goes to the empty constraint
-- () (() :: Constraint as well as
-- Type).
--
-- These checks were always done on the type-level, but they were
-- "inline" with the rest of the type class. By pulling them out, we
-- *should* be incurring some compile-time performance penalty (albeit
-- hopefully minor due to the simple nature of the checks), but making no
-- change to runtime.
module Generic.Data.Rep.Assert
-- | Generic representation assertions, on the constructor level (bits that
-- come after D1).
data GCAssert
-- | Is not an empty type (does not have 0 constructors)
NoEmpty :: GCAssert
-- | Is not a sum type (has 0 or 1 constructors)
NoSum :: GCAssert
-- | Is a sum type (has 0 or >2 constructors)
NeedSum :: GCAssert
-- | Convert a generic representation constructor-level assertion "label"
-- to the assertion it represents, and make that assertion.
type family ApplyGCAssert x a
-- | Apply a list of generic representation constructor-level assertions.
type family ApplyGCAsserts ls a
type family GCNoEmpty a
type family GCNoSum a
type family GCNeedSum a
module Generic.Data.Function.Traverse.Sum
-- | Sum type monads that can be generically traversed.
--
-- For sum types, we require a monad with choice to differentiate
-- constructors.
class (GenericTraverse f, Alternative f, Monad f) => GenericTraverseSum f
-- | Try to parse a prefix tag of type pt.
--
-- Relevant metadata is provided as arguments.
genericTraverseSumPfxTagAction :: (GenericTraverseSum f, GenericTraverseC f pt) => String -> f pt
-- | Parse error due to no constructor matching the parsed prefix tag.
--
-- Relevant metadata is provided as arguments.
genericTraverseSumNoMatchingCstrAction :: GenericTraverseSum f => String -> [String] -> Text -> f a
-- | How to use a type as a prefix tag in a generic sum type parser.
data PfxTagCfg a
PfxTagCfg :: (String -> a) -> (a -> a -> Bool) -> (a -> Text) -> PfxTagCfg a
-- | How to turn a constructor name into a prefix tag.
[pfxTagCfgFromCstr] :: PfxTagCfg a -> String -> a
-- | How to compare prefix tags for equality.
--
-- By shoving this into our generic derivation config, we can avoid
-- adding an insidious Eq constraint. In general, you will want to
-- set this to (==).
[pfxTagCfgEq] :: PfxTagCfg a -> a -> a -> Bool
-- | Make a prefix tag human-readable. show is often appropriate.
[pfxTagCfgShow] :: PfxTagCfg a -> a -> Text
class GTraverseSum (opts :: SumOpts) cd f f'
gTraverseSum :: (GTraverseSum opts cd f f', GenericTraverseC f pt) => PfxTagCfg pt -> f (f' p)
gTraverseSum' :: forall {p} cd f f' pt. (GenericTraverseC f pt, GenericTraverseSum f, GTraverseCSum cd f f', Datatype cd) => PfxTagCfg pt -> f (f' p)
-- | Generic getter (constructor sum level).
class GTraverseCSum cd f f'
gTraverseCSum :: GTraverseCSum cd f f' => PfxTagCfg pt -> pt -> f (f' p)
instance forall k1 k2 (f :: Type -> Type) (cd :: k1) (l :: k2 -> Type) (r :: k2 -> Type) (opts :: Generic.Data.Function.Common.SumOpts). (Generic.Data.Function.Traverse.Sum.GenericTraverseSum f, Generic.Data.Function.Traverse.Sum.GTraverseCSum cd f (l GHC.Generics.:+: r), GHC.Generics.Datatype cd) => Generic.Data.Function.Traverse.Sum.GTraverseSum opts cd f (l GHC.Generics.:+: r)
instance forall k1 k2 (f :: Type -> Type) (cd :: k1) (cc :: GHC.Generics.Meta) (f' :: k2 -> Type). (Generic.Data.Function.Traverse.Sum.GenericTraverseSum f, Generic.Data.Function.Traverse.Sum.GTraverseCSum cd f (GHC.Generics.C1 cc f'), GHC.Generics.Datatype cd) => Generic.Data.Function.Traverse.Sum.GTraverseSum 'Generic.Data.Function.Common.AllowSingletonSum cd f (GHC.Generics.C1 cc f')
instance forall k1 k2 (f :: Type -> Type) (cd :: k1) (l :: k2 -> Type) (r :: k2 -> Type). (GHC.Base.Functor f, GHC.Base.Alternative f, Generic.Data.Function.Traverse.Sum.GTraverseCSum cd f l, Generic.Data.Function.Traverse.Sum.GTraverseCSum cd f r) => Generic.Data.Function.Traverse.Sum.GTraverseCSum cd f (l GHC.Generics.:+: r)
instance forall k1 k2 (f :: Type -> Type) (cd :: k1) (cc :: GHC.Generics.Meta) (f' :: k2 -> Type). (GHC.Base.Alternative f, Generic.Data.Function.Traverse.Constructor.GTraverseC cd cc 0 f f', GHC.Generics.Constructor cc) => Generic.Data.Function.Traverse.Sum.GTraverseCSum cd f (GHC.Generics.C1 cc f')
instance forall k1 k2 (cd :: k1) (f :: Type -> Type) (cc :: GHC.Generics.Meta) (f' :: k2 -> Type). Generic.Data.Function.Traverse.Sum.GTraverseSum 'Generic.Data.Function.Common.SumOnly cd f (GHC.Generics.C1 cc f')
instance forall k1 k2 (opts :: Generic.Data.Function.Common.SumOpts) (cd :: k1) (f :: Type -> Type). Generic.Data.Function.Traverse.Sum.GTraverseSum opts cd f GHC.Generics.V1
-- | traverse for generic data types.
--
-- TODO This is harder to conceptualize than generic foldMap. No
-- nice clean explanation yet.
--
-- This function can provide generic support for simple parser-esque
-- types.
module Generic.Data.Function.Traverse
-- | Applicative functors that can be generically traversed.
class GenericTraverse f where {
-- | The type class providing (applicative) actions for permitted types.
type GenericTraverseC f a :: Constraint;
}
-- | The action in traverse (first argument).
--
-- We include data type metadata because this function is useful for
-- monadic parsers, which can record it in error messages. (We don't do
-- it for foldMap because it's pure.)
genericTraverseAction :: (GenericTraverse f, GenericTraverseC f a) => String -> String -> Maybe String -> Natural -> f a
-- | Generic traverse over a term of non-sum data type f a.
genericTraverseNonSum :: forall {cd} {gf} asserts f a. (Generic a, Rep a ~ D1 cd gf, GTraverseNonSum cd f gf, ApplyGCAsserts asserts f, Functor f) => f a
class GTraverseNonSum (cd :: Meta) f f'
-- | Sum type monads that can be generically traversed.
--
-- For sum types, we require a monad with choice to differentiate
-- constructors.
class (GenericTraverse f, Alternative f, Monad f) => GenericTraverseSum f
-- | Try to parse a prefix tag of type pt.
--
-- Relevant metadata is provided as arguments.
genericTraverseSumPfxTagAction :: (GenericTraverseSum f, GenericTraverseC f pt) => String -> f pt
-- | Parse error due to no constructor matching the parsed prefix tag.
--
-- Relevant metadata is provided as arguments.
genericTraverseSumNoMatchingCstrAction :: GenericTraverseSum f => String -> [String] -> Text -> f a
-- | How to use a type as a prefix tag in a generic sum type parser.
data PfxTagCfg a
PfxTagCfg :: (String -> a) -> (a -> a -> Bool) -> (a -> Text) -> PfxTagCfg a
-- | How to turn a constructor name into a prefix tag.
[pfxTagCfgFromCstr] :: PfxTagCfg a -> String -> a
-- | How to compare prefix tags for equality.
--
-- By shoving this into our generic derivation config, we can avoid
-- adding an insidious Eq constraint. In general, you will want to
-- set this to (==).
[pfxTagCfgEq] :: PfxTagCfg a -> a -> a -> Bool
-- | Make a prefix tag human-readable. show is often appropriate.
[pfxTagCfgShow] :: PfxTagCfg a -> a -> Text
-- | Generic traverse over a term of sum data type f a.
--
-- You must provide a configuration for how to handle constructors.
genericTraverseSum :: forall {cd} {gf} opts asserts f a pt. (Generic a, Rep a ~ D1 cd gf, GTraverseSum opts cd f gf, ApplyGCAsserts asserts f, GenericTraverseC f pt, Functor f) => PfxTagCfg pt -> f a
class GTraverseSum (opts :: SumOpts) cd f f'
-- | Construct a prefix tag config using existing Eq and Show
-- instances.
--
-- The user only needs to provide the constructor name parser.
eqShowPfxTagCfg :: (Eq a, Show a) => (String -> a) -> PfxTagCfg a
-- | foldMap for sum types where constructors are encoded by mapping
-- the constructor name.
--
-- Note that constructor names are unique per type. So as long as your
-- mapping function similarly outputs unique values of your monoid for
-- each constructor, you should be able to "reverse" the process (e.g.
-- for generic traverse).
module Generic.Data.Function.FoldMap.Sum
class GFoldMapSum (opts :: SumOpts) m f
gFoldMapSum :: GFoldMapSum opts m f => (String -> m) -> f p -> m
-- | Sum type handler prefixing constructor contents with their mapped
-- constructor name via a provided String -> m.
--
-- TODO rename
class GFoldMapCSum m f
gFoldMapCSum :: GFoldMapCSum m f => (String -> m) -> f p -> m
instance forall k m (l :: k -> Type) (r :: k -> Type) (opts :: Generic.Data.Function.Common.SumOpts). Generic.Data.Function.FoldMap.Sum.GFoldMapCSum m (l GHC.Generics.:+: r) => Generic.Data.Function.FoldMap.Sum.GFoldMapSum opts m (l GHC.Generics.:+: r)
instance forall k m (c :: GHC.Generics.Meta) (f :: k -> Type). Generic.Data.Function.FoldMap.Sum.GFoldMapCSum m (GHC.Generics.C1 c f) => Generic.Data.Function.FoldMap.Sum.GFoldMapSum 'Generic.Data.Function.Common.AllowSingletonSum m (GHC.Generics.C1 c f)
instance forall k m (l :: k -> Type) (r :: k -> Type). (Generic.Data.Function.FoldMap.Sum.GFoldMapCSum m l, Generic.Data.Function.FoldMap.Sum.GFoldMapCSum m r) => Generic.Data.Function.FoldMap.Sum.GFoldMapCSum m (l GHC.Generics.:+: r)
instance forall k m (c :: GHC.Generics.Meta) (f :: k -> Type). (GHC.Base.Semigroup m, GHC.Generics.Constructor c, Generic.Data.Function.FoldMap.Constructor.GFoldMapC m f) => Generic.Data.Function.FoldMap.Sum.GFoldMapCSum m (GHC.Generics.C1 c f)
instance forall k m (c :: GHC.Generics.Meta) (f :: k -> Type). Generic.Data.Function.FoldMap.Sum.GFoldMapSum 'Generic.Data.Function.Common.SumOnly m (GHC.Generics.C1 c f)
instance Generic.Data.Function.FoldMap.Sum.GFoldMapSum opts m GHC.Generics.V1
module Generic.Data.Function.FoldMap.NonSum
-- | 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 (<>).
class GFoldMapNonSum m f
gFoldMapNonSum :: GFoldMapNonSum m f => f p -> m
instance forall k m (f :: k -> Type) (c :: GHC.Generics.Meta). Generic.Data.Function.FoldMap.Constructor.GFoldMapC m f => Generic.Data.Function.FoldMap.NonSum.GFoldMapNonSum m (GHC.Generics.C1 c f)
instance forall k m (l :: k -> Type) (r :: k -> Type). Generic.Data.Function.FoldMap.NonSum.GFoldMapNonSum m (l GHC.Generics.:+: r)
instance Generic.Data.Function.FoldMap.NonSum.GFoldMapNonSum m GHC.Generics.V1
-- | foldMap for generic data types.
--
-- foldMap can be considered a two-step process:
--
--
-- - map every element a of a t a (where
-- Foldable t) to some Monoid m
-- - combine elements using (<>)
--
--
-- Applying this to generic data types:
--
--
-- - map every field of a constructor to some Monoid
-- m
-- - combine elements using (<>)
--
--
-- 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.
module Generic.Data.Function.FoldMap
-- | Monoids that can be generically foldMapped to.
class GenericFoldMap m where {
-- | The type class that enables mapping permitted types to the monoid.
--
-- The type class should provide a function that looks like
-- genericFoldMapF.
type GenericFoldMapC m a :: Constraint;
}
-- | The "map" function in foldMap (first argument).
genericFoldMapF :: (GenericFoldMap m, GenericFoldMapC m a) => a -> m
-- | Generic foldMap over a term of non-sum data type a.
--
-- a must have exactly one constructor.
genericFoldMapNonSum :: forall {cd} {f} asserts m a. (Generic a, Rep a ~ D1 cd f, GFoldMapNonSum m f, ApplyGCAsserts asserts f) => a -> m
-- | 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 (<>).
class GFoldMapNonSum m f
-- | 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.
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
-- | 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.
genericFoldMapSumConsByte :: forall m a. (Generic a, GFoldMapSumConsByte m (Rep a)) => (Word8 -> m) -> a -> m
class GFoldMapSumConsByte m f
module Generic.Data.Function.Example
data X
X1 :: X
X2 :: X
data Y
Y :: Y
newtype Showly
Showly :: [String] -> Showly
[unShowly] :: Showly -> [String]
showGeneric :: forall {cd} {f} opts asserts a. (Generic a, Rep a ~ D1 cd f, GFoldMapSum opts Showly f, ApplyGCAsserts asserts f) => a -> String
showGeneric' :: forall {cd} {f} asserts a. (Generic a, Rep a ~ D1 cd f, GFoldMapNonSum Showly f, ApplyGCAsserts asserts f) => a -> String
instance GHC.Generics.Generic Generic.Data.Function.Example.X
instance GHC.Generics.Generic Generic.Data.Function.Example.Y
instance GHC.Base.Monoid Generic.Data.Function.Example.Showly
instance GHC.Base.Semigroup Generic.Data.Function.Example.Showly
instance Generic.Data.Function.FoldMap.Constructor.GenericFoldMap Generic.Data.Function.Example.Showly