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

Moduletrifecta-2.1.4Haskell2010

Text.Trifecta.Util.It

harder, better, faster, stronger...

  • 1 type
  • 8 values
  • Packagetrifecta-2.1.4
  • Exports9
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceIt.hs
datadata It r a
#

It is an Iteratee that can produce partial results.

It r a consumes a feed of rs and produces as on the way. New values can be fed using simplifyIt, the current (partial or final) result is extracted using extract.

Example2 expressions
let keepIt    a = Pure alet replaceIt a = It a replaceIt
Example1 expression
extract (keepIt 0)0
Example1 expression
extract (replaceIt 0)0
Example1 expression
extract (simplifyIt (keepIt 0) 5)0
Example1 expression
extract (simplifyIt (replaceIt 0) 5)5

Constructors

  • Pure a

    Final result, rest of the feed is discarded

  • It a (r -> It r a)

    Intermediate result, consumed values produce new results

Instances7Profunctor, Monad, Functor, Applicative, Comonad, ComonadApply, …
  • Profunctor ItDefined in trifecta-2.1.4 · Text.Trifecta.Util.It
  • Monad (It r)Defined in trifecta-2.1.4 · Text.Trifecta.Util.It
  • Functor (It r)Defined in trifecta-2.1.4 · Text.Trifecta.Util.It
  • Applicative (It r)Defined in trifecta-2.1.4 · Text.Trifecta.Util.It
  • Comonad (It r)Defined in trifecta-2.1.4 · Text.Trifecta.Util.It

    It is a cofree comonad

  • ComonadApply (It r)Defined in trifecta-2.1.4 · Text.Trifecta.Util.It
  • Show a => Show (It r a)Defined in trifecta-2.1.4 · Text.Trifecta.Util.It
valueneedIt
  1. :: a

    Initial result

  2. -> (r -> Maybe a)

    Produce a result if possible

  3. -> It r a
#

Consumes input until a value can be produced.

Example1 expression
:{let needTen = needIt 0 (\n -> if n < 10 then Nothing else Just n) :: It Int Int:}
Example1 expression
extract needTen0
Example1 expression
extract (simplifyIt needTen 5)0
Example1 expression
extract (simplifyIt needTen 11)11
Example1 expression
extract (simplifyIt (simplifyIt (simplifyIt needTen 5) 11) 15)11
valuewantIt
  1. :: a

    Initial result

  2. -> (r -> (# Bool, a #))

    Produce a partial or final result

  3. -> It r a
#

Consumes input and produces partial results until a condition is met. Unlike needIt, partial results are already returned when the condition is not fulfilled yet.

>>> :{
let wantTen :: It Int Int
    wantTen = wantIt 0 (\n -> (# n >= 10, n #))
:}
>>> extract wantTen
0
>>> extract (simplifyIt wantTen 5)
5
>>> extract (simplifyIt wantTen 11)
11
>>> extract (simplifyIt (simplifyIt (simplifyIt wantTen 5) 11) 15)
11
valuesimplifyIt :: It r a -> r -> It r a
#

Feed a value to It, obtaining a new (partial or final) result.

valuefoldIt :: (a -> o) -> (a -> (r -> o) -> o) -> It r a -> o
#

The generalized fold (Böhm-Berarducci decoding) over 'It r a'.

foldIt satisfies the property:

foldIt Pure It = id
valuerunIt :: (a -> o) -> (a -> (r -> It r a) -> o) -> It r a -> o
#

Scott decoding of 'It r a'.

The scott decoding is similar to the generalized fold over a data type, but leaves the recursion step to the calling function.

runIt satiesfies the property:

runIt Pure It = id

See also the Scott decoding of lists:

runList :: (a -> [a] -> b) -> b -> [a] -> b

and compare it with foldr (the Böhm-Berarducci decoding for lists):

foldr :: (a -> b -> b) -> b -> [a] -> b
valuefillIt :: r -> (Delta -> ByteString -> r) -> Delta -> It Rope r
#

Given a position, go there, and grab the rest of the line forward from that point.

Example2 expressions
:set -XOverloadedStringslet secondLine = fillIt Nothing (const Just) (delta ("foo\nb" :: Strict.ByteString))
Example1 expression
extract secondLineNothing
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo"))Nothing
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo\nbar"))Just "ar"
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo\nbar\nbaz"))Just "ar\n"

Return the text of the line that contains a given position

Example2 expressions
:set -XOverloadedStringslet secondLine = rewindIt (delta ("foo\nb" :: Strict.ByteString))
Example1 expression
extract secondLineNothing
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo"))Nothing
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo\nbar"))Just "bar"
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo\nbar\nbaz"))Just "bar\n"
valuesliceIt :: Delta -> Delta -> It Rope ByteString
#

Return the text between two offsets.

Example2 expressions
:set -XOverloadedStringslet secondLine = sliceIt (delta ("foo\n" :: Strict.ByteString)) (delta ("foo\nbar\n" :: Strict.ByteString))
Example1 expression
extract secondLine""
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo"))""
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo\nbar"))"bar"
Example1 expression
extract (simplifyIt secondLine (ropeBS "foo\nbar\nbaz"))"bar\n"