When can eta reduction change a function's type?

喜欢而已 提交于 2020-01-21 07:03:51

问题


What exactly is going on with the following?

> let test = map show

> :t test
test :: [()] -> [String]

> :t (map show)
(map show) :: Show a => [a] -> [String]

I am wondering how I failed to notice this before? I actually encountered the problem with "map fromIntegral" rather than show - my code doesn't compile with the pointfree form, but works fine without eta reduction.

Is there a simple explanation of when eta reduction can change the meaning of Haskell code?


回答1:


This is the monomorphism restriction, which applies when a binding doesn't take parameters and allows the binding to be shareable when it otherwise wouldn't be due to polymorphism, on the theory that if you don't give it a parameter you want to treat it as something "constant"-ish (hence shared). You can disable it in ghci with :set -XNoMonomorphismRestriction; this is often useful in ghci, where you often intend such expressions to be fully polymorphic. (In a Haskell source file, make the first line

 {-# LANGUAGE NoMonomorphismRestriction #-}

instead.)



来源:https://stackoverflow.com/questions/10310374/when-can-eta-reduction-change-a-functions-type

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