Alternative
, an extension of Applicative
, declares empty
, <|>
and these two functions:
One or more:
some :: f a -> f [a]
Zero or more:
many :: f a -> f [a]
If defined,
some
andmany
should be the least solutions of the equations:some v = (:) <$> v <*> many v many v = some v <|> pure []
I couldn't find an instance for which some
and many
are defined. What is their meaning and practical use? Are they used at all? I've been unable to grasp their purpose just from this definition.
Update: I'm not asking what is Alternative
, just what are some
and many
Will provided a good example motivating the use of those methods, but you seem to still have a misunderstanding about type classes.
A type class definition lists the type signatures for the methods that exist for all instances of the type class. It may also provide default implementations of those methods, which is what is happening with Alternative's some and many methods.
In order to be valid instances, all of the methods have to be defined for the instance. So the ones that you found that did not specifically define instances for some or many used the default implementations, and the code for them is exactly as listed in your question.
So, just to be clear, some and many are indeed defined and can be used with all Alternative instances thanks to the default definitions given with the type class definition.
I tend to see them in
Applicative
parser combinator libraries.and I see
many
used for purpose in the default definitions ofParsing
inparsers
.I think Parsec being the primary example of a parser combinator library hides the use of
some
/many
since it redefines things like(<|>)
.An elementary example instance: with
we have
Then, with
we get
In the STM Applicative,
some
would mean: Keep trying until it succeeds at least once, and then keep doing it until it fails.many
would mean: Do this as many times as you can until failure.