Going through the source code for the prelude brings up weirdness

霸气de小男生 提交于 2019-12-05 14:36:06

问题


I was looking for the definition of seq and came across this weirdness. Why do all these functions have the same/similar definitions?

seq :: a -> b -> b
seq = let x = x in x

inline :: a -> a
inline = let x = x in x    

lazy :: a -> a
lazy = let x = x in x

There are many more with this definition in the source code. What's going on?


回答1:


What's going on is that these functions cannot be implemented in Haskell, but they should appear in the docs. Since haddock needs a syntactically correct (and well-typed) definition for each signature, the source must contain dummy definitions. Further, at the point where they are defined (in the ghc-prim package), error (and hence undefined) are not yet available, so the more obvious seq = error "Not implementable in Haskell" can't be used, thus the circular definition.




回答2:


These definitions are a ruse: they're provided primitively by the GHC runtime. It turns out that the infinite loop let x = x in x can be given any type, so it's as good a ruse definition as any.



来源:https://stackoverflow.com/questions/8652368/going-through-the-source-code-for-the-prelude-brings-up-weirdness

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