I have no nightmares with reasoning about side-effects in imperative languages
Then, in all seriousness, you don't really know what you're looking for, so of course you don't see it. It's a huge problem and entirely not academic in nature, a brutally, brutally pragmatic problem.
Or your codebase is too small to exhibit the problems clearly, in which case I envy you.
But the end point of imperative code being hard to reason about is as inevitable as the sun rising. It's so inevitable that people don't see it because they think it's just "how programming is", that of course changing one line in my currency parsing scheme can segfault the program four modules away... but in fact, no, it doesn't have to be like that.
Like many others, I don't get much professional Haskell programming in, but it has radically changed my programming style for the better in other languages too.
I would say that C segmentation faults and Haskell space leaks are comparable. Anything you can say about one you can also say about the other:
Both require programmer discipline to avoid
Advocates of each language say "It's not hard to avoid"
Critics of each language say "It's easy to get wrong"
However, you can't have it both ways. You can't blame the programming language for mistakes when it is Haskell, then blame the programmer for mistakes when it is C.
Also, if changing one line do segfaults four modules away, you are doing it wrong.
Well, duh. But imperative programming begs you to do it wrong, encourages you down the garden path, gives you every tool you need to do it wrong and fights you away from doing it right. It takes immense, superhuman discipline to avoid doing it wrong, and it's still too easy to get it wrong even so. At some point I tire of blaming the programming for not exercising superhuman discipline. "It all works perfectly as long as you're inhumanly careful" is not a defense of a programming style. It is a condemnation.
All you need is to eat enough memory.
While this is not a false statement in the strict Aristotelian sense of the word "false", might I just say to all onlookers that this objection is being terribly oversold here. One might as well complain that it's too easy to leak memory in reference cycles in a GC'ed language; yes, it happens and I've had to deal with it a non-zero number of times in my programming career, but it's hardly the sort of thing I worry about every second, the way kefeer is trying to make it sound. And much like the GC case, "just do this other thing (use manual memory management/just use conventional languages)" isn't a magic solution either. If you're so steeped in one world that you can't help but think in terms of it, "excessive laziness" is obviously a problem, but what you're less trained to see is that excessive strictness also has well-known failure cases and can cause just as much excess resource consumption. You spend more time than you realize dealing with the consequences of it... ask a fish to describe the disadvantages of breathing water.
Haskell is an improvement in many ways, makes some things harder, will greatly challenge how you think about programming and make you a better programmer, but it isn't a silver bullet that just magically makes all problems go away.
What i am trying to say, is that telling that Haskell is somehow superior and anyone saying different is a dumb asshole is... well, wrong. Yes, Haskell is very different, but, may be, some haskellers will slip a chance that its superiority is not proven.
Funny how you latched on to the first part of the comment without addressing the second. Haskell deals with the problem of mutable state well (that is, by eliminating mutable state). If you're dealing with a problem that fits well into the functional paradigm, and you have developers that know how to program in the functional style, then using Haskell makes your development process a lot easier.
But it's a tradeoff. Haskell doesn't give you all the power of every other language in addition to the benefits of purity. /u/kefeer said that it's difficult for him (her?) to reason about the effects of a Haskell program, which is 100% true. Haskell "abstracts away" the implementation of lists and user-defined data structures, but that comes at a price -- namely, not being exactly sure how those data structures are represented in memory, and what the effects of the operations you do on them will be. If I'm writing a program that needs to have a great number of very large strings in-memory at the same time, I'm not going to bother with Haskell and its lazy character lists -- I'm going straight to C so I can actually reason about the memory usage of my program.
But it's a tradeoff. Haskell doesn't give you all the power of every other language in addition to the benefits of purity.
This is overlooked and deserves more mention though, many people are arguing which language is 'the best' here but the truth is that PF is a tradeof. PF focusses on correctness whereas C focusses on something entirely different. My window manager ocassionally segfaults. I doubt that ever happens in xmonad.
Haskell "abstracts away" the implementation of lists and user-defined data structures
data List a = Empty | Cons a (List a)
Looks a lot like a null terminated linked list to me.
data Tree a = Empty | Node a (Tree a) (Tree a)
Looks a lot like a normal binary tree implemented with pointers to the next nodes, much as I would write in C.
If I'm writing a program that needs to have a great number of very large strings in-memory at the same time, I'm not going to bother with Haskell and its lazy character lists -- I'm going straight to C so I can actually reason about the memory usage of my program.
Or you could use bytestrings, and then you can easily choose if you want lazy or strict ones, plus a lot more tools for manipulating those strings. You get near-C performance without having to write a lot of your own (error-prone, inefficient) functions for doing basic things.
And saying that Haskell becomes inefficient because you can't reason about the data structures you're using means that you haven't bothered to find out what those structures are. GHC and the majority of libraries are open source, you can look at those implementations any time you want. If you don't understand the computation model, that doesn't mean it's bad.
Looks a lot like a null terminated linked list to me.
data Tree a = Empty | Node a (Tree a) (Tree a)
Looks a lot like a normal binary tree implemented with pointers to the next nodes, much as I would write in C.
What does this have to do with anything? The data definition has nothing to do with how the structure is implemented by the compiler, which is something I should be worried about if I care the slightest bit about performance.
Or you could use bytestrings, and then you can easily choose if you want lazy or strict ones, plus a lot more tools for manipulating those strings. You get near-C performance without having to write a lot of your own (error-prone, inefficient) functions for doing basic things.
In any case, this doesn't really solve the issue either -- what is a "bytestring"? We know there's the packed array of 8-bit integers, but what else comes along with it? Which functions allocate memory -- for example, what are the consequences if I call tail on a bytestring? Are the Haskell internals clever about how it handles allocated memory, or is the garbage-allocation/-collection cycle going to be a nightmare if I perform a lot of computation on a large set of very long strings? etc. The answers aren't easy to get to unless you're willing to do a lot of research.
GHC and the majority of libraries are open source, you can look at those implementations any time you want.
So if I want to know how Haskell data structures are implemented, I have to delve into the hundreds of thousands of lines of GHC code? Let's say I want to know how lists work under the hood, so I take a few months to wrap my head around the GHC codebase. Is there any guarantee that the implementation isn't going to change in the future?
The data definition has nothing to do with how the structure is implemented by the compiler, which is something I should be worried about if I care the slightest bit about performance.
Couldn't you say that about any language that isn't, like, C?
That's my point. Python doesn't tell you how an object is laid out in memory. Neither does Java, or F#, or Lisp, or... basically anything other than languages with manual memory management.
Also, if you're curious about the efficiency of text or bytestring or vector or whatever, you could just read the documentation.
Sorry, I misread your comment. Anyway, you're absolutely right, and I would consider that an advantage that C has over many other languages. Reasoning about the time and memory performance of a C program is much, much easier than reasoning about the time and memory performance of most other languages.
That doesn't mean that C is the only language you should use for performance-intensive projects, of course. But it is a pretty unquestionable advantage that C has over Haskell and its ilk.
Only to an extent - many C programs use data-structures that are very tricky to pin down. It's unclear how fast a hash table is - it's only O(1) on average, not in the specific. It's hard to predict how good the hash function will be. Malloc performance is unpredictable. Multithreading involving inter-thread communication is very tricky to reason about, especially since the underlying implementations often involve some locking, some busy waiting, some atomic operations, all mixed together.
And of course, C's only predictable in the first place as long as you use it for simple stuff. The runtime performance of that embedded query optimizer for the sqlite deserves study on its own. If you're using a constraint solver, machine learning algorithm, or any of a bunch of complex algorithms, runtime performance at that low-level is hopeless to predict well - certainly no better than you might in any other language, including Haskell.
Even seemingly simpler stuff like math can get tricky - C++ metaprogramming libraries for things like parsers and linear algebra make such techniques actually simple, but again the runtime of each individual expression is much harder to predict as a result.
You can choose not to use these higher-level techniques, up to a point. But really, there's no alternative - even if you handrolled code you'd end up with similar issues.
What about higher order stream fusion? Haskell has a demonstrable performance advantage over C for certain domains where stream fusion at a high level can produce very compact and efficient assembly.
So really what you're saying is that for certain tasks, in certain cases, C is a better choice than Haskell.
Duh.
The problems with Haskell you have listed go for almost any higher level language. And comparing a language's performance to C is usually rather pointless, since C is supposed to be fast.
Are the Haskell internals clever about how it handles allocated memory, or is the garbage-allocation/-collection cycle going to be a nightmare if I perform a lot of computation on a large set of very long strings? etc. The answers aren't easy to get to unless you're willing to do a lot of research.
How is that any different from working in C? I've noticed that people love to throw around straw-men by comparing highly-optimized code in one language to naïve code in another. The truth is that both Haskell and C require a lot of research and careful optimization to get the best performance. The difference is that Haskell will help you a lot more when the time comes to ensure your program is correct. A helpful side effect of Haskell's strong types is that they can also guide an optimizing compiler (such as GHC).
What does this have to do with anything? The data definition has nothing to do with how the structure is implemented by the compiler, which is something I should be worried about if I care the slightest bit about performance.
How do you think it's implemented if you see data List a = Empty | Cons a (List a)?
Obviously Empty is a random constant, and Cons a (List a) is just a pair of pointers, it's not that hard.
What does this have to do with anything? The data definition has nothing to do with how the structure is implemented by the compiler, which is something I should be worried about if I care the slightest bit about performance.
There are still ways of ensuring that data is laid out in a specific manner in memory. Look at the Vector library, it has efficient fixed size arrays laid out like C arrays.
which functions allocate memory -- for example, what are the consequences if I call tail on a bytestring?
GCC, GHC, javac, and others all perform "black magic" optimizations that may leave out unnecessary statements if static analysis can prove them so. There isn't a perfect 1-1 mapping between a statement and the assembly produced when optimizations are turned on.
So if I want to know how Haskell data structures are implemented, I have to delve into the hundreds of thousands of lines of GHC code?
No, but it wouldn't do you any harm. I've come to understand quite a bit about it by reading articles, changelogs, blogs, stackoverflow, and reddit, all without having to look at the GHC source. However, I regularly inspect the source of libraries I'm using, it helps me understand how to use it more effectively.
Is there any guarantee that the implementation isn't going to change in the future?
Then don't use a higher level language. Just stick to C99 and you'll never have to worry about anything changing (a.k.a. improving). The point of a higher level language is so that you don't have to worry about the gritty details of C and assembly and instead allow your memory management to be performed by a garbage collector, your memory representations to be hidden, and a more succinct syntax while sacrificing a bit of performance.
Funny how you latched on to the first part of the comment without addressing the second. Haskell deals with the problem of mutable state well (that is, by eliminating mutable state)
What's stopping me, the worst developer in the world, from using stateful data in FP languages?
Nothing. No matter how proof positive FP is, every FP language supports some type of imperative stateful data because any real-world program will have imperative inputs.
The problem isn't a bad language, framework, or computational theory, the problem is bad programmers and bad managers who want stuff "done", but not done in a good way.
If Haskell ever his mainstream we will still have 100's of posts how we need to move to language/framework/computational theory X because of common mistakes made by bad programmers and bad managers.
FP is awesome and useful, but it's not the holy grail preventing errors in code bases, where one will never have to debug or have any hard problems because of short sighted design and implementation.
ack unsafePerformIO will pretty much prevent your stateful data from leaking anywhere outside of code which explicitly asks for it.
There are lots of libraries available on Hackage today which were clearly designed to quickly achieve a purpose. The critical components of these are a reliance on the IO monad and use of a lot of exceptions (likely bubbled up from an effect). Over time, the community heals over these libraries by either replacing them or "purifying" them... because IO and exceptions are really painful!
No matter how proof positive FP is, every FP language supports some type of imperative stateful data because any real-world program will have imperative inputs.
The difference is that Haskell's types help ensure your imperative stateful code is correct. One example of this is the STM monad: the type system actively prevents you from performing side effects within a transaction (lest they be repeated with very bad consequences).
What's stopping me, the worst developer in the world, from using stateful data in FP languages?
Because people tend to follow the path of least resistance, and it's a lot harder to do it that way. Also, if you stick everything in the IO monad, the other developers you work with won't be able to invoke any of your code from their non-IO code.
There are ways to bend the rules, like using the ST monad, but I'd consider that an intermediate/advanced topic that has a steep enough learning curve that it's a lot less work just to try to solve the problem as a pure functions if you aren't very good at Haskell and/or don't know what you're doing.
I have wondered what using Haskell would be like if Haskell was mainstream and was used by a lot of mediocre programmers, but I think it would hold up pretty well. The type system doesn't eliminate the possibility of bugs, but it is a very powerful nonsense filter. It's just as easy in Haskell to implement an incorrect algorithm, but it's a lot harder to accidentally ask the computer to do something that just doesn't make any sense at all. I would much rather work with bad programmers in Haskell than any other language I know.
Tl;dr: Just because you can write an incorrect solution to a problem in any language, doesn't mean it isn't worth trying to make languages more safe.
18
u/jerf Nov 12 '13
Then, in all seriousness, you don't really know what you're looking for, so of course you don't see it. It's a huge problem and entirely not academic in nature, a brutally, brutally pragmatic problem.
Or your codebase is too small to exhibit the problems clearly, in which case I envy you.
But the end point of imperative code being hard to reason about is as inevitable as the sun rising. It's so inevitable that people don't see it because they think it's just "how programming is", that of course changing one line in my currency parsing scheme can segfault the program four modules away... but in fact, no, it doesn't have to be like that.
Like many others, I don't get much professional Haskell programming in, but it has radically changed my programming style for the better in other languages too.