News for hackers. Stuff that matters.
Re:Haskell for Web Developers by runT1ME on Friday July 26, 2013 @02:01PM (#6109528)
>The Python code has the added advantage of actually being correct, assuming that the intended behaviour really is to add up the total of all entered numbers over time as the output suggests.

Only in a single threaded environment. However, concurrently, the Haskell code stays correct in a concurrent environment while your python code has bugs.

Extrapolate a bit, and think about how many python functions may be slightly less trivial than yours and yet still have the same concurrency issues...

Re:Haskell for Web Developers by Chris_Newton on Friday July 26, 2013 @02:28PM (#6109695)
Contrary to the hype, I think plenty of useful code does still run in a single-threaded environment, so I find your argument unconvincing.

That said, even in an environment with concurrency, I’m not advocating uncontrolled state changes as typified by current imperative programming languages. I’m arguing that we could benefit from the robust theory underlying languages like Haskell, but used more transparently so the developer only has to worry about it when it matters.

For example, you could be running a second thread in parallel with the one we’re looking at here, but if its effects are constrained to replying to a network-supplied request for the current time and reading the system’s clock, it doesn’t matter at all that our interactive adder is running at the same time. These effects affect different, independent resources and will never interact, so there is no benefit to cluttering either code path with extra details.

What I’d really like to see is a language that could understand those resources and how they are affected by each thread in terms of reads and updates. I want it to stop me from modifying the code later so that it accidentally becomes vulnerable to interactions that I’m not fully controlling. I want it to provide me with tools to specify how potential interactions should be handled, in a way that is systematic and robust but otherwise as simple as possible, and that can be applied at whatever level of my design makes the most sense.

Research into type and effect systems is addressing challenges like these, but I’m hoping that when the dust settles, the final version will look a lot more like Python with a few strategically placed annotations than Haskell where these implementation details are in your face all the time (even if most of the time you don’t actually need to worry about them).

Re:Haskell for Web Developers by runT1ME on Friday July 26, 2013 @02:51PM (#6109822)
>I want it to stop me from modifying the code later so that it accidentally becomes vulnerable to interactions that I’m not fully controlling. I want it to provide me with tools to specify how potential interactions should be handled, in a way that is systematic and robust but otherwise as simple as possible, and that can be applied at whatever level of my design makes the most sense.

That's...the state monad. The compiler tells you when you try to access it in a way that is not allowed.

You have to be explicit with sharing.

Re:Haskell for Web Developers by Chris_Newton on Friday July 26, 2013 @03:16PM (#6109943)
That's...the state monad. The compiler tells you when you try to access it in a way that is not allowed.

But the price for that is that I always have to access it in a cumbersome way, even when being so explicit makes no useful difference to the correctness of my code.

You have to be explicit with sharing.

I don’t want to be explicit with sharing.

I want to be explicit with sharing when it matters, and otherwise not to worry about it, safe in the knowledge that the language will warn me if I try to do something later that invalidates any assumptions I’m making now.

Re:Haskell for Web Developers by pekk on Friday July 26, 2013 @04:50PM (#6110494)
If your argument is that it's better to be explicit with sharing, then why would you want to use threads that implicitly share 'total'?
Re:Haskell for Web Developers by millstone on Friday July 26, 2013 @07:44PM (#6111176)
I'm intrigued by the claim that the Haskell code is correct in a concurrent environment. If I run two copies of this function concurrently, won't they step on each others' toes as they fight over the file offset of stdin?
Re:Haskell for Web Developers by dllthomas on Friday July 26, 2013 @02:17PM (#6109623)
You mean the Haskell code stays equally incorrect in a concurrent environment...
Re:Haskell for Web Developers by asdasf on Friday July 26, 2013 @02:31PM (#6109712)
What is incorrect about it?
Re:Haskell for Web Developers by tel on Friday July 26, 2013 @02:38PM (#6109758)
The thread op assumed that the code was supposed to accumulate the amounts ever entered. The Haskell code doesn't accumulate, it adds you input to the prior state, prints that sum, then stores your input, not the sum as the next state.

Of course, that's just a (likely true) assumption of what the behavior was supposed to be and it extrapolates far from the point. The example didn't really intend to show off a particular behavior but instead the interweaving of effects.

Re:Haskell for Web Developers by Chris_Newton on Friday July 26, 2013 @02:53PM (#6109832)
The example didn't really intend to show off a particular behavior but instead the interweaving of effects.

Indeed. My point was not to nitpick an interesting article. I was trying to illustrate how making so many details explicit in Haskell has a real, practical downside: what seems to be an obvious logic error had slipped through, where it surely would have been spotted easily in the simpler Python code. Moreover, no-one had even mentioned this possible bug after a couple of hours and dozens of posts here, though of course that could just be because no-one else felt this point was important enough to make rather than because they hadn’t noticed.

Re:Haskell for Web Developers by dllthomas on Friday July 26, 2013 @03:00PM (#6109860)
FWIW, I tend to prefer using modify rather than set wherever that makes sense, which would make this particular bug harder to express.
Re:Haskell for Web Developers by tel on Friday July 26, 2013 @03:35PM (#6110050)
Yeah. I didn't follow your point since I didn't feel like this was a terrifically idiomatic example. I felt like the bug was equally easy to catch in either expression.
Re:Haskell for Web Developers by nightski on Friday July 26, 2013 @04:35PM (#6110418)
No offense but you failed miserably. If you were trying to reproduce the python code in Haskell, the example is not what you would write. This is a 1 liner in Haskell. The two examples do not represent the same thing.
Re:Haskell for Web Developers by Chris_Newton on Friday July 26, 2013 @05:33PM (#6110727)
The two examples do not represent the same thing.

This is a claim I don’t understand. They are both simple examples of representing an algorithm that maintains state in their respective languages. Of course you could write it as a one-liner using a different style in Haskell, and in various imperative languages for that matter. However, my point was that making these monadic abstractions explicit everywhere in Haskell does add weight to the code. That continues to be true for more complicated stateful code that can’t be rewritten as a one-liner or expressed more elegantly using recursion anyway.

Re:Haskell for Web Developers by nightski on Friday July 26, 2013 @09:04PM (#6111413)
I simply do not agree that the abstractions add weight to the code. If anything I think that they make it more concise. It scales much better than constructs given to you in Python. But don't take my word for it, try writing a complicated Haskell program with lots of effects. I think you will find the capability Haskell provides to compose and abstract a huge boon.
Re:Haskell for Web Developers by Chris_Newton on Friday July 26, 2013 @09:26PM (#6111472)
But don't take my word for it, try writing a complicated Haskell program with lots of effects.

Unfortunately it’s late and I don’t have time to spend the next few weeks writing Haskell code to try your experiment. I’m guessing no-one else reading this does either. Perhaps you could instead direct us all to some existing projects that demonstrate the advantages you’re describing, so we could see the kind of code you’re thinking of and make up our own minds?

Re:Haskell for Web Developers by nightski on Saturday July 27, 2013 @12:57AM (#6111924)
The thing is I could throw a bunch of Haskell at you but in the end it wouldn't really mean anything. When I embarked on learning the language there were many times I almost threw in the towel. It was hard to see beyond the initial frustrations. Haskell code looked confusing, verbose, ugly, and so on. It was only by taking the time of going through this process that I came to an understanding - followed by appreciation.

But I don't claim to be incredibly intelligent. It is very possible you or someone else here could look at a heap of Haskell code and just immediately understand the advantages/disadvantages without having gone through learning all of the "why".

Your time is yours. Don't let me try to tell you what to do with it. Heck you can make a fortune just writing PHP so is there any reason to learn anything else? I'd say anyone who loves their craft would want to at least give all new things a shot and invest some time into learning what is out there.

The project I took on initially to learn Haskell was translating a real time sparse 3D reconstruction computer vision algorithm. It was incredibly daunting and challenging. But in the end very worth it!

Re:Haskell for Web Developers by Chris_Newton on Saturday July 27, 2013 @09:30AM (#6112807)
The thing is I could throw a bunch of Haskell at you but in the end it wouldn't really mean anything.

It is odd that you seem to be assuming I’ve never written anything in Haskell and that the concerns I’ve expressed are based on a sceptical reading of one article. I’m quite sure I’ve never written anything to that effect here.

I make no claim to special expertise or authority. Indeed, I’m almost entirely self-taught, so it’s certainly possible that I have completely missed some key point. However, I’ve been following Haskell’s development for years and written enough small/medium projects in it to have formed my own opinions. My experience has been that for mostly pure code where it’s a natural fit, Haskell has a lot to like, but for more interactive systems, it often does become rather awkward for exactly the reasons we’ve been discussing. (Edit: There’s a good example of this in the article: the chat room using WebSockets.)

In the real world, people do sometimes just dump half of their program into IO to be done with it. In the real world, you do wind up repeating essentially the same logic multiple times to cope with monadic and non-monadic cases (or the authors of libraries you use did, but sometimes you are that author) and at times you do need things like explicit lifts. In the real world, sometimes you do want to be in the same monad twice. In the real world, there are awkward corner cases in the language, and you do wind up relying on GHC extensions to help clean them up. In the real world, people do write elegant code like the popular not-quicksort example despite its dubious performance characteristics.

None of this takes away from the power and usefulness of Haskell’s type system, or neat ideas like STM if concurrency is relevant to your application. But this isn’t some kind of either/or world, where having some good points automatically means a language can’t have any bad ones.

Re:Haskell for Web Developers by asdasf on Friday July 26, 2013 @08:42PM (#6111367)
It is made explicit and verbose in that contrived example because it specifically an example of those things. I am having a really hard time believing that you genuinely don't understand this obvious fact.
Re:Haskell for Web Developers by Chris_Newton on Friday July 26, 2013 @09:22PM (#6111464)
Of course I understand that this is a contrived example.

What I don’t accept — without evidence to the contrary — is that it is unrepresentative. Would more “serious” examples of implementing stateful algorithms using monads and similar tools not be similarly verbose, given the explicit nature of such details in Haskell? Maybe that really is true for experts, in which case I would be happy to learn about it and update my views. Examples are welcome.

For now, I have never personally seen it, and it’s not immediately obvious to me how Haskell code would beat a dedicated imperative language at its own game in terms of conciseness. Correctness guarantees, sure. Performance, maybe, if we reach the point where powerful optimisations are possible in Haskell because of the extra information available. And for conciseness, I’m not suggesting it’s orders of magnitude worse or anything silly like that. But when, by design, your effects and sequencing have to be singled out and specified explicitly every time, you’re starting at a disadvantage compared to systems that incorporate that behaviour implicitly.

Please also see my comments on quicksort elsewhere in this thread.

Re:Haskell for Web Developers by dllthomas on Friday July 26, 2013 @02:41PM (#6109772)
"Total: " implies that it's meant to be summing the input values. It's actually only summing the current and the previous, because it puts back val not (val + state).
Re:Haskell for Web Developers by pekk on Friday July 26, 2013 @04:48PM (#6110488)
Why are you having multiple threads share 'total' at all?