How to use the function `some`?

122 views Asked by At

I would like to use the some function from Alternative http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#some.

I've tried:

*MyParser Data.Attoparsec.Text Control.Applicative Data.Text> some [3443]


ewrewrew
ewrwwwwww545
43535
435

^CInterrupted. 

As you can see, I interrupted the input. How can I use the some function?

1

There are 1 answers

1
chepner On

Consider what happens when you try to expand some [1]:

some [3443] == some_v
            == liftA2 (:) [3443]  many_v  -- definition of some_v
            == [x:y | x <- [3443], y <- many_v]  -- definition of liftA2
            == [x:y | x <- [3443], y <- some_v ++ pure []] -- definition of many_v

Here's I've immediately replaced <|> with (++). Because (++) is strict in its first argument, you have to evaluate some_v before proceeding, but that gets us into an infinite loop.


Alternative is described as a monoid for applicative functors. If I understand correctly, some xs would be the infinite list of non-empty lists you could create by taking one element from xs at a time.

some [3443] == [[3443], [3443, 3443], [3443, 3443, 3443], ...]

and many xs would be the infinite list of (possibly empty) lists (essentially, just []:some xs.) Because of the strictness of (++), though, the result is not computed lazily, so you never actually terminate.