How (fmap . fmap) typechecks

后端 未结 3 585
囚心锁ツ
囚心锁ツ 2020-12-29 06:36

I have been going through a article(http://comonad.com/reader/2012/abstracting-with-applicatives/) and found the following snippet of code there:

newtype Com         


        
3条回答
  •  Happy的楠姐
    2020-12-29 07:09

    First let's change the type variables' names to be unique:

    (.)  :: (a -> b) -> (r -> a) -> (r -> b)
    fmap :: Functor f => (c -> d) -> f c -> f d
    fmap :: Functor g => (x -> y) -> g x -> g y
    

    Now the first parameter to . has type a -> b and we supply an argument of type (c -> d) -> (f c -> f d), so a is c -> d and b is f c -> f d. So so far we have:

    (.) :: Functor f => -- Left operand
                        ((c -> d) -> (f c -> f d)) ->
                        -- Right operand
                        (r -> (c -> d)) ->
                        -- Result
                        (r -> (f c -> f d))
    

    The second parameter to . has type r -> a a.k.a. r -> (c -> d) and the argument we give has type (x -> y) -> (g x -> g y), so r becomes x -> y, c becomes g x and d becomes g y. So now we have:

    (.)       :: (Functor f, Functor g) => -- Left operand
                                           ((g x -> g y) -> (f (g x) -> f (g y))) -> 
                                           -- Right operand
                                           ((x -> y) -> (g x -> g y)) ->
                                           -- Result
                                           (x -> y) -> f (g x) -> f (g y)
    fmap.fmap :: (Functor f, Functor g) => (x -> y) -> f (g x) -> f (g y)
    

提交回复
热议问题