简体   繁体   English

在HLint的上下文中,eta减少了什么意思

[英]What does eta reduce mean in the context of HLint

I'm looking at the tutorial http://haskell.org/haskellwiki/How_to_write_a_Haskell_program 我正在看教程http://haskell.org/haskellwiki/How_to_write_a_Haskell_program

import System.Environment

main :: IO ()
main = getArgs >>= print . haqify . head

haqify s = "Haq! " ++ s

When running this program under HLint it gives the following error; 在HLint下运行此程序时,它会出现以下错误;

./Haq.hs:11:1: Warning: Eta reduce
Found:
  haqify s = "Haq! " ++ s
Why not:
  haqify = ("Haq! " ++ )

Can someone shed some light on what exactly "Eta Reduce" means in this context? 有人能否说明“Eta Reduce”在这种背景下究竟意味着什么?

Eta reduction is turning \\x -> fx into f as long as f doesn't have a free occurence of x . 埃塔减少转弯\\x -> fxf只要f不具有的自由occurence x

To check that they're the same, apply them to some value y : 要检查它们是否相同,请将它们应用于某个值y

(\x -> f x) y === f' y -- (where f' is obtained from f by substituting all x's by y)
              === f y  -- since f has no free occurrences of x

Your definition of haqify is seen as \\s -> "Haq! " ++ s , which is syntactic sugar for \\s -> (++) "Haq! " s . 你对haqify定义被看作是\\s -> "Haq! " ++ s ,它是\\s -> (++) "Haq! " s语法糖。 That, in turn can be eta-reduced to (++) "Haq! " , or equivalently, using section notation for operators, ("Haq! " ++) . 反过来,可以将其缩减为(++) "Haq! " ,或等效地使用节符号表示运算符("Haq! " ++)

Well, eta reduction is (one way) to make point-free functions, and usually means that you can remove the last parameter of a function if it appears at the end on both sides of an expression. η减少是(单向)制作无点函数,通常意味着如果函数的最后一个参数出现在表达式两边的末尾,则可以删除它。

f :: Int -> Int
g :: Int -> Int -> Int
f s = g 3 s 

can be converted to 可以转换为

f = g 3

However, in this case it is slightly more complicated, since there is the syntactic sugar of two-parameter operator (++) on the rhs, which is type [a] -> [a] -> [a] . 然而,在这种情况下,它稍微复杂一些,因为在rhs上存在双参数运算符(++)的语法糖,类型为[a] -> [a] -> [a] However, you can convert this to a more standard function: 但是,您可以将其转换为更标准的功能:

 haqify ::  [Char] -> [Char]
 haqify = (++) "Haq! "

Because (++) is an operator, there are other possibilities: 因为(++)是一个运算符,还有其他可能性:

haqify = ("Haq! " ++ )

That is, the parens convert this into a one -parameter function which applies "Haq!" ++ 也就是说,parens将其转换为一个参数"Haq!" ++参数函数"Haq!" ++ "Haq!" ++ to its argument. "Haq!" ++来论证。

From lambda calculus, we define eta conversion as the equality: 从lambda演算中,我们将eta转换定义为相等:

 \x -> M x == M      -- if x is not free in M.

See Barendregt, HP The Lambda Calculus: Its Syntax and Semantics , 1984. 参见Barendregt,HP The Lambda Calculus:Syntax and Semantics ,1984。


In the Haskell context, see the definition on the Haskell wiki , 在Haskell上下文中,请参阅Haskell wiki上的定义

n eta conversion (also written η-conversion) is adding or dropping of abstraction over a function. nta转换(也写成η-转换)是在函数上添加或删除抽象。 For example, the following two values are equivalent under η-conversion: 例如,以下两个值在η-conversion下是等效的:

\x -> abs x

and

abs

Converting from the first to the second would constitute an eta reduction, and moving from the second to the first would be an eta abstraction. 从第一个到第二个的转换将构成一个eta减少,从第二个到第一个的转换将是一个eta抽象。 The term 'eta conversion' can refer to the process in either direction. 术语“eta转换”可以指向任一方向的过程。 Extensive use of η-reduction can lead to Pointfree programming. 广泛使用η减少可以导致无点编程。 It is also typically used in certain compile-time optimisations. 它通常也用于某些编译时优化。

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

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