| Safe Haskell | Safe |
|---|---|
| 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
- genericPartialSemigroupOp :: (Generic a, PartialSemigroupRep (Rep a)) => a -> a -> Maybe a
- class PartialSemigroupRep rep where
- 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:
Minimal complete definition
Methods
repPartialSemigroupOp :: rep a -> rep a -> Maybe (rep a) Source #
Instances
| PartialSemigroup a => PartialSemigroupRep (K1 i a) Source # | |
| (PartialSemigroupRep rep1, PartialSemigroupRep rep2) => PartialSemigroupRep ((:+:) rep1 rep2) Source # | |
| (PartialSemigroupRep rep1, PartialSemigroupRep rep2) => PartialSemigroupRep ((:*:) rep1 rep2) Source # | |
| PartialSemigroupRep rep => PartialSemigroupRep (M1 i meta rep) Source # | |
Re-exports
Representable types of kind *. This class is derivable in GHC with the DeriveGeneric flag on.
Instances
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:
Minimal complete definition
Instances
| PartialSemigroup () Source # | |
| PartialSemigroup [a] Source # | |
| PartialSemigroup a => PartialSemigroup (Identity a) Source # | |
| PartialSemigroup a => PartialSemigroup (ZipList a) Source # | |
| Num a => PartialSemigroup (Sum a) Source # | |
| Num a => PartialSemigroup (Product a) Source # | |
| Semigroup a => PartialSemigroup (Total a) Source # | |
| (PartialSemigroup a, PartialSemigroup b) => PartialSemigroup (Either a b) Source # | |
| (PartialSemigroup a, PartialSemigroup b) => PartialSemigroup (a, b) Source # | |
| PartialSemigroup b => PartialSemigroup (AppendRight a b) Source # | |
| PartialSemigroup a => PartialSemigroup (AppendLeft a b) Source # | |
| (PartialSemigroup a, PartialSemigroup b, PartialSemigroup c) => PartialSemigroup (a, b, c) Source # | |