Why does `peek` with a polymorphic Ptr return GHC.Prim.Any when used with a bind?

假装没事ソ 提交于 2019-12-06 02:34:55

To expand somewhat on @user2407038's comment:

When you do x <- peek (ptr :: Ptr (Ptr a)) in the GHCi prompt, the type variable a must be instantiated to some concrete type. This is because the do notation x <- peek p means peek p >>= \x -> ..., where ... is what you type into GHCi afterwards. Since GHCi can't know the future, it has to "cheat" during typechecking.

Recall that in peek p >>= \x -> ..., the right-hand argument to >>=, namely the lambda abstraction \x -> ..., is monomorphic in its argument. This is why GHCi has to assign a monomorphic type to x.

GHC.Prim.Any is the placeholder type that GHC uses in situations like this where a concrete, monomorphic type needs to be assigned to something that has no other constraints.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!