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.Instances.ByteString.UTF8

This module defines the ByteStringUTF8 newtype wrapper around ByteString, together with its TextualMonoid instance. The FactorialMonoid instance of a wrapped ByteStringUTF8 value differs from the original ByteString: the prime factors of the original value are its bytes, and for the wrapped value the prime factors are its valid UTF8 byte sequences. The following example session demonstrates the relationship:

> let utf8@(ByteStringUTF8 bs) = fromString "E=mc\xb2"
> bs
"E=mc\194\178"
> factors bs
["E","=","m","c","\194","\178"]
> utf8
"E=mc²"
> factors utf8
["E","=","m","c","²"]

The TextualMonoid instance follows the same logic, but it also decodes all valid UTF8 sequences into characters. Any invalid UTF8 byte sequence from the original ByteString is preserved as a single prime factor:

> let utf8'@(ByteStringUTF8 bs') = ByteStringUTF8 (Data.ByteString.map pred bs)
> bs'
"D<lb\193\177"
> factors bs'
["D","<","l","b","\193","\177"]
> utf8'
"D<lb\[193,177]"
> factors utf8'
["D","<","l","b","\[193,177]"]
  • 1 type
  • 1 value
newtypenewtype ByteStringUTF8
#
Instances15Eq, Data, Ord, Show, IsString, Semigroup, …