[Haskell-cafe] pointfree-trouble

Daniel Fischer daniel.is.fischer at web.de
Thu Dec 24 18:59:57 EST 2009

Am Freitag 25 Dezember 2009 00:19:58 schrieb Mark Lentczner:
> For the record, all of these compile with -O to exactly the same code!
> > reMatr1 f m = Matr (f (unMatr m))
> > reMatr2 f m = Matr $ f $ unMatr m
> > reMatr3 f m = Matr . f . unMatr $ m
> > reMatr4 f   = Matr . f . unMatr
> > reMatr5 f   = Matr . (flip (.) unMatr) f
> > reMatr6     = (Matr .) . (. unMatr)
> And that code is the same as what reMatr1 compiles to with no optimization!
> Under no optimization, they all compile to direct implementations of their
> approach - and hence, reMatr1 is the most efficient. The others must wend
> through quite a number of library functions to do their work.

Yes, GHC doesn't inline across module boundaries without optimisations.

> Since there is no real efficiency issue (surely matrix manipulation code
> will be compiled -O for actual use), the only reason I can see to choose
> one of these over the other is how it conveys the intention from one
> programmer to the next.  (Or one's self six months later...) In which case
> seems to me that reMatr4 most cleanly encapsulates the programmer's
> intention.

I think so, too. Version 1 is fine, too. Version 2 could be okay if the function had a 
longer name than f, as it is, the f is squashed by the two adjacent $s. Version 3 is not a 
good point to stop on the way from 1 to 4 IMO.

> Of course, I've had some Haskell experience and I get the idiom
> - to someone less versed in Haskell, reMatr1 is probably the clearest - to
> someone with much Haskell experience, perhaps reMatr6, though it just looks
> "clever" to me, rather than clear.

It's easy enough to understand with sufficient experience, and among the completely 
pointfree versions, I think it's the clearest (also clearer than version 5, although 
that's not yet completely point free). But although it's fairly benign, it already shows 
that completely pointfree style tends to obfuscate except in the simplest cases.

> I'm curious why the original poster was motivated to pursue pointfree style
> to the point of no explicit arguments?

I can't speak for the OP, but pointfreeing is always an interesting mental exercise, it's 
often fun to push it deep into obfuscation just for the heck of it.
But, more importantly, you learn to understand the combinators better, and you develop a 
sense of how far to go with pointfreeing.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091224/eb53de00/attachment.html

More information about the Haskell-Cafe mailing list