Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no way to derive Applicative Functors in Haskell?

In Haskell, you can derive Functor, Foldable and Traversable automatically using deriving. There is no way to derive Applicative, though. Considering there is one obvious way to define an Applicative instance (which would amount to a zipped application), isn't there any way to enable deriving Applicative?

like image 902
MaiaVictor Avatar asked Apr 22 '15 03:04

MaiaVictor


People also ask

What is applicative in Haskell?

In Haskell, an applicative is a parametrized type that we think of as being a container for data of that type plus two methods pure and <*> . Consider a parametrized type f a . The pure method for an applicative of type f has type. pure :: a -> f a. and can be thought of as bringing values into the applicative.

Is every applicative a monad?

Monads are not a replacement for applicative functors Instead, every monad is an applicative functor (as well as a functor). It is considered good practice not to use >>= if all you need is <*>, or even fmap.

What are Functors Haskell?

Functor in Haskell is a kind of functional representation of different Types which can be mapped over. It is a high level concept of implementing polymorphism. According to Haskell developers, all the Types such as List, Map, Tree, etc. are the instance of the Haskell Functor.

Could you comfortably explain the difference between a monad and an applicative functor?

Functors apply a function to a wrapped value: Applicatives apply a wrapped function to a wrapped value: Monads apply a function that returns a wrapped value to a wrapped value. Monads have a function >>= (pronounced "bind") to do this.


1 Answers

No, this is not obvious at all. Compare the following Applicative instances:

  1. []
  2. ZipList
  3. Data.Sequence.Seq, whose Applicative instance declaration runs to several hundred lines.
  4. IO
  5. (->) r
  6. Parsers in parsec, attoparsec, regex-applicative.
  7. Proxy in the pipes package.

There very little uniformity here, and most of the instances are non-obvious.


As David Young comments, the [] and ZipList instances "are both, ultimately, two different, equally valid Applicative instances for the list type."

like image 165
dfeuer Avatar answered Oct 06 '22 07:10

dfeuer