Questions tagged [alternative-functor]

Questions about alternative functors, including Haskell's Alternative type class as well as implementations of the concept in other languages.

70 questions
91
votes
1 answer

Distinction between typeclasses MonadPlus, Alternative, and Monoid?

The standard-library Haskell typeclasses MonadPlus, Alternative, and Monoid each provide two methods with essentially the same semantics: An empty value: mzero, empty, or mempty. An operator a -> a -> a that joins values in the typeclass together:…
00dani
  • 1,498
  • 15
  • 17
76
votes
4 answers

What does Haskell's <|> operator do?

Going through Haskell's documentation is always a bit of a pain for me, because all the information you get about a function is often nothing more than just: f a -> f [a] which could mean any number of things. As is the case of the <|> function. All…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
76
votes
5 answers

Confused by the meaning of the 'Alternative' type class and its relationship to other type classes

I've been going through the Typeclassopedia to learn the type classes. I'm stuck understanding Alternative (and MonadPlus, for that matter). The problems I'm having: the 'pedia says that "the Alternative type class is for Applicative functors…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
53
votes
5 answers

What are Alternative's "some" and "many" useful for?

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 and many should be the least solutions of the equations: some v = (:) <$>…
Petr
  • 62,528
  • 13
  • 153
  • 317
35
votes
3 answers

Lax monoidal functors with a different monoidal structure

Applicative functors are well-known and well-loved among Haskellers, for their ability to apply functions in an effectful context. In category-theoretic terms, it can be shown that the methods of Applicative: pure :: a -> f a (<*>) :: f (a -> b) ->…
31
votes
2 answers

Haskell - What is Control.Applicative.Alternative good for?

I was looking at the Applicative class within Haskell libraries and stumbled across Alternative. What is this class good for? A google search did not reveal anything particularly insightful. And it seems to be completely out of place, bundled as it…
Anupam Jain
  • 7,851
  • 2
  • 39
  • 74
26
votes
1 answer

What’s an example of a Monad which is an Alternative but not a MonadPlus?

In his answer to the question “Distinction between typeclasses MonadPlus, Alternative, and Monoid?”, Edward Kmett says that Moreover, even if Applicative was a superclass of Monad, you’d wind up needing the MonadPlus class anyways, because…
22
votes
2 answers

'some' and 'many' functions from the 'Alternative' type class

What are the functions some and many in the Alternative type class useful for? Docs provide a recursive definition which I was unable to comprehend.
18
votes
1 answer

Generate optparse-applicative parser from free alternative functor

Consider the following type signatures: data Foo x = Foo { name :: String , reader :: String -> x } instance Functor Foo where fmap f (Foo n r) = Foo n $ f . r Now I show a natural transformation from Foo to optparse-applicative's Parser…
18
votes
1 answer

Why does Parsec not use Control.Applicative operators

Using Control.Applicative is very useful with Parsec, but you need to always hide <|> and similar objects as they conflict with Parsec's own: import Control.Applicative hiding ((<|>), many, optional) import Text.Parsec.Combinator import…
luispedro
  • 6,934
  • 4
  • 35
  • 45
17
votes
2 answers

Why is there no alternative instance for Either but a semigroup that behaves similarily to alternative?

I am a Haskell newbie and I wonder why there is no alternative instance for Either but a semigroup, which behaves as I would expect it from alternative: instance Semigroup (Either a b) where Left _ <> b = b a <> _ = a This instance discards or…
user6445533
16
votes
3 answers

How to combine and then branch in MonadPlus/Alternative

I recently wrote do e <- (Left <$> m) <|> (Right <$> n) more actions case e of Left x -> ... Right y -> ... This seems awkward. I know that protolude (and some other packages) define -- Called eitherP in parser combinator…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
16
votes
1 answer

Why is there not 'Alternative' instance for 'Control.Applicative.Const'

There is an instance Monoid a => Monoid (Const a b) for the Const functor from Control.Applicative. There is also an instance Monoid m => Applicative (Const m). I would therefore expect that there is also an instance Monoid m => Alternative (Const…
meiersi
  • 308
  • 1
  • 5
15
votes
1 answer

Structurally enforced Free Alternative, without left distributivity

There is a nice Free Alternative in the great free package, which lifts a Functor to a left-distributive Alternative. That is, the claim is that: runAlt :: Alternative g => (forall x. f x -> g x) -> Alt f a -> g a is an Alternative homomorphism,…
15
votes
1 answer

Why does the Alternative typeclass need to be a sub-class of Control.Applicative

Haskell provides a standard typeclass 'Alternative' that effectively provides the <|> operator for any type that is also an Applicative. As I understand it Alternative is considered a Monoid on Applicative's, however the <|> operator seems to make…
James Davies
  • 9,602
  • 5
  • 38
  • 42
1
2 3 4 5