简体   繁体   English

eta缩减何时可以更改函数的类型?

[英]When can eta reduction change a function's type?

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. 我实际上遇到了“ map fromIntegral”问题,而不是显示问题-我的代码无法使用无点格式编译,但是可以在不减少eta的情况下正常工作。

Is there a simple explanation of when eta reduction can change the meaning of Haskell code? 关于eta减少何时可以更改Haskell代码的含义是否有简单的解释?

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 ; 您可以在ghci使用:set -XNoMonomorphismRestriction禁用它; this is often useful in ghci , where you often intend such expressions to be fully polymorphic. 这在ghci通常很有用,在ghci ,您经常希望此类表达式是完全多态的。 (In a Haskell source file, make the first line (在Haskell源文件中,第一行

 {-# LANGUAGE NoMonomorphismRestriction #-}

instead.) 代替。)

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

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