Re:Haskell : polymorphism at value level, not method paramet...
by Miky on Sunday October 16, 2011 @03:47PM
(#3118105)
Thinking of values as functions is more misleading than it is illuminating. There are values that are functions, and there are values that are not, and the type system is extremely clear about which are which. It's just that all values can be polymorphic, including functions.
Conal Elliott has a good blog post about it. http://conal.net/blog/posts/everything-is-a-function-in-hask...
Re:Haskell : polymorphism at value level, not method paramet...
by tel on Sunday October 16, 2011 @11:27PM
(#3119052)
True. It's most accurate to think of functions as values which are of the type (a -> b) "mappings".
But really it's all confusion deriving from the fact that functions in Haskell mean something completely different from other languages.
Read is a great example of this working very well. It's, as you say, not really robust enough to work at the API or program level, but Show/Read provides a pretty quick, everywhere available serialization for many values. It's vital for debugging at the REPL.
Probably the most notorious example is the newbie-killer Regex library which uses polymorphic return types to dispatch what kind of result you want from your match, such as the match index or a list of group matches. It seems really clever since you just call the match function and affect whatever kind of pattern matching you want in order to pull values out, but it has always smelled to me since it's very hard to discover the functionality — the polymorphic return types aren't opaque, but are burdensome — and seems quite a bit too magical.
So I wouldn't personally go wild with return type polymorphism. It's obviously necessary in order to write things like filter, but unless the return type is simple and easily inferred by both compilers and people it can get hairy fast.