Data.Semigroup.Factorial

monoid-subclasses-1.2.5.1: Subclasses of Monoid

Safe Haskell | Trustworthy
Language | Haskell2010

Data.Semigroup.Factorial

Contents

- Classes
- Monad function equivalents

Description

This module defines the `Semigroup` => `Factorial` => `StableFactorial` classes and some of their instances.

Synopsis

- class Semigroup m => Factorial m where
  - factors :: m -> [m]
  - primePrefix :: m -> m
  - primeSuffix :: m -> m
  - foldl :: (a -> m -> a) -> a -> m -> a
  - foldl' :: (a -> m -> a) -> a -> m -> a
  - foldr :: (m -> a -> a) -> a -> m -> a
  - length :: m -> Int
  - foldMap :: Monoid n => (m -> n) -> m -> n
  - reverse :: m -> m
- class Factorial m => StableFactorial m
- mapM :: (Factorial a, Semigroup b, Monoid b, Monad m) => (a -> m b) -> a -> m b
- mapM_ :: (Factorial a, Applicative m) => (a -> m b) -> a -> m ()

# Classes

class Semigroup m => Factorial m where #

Class of semigroups that can be split into irreducible (i.e., atomic or prime) `factors` in a unique way. Factors of a `Product` are literally its prime factors:

    factors (Product 12) == [Product 2, Product 2, Product 3]

Factors of a list are not its elements but all its single-item sublists:

    factors "abc" == ["a", "b", "c"]

The methods of this class satisfy the following laws:

    maybe id sconcat  . nonEmpty . factors == id
    List.all (\prime-> factors prime == [prime]) . factors
    primePrefix s == foldr const s s
    foldl f a == List.foldl f a . factors
    foldl' f a == List.foldl' f a . factors
    foldr f a == List.foldr f a . factors

A minimal instance definition must implement `factors` or `foldr`. Other methods can and should be implemented only for performance reasons.

Minimal complete definition

factors | foldr

Methods

factors :: m -> [m] #

Returns a list of all prime factors; inverse of mconcat.

primePrefix :: m -> m #

The prime prefix; `primePrefix mempty == mempty` for monoids.

primeSuffix :: m -> m #

The prime suffix; `primeSuffix mempty == mempty` for monoids.

foldl :: (a -> m -> a) -> a -> m -> a #

Like `foldl` from Data.List on the list of prime `factors`.

foldl' :: (a -> m -> a) -> a -> m -> a #

Like `foldl'` from Data.List on the list of prime `factors`.

foldr :: (m -> a -> a) -> a -> m -> a #

Like `foldr` from Data.List on the list of prime `factors`.

length :: m -> Int #

The `length` of the list of prime `factors`.

foldMap :: Monoid n => (m -> n) -> m -> n #

Generalizes `foldMap` from Data.Foldable, except the function arguments are prime `factors` rather than the structure elements.

reverse :: m -> m #

Equivalent to `reverse` from Data.List.

#### Instances

Instances details

Factorial ByteString # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: ByteString -> [ByteString] # primePrefix :: ByteString -> ByteString # primeSuffix :: ByteString -> ByteString # foldl :: (a -> ByteString -> a) -> a -> ByteString -> a # foldl' :: (a -> ByteString -> a) -> a -> ByteString -> a # foldr :: (ByteString -> a -> a) -> a -> ByteString -> a # length :: ByteString -> Int # foldMap :: Monoid n => (ByteString -> n) -> ByteString -> n # reverse :: ByteString -> ByteString #
Factorial ByteString # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: ByteString -> [ByteString] # primePrefix :: ByteString -> ByteString # primeSuffix :: ByteString -> ByteString # foldl :: (a -> ByteString -> a) -> a -> ByteString -> a # foldl' :: (a -> ByteString -> a) -> a -> ByteString -> a # foldr :: (ByteString -> a -> a) -> a -> ByteString -> a # length :: ByteString -> Int # foldMap :: Monoid n => (ByteString -> n) -> ByteString -> n # reverse :: ByteString -> ByteString #
Factorial IntSet # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: IntSet -> [IntSet] # primePrefix :: IntSet -> IntSet # primeSuffix :: IntSet -> IntSet # foldl :: (a -> IntSet -> a) -> a -> IntSet -> a # foldl' :: (a -> IntSet -> a) -> a -> IntSet -> a # foldr :: (IntSet -> a -> a) -> a -> IntSet -> a # length :: IntSet -> Int # foldMap :: Monoid n => (IntSet -> n) -> IntSet -> n # reverse :: IntSet -> IntSet #
Factorial ByteStringUTF8 # |
Instance details Defined in Data.Monoid.Instances.ByteString.UTF8 Methods factors :: ByteStringUTF8 -> [ByteStringUTF8] # primePrefix :: ByteStringUTF8 -> ByteStringUTF8 # primeSuffix :: ByteStringUTF8 -> ByteStringUTF8 # foldl :: (a -> ByteStringUTF8 -> a) -> a -> ByteStringUTF8 -> a # foldl' :: (a -> ByteStringUTF8 -> a) -> a -> ByteStringUTF8 -> a # foldr :: (ByteStringUTF8 -> a -> a) -> a -> ByteStringUTF8 -> a # length :: ByteStringUTF8 -> Int # foldMap :: Monoid n => (ByteStringUTF8 -> n) -> ByteStringUTF8 -> n # reverse :: ByteStringUTF8 -> ByteStringUTF8 #
Factorial Text # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Text -> [Text] # primePrefix :: Text -> Text # primeSuffix :: Text -> Text # foldl :: (a -> Text -> a) -> a -> Text -> a # foldl' :: (a -> Text -> a) -> a -> Text -> a # foldr :: (Text -> a -> a) -> a -> Text -> a # length :: Text -> Int # foldMap :: Monoid n => (Text -> n) -> Text -> n # reverse :: Text -> Text #
Factorial Text # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Text -> [Text] # primePrefix :: Text -> Text # primeSuffix :: Text -> Text # foldl :: (a -> Text -> a) -> a -> Text -> a # foldl' :: (a -> Text -> a) -> a -> Text -> a # foldr :: (Text -> a -> a) -> a -> Text -> a # length :: Text -> Int # foldMap :: Monoid n => (Text -> n) -> Text -> n # reverse :: Text -> Text #
Factorial () # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: () -> [()] # primePrefix :: () -> () # primeSuffix :: () -> () # foldl :: (a -> () -> a) -> a -> () -> a # foldl' :: (a -> () -> a) -> a -> () -> a # foldr :: (() -> a -> a) -> a -> () -> a # length :: () -> Int # foldMap :: Monoid n => (() -> n) -> () -> n # reverse :: () -> () #
Factorial (IntMap a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: IntMap a -> [IntMap a] # primePrefix :: IntMap a -> IntMap a # primeSuffix :: IntMap a -> IntMap a # foldl :: (a0 -> IntMap a -> a0) -> a0 -> IntMap a -> a0 # foldl' :: (a0 -> IntMap a -> a0) -> a0 -> IntMap a -> a0 # foldr :: (IntMap a -> a0 -> a0) -> a0 -> IntMap a -> a0 # length :: IntMap a -> Int # foldMap :: Monoid n => (IntMap a -> n) -> IntMap a -> n # reverse :: IntMap a -> IntMap a #
Factorial (Seq a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Seq a -> [Seq a] # primePrefix :: Seq a -> Seq a # primeSuffix :: Seq a -> Seq a # foldl :: (a0 -> Seq a -> a0) -> a0 -> Seq a -> a0 # foldl' :: (a0 -> Seq a -> a0) -> a0 -> Seq a -> a0 # foldr :: (Seq a -> a0 -> a0) -> a0 -> Seq a -> a0 # length :: Seq a -> Int # foldMap :: Monoid n => (Seq a -> n) -> Seq a -> n # reverse :: Seq a -> Seq a #
Ord a => Factorial (Set a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Set a -> [Set a] # primePrefix :: Set a -> Set a # primeSuffix :: Set a -> Set a # foldl :: (a0 -> Set a -> a0) -> a0 -> Set a -> a0 # foldl' :: (a0 -> Set a -> a0) -> a0 -> Set a -> a0 # foldr :: (Set a -> a0 -> a0) -> a0 -> Set a -> a0 # length :: Set a -> Int # foldMap :: Monoid n => (Set a -> n) -> Set a -> n # reverse :: Set a -> Set a #
Factorial a => Factorial (Dual a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Dual a -> [Dual a] # primePrefix :: Dual a -> Dual a # primeSuffix :: Dual a -> Dual a # foldl :: (a0 -> Dual a -> a0) -> a0 -> Dual a -> a0 # foldl' :: (a0 -> Dual a -> a0) -> a0 -> Dual a -> a0 # foldr :: (Dual a -> a0 -> a0) -> a0 -> Dual a -> a0 # length :: Dual a -> Int # foldMap :: Monoid n => (Dual a -> n) -> Dual a -> n # reverse :: Dual a -> Dual a #
Integral a => Factorial (Product a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Product a -> [Product a] # primePrefix :: Product a -> Product a # primeSuffix :: Product a -> Product a # foldl :: (a0 -> Product a -> a0) -> a0 -> Product a -> a0 # foldl' :: (a0 -> Product a -> a0) -> a0 -> Product a -> a0 # foldr :: (Product a -> a0 -> a0) -> a0 -> Product a -> a0 # length :: Product a -> Int # foldMap :: Monoid n => (Product a -> n) -> Product a -> n # reverse :: Product a -> Product a #
(Integral a, Eq a) => Factorial (Sum a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Sum a -> [Sum a] # primePrefix :: Sum a -> Sum a # primeSuffix :: Sum a -> Sum a # foldl :: (a0 -> Sum a -> a0) -> a0 -> Sum a -> a0 # foldl' :: (a0 -> Sum a -> a0) -> a0 -> Sum a -> a0 # foldr :: (Sum a -> a0 -> a0) -> a0 -> Sum a -> a0 # length :: Sum a -> Int # foldMap :: Monoid n => (Sum a -> n) -> Sum a -> n # reverse :: Sum a -> Sum a #
(Factorial a, PositiveMonoid a) => Factorial (Concat a) # |
Instance details Defined in Data.Monoid.Instances.Concat Methods factors :: Concat a -> [Concat a] # primePrefix :: Concat a -> Concat a # primeSuffix :: Concat a -> Concat a # foldl :: (a0 -> Concat a -> a0) -> a0 -> Concat a -> a0 # foldl' :: (a0 -> Concat a -> a0) -> a0 -> Concat a -> a0 # foldr :: (Concat a -> a0 -> a0) -> a0 -> Concat a -> a0 # length :: Concat a -> Int # foldMap :: Monoid n => (Concat a -> n) -> Concat a -> n # reverse :: Concat a -> Concat a #
(StableFactorial a, MonoidNull a) => Factorial (Measured a) # |
Instance details Defined in Data.Monoid.Instances.Measured Methods factors :: Measured a -> [Measured a] # primePrefix :: Measured a -> Measured a # primeSuffix :: Measured a -> Measured a # foldl :: (a0 -> Measured a -> a0) -> a0 -> Measured a -> a0 # foldl' :: (a0 -> Measured a -> a0) -> a0 -> Measured a -> a0 # foldr :: (Measured a -> a0 -> a0) -> a0 -> Measured a -> a0 # length :: Measured a -> Int # foldMap :: Monoid n => (Measured a -> n) -> Measured a -> n # reverse :: Measured a -> Measured a #
(StableFactorial m, TextualMonoid m) => Factorial (LinePositioned m) # |
Instance details Defined in Data.Monoid.Instances.Positioned Methods factors :: LinePositioned m -> [LinePositioned m] # primePrefix :: LinePositioned m -> LinePositioned m # primeSuffix :: LinePositioned m -> LinePositioned m # foldl :: (a -> LinePositioned m -> a) -> a -> LinePositioned m -> a # foldl' :: (a -> LinePositioned m -> a) -> a -> LinePositioned m -> a # foldr :: (LinePositioned m -> a -> a) -> a -> LinePositioned m -> a # length :: LinePositioned m -> Int # foldMap :: Monoid n => (LinePositioned m -> n) -> LinePositioned m -> n # reverse :: LinePositioned m -> LinePositioned m #
StableFactorial m => Factorial (OffsetPositioned m) # |
Instance details Defined in Data.Monoid.Instances.Positioned Methods factors :: OffsetPositioned m -> [OffsetPositioned m] # primePrefix :: OffsetPositioned m -> OffsetPositioned m # primeSuffix :: OffsetPositioned m -> OffsetPositioned m # foldl :: (a -> OffsetPositioned m -> a) -> a -> OffsetPositioned m -> a # foldl' :: (a -> OffsetPositioned m -> a) -> a -> OffsetPositioned m -> a # foldr :: (OffsetPositioned m -> a -> a) -> a -> OffsetPositioned m -> a # length :: OffsetPositioned m -> Int # foldMap :: Monoid n => (OffsetPositioned m -> n) -> OffsetPositioned m -> n # reverse :: OffsetPositioned m -> OffsetPositioned m #
(FactorialMonoid m, StableFactorial m) => Factorial (Shadowed m) # |
Instance details Defined in Data.Monoid.Instances.PrefixMemory Methods factors :: Shadowed m -> [Shadowed m] # primePrefix :: Shadowed m -> Shadowed m # primeSuffix :: Shadowed m -> Shadowed m # foldl :: (a -> Shadowed m -> a) -> a -> Shadowed m -> a # foldl' :: (a -> Shadowed m -> a) -> a -> Shadowed m -> a # foldr :: (Shadowed m -> a -> a) -> a -> Shadowed m -> a # length :: Shadowed m -> Int # foldMap :: Monoid n => (Shadowed m -> n) -> Shadowed m -> n # reverse :: Shadowed m -> Shadowed m #
Factorial (Vector a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Vector a -> [Vector a] # primePrefix :: Vector a -> Vector a # primeSuffix :: Vector a -> Vector a # foldl :: (a0 -> Vector a -> a0) -> a0 -> Vector a -> a0 # foldl' :: (a0 -> Vector a -> a0) -> a0 -> Vector a -> a0 # foldr :: (Vector a -> a0 -> a0) -> a0 -> Vector a -> a0 # length :: Vector a -> Int # foldMap :: Monoid n => (Vector a -> n) -> Vector a -> n # reverse :: Vector a -> Vector a #
Factorial a => Factorial (Maybe a) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Maybe a -> [Maybe a] # primePrefix :: Maybe a -> Maybe a # primeSuffix :: Maybe a -> Maybe a # foldl :: (a0 -> Maybe a -> a0) -> a0 -> Maybe a -> a0 # foldl' :: (a0 -> Maybe a -> a0) -> a0 -> Maybe a -> a0 # foldr :: (Maybe a -> a0 -> a0) -> a0 -> Maybe a -> a0 # length :: Maybe a -> Int # foldMap :: Monoid n => (Maybe a -> n) -> Maybe a -> n # reverse :: Maybe a -> Maybe a #
Factorial [x] # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: [x] -> [[x]] # primePrefix :: [x] -> [x] # primeSuffix :: [x] -> [x] # foldl :: (a -> [x] -> a) -> a -> [x] -> a # foldl' :: (a -> [x] -> a) -> a -> [x] -> a # foldr :: ([x] -> a -> a) -> a -> [x] -> a # length :: [x] -> Int # foldMap :: Monoid n => ([x] -> n) -> [x] -> n # reverse :: [x] -> [x] #
Ord k => Factorial (Map k v) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: Map k v -> [Map k v] # primePrefix :: Map k v -> Map k v # primeSuffix :: Map k v -> Map k v # foldl :: (a -> Map k v -> a) -> a -> Map k v -> a # foldl' :: (a -> Map k v -> a) -> a -> Map k v -> a # foldr :: (Map k v -> a -> a) -> a -> Map k v -> a # length :: Map k v -> Int # foldMap :: Monoid n => (Map k v -> n) -> Map k v -> n # reverse :: Map k v -> Map k v #
(FactorialMonoid a, FactorialMonoid b) => Factorial (Stateful a b) # |
Instance details Defined in Data.Monoid.Instances.Stateful Methods factors :: Stateful a b -> [Stateful a b] # primePrefix :: Stateful a b -> Stateful a b # primeSuffix :: Stateful a b -> Stateful a b # foldl :: (a0 -> Stateful a b -> a0) -> a0 -> Stateful a b -> a0 # foldl' :: (a0 -> Stateful a b -> a0) -> a0 -> Stateful a b -> a0 # foldr :: (Stateful a b -> a0 -> a0) -> a0 -> Stateful a b -> a0 # length :: Stateful a b -> Int # foldMap :: Monoid n => (Stateful a b -> n) -> Stateful a b -> n # reverse :: Stateful a b -> Stateful a b #
(Factorial a, Factorial b, MonoidNull a, MonoidNull b) => Factorial (a, b) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: (a, b) -> [(a, b)] # primePrefix :: (a, b) -> (a, b) # primeSuffix :: (a, b) -> (a, b) # foldl :: (a0 -> (a, b) -> a0) -> a0 -> (a, b) -> a0 # foldl' :: (a0 -> (a, b) -> a0) -> a0 -> (a, b) -> a0 # foldr :: ((a, b) -> a0 -> a0) -> a0 -> (a, b) -> a0 # length :: (a, b) -> Int # foldMap :: Monoid n => ((a, b) -> n) -> (a, b) -> n # reverse :: (a, b) -> (a, b) #
(Factorial a, Factorial b, Factorial c, MonoidNull a, MonoidNull b, MonoidNull c) => Factorial (a, b, c) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: (a, b, c) -> [(a, b, c)] # primePrefix :: (a, b, c) -> (a, b, c) # primeSuffix :: (a, b, c) -> (a, b, c) # foldl :: (a0 -> (a, b, c) -> a0) -> a0 -> (a, b, c) -> a0 # foldl' :: (a0 -> (a, b, c) -> a0) -> a0 -> (a, b, c) -> a0 # foldr :: ((a, b, c) -> a0 -> a0) -> a0 -> (a, b, c) -> a0 # length :: (a, b, c) -> Int # foldMap :: Monoid n => ((a, b, c) -> n) -> (a, b, c) -> n # reverse :: (a, b, c) -> (a, b, c) #
(Factorial a, Factorial b, Factorial c, Factorial d, MonoidNull a, MonoidNull b, MonoidNull c, MonoidNull d) => Factorial (a, b, c, d) # |
Instance details Defined in Data.Semigroup.Factorial Methods factors :: (a, b, c, d) -> [(a, b, c, d)] # primePrefix :: (a, b, c, d) -> (a, b, c, d) # primeSuffix :: (a, b, c, d) -> (a, b, c, d) # foldl :: (a0 -> (a, b, c, d) -> a0) -> a0 -> (a, b, c, d) -> a0 # foldl' :: (a0 -> (a, b, c, d) -> a0) -> a0 -> (a, b, c, d) -> a0 # foldr :: ((a, b, c, d) -> a0 -> a0) -> a0 -> (a, b, c, d) -> a0 # length :: (a, b, c, d) -> Int # foldMap :: Monoid n => ((a, b, c, d) -> n) -> (a, b, c, d) -> n # reverse :: (a, b, c, d) -> (a, b, c, d) #

class Factorial m => StableFactorial m #

A subclass of `Factorial` whose instances satisfy the following additional laws:

    factors (a <> b) == factors a <> factors b
    factors . reverse == List.reverse . factors
    primeSuffix s == primePrefix (reverse s)

#### Instances

Instances details

StableFactorial ByteString # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial ByteString # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial Text # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial Text # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial () # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial (Seq a) # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial a => StableFactorial (Dual a) # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial (Sum Natural) # |
Instance details Defined in Data.Semigroup.Factorial
(Factorial a, PositiveMonoid a) => StableFactorial (Concat a) # |
Instance details Defined in Data.Monoid.Instances.Concat
(StableFactorial a, MonoidNull a) => StableFactorial (Measured a) # |
Instance details Defined in Data.Monoid.Instances.Measured
(StableFactorial m, TextualMonoid m) => StableFactorial (LinePositioned m) # |
Instance details Defined in Data.Monoid.Instances.Positioned
StableFactorial m => StableFactorial (OffsetPositioned m) # |
Instance details Defined in Data.Monoid.Instances.Positioned
(StableFactorial m, FactorialMonoid m) => StableFactorial (Shadowed m) # |
Instance details Defined in Data.Monoid.Instances.PrefixMemory
StableFactorial (Vector a) # |
Instance details Defined in Data.Semigroup.Factorial
StableFactorial [x] # |
Instance details Defined in Data.Semigroup.Factorial
(FactorialMonoid a, FactorialMonoid b, StableFactorial a, StableFactorial b) => StableFactorial (Stateful a b) # |
Instance details Defined in Data.Monoid.Instances.Stateful

# Monad function equivalents

mapM :: (Factorial a, Semigroup b, Monoid b, Monad m) => (a -> m b) -> a -> m b #

A `mapM` equivalent.

mapM_ :: (Factorial a, Applicative m) => (a -> m b) -> a -> m () #

A `mapM_` equivalent.