[英]Type of f g x = g . g x
It is not clear to me why the function defined as 我不清楚为什么函数被定义为
f g x = g . g x
has the type 有类型
f :: (b -> a -> b) -> b -> a -> a -> b
I would have thought it would be of type 我原以为它会是类型的
f :: (t -> t) -> t -> t
Can anyone explain to me how the expression is broken down? 任何人都可以向我解释表达方式是如何分解的? Thanks!
谢谢!
Note that function application has the highest priority; 请注意,功能应用程序具有最高优先级; operators come later.
经营者以后来。
So, the term g . gx
所以,术语
g . gx
g . gx
first applies g
to x
, and then composes the result and g
itself. g . gx
首先将g
应用于x
,然后组合结果和g
本身。 If x
has type b
, g
must have type b -> c
. 如果
x
具有类型b
,则g
必须具有类型b -> c
。 Since we compose g
with gx
(the latter of type c
), c
must be a function type returning b
, so c = a -> b
. 由于我们用
gx
(后者是c
类型)组成g
, c
必须是返回b
的函数类型,所以c = a -> b
。 Now, the type of g
is b -> a -> b
and the type of g . gx
现在,
g
的类型是b -> a -> b
和g . gx
的类型g . gx
g . gx
is a -> (a -> b)
; g . gx
是a -> (a -> b)
; the type of f
happens to be (b -> a -> b) -> b -> a -> a -> b
. f
的类型恰好是(b -> a -> b) -> b -> a -> a -> b
。
If you wanted something like (a -> a) -> a -> a
instead, you could try one of this 如果你想要
(a -> a) -> a -> a
,你可以试试其中之一
f g x = g (g x)
f g x = (g . g) x
f g x = g . g $ x
f g = g . g
The idea is about understanding the (.)
operator, it has a type of 这个想法是关于理解
(.)
运算符,它有一种类型
(.) :: (b -> c) -> (a -> b) -> a -> c
It takes two functions each with one parameter and compose them, after applying gx
the compiler assumed g
is actually g :: a -> b -> c
in order to satisfy the signature of (.) :: (b -> c) -> (a -> b) -> a -> c
which takes two functions with one argument. 它需要两个函数,每个函数都有一个参数并组成它们,在应用
gx
,编译器假定g
实际上是g :: a -> b -> c
,以满足(.) :: (b -> c) -> (a -> b) -> a -> c
的签名(.) :: (b -> c) -> (a -> b) -> a -> c
,它带有一个参数的两个函数。 Otherwise the code won't compile. 否则代码将无法编译。
And finally if you want the signature f :: (t -> t) -> t -> t
you need something like this: 最后如果你想要签名
f :: (t -> t) -> t -> t
你需要这样的东西:
λ> let applyTwice g = g.g
λ> :t applyTwice
applyTwice :: (a -> a) -> a -> a
λ> applyTwice (*2) 3
12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.