如何手动推断'(.)的类型. (.). (.)'? [英] How to manually infer the type of '(.) . (.) . (.)'?

查看:79
本文介绍了如何手动推断'(.)的类型. (.). (.)'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Edward Kmett的演讲幻灯片,折叠和遍历中,力量就是力量在点中",他显示(.) . (.) . (.)的类型为

In Edward Kmett's talk Lenses, Folds, and Traversals, on the slide "The Power is in the Dot", he shows the type of (.) . (.) . (.) is

(a -> b) -> (c -> d -> e -> a) -> c -> d -> e -> b

我可以通过在GHCI中显示其类型来查看它.但是我也想知道为什么.我想了解的另一件事是,为什么在参数从(.)(.) . (.)(.) . (.) . (.)的规则更改中存在模式:

I can see it by showing its type in GHCI. But I'd also like to know why. The other thing I'd like to understand is why there's the pattern in the regular change of parameters from (.) to (.) . (.) and (.) . (.) . (.):

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

P.S.我试图通过扩展(.) . (.)的函数定义来自己解决(.) . (.)的问题.应用(.)的定义后,我得到:

P.S. I tried to resolve (.) . (.) myself by expanding the function definition of (.) . (.). After applying the definition of (.) I got:

\x y z t -> x ((y z) t)

所以我推断出类型:

x :: a -> b
y :: c -> d -> a
z :: c
t :: d

但是我在(.) . (.) . (.)上迷路了.而且我不知道这是否是进行手动类型推断的正确方法.

However I got lost on (.) . (.) . (.). And I don't know if this is the right way to do manual type inference.

推荐答案

具有功能,

instance Functor ((->) r) where
   -- fmap :: (a -> b) ->   f   a  ->   f   b
   --         (a -> b) -> (r -> a) -> (r -> b)
   fmap          p           q         x = p (q x)   -- fmap = (.)

所以您实际拥有的是fmap . fmap . fmap:

fmap               :: (a -> b) -> f       a   -> f       b
fmap . fmap        :: (a -> b) -> f (g    a)  -> f (g    b)
fmap . fmap . fmap :: (a -> b) -> f (g (h a)) -> f (g (h b))

这是

 (a -> b) -> (c -> (d -> (e -> a))) -> (c -> (d -> (e -> b)))   ~
 (a -> b) -> (c ->  d ->  e -> a)   -> (c ->  d ->  e -> b)     

为什么是fmap . fmap :: (a -> b) -> f (g a) -> f (g b)?因为,

(fmap . fmap) foo = fmap (fmap foo)
{-
fmap            :: (a  ->  b) -> f a     -> f b
foo             ::  a  ->  b
fmap foo        ::               f a     -> f b
fmap foo        :: g a -> g b
fmap (fmap foo) ::               f (g a) -> f (g b)
-}

机械类型推导与类型变量的替换和一致重命名有关.查看更多此处这里.

Mechanical type derivation is all about substitution and consistent renaming of type variables. See more e.g. here or here.

这篇关于如何手动推断'(.)的类型. (.). (.)'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆