Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concrete Type Example of a Functor that Fails to be an Applicative? [duplicate]

From functors that are not applicatives:

A type constructor which is a Functor but not an Applicative. A simple example is a pair:

instance Functor ((,) r) where
    fmap f (x,y) = (x, f y)

But there is no way how to define its Applicative instance without imposing additional restrictions on r. In particular, there is no way how to define pure :: a -> (r, a) for an arbitrary r.

Here, pure fails to be definable for all types at once; however, for any concrete type T, one can make ((,) T) an applicative.

Question: Is there an example of a concrete functor (i.e., no type variables involved) that is a functor but not an applicative?

like image 836
George Avatar asked May 23 '17 03:05

George


People also ask

Is it possible to implement applicative functors in F #?

Applicative functors aren't explicitly modelled in F#, but they're easy enough to add if you need them. F# doesn't have typeclasses, so implementing applicative functors tend to be more on a case-by-case basis.

How do you make a constructor an instance of a functor?

If we want to make a type constructor an instance of Functor, it has to have a kind of * -> *, which means that it has to take exactly one concrete type as a type parameter. For example, Maybe can be made an instance because it takes one type parameter to produce a concrete type, like Maybe Int or Maybe String.

What is an applicative functor in Haskell?

As a variation, an applicative functor maps objects in an 'elevated world' using functions from the same 'elevated world'. In Haskell, an applicative functor is defined like this:

What is a functor?

So what's a functor F: BG → BH F: B G → B H? It's precisely a group homomorphism from G G to H H! So in this example, a functor is just a function (which happens to be compatible with the group structure).


2 Answers

I don't have 50 reputation to comment here, so I'll try to do it as an answer:

however, for any concrete type T, one can make ((,) T) an applicative.

...

There's a theorem in mathematics that any collection with at least 2 elements can be made into a monoid. So for any concrete type T, it could in principle be made a member of Monoid, and then could in principle be made Applicative. What's wrong with this reasoning?

What about the tuple from the uninhabited type? (,) Void

It is a Functor,right?

Could you derive Applicative for it? How would pure be implemented?

like image 169
rbasso Avatar answered Oct 20 '22 01:10

rbasso


There is nice example in reactive-banana library.

It features Event a types, which represents a single simultaneous event in time (think, an impulse), and Behavior a type, which represents a value that is available in any moment (for instance, emitting a value from the last event).

Behavior is an Applicative, because you can combine two of them - they have a value in any point of time.

enter image description here

Event, though, is a Functor only, because you can't combine them. Given two Events you can't be sure they will happen simultaneosly.

Event

like image 26
arrowd Avatar answered Oct 20 '22 00:10

arrowd