Feature Reader Application

Looking at reader or environment applicativefrom Brent Yorgey UPenn 2013 Lecture :

instance Functor ((->) e) where
  fmap = (.)

instance Applicative ((->) e) where
  pure = const
  f <*> x = \e -> (f e) (x e)

I am trying to get an intuition for an applicative instance.

Given:

(<*>) :: Applicative f => f (a -> b) -> f a -> f b

then how does it \e -> (f e) (x e)lead to type f b?

+4
source share
2 answers

If we substitute ((->) e)for f(remembering that this is a function with eas the type of the argument), we get:

(<*>) ::     f    (a -> b)  ->    f     a  ->    f    b
(<*>) :: ((->) e) (a -> b)  -> ((->) e) a  -> ((->) e) b  -- Replace f with ((->) e)
(<*>) ::    (e -> (a -> b)) ->    (e -> a) ->    (e -> b) -- Apply ((->) e)
(<*>) ::    (e ->  a -> b)  ->    (e -> a) ->     e -> b  -- Remove unneeded parentheses

It is important to remember what ((->) e) acoincides with e -> a. Notation may look a little misleading at the beginning.

The only implementation for this type is the one you provided in your question. This definition can also be written as:

f <*> x = \e -> f e (x e)

or using prefix notation:

(<*>) f x e = f e (x e)
+8

S combinator. ( , .)

:

zipping. (f a), e. , , , e: e . :

f 0, f 1, f 2 .. 0, a 1, a 2 .. (f 0, a 0), (f 1, 1), (f 2, a 2) ..

fi ai, (f 0 a 0), (f 1 a 1), (f 2 a 2) .. , f a, ( "" ), , . , S-.

-2

Source: https://habr.com/ru/post/1569584/


All Articles