何时可以减少改变功能的类型? [英] When can eta reduction change a function's type?

查看:78
本文介绍了何时可以减少改变功能的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到底发生了什么?

 > let test = map show 

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

> :t(地图显示)
(地图显示)::显示a => [a] - > [String]

我想知道我以前没有注意到这一点?我实际上遇到了map fromIntegral的问题,而不是显示 - 我的代码没有使用pointfree形式编译,但工作正常,没有减少eta。



有没有简单的解释何时减少可以改变Haskell代码的含义? monomorphism restriction ,它适用于绑定不带参数并且允许绑定在其它情况下不会由于多态性而可共享的情况,理论上如果您不给它一个参数你想把它当作常量-ish(因此是共享的)。你可以在 ghci 中用:set -XNoMonomorphismRestriction 来禁用它。这在 ghci 中通常很有用,在这种情况下,你经常希望这些表达式是完全多态的。 (在Haskell源文件中,创建第一行

  { - #LANGUAGE NoMonomorphismRestriction# - } 

)。


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?

解决方案

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.)

这篇关于何时可以减少改变功能的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆