Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial application and subexpressions depending on just a subset of a function's arguments

Tags:

scope

haskell

Should I be able to expect a Haskell compiler be smart enough to optimize the following definition:

h x y = p (m x) (n y)

into something like this:

h x = let z = m x in \y -> p z (n y)

? This could be convenient if m were expensive to evaluate, and I used h's definition in the following way:

main = print $ map (h 2) hugeList
like image 794
pyon Avatar asked Nov 06 '12 13:11

pyon


1 Answers

But what if m is cheap to evaluate, but its result expensive to store? Say m x = [x .. ] and p needs to traverse different prefixes of that list, depending on n y. If then m 2 is shared in map (h 2) hugeList, and any of the list elements requires a long prefix, you have huge memory requirements even if all subsequent elements only require the first element of the list to return the result.

So an automatic sharing of m x can also be a pessimisation, hence you should not expect all compilers to automatically share it.

Generally, it is difficult for the compiler to decide whether sharing is beneficial or even detrimental, so you should make sharing explicit where you really want it. (Nevertheless, expect compilers to introduce sharing even where you do not want it sometimes.)

like image 89
Daniel Fischer Avatar answered Nov 15 '22 06:11

Daniel Fischer