How can I understand “(.) . (.)”?

前端 未结 5 1879
我寻月下人不归
我寻月下人不归 2020-12-08 00:40

I believe I understand fmap . fmap for Functors, but on functions it\'s hurting my head for months now.

I\'ve seen that you can just apply the definitio

5条回答
  •  猫巷女王i
    2020-12-08 01:31

    You can also use your understanding of fmap . fmap.

    If you have two Functors foo and bar, then

    fmap . fmap :: (a -> b)  ->  foo (bar a)    ->   foo (bar b)
    

    fmap . fmap takes a function and produces an induced function for the composition of the two Functors.

    Now, for any type t, (->) t is a Functor, and the fmap for that Functor is (.).

    So (.) . (.) is fmap . fmap for the case where the two Functors are (->) s and (->) t, and thus

    (.) . (.) :: (a -> b) -> ((->) s) ((->) t a) -> ((->) s) ((->) t b)
              =  (a -> b) -> (s -> (t -> a))     -> (s -> (t -> b))
              =  (a -> b) -> (s ->  t -> a )     -> (s ->  t -> b )
    

    it "composes" a function f :: a -> b with a function of two arguments, g :: s -> t -> a,

    ((.) . (.)) f g = \x y -> f (g x y)
    

    That view also makes it clear that, and how, the pattern extends to functions taking more arguments,

    (.)             :: (a -> b) -> (s ->           a) -> (s ->           b)
    (.) . (.)       :: (a -> b) -> (s -> t ->      a) -> (s -> t ->      b)
    (.) . (.) . (.) :: (a -> b) -> (s -> t -> u -> a) -> (s -> t -> u -> b)
    

    etc.

提交回复
热议问题