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
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.
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.
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'.
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.
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().
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.
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?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With