Haskell function composition operator of type (c→d) → (a→b→c) → (a→b→d)

后端 未结 6 670
耶瑟儿~
耶瑟儿~ 2020-11-28 04:21

Ordinary function composition is of the type

(.) :: (b -> c) -> (a -> b) -> a -> c

I figure this should generalize to types

6条回答
  •  隐瞒了意图╮
    2020-11-28 04:30

    Here's what I think is an elegant way to achieve what you want. The Functor type class gives a way to 'push' a function down into a container so you can apply it to each element using fmap. You can think of a function a -> b as a container of bs with each element indexed by an element of a. So it's natural to make this instance:

    instance Functor ((->) a) where
      fmap f g = f . g
    

    (I think you can get that by importing a suitable library but I can't remember which.)

    Now the usual composition of f with g is trivially an fmap:

    o1 :: (c -> d) -> (b -> c) -> (b -> d)
    f `o1` g = fmap f g
    

    A function of type a -> b -> c is a container of containers of elements of type c. So we just need to push our function f down twice. Here you go:

    o2 :: (c -> d) -> (a -> (b -> c)) -> a -> (b -> d)
    f `o2` g = fmap (fmap f) g
    

    In practice you might find you don't need o1 or o2, just fmap. And if you can find the library whose location I've forgotten, you may find you can just use fmap without writ ing any additional code.

提交回复
热议问题