News for hackers. Stuff that matters.
Re:John Carmack on Functional Programming (2013) [video] by jandrese on Thursday August 17, 2017 @11:13AM (#15037466)
Interesting that he ran into friction exactly where you would expect him to.

He was talking about having the AI scripts running in different purely functional threads. Later (around 21 minutes in) he mentions what happens when two AIs decide to move to the same place at the same time, and has not figured out the solution.

Of course parallel programming is easy if you ignore the data dependencies, but eventually you run into unavoidable dependencies and your clean elegant system has to pick up some smell.

Re:John Carmack on Functional Programming (2013) [video] by craigsmansion on Thursday August 17, 2017 @12:21PM (#15038130)
As I heard it, he talked about independent agents which traverse a world by taking it as an input and reporting their own state as an output, and how easy to code such a model was in functional languages because the world was immutable. You don't have to mess around with the world data to keep the actions in sync.

Of course there's a level of conflict resolution needed at some point, which wasn't something he had not figured out, but something that was out of line with the elegance and simplicity of how the basic game mechanics were implemented so far.

As I understood if, it's wasn't so much about easy parallel programming as it was his discovery of clean models and elegant solutions as encouraged by functional programming.

Re:John Carmack on Functional Programming (2013) [video] by crimsonalucard on Thursday August 17, 2017 @01:44PM (#15039017)
It's not about either. He is illustrating a problem that only occurs in functional programming:

There is no concept of time in pure functions.

In imperative programming when two actors approach the same spot, whether the application is parallel or not, one actor ALWAYS arrives first and the other will arrive subsequently. Therefore the Second actor can read the state of the first and act accordingly...

This is not the case for functional programming. Per frame the state of each actor changes (or in other words: new actors with updated states are created) at the same time and thus you can have two actors approach the same spot at the same time. You will then need an extra step for conflict resolution. Of course there's a bunch of ways to deal with this. Carmack briefly mentioned something about using some other attribute of the actor as a priority number... it's not like this is some crazy issue that's impossible to solve.

Re:John Carmack on Functional Programming (2013) [video] by craigsmansion on Thursday August 17, 2017 @04:14PM (#15040466)
> There is no concept of time in pure functions.

Very true, but there is a fundamental concept of time, almost by definition, in game-worlds.

Whereas in imperative programming it's true that one actor always arrives first in computation, he might not have arrived first "in the game": conflict resolution is still a necessity.

As such I don't think this is a problem that only occurs in functional programming.

It's my impression that it's the events up to any conflict are much cleaner to express functionally.

Re:John Carmack on Functional Programming (2013) [video] by crimsonalucard on Thursday August 17, 2017 @06:48PM (#15041779)
Correct. But when things arrive at the same time and the same place "in game" the imperative programming style resolves this automatically by giving precedence to the object that was computed first, thus this problem is handled automagically in the imperative world. In fact you probably don't even have to really think about this issue when using the imperative style, unlike the functional style where this issue must be explicitly dealt with.

FYI I'm not saying either paradigm is worse or better, it is what it is.

Re:John Carmack on Functional Programming (2013) [video] by ece on Thursday August 17, 2017 @02:07PM (#15039239)
But one actor can always read the state of the other actor, and modify their actions accordingly. The state of each actor is immutable, so each action taken based on the state is sure to be conflict free. As opposed to imperative programming, were each actors position might not get updated correctly or in time until too late. Deadlock and starvation don't happen when you have pure functions with no side effects and immutable state like they do in non-FP languages.

Timing is harder to get right when everything is async, but if your game design is good, you just need to better implement it to get it right frame by frame. He also talks about having monads to handle things like this.

Re:John Carmack on Functional Programming (2013) [video] by crimsonalucard on Thursday August 17, 2017 @02:52PM (#15039700)
If two actors moved to one spot at the same time which actor occupies that spot? How can an actor act accordingly if they moved at the same time?

In functional programming this can happen... in imperative programming it NEVER happens, because the actors move imperatively, aka step by step or one at a time. This is the problem he is talking about.

Re:John Carmack on Functional Programming (2013) [video] by ves on Thursday August 17, 2017 @05:02PM (#15040980)
FP is not gonna wall you off from doing stuff like this. For example, and this is just off the top of my head, you could:

* provide an explicit sequencing of when actors take their turns, passing updated resources to each in turn. This is your "step by step" imperative approach, and you're basically writing a main game loop. * don't sequence the actors, but implement a lock on the resource using a TVar. This is the simplest async approach. * many more approaches I haven't thought of

The point of FP is to be provide as much information about the logic of the program as possible. If two actors can move to the same point at the same time, then you need to write down logic to handle that case, whether actors move in sequence or independently.

Not writing down that logic because "I have an imperative language" is how you get bugs, especially race conditions.

Re:John Carmack on Functional Programming (2013) [video] by crimsonalucard on Thursday August 17, 2017 @06:51PM (#15041793)
I'm not arguing about which paradigm is better. I'm not saying theres no way around it. I'm just describing a single pitfall in the functional paradigm. That's all.
Re:John Carmack on Functional Programming (2013) [video] by ece on Thursday August 17, 2017 @07:40PM (#15042085)
Being able to write asynchronous (multi-threaded or not) code so easily using FP is most definitely not a pitfall.

Even in the most simple multi-threaded imperative programs, no compiler or hardware will give you almost any sort ordering guarantees by default. You have to use atomics or volatile or something else to get specific ordering guarantees. It's the same in FP compilers too.

Re:John Carmack on Functional Programming (2013) [video] by crimsonalucard on Thursday August 17, 2017 @11:43PM (#15043379)
>Being able to write asynchronous (multi-threaded or not) code so easily using FP is most definitely not a pitfall.

Your point? You're saying this as if I disagreed with you. What are you trying to argue here? That FP is the better than imperative? Did I ever dispute that claim? Where are you going with this?

Re:John Carmack on Functional Programming (2013) [video] by ece on Thursday August 17, 2017 @04:28PM (#15040637)
It can only never happen if you use a lock correctly. In separate threads, this can absolutely happen in imperative programming. As a reader-writer or dining philosopher problem will show, two actors can try to do the same thing at the same time unless you EXPLICITLY stop them by using locks.

In FP, you will have an immutable variable for state instead of a mutable variable wrapped in a lock. You will be passing a whole new immutable state variable to a pure side effect free function every time, and won't have to worry about getting a lock and releasing it explicitly (these would be side effects). As long as the state is immutable and synced across threads, actors in any thread can just plot their next actions using that state.

In imperative programming, like I said, you'll have to explicitly get and release locks to make sure two actors don't occupy the same spot.

So, if you use atomic immutable variables with pure functions, and the logic in your actors can be conflict free, you can have horizontal scalability across as many cores as you want pretty easily. If your actors cannot be conflict free, you will need to wrap a lock of your choice in a monad and use that, but you will still have gained better debugging, testing and maintainability by using FP.

Now if only every zero-cost OO abstraction had a straight forward FP alternative that was also zero-cost, we'd all be doing FP as of yesterday.

Re:John Carmack on Functional Programming (2013) [video] by ves on Thursday August 17, 2017 @05:12PM (#15041060)
I don't think the latter point is true. FP requires a lot of thinking up front, which is not how most engineers like to work.

That's not to say that FP is at odds with iterative programming, but it means that you have to work out a "specification" of your code pretty completely from the beginning.

Although, even then it's not so bad because the excellent type systems and compilers mean you can develop the specification interactively (see Idris' typed holes).

Re:John Carmack on Functional Programming (2013) [video] by ece on Thursday August 17, 2017 @06:15PM (#15041560)
Which part? I think even John Carmack in the video talked about the upfront thinking required for FP. If every CS student learned category and type theory like they learn complexity theory; and FP compilers could optimize type classes and all the object creation, FP would definitely be more widely used. But, yes, there is definitely a cost to picking up FP today in almost any domain, but it's getting lower.
Re:John Carmack on Functional Programming (2013) [video] by crimsonalucard on Thursday August 17, 2017 @11:45PM (#15043394)
Man. You are arguing with me as if I disagree. When did I bring up threads and locks? I'm just elucidating the problem Carmack brought up. Jesus.
Re:John Carmack on Functional Programming (2013) [video] by ece on Thursday August 17, 2017 @11:40AM (#15037728)
He talked about a whole FP-powered architecture to solve this.
Re:John Carmack on Functional Programming (2013) [video] by jandrese on Thursday August 17, 2017 @12:28PM (#15038192)
Yes, and I'm curious how it turned out. Are there any followup talks on this that anybody knows about? It's certainly a solvable problem, but how many of the advantages remain once the data dependency problem is solved?
Re:John Carmack on Functional Programming (2013) [video] by ece on Thursday August 17, 2017 @02:40PM (#15039557)
I think the crux of his point is really between constantly having to update state in a mutable way (with a decent probability of starvation and possibly deadlock) vs. pure functions, immutability and the async FP architecture he talked about. The advantage of the later is it brings maintainable code, and this stays true throughout the life of the code.

Off course, you'll have to hire or train FPers as he mentions.

Re:John Carmack on Functional Programming (2013) [video] by sololipsist on Thursday August 17, 2017 @11:46AM (#15037798)
Motivated reasoning. He prefers imperative programming (or something other than functional programming), so he hears the "criticism" without hearing the solution. I mean, of course he physically hears both, but one is prominent in his consciousness. It doesn't matter that there is a solution, he heard what he needed to hear to get the evidence he needs to maintain his bias, and shuts off his frontal lobe.

To be clear: Everyone does this. I just think it's interesting.

Re:John Carmack on Functional Programming (2013) [video] by scott_s on Thursday August 17, 2017 @11:52AM (#15037871)
I think you misunderstand Carmack's position. As of this talk, he had not yet solved this problem, but he is optimistic that it can be solved, and ece mentioned the solutions he outlines. He goes on to say such a system would be beneficial to all game developers.
Re:John Carmack on Functional Programming (2013) [video] by sololipsist on Thursday August 17, 2017 @04:27PM (#15040628)
He says it's a problem that can be tackled, it just hasn't been yet. Like you look at a square block, and you need to get it in a square hole, you just haven't gotten to it.

It's not the language of someone that isn't confident that there isn't a perfectly fine solution. I mean, it's likely that other people have already figured this out, just not him.

Re:John Carmack on Functional Programming (2013) [video] by RUG3Y on Thursday August 17, 2017 @11:47AM (#15037814)
You can train yourself to recognize this pattern in your own behavior.
Re:John Carmack on Functional Programming (2013) [video] by sololipsist on Thursday August 17, 2017 @04:26PM (#15040612)
Mhm. I try to. Why?

Are you implying I prefer functional programming? I don't. I don't have a horse in this race.

Re:John Carmack on Functional Programming (2013) [video] by RUG3Y on Thursday August 17, 2017 @05:16PM (#15041090)
No, I was commenting on statement, "everyone does this". What I was trying to say is that with effort, you can begin to identify times when you are biased toward a certain thing (in this case, different programming styles), and consciously choose to leave your emotions out of the equation.
Re:John Carmack on Functional Programming (2013) [video] by sololipsist on Friday August 18, 2017 @12:15PM (#15047478)
Okay. Sorry, I'm used to reddit were everything is a veiled accusation.

Yeah, you can train yourself, but everyone does it nonetheless. Everyone does it by default, and I don't think it's reasonable to claim that anyone has completely or effectively rid themselves of motivated reasoning.

I try to do this as much as possible, though. It usually ends up with me just having no opinions, rather than an "unbiased" one.

Though I'm not sure how effective training yourself in this way is. There have been studies that show it's difficult to induce this in people, but that statistical, of course. Some people likely take to it much better than others - some people may even take to it very well. I like to think I've had some success, but, you know, that might be motivated, so I don't really have a strong opinion.