如何推导出 Haskell 中的构图类型

How to derive type of composition in Haskell

我是 Haskell 的新手。 我想了解类型的组合是如何工作的。

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

fmap :: Functor f => (x -> y) -> f x -> f y
fmap . fmap :: (Functor f1, Functor f2) => (x -> y) -> f1 (f2 x) -> f1 (f2 y)

以上类型信息我是怎么理解的


1. (x -> y) -> f1 x -> f1 y  -- First fmap
      -> (x' -> y') -> f2 x' -> f2 y'  -- Second fmap
2. Compare the (1) with (.) type signature then we get
     In (b -> c)
        b = (x -> y)
        c = f1 x -> f1 y
     In (a -> b)
        a = (x' -> y')
        b = f2 x' -> f2 y'
3. Now the result is a -> c but before that b in (b -> c) should be b in (a -> b)
     (x -> y) === f2 x' -> f2 y'
     That means x = f2 x' & y = f2 y'
4. Result is a -> c
     (x' -> y') -> f1 x -> f1 y
     Substituting (3) results here
     (x' -> y') -> f1 (f2 x') -> f1 (f2 y')
     This is Alpha equivalent to  (x -> y) -> f1 (f2 x) -> f1 (f2 y)

为了测试我的理解,我尝试了各种数据类型,但我在少数情况下取得了成功。 下面是我无法弄清楚的类型签名。


 f = undefined :: (x -> y -> w -> z -> a) -> g x -> g y
 -- Type of f . f in prelude
 f . f :: (x -> (w1 -> z1 -> a1) -> w2 -> z2 -> a2) -> g (y -> x) -> g y

我对上述的做法

1. (x -> y -> w -> z -> a) -> g x -> g y
     -> (x' -> y' -> w' -> z' -> a') -> g' x' -> g' y'
2. Comparing with (.) then 
     In (b -> c)
        b = (x -> y -> w -> z -> a)
        c = g x -> g y
     In (a -> b)
        a = (x' -> y' -> w' -> z' -> a')
        b = g' x' -> g' y'
3. Comparing b from (b -> c) & (a -> b)
     (x -> y -> w -> z -> a) === g' x' -> g' y'
     That means x = g' x'  &  y -> w -> z -> a = g' y'
4. Result is a -> c
     (x' -> y' -> w' -> z' -> a') -> g x -> g y
     Now there is no direct y I can substitute in g y

我看作文的方法不适合我。

类型派生是一个纯粹的 mechanical 过程。

(.)  ::              (b          -> c             ) -> (a          -> b             ) -> 
                                                       (a                             -> c)
fmap₂ :: Functor f => (t3 -> t4) -> (f t3 -> f t4)
fmap₁ :: Functor g =>                                  ((t1 -> t2) -> (g t1 -> g t2))
-------------------------------------------------------------------------------------------
                                                   a ~ (t1 -> t2)  b ~ (g t1 -> g t2)
                 b ~ (t3 -> t4)  c ~ (f t3 -> f t4)                b ~ (t3   -> t4  )
-------------------------------------------------------------------------------------------
fmap₂ . fmap₁ :: (Functor f, Functor g) 
              =>  a          ->  c
           ~      (t1 -> t2) ->  (f t3     -> f t4    )
           ~      (t1 -> t2) ->  (f (g t1) -> f (g t2))

(.) 的堂兄 (>>>) fmap₁ fmap₂ = fmap₂ . fmap₁:

(>>>)  ::            (a           -> b                               ) -> 
                     (               b              -> c             ) -> 
                     (a                             -> c             )
fmap₁ :: Functor g =>  (t1 -> t2) -> (g t1 -> g t2)
fmap₂ :: Functor f =>                (t3   -> t4  ) -> (f t3 -> f t4) 
------------------------------------------------------------------------------
                  a ~ (t1 -> t2)   b ~ (g t1 -> g t2)
                                   b ~ (t3   -> t4  )  c ~ (f t3 -> f t4)
------------------------------------------------------------------------------
fmap₁ >>> fmap₂ :: (Functor f, Functor g) 
              =>    a             ->                   c
           ~          (t1 -> t2)  ->                   (f t3     -> f t4    )
           ~          (t1 -> t2)  ->                   (f (g t1) -> f (g t2))

针对您的具体问题:

f = undefined :: (x -> y -> w -> z -> a) -> g x -> g y

f₂ . f₁ = f₁ >>> f₂ :: 
 (x -> y -> w -> z -> a) -> ((->) (g x) (g       y               ))
                            ((->) x2    ((->) y2 (w2 -> z2 -> a2))) -> (g2 x2 -> g2 y2)
 ---------------------------------------------------------------------------------------
                       x2 ~ g x     g ~ ((->) y2)    y ~ (w2 -> z2 -> a2)
 ---------------------------------------------------------------------------------------
 (x -> y                -> w -> z -> a)                    ->  ( g2 x2        -> g2 y2)  ~
 (x -> y                -> w -> z -> a)                    ->  ( g2 (g     x) -> g2 y2)  ~
 (x -> (w2 -> z2 -> a2) -> w -> z -> a)                    ->  ( g2 (y2 -> x) -> g2 y2)

这是你从 GHCi 得到的,直到重命名变量。