[英]Find type of square (f (g x)) in Haskell
Hello I have a problem with finding type for square (f (gx)). 您好,我在查找正方形(f(gx))时遇到问题。 Here is what I have.
这就是我所拥有的。
square :: Int -> Int
{x : b} |- g x :: (b -> c) -> c
f :: (((b -> c) -> c) -> d) -> d
I am a bit confused if it's correct and what now. 如果正确的话,我有点困惑。
d
must be an Int
as well as whole expression (((b -> c) -> c) -> d)
but I feel like I did sth wrong here... d
必须是一个Int
以及整个表达式(((b -> c) -> c) -> d)
但我觉得我在这里做错了...
You are overgeneralizing in some way I don't quite follow. 您在某种程度上过于笼统,我不太了解。
If you are stating that x :: b
, then if you can apply g
to x
, g :: b -> c
for some type c
. 如果要声明
x :: b
,则可以将g
应用于x
,对于某些类型c
可以将g :: b -> c
应用于。
Similarly, since you can apply f
to gx
, then f :: c -> d
for some type d
; 同样,由于您可以将
f
应用于gx
,所以对于某些类型d
, f :: c -> d
d
; it's argument type must be the same as the return type of g
. 它的参数类型必须与
g
的返回类型相同。
Finally, you know that square :: Int -> Int
and that you can apply square
to the return value of f
. 最后,您知道
square :: Int -> Int
,并且可以将square
应用于f
的返回值。 That means f :: c -> Int
by unifying the return type of f
with the argument type of square
. 这意味着
f :: c -> Int
通过将f
的返回类型与square
类型的参数类型统一。
That's as far as you can go, though: 不过,这是您可以做到的:
square :: Int -> Int
f :: c -> Int
g :: b -> c
x :: b
You then know that gx :: c
and f (gx) :: Int
, meaning square (f (gx)) :: Int
as well. 然后,您知道
gx :: c
和f (gx) :: Int
,也意味着square (f (gx)) :: Int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.