| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Data.PartialSemigroup.Generics
Description
If a type derives Generic and all of its fields have PartialSemigroup
instances, you can get a PartialSemigroup for free using
genericPartialSemigroupOp.
Example
For this demonstration we'll define a contrived example type T with two
constructors, A and B.
>>>data T = A String (Either String String) | B String deriving (Generic, Show)
And then define its PartialSemigroup instance using
genericPartialSemigroupOp.
>>>instance PartialSemigroup T where (<>?) = genericPartialSemigroupOp
This gives us an implementation of <>? which combines values only if they have
the same structure.
>>>A "s" (Left "x") <>? A "t" (Left "y")Just (A "st" (Left "xy"))
>>>B "x" <>? B "y"Just (B "xy")
For values that do not have the same structure, <>? produces Nothing.
>>>A "s" (Left "x") <>? A "t" (Right "y")Nothing
>>>A "x" (Left "y") <>? B "z"Nothing
Synopsis
- genericPartialSemigroupOp :: (Generic a, PartialSemigroupRep (Rep a)) => a -> a -> Maybe a
- class PartialSemigroupRep rep where
- repPartialSemigroupOp :: rep a -> rep a -> Maybe (rep a)
- class Generic a
- class PartialSemigroup a where
The generic PartialSemigroup operator
genericPartialSemigroupOp :: (Generic a, PartialSemigroupRep (Rep a)) => a -> a -> Maybe a Source #
Implementation details
class PartialSemigroupRep rep where Source #
The class of generic type Reps for which we can automatically derive
PartialSemigroup:
Methods
repPartialSemigroupOp :: rep a -> rep a -> Maybe (rep a) Source #
Instances
| PartialSemigroup a => PartialSemigroupRep (K1 i a :: Type -> Type) Source # | |
Defined in Data.PartialSemigroup.Generics | |
| (PartialSemigroupRep rep1, PartialSemigroupRep rep2) => PartialSemigroupRep (rep1 :+: rep2) Source # | |
Defined in Data.PartialSemigroup.Generics | |
| (PartialSemigroupRep rep1, PartialSemigroupRep rep2) => PartialSemigroupRep (rep1 :*: rep2) Source # | |
Defined in Data.PartialSemigroup.Generics | |
| PartialSemigroupRep rep => PartialSemigroupRep (M1 i meta rep) Source # | |
Defined in Data.PartialSemigroup.Generics | |
Re-exports
Representable types of kind *.
This class is derivable in GHC with the DeriveGeneric flag on.
A Generic instance must satisfy the following laws:
from.to≡idto.from≡id
Instances
| Generic Bool | Since: base-4.6.0.0 |
| Generic Ordering | Since: base-4.6.0.0 |
| Generic () | Since: base-4.6.0.0 |
| Generic All | Since: base-4.7.0.0 |
| Generic Any | Since: base-4.7.0.0 |
| Generic Fixity | Since: base-4.7.0.0 |
| Generic Associativity | Since: base-4.7.0.0 |
Defined in GHC.Generics Associated Types type Rep Associativity :: Type -> Type # | |
| Generic SourceUnpackedness | Since: base-4.9.0.0 |
Defined in GHC.Generics Associated Types type Rep SourceUnpackedness :: Type -> Type # Methods from :: SourceUnpackedness -> Rep SourceUnpackedness x # to :: Rep SourceUnpackedness x -> SourceUnpackedness # | |
| Generic SourceStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics Associated Types type Rep SourceStrictness :: Type -> Type # Methods from :: SourceStrictness -> Rep SourceStrictness x # to :: Rep SourceStrictness x -> SourceStrictness # | |
| Generic DecidedStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics Associated Types type Rep DecidedStrictness :: Type -> Type # Methods from :: DecidedStrictness -> Rep DecidedStrictness x # to :: Rep DecidedStrictness x -> DecidedStrictness # | |
| Generic [a] | Since: base-4.6.0.0 |
| Generic (Maybe a) | Since: base-4.6.0.0 |
| Generic (Par1 p) | Since: base-4.7.0.0 |
| Generic (Min a) | Since: base-4.9.0.0 |
| Generic (Max a) | Since: base-4.9.0.0 |
| Generic (First a) | Since: base-4.9.0.0 |
| Generic (Last a) | Since: base-4.9.0.0 |
| Generic (WrappedMonoid m) | Since: base-4.9.0.0 |
Defined in Data.Semigroup Associated Types type Rep (WrappedMonoid m) :: Type -> Type # Methods from :: WrappedMonoid m -> Rep (WrappedMonoid m) x # to :: Rep (WrappedMonoid m) x -> WrappedMonoid m # | |
| Generic (Option a) | Since: base-4.9.0.0 |
| Generic (ZipList a) | Since: base-4.7.0.0 |
| Generic (Identity a) | Since: base-4.8.0.0 |
| Generic (First a) | Since: base-4.7.0.0 |
| Generic (Last a) | Since: base-4.7.0.0 |
| Generic (Dual a) | Since: base-4.7.0.0 |
| Generic (Endo a) | Since: base-4.7.0.0 |
| Generic (Sum a) | Since: base-4.7.0.0 |
| Generic (Product a) | Since: base-4.7.0.0 |
| Generic (Down a) | Since: base-4.12.0.0 |
| Generic (NonEmpty a) | Since: base-4.6.0.0 |
| Generic (Either a b) | Since: base-4.6.0.0 |
| Generic (V1 p) | Since: base-4.9.0.0 |
| Generic (U1 p) | Since: base-4.7.0.0 |
| Generic (a, b) | Since: base-4.6.0.0 |
| Generic (Arg a b) | Since: base-4.9.0.0 |
| Generic (WrappedMonad m a) | Since: base-4.7.0.0 |
Defined in Control.Applicative Associated Types type Rep (WrappedMonad m a) :: Type -> Type # Methods from :: WrappedMonad m a -> Rep (WrappedMonad m a) x # to :: Rep (WrappedMonad m a) x -> WrappedMonad m a # | |
| Generic (Proxy t) | Since: base-4.6.0.0 |
| Generic (Rec1 f p) | Since: base-4.7.0.0 |
| Generic (URec (Ptr ()) p) | Since: base-4.9.0.0 |
| Generic (URec Char p) | Since: base-4.9.0.0 |
| Generic (URec Double p) | Since: base-4.9.0.0 |
| Generic (URec Float p) | |
| Generic (URec Int p) | Since: base-4.9.0.0 |
| Generic (URec Word p) | Since: base-4.9.0.0 |
| Generic (a, b, c) | Since: base-4.6.0.0 |
| Generic (WrappedArrow a b c) | Since: base-4.7.0.0 |
Defined in Control.Applicative Associated Types type Rep (WrappedArrow a b c) :: Type -> Type # Methods from :: WrappedArrow a b c -> Rep (WrappedArrow a b c) x # to :: Rep (WrappedArrow a b c) x -> WrappedArrow a b c # | |
| Generic (Ap f a) | Since: base-4.12.0.0 |
| Generic (Alt f a) | Since: base-4.8.0.0 |
| Generic (K1 i c p) | Since: base-4.7.0.0 |
| Generic ((f :+: g) p) | Since: base-4.7.0.0 |
| Generic ((f :*: g) p) | Since: base-4.7.0.0 |
| Generic (a, b, c, d) | Since: base-4.6.0.0 |
| Generic (M1 i c f p) | Since: base-4.7.0.0 |
| Generic ((f :.: g) p) | Since: base-4.7.0.0 |
| Generic (a, b, c, d, e) | Since: base-4.6.0.0 |
| Generic (a, b, c, d, e, f) | Since: base-4.6.0.0 |
| Generic (a, b, c, d, e, f, g) | Since: base-4.6.0.0 |
class PartialSemigroup a where Source #
A PartialSemigroup is like a Semigroup, but with an operator returning
rather than Maybe aa.
For comparison:
(<>) ::Semigroupa => a -> a -> a (<>?) ::PartialSemigroupa => a -> a ->Maybea
The associativity axiom for partial semigroups
For all x, y, z:
Relationship to the semigroup associativity axiom
The partial semigroup associativity axiom is a natural adaptation of the semigroup associativity axiom
x<>(y<>z) = (x<>y)<>z
with a slight modification to accommodate situations where <> is undefined. We
may gain some insight into the connection between Semigroup and
PartialSemigroup by rephrasing the partial semigroup associativity in terms of
a partial <> operator thusly:
For all x, y, z: