How to map a function over the arguments of another function in Haskell?

So, initially I wrote:

xs <- getAddrInfo (Just hints) (Just addr) (Just port)

then it seemed to me that the function "Just :: a → Maybe a" is "displayed" above the "hints", "addr" and "port", so I came up with something like this:

map_arg g f a b c = f (g a) (g b) (g c)
xs <- map_arg Just getAddrInfo hints addr port

but the GHC expects (ga), (gb) and (gc) to be of the same type, so this does not check the type.

Is there a way to do this, or more general, is there a way to map a function over the arguments of another function?

+4
source share
3 answers

The most common type signature will look like

map_arg :: (forall b.b -> a b) -> (a b -> a c -> a d -> e) -> b -> c -> d -> e
map_arg g f a b c = f (g a) (g b) (g c)

for your case, if you decide not to put gas a parameter, you can do

map_just f a b c = f (g a) (g b) (g c) where g = Just
xs <- map_just getAddrInfo hints addr port

g:

map_arg (g :: forall b.b -> a b) f a b c = f (g a) (g b) (g c)

, , Control.Functor.Pointed, :

map_arg :: Pointed p => (p a -> p b -> p c -> d) -> a -> b -> c -> d
map_arg f a b c = f (point a) (point b) (point c)

( Pointed Maybe - Just, )

, ,

map1 :: Pointed p => (p a -> b) -> a -> b
map1 f = f . point

map2 :: Pointed p => (p a -> p b -> c) -> a -> b -> c
map2 f = map1 . map1 f

map3 :: Pointed p => (p a -> p b -> p c -> d) -> a -> b -> c -> d
map3 f = map2 . map1 f

? map1, - !

+5

- ( map_arg, ).

, Oleg, , .

+4

map_arg g .

map_arg :: (forall a. a -> Maybe a) ->
           (Maybe AddrInfo -> Maybe HostName -> Maybe ServiceName -> IO [AddrInfo]) ->
           AddrInfo -> HostName -> ServiceName -> IO [AddrInfo]
map_arg g f a b c = f (g a) (g b) (g c)

Rank2Types RankNTypes.

+2

All Articles