What does eta reduce mean in the context of HLint
Asked Answered
A

3

16

I'm looking at the tutorial 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;

./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?

Arrack answered 26/4, 2011 at 17:6 Comment(2)
Eta reduction is a term from the lambda calculusFrancis
Typo, I think: haqify = ("Haq! " ++ )Protrusive
B
23

Eta reduction is turning \x -> f x into f as long as f doesn't have a free occurence of x.

To check that they're the same, apply them to some value 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. That, in turn can be eta-reduced to (++) "Haq! ", or equivalently, using section notation for operators, ("Haq! " ++).

Bernard answered 26/4, 2011 at 17:23 Comment(0)
P
16

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]. 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!" ++ to its argument.

Protrusive answered 26/4, 2011 at 17:26 Comment(0)
A
13

From lambda calculus, we define eta conversion as the equality:

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

See Barendregt, H. P. The Lambda Calculus: Its Syntax and Semantics, 1984.


In the Haskell context, see the definition on the Haskell wiki,

n eta conversion (also written η-conversion) is adding or dropping of abstraction over a function. For example, the following two values are equivalent under η-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. The term 'eta conversion' can refer to the process in either direction. Extensive use of η-reduction can lead to Pointfree programming. It is also typically used in certain compile-time optimisations.

Assets answered 26/4, 2011 at 17:26 Comment(2)
Is there a good reference for when eta-reduction is likely to lead to better optimisations, and when it doesn't?Fictive
GHC does it for you, so, no. It's just a matter of style.Assets

© 2022 - 2024 — McMap. All rights reserved.