HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulemonoid-subclasses-1.2.5.1Haskell2010

Data.Semigroup.Cancellative

This module defines the Semigroup => Reductive => Cancellative class hierarchy.

The Reductive class introduces operation </> which is the inverse of <>. For the Sum semigroup, this operation is subtraction; for Product it is division and for Set it's the set difference. A Reductive semigroup is not a full group because </> may return Nothing.

The Cancellative subclass does not add any operation but it provides the additional guarantee that <> can always be undone with </>. Thus Sum is Cancellative but Product is not because (0*n)/0 is not defined.

All semigroup subclasses listed above are for Abelian, i.e., commutative or symmetric semigroups. Since most practical semigroups in Haskell are not Abelian, each of the these classes has two symmetric superclasses:

  • 8 classes

Symmetric, commutative semigroup classes

4 declarations
classclass Semigroup g => Commutative g
#

A Commutative semigroup is a Semigroup that follows the rule:

a <> b == b <> a
Instances25Commutative, …
classclass (Commutative m, LeftReductive m, RightReductive m) => Reductive m where
#

Class of Abelian semigroups with a partial inverse for the Semigroup <> operation. The inverse operation </> must satisfy the following laws:

maybe a (b <>) (a </> b) == a
maybe a (<> b) (a </> b) == a

The </> operator is a synonym for both stripPrefix and stripSuffix, which must be equivalent as <> is both associative and commutative.

(</>) = flip stripPrefix
(</>) = flip stripSuffix

Methods

Instances16Reductive, …

Subclass of Reductive where </> is a complete inverse of the Semigroup <> operation. The class instances must satisfy the following additional laws:

(a <> b) </> a == Just b
(a <> b) </> b == Just a
Instances8Cancellative, …
classclass Num a => SumCancellative a where
#

Helper class to avoid FlexibleInstances

Methods

Instances4SumCancellative

Asymmetric semigroup classes

4 declarations
classclass Semigroup m => LeftReductive m where
#

Class of semigroups with a left inverse of <>, satisfying the following law:

isPrefixOf a b == isJust (stripPrefix a b)
maybe b (a <>) (stripPrefix a b) == b
a `isPrefixOf` (a <> b)

Every instance definition has to implement at least the stripPrefix method.

Methods

Instances32LeftReductive, …
classclass Semigroup m => RightReductive m where
#

Class of semigroups with a right inverse of <>, satisfying the following law:

isSuffixOf a b == isJust (stripSuffix a b)
maybe b (<> a) (stripSuffix a b) == b
b `isSuffixOf` (a <> b)

Every instance definition has to implement at least the stripSuffix method.

Methods

Instances31RightReductive, …
classclass LeftReductive m => LeftCancellative m
#

Subclass of LeftReductive where stripPrefix is a complete inverse of <>, satisfying the following additional law:

stripPrefix a (a <> b) == Just b
Instances16LeftCancellative, …
classclass RightReductive m => RightCancellative m
#

Subclass of LeftReductive where stripPrefix is a complete inverse of <>, satisfying the following additional law:

stripSuffix b (a <> b) == Just a
Instances15RightCancellative, …