9 ms·
the post focusses on the practicality of haskell and addresses purity but leaves the biggest problem out: laziness, which is more or less the reason haskell exi
by Barrin92 7y ago
the post focusses on the practicality of haskell and addresses purity but leaves the biggest problem out: laziness, which is more or less the reason haskell exists, is the wrong default. In particular lazy I/O which can introduce horrible bugs or straight up mess with execution order of critical code, which in my opinion is an absolute no-go in an industrial language.
- theemathas 7y agoLazyness in Haskell can never change I/O (maybe except performance) unless you use unsafePerformIO, right? Also, there's a link to a page about laziness on the left.
- kreetx 7y agoI guess it's explained elsewhere here aswell, but the "lazy IO" discussed is pretty much a bug: you read a file to String, but this String is loaded from the lazily while you consume it. But the problem is: you already get the String thinking all is well, but if something happens to the file handle you get a runtime error. This shouldn't happen with pure values.
- nothrabannosir 7y ago> lazy I/O which can introduce horrible bugs or straight up mess with execution order of critical code I've never seriously programmed Haskell, so honest question: I understand the first point, but how is the second possible? Isn't the point of passing RealWorld around that you enforce the order of execution through data dependencies rather than expression order? It always seemed like a very elegant (and incredibly impractical :) ) solution to me.
- almostdeadguy 7y agoMonadic IO allows you to reliably sequence IO operations, but the results of those operations are not produced until their results are forced. Here's an example of the problems with this: http://dev.stephendiehl.com/hask/#lazy-io http://dev.stephendiehl.com/hask/#lazy-io . It's not really a language flaw, but more of a design flaw of the functions in base. Almost nobody uses lazy IO anymore though. Most IO is done w/ strict IO functions from the bytestring library and often with stream processing libraries like conduit or pipes.
- dllthomas 7y ago> Almost nobody uses lazy IO ... in anything large. If I'm writing something small of the form "read stuff from stdin", lazily consuming the results of getContents is a perfectly fine way of shipping data through the program. As soon as it starts getting complicated, though, it becomes important to refactor (which is often super easy in Haskell). I actually treat unsafePerformIO pretty similar, though with more of a requirement that the project be expected to stay tiny. Worth noting that it's much more important you understand Haskell's evaluation model in that case. ... assuming what you're doing matters. If you're just having fun, it can be a good way to learn a little more about Haskell's evaluation model.
- tsuraan 7y agoOP is somewhat conflating two different things: non-strict function evaluation and lazy IO. With lazy IO, you can get, for example, a String from a file. That string is actually a lazily-constructed chain of Cons cells, so if you're into following linked lists and processing files one char at a time, then it's fun to use. The dangerous bit comes in when you close the file after evaluating its contents as a string: fd <- open "/some/path" s <- readContentsLazy fd close fd pure $ processString s Now, processString is getting a string with the file's contents, right? Nope, you have a cons cell that probably contains the first character of the file, and maybe even a few more up to the first page that got read from disk, but eventually as you're processing that string, you'll hit a point where your pure string processing actually tries to do IO on the file that isn't open anymore, and your perfect sane and pure string processing code will throw an exception. So, that's gross. That's a real issue that will hit beginners. There's been a lot of work done to make ergonomic and performant libraries that handle this without issues; I think that right now pipes[0] and conduit[1] are the big ones, but it's a space that people like to play with. [0] - https://hackage.haskell.org/package/pipes https://hackage.haskell.org/package/pipes [1] - https://github.com/snoyberg/conduit https://github.com/snoyberg/conduit
- whatshisface 7y agoWhy is it possible to "close" a file? You could have a function that mapped open files to closed files, but the open files would still be there... I think the reason why this weird behavior is cropping up is that the entire language is designed around functions, and here you are reaching into the internal datastructures, mutating state.
- dllthomas 7y ago> Why is it possible to "close" a file? Because the program we're compiling needs to work on actual computers, running under actual (usually at least vaguely POSIX) operating systems. In that context, it's unavoidable that the set of open file descriptors sometimes matters. It can matter because of resource limits. It can also change whether another process gets an SIGPIPE versus blocking forever. It can affect locking.
- PhineasRex 7y agoYou're absolutely correct however Haskell has a couple of function that let you circumvent it's normal evaluation rules which you can see in https://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO-Unsafe.html#v:unsafePerformIO https://hackage.haskell.org/package/base-4.12.0.0/docs/Syste.... These are sometimes useful for optimization but in an unfortunate bit of history, some developers decided to use these to "simulate" Haskell's lazy semantics for side-effecting I/O operations, the so-called "lazy I/O". Fortunately this is just a few functions in the standard library which you can simply avoid and serious Haskell codebases should use a linter to prevent people from calling them.
- uryga 7y agorelevant quote from the linked docs: > `unsafeInterleaveIO` allows an IO computation to be deferred lazily. When passed a value of type IO a, the IO will only be performed when the value of the a is demanded. This is used to implement lazy file reading, see `hGetContents`.
- tikhonj 7y agoLazy IO refers to a set of functions like readFile that explicitly break that abstraction, tying observable IO to the order expressions are evaluated. Under the hood, the lazy IO functions rely on a GHC function called unsafeInterleaveIO, whose name hints at how it works and that it's not safe. The fact that lazy IO behaves so unintuitively and causes significant problems strongly signals that that particular abstraction shouldn't be broken.
- ocharles 7y agoLazy I/O is widely regarded as a mistake, I don't really know any Haskell programmers that advocate that anymore. We have plenty of other approaches that have a better foundation.
- vamega 7y agoAre you referring to the various streaming libraries (Conduit, Pipes and the like)?
- whateveracct 7y agoThat is what people use instead of lazy IO. And those libraries aren't just "hacks" to deal with laziness or purity. They're wonderful APIs for writing constant-memory streaming programs. I always wish other languages had libraries on their level.
- lallysingh 7y agoMost popular languages have aspects that you can consider failed experiments. Like java's 1.0 libs or C's declaration syntax.
- whateveracct 7y agoLazy I/O is not especially popular. So I wouldn't really say it's a problem with Haskell since most Haskellers would use other solutions. And Haskell is cutting-edge in more ways than laziness so I don't think this is a big deal. It's _technically_ "why it exists" but in practice there's a variety of other reasons people pick Haskell nowadays besides laziness.
- jeremyjh 7y agoThis is something non-Haskellers worry about quite a lot, and Haskellers not much if at all. Others have mentioned that lazy I/O is not really a thing in real world Haskell. But its important to know that lazy by default has a lot of benefit in the composition of pure functions, e.g. `take 10 . sort`
- dean177 7y agoSorting has to operate on the entire input, how is laziness an advantage here?
- ssijak 7y agobecause maybe later in the program you will deicide that that computation does not need to be executed at all.
- dean177 7y agoCan you give an example of what you mean by this? That’s the kind of thing you would use “if” for right?
- garmaine 7y agoOne was given earlier in the thread: `take 10 . sort` This should be read from right to left: it sorts an (implicit) list, then reads the first 10 elements from the beginning of the now-sorted list, and throws away the rest. Except under the hood in Haskell, sort is not a function that takes a list and returns a list. It is a function that takes a list, and returns the smallest element of that list and a function to fetch the rest of the sorted list. (A function which actually returns the second-smallest element and a function that if called returns the third smallest element and another function, etc.) These ephemeral functions / continuations / "thunks" are allowed to have internal state and thereby perform more complex sorting operations such as quicksort, and might actually be represented by an internal tree of branch points and their returned value or unexecuted thunks. But all of this complexity is hidden by the compiler. It's not some special property of `sort` either, which might look like this: sort [] = [] sort (x:xs) = sort small ++ (x : sort large) where small = [y | y <- xs, y <= x] large = [y | y <- xs, y > x] "The sort of an empty list is the empty list. The sort of a non-empty list is the elements of the tail less than or equal to the head of the list, sorted, followed by the head of the list, followed by the elements of the tail greater than the head of the lest, sorted." The Haskell compiler does the magic of turning this into a lazy-optimized implementation which in the case of `take 10 . sort` doesn't bother to calculate any remaining `sort large` partitions once the first 10 smallest elements have been found.