Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In applicative, how can `<*>` be represented in terms of `fmap_i, i=0,1,2,...`?

Class Applicative is declared as:

class Functor f   =>  Applicative f   where
pure  ::  a   ->  f   a
(<*>) ::  f   (a  ->  b)  ->  f   a   ->  f   b

We can represent fmapi, i=0,1,2,... in terms of pure and (<*>):

fmap0 ::  a   ->  f   a
fmap0 =   pure
fmap1 ::  (a  ->  b)  ->  f   a   ->  f   b
fmap1 g   x   =   pure    g   <*> x
fmap2 ::  (a  ->  b   ->  c)  ->  f   a   ->  f   b   ->  f   c
fmap2 g   x   y   =   pure    g   <*> x   <*> y
fmap3 ::  (a  ->  b   ->  c   ->  d)  ->  f   a   ->  f   b   ->  f   c   ->  f   d
fmap3 g   x   y   z   =   pure    g   <*> x   <*> y   <*> z

In applicative, how can <*> be represented in terms of fmap_i, i=0,1,2,...?

Thanks.

See also Is the implementation of `<*>` based on `fmap` special to Maybe applicative or can it be generalized to other applicatives?

like image 378
Tim Avatar asked Dec 31 '22 18:12

Tim


1 Answers

You can write:

(<*>) = fmap2 ($)

or, if you find it less obscure:

f <*> a = fmap2 apply f a
  where apply g x = g x
like image 60
K. A. Buhr Avatar answered Apr 27 '23 22:04

K. A. Buhr