简体   繁体   中英

Find type of square (f (g x)) in Haskell

Hello I have a problem with finding type for square (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...

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 .

Similarly, since you can apply f to gx , then f :: c -> d for some type d ; it's argument type must be the same as the return type of g .

Finally, you know that square :: Int -> Int and that you can apply square to the return value of f . That means f :: c -> Int by unifying the return type of f with the argument type of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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