Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance of a Functor

I have a following type newtype Arr2 e1 e2 a = Arr2 { getArr2 :: e1 -> e2 -> a }. And I got to write Functor instance for it, yet i don't really understand how I tried

instance Functor (Arr2 e1 e2) where
  fmap g (Arr2 a) = Arr2 (g a)

and

instance Functor (Arr2 e1 e2) where
  fmap g = g . getArr2

which actually results in type

(a -> b) -> Arr2 e1 e2 a -> b

instead of desired

(a -> b) -> Arr2 e1 e2 a -> Arr2 e1 e2 b

So please, help me

like image 213
Atin Avatar asked Apr 03 '17 07:04

Atin


People also ask

How many methods should a functor instance have?

Functor in Haskell is a typeclass that provides two methods – fmap and (<$) – for structure-preserving transformations. To implement a Functor instance for a data type, you need to provide a type-specific implementation of fmap – the function we already covered.

What is a functor in programming?

In functional programming, a functor is a design pattern inspired by the definition from category theory, that allows for a generic type to apply a function inside without changing the structure of the generic type. This idea is encoded in Haskell using type class.

Is a list a functor?

For example, lists are functors over some type. That is, given an algebra over a type 'a', you can generate a compatible algebra of lists containing things of type 'a'.

Why is functor useful?

Functor is also important in its role as a superclass of Applicative and of Traversable . When working with these more powerful abstractions, it's often very useful to reach for the fmap method. Show activity on this post. For example, it's possible to derive the function lift in a way that works for any functor.

What is functor in C++ with example?

Functors in C++. A functor (or function object) is a C++ class that acts like a function. Functors are called using the same old function call syntax. To create a functor, we create a object that overloads the operator().

Why can't we make (->) an instance of a functor?

But remember, we said that a type constructor has to take exactly one type parameter so that it can be made an instance of Functor. That's why we can't make (->) an instance of Functor, but if we partially apply it to (->) r, it doesn't pose any problems.

Is (->R) also a functor?

With Maybe, the function f, is applied to a value within a Just or not applied if the value is a Nothing. When we hear that (-> r) is also a Functor it can boggle our minds a little. How do we define an instance for that? instance Functor ( -> r) where fmap f = -- what goes here?

What is the use of instanceof operator?

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value. Its behavior can be customized with Symbol.hasInstance. The object to test. Constructor to test against.


Video Answer


1 Answers

The Functor class has as definition:

class Functor f where:
    fmap :: (a -> b) -> f a -> f b
    (<$) :: a -> f b -> f a

The (<$) has a default implementation: (<$) = fmap . const which works fine.

So that means that if we enter a function (g :: a -> b) as first argument, and an Arr2 that produces an a, we have to generate an Arr2 that calls that g on the outcome of the arrow if it is applied.

As a result the definition of fmap for your Arr2 is:

instance Functor (Arr2 e1 e2) where
  fmap g (Arr2 a) = Arr2 (\x y -> g (a x y))

Or more elegantly:

instance Functor (Arr2 e1 e2) where
  fmap g (Arr2 a) = Arr2 (\x -> g . (a x))

Or a more elegant version - commented by @Alec:

instance Functor (Arr2 e1 e2) where
  fmap g (Arr2 a) = Arr2 ((g .) . a)

(you can convert expressions to pointfree ones using this tool)

like image 74
Willem Van Onsem Avatar answered Oct 09 '22 04:10

Willem Van Onsem