Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
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 Rep
s for which we can automatically derive
PartialSemigroup
:
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
≡id
to
.from
≡id
Instances
class PartialSemigroup a where Source #
A PartialSemigroup
is like a Semigroup
, but with an operator returning
rather than Maybe
aa
.
For comparison:
(<>
) ::Semigroup
a => a -> a -> a (<>?
) ::PartialSemigroup
a => a -> a ->Maybe
a
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
: