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.Monoid.Textual

This module defines the TextualMonoid class and several of its instances.

  • 1 class
classclass (IsString t, LeftReductive t, LeftGCDMonoid t, FactorialMonoid t) => TextualMonoid t where
#

The TextualMonoid class is an extension of FactorialMonoid specialized for monoids that can contain characters. Its methods are generally equivalent to their namesake functions from Data.List and Data.Text, and they satisfy the following laws:

unfoldr splitCharacterPrefix . fromString == id
splitCharacterPrefix . primePrefix == fmap (\(c, t)-> (c, mempty)) . splitCharacterPrefix

map f . fromString == fromString . List.map f
concatMap (fromString . f) . fromString == fromString . List.concatMap f

foldl  ft fc a . fromString == List.foldl  fc a
foldr  ft fc a . fromString == List.foldr  fc a
foldl' ft fc a . fromString == List.foldl' fc a

scanl f c . fromString == fromString . List.scanl f c
scanr f c . fromString == fromString . List.scanr f c
mapAccumL f a . fromString == fmap fromString . List.mapAccumL f a
mapAccumL f a . fromString == fmap fromString . List.mapAccumL f a

takeWhile pt pc . fromString == fromString . takeWhile pc
dropWhile pt pc . fromString == fromString . dropWhile pc

mconcat . intersperse (singleton c) . split (== c) == id
find p . fromString == List.find p
elem c . fromString == List.elem c

A TextualMonoid may contain non-character data insterspersed between its characters. Every class method that returns a modified TextualMonoid instance generally preserves this non-character data. Methods like foldr can access both the non-character and character data and expect two arguments for the two purposes. For each of these methods there is also a simplified version with underscore in name (like foldr_) that ignores the non-character data.

All of the following expressions are identities:

map id
concatMap singleton
foldl  (<>) (\a c-> a <> singleton c) mempty
foldr  (<>) ((<>) . singleton) mempty
foldl' (<>) (\a c-> a <> singleton c) mempty
scanl1 (const id)
scanr1 const
uncurry (mapAccumL (,))
uncurry (mapAccumR (,))
takeWhile (const True) (const True)
dropWhile (const False) (const False)
toString undefined . fromString
toText undefined . fromText

Methods

Instances12TextualMonoid, …