简体   繁体   English

在Haskell中查找正方形(f(gx))的类型

[英]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 ,所以对于某些类型df :: 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 :: cf (gx) :: Int ,也意味着square (f (gx)) :: Int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM