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

Moduledeferred-folds-0.9.18.6Haskell2010

DeferredFolds.Unfoldl

  • 1 type
  • 12 values
newtypenewtype Unfoldl a
#

A projection on data, which only knows how to execute a strict left-fold.

It is a monad and a monoid, and is very useful for efficiently aggregating the projections on data intended for left-folding, since its concatenation (<>) has complexity of O(1).

Intuition

The intuition for this abstraction can be derived from lists.

Let's consider the Data.List.foldl' function for lists:

foldl' :: (b -> a -> b) -> b -> [a] -> b

If we rearrange its parameters we get

foldl' :: [a] -> (b -> a -> b) -> b -> b

Which in Haskell is essentially the same as

foldl' :: [a] -> (forall b. (b -> a -> b) -> b -> b)

We can isolate that part into an abstraction:

newtype Unfoldl a = Unfoldl (forall b. (b -> a -> b) -> b -> b)

Then we get to this simple morphism:

list :: [a] -> Unfoldl a
list list = Unfoldl (\ step init -> foldl' step init list)

We can do the same with say Data.Text.Text:

text :: Text -> Unfoldl Char
text text = Unfoldl (\ step init -> Data.Text.foldl' step init text)

And then we can use those both to concatenate with just an O(1) cost:

abcdef :: Unfoldl Char
abcdef = list ['a', 'b', 'c'] <> text "def"

Please notice that up until this moment no actual data materialization has happened and hence no traversals have appeared. All that we've done is just composed a function, which only specifies which parts of data structures to traverse to perform a left-fold. Only at the moment where the actual folding will happen will we actually traverse the source data. E.g., using the "fold" function:

abcdefLength :: Int
abcdefLength = fold Control.Foldl.length abcdef

Constructors

  • Unfoldl (forall x. (x -> a -> x) -> x -> x)
Instances12Monad, Functor, Applicative, Foldable, Alternative, MonadPlus, …
  • Monad UnfoldlDefined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Functor UnfoldlDefined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Applicative UnfoldlDefined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Foldable UnfoldlDefined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Alternative UnfoldlDefined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • MonadPlus UnfoldlDefined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • IsList (Unfoldl a)Defined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Eq a => Eq (Unfoldl a)Defined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Show a => Show (Unfoldl a)Defined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Semigroup (Unfoldl a)Defined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • Monoid (Unfoldl a)Defined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
  • type Item (Unfoldl a) = aDefined in deferred-folds-0.9.18.6 · DeferredFolds.Defs.Unfoldl · orphan
valuefold :: Fold input output -> Unfoldl input -> output
#

Apply a Gonzalez fold