99 points · by gus_leonel · 3y ago · 155 comments
new_game_state = simulate_a_frame(old_game_state, user_input, time_delta)
So of course it's possible. Whether it's practical is another question.
This abstraction was almost fully stripped away by the compiler. Since the original Machine object is no longer required when the step function returns from executing an instruction, and since the size of the emulated RAM never changes, the compiler can, for example, represent the list of RAM values as an array, and mutate the array in place and internally just keep track of the pointer to the mutated object and return that. It isn't necessary to copy most of the data structure most of the time.
minihs keeps itself small, uses a stack of copies of source to implement []. Whereas the larger version parses the source to a vector of opcodes (which collapses repeated +-<>) & includes a jump table
Although in practice "purely functional" programming is a bit silly here. The point of PacMan is all the IO which is fundamentally all side effects. There are a lot of practical compromises that would be called "functional" like storing all the major state of the game in a big array (something like [turn1, turn2, turn3, ...]) and then implement a turnX -> turnX+1 animation function.
The fundamental insight of the functional approach is implementing a frame->frame function was already something that implicitly had to be done to make the game. If it is an explicit function then debugging it is easier. Because the input can be saved. No extra work is needed.
In fact, the point of a huge proportion of programming is IO and is "all side effects." It's still possible to adopt a functional style for internal calculations (and in fact this helps to decouple components and makes testing much easier, at the expense of relatively negligible copying overhead, so it's worth doing) but once you get beyond your internal toolkit and start actually trying to test the whole system, you're interacting with the outside world.
I confess that sometimes things do, in fact, get a ton easier if you expand your abstraction out such that you are abstracting the whole board, and not a bunch of individual pieces. That comes with its own set of road blocks, though. In particular, game worlds are a lot bigger now than they used to be.
How far away are we from being able to represent the GPU state and rendering instructions with simple datastructures ready to be submitted wholesale to the driver?
And I can’t think of any reason why you’d combine them.
Recording it is still command driven but trying to give an entire app scene in one go is really messy. We tried this internally on our gpu before Vulkan.
I imagine that comes down to how smart the compiler is.
[1] https://wiki.haskell.org/Frag
[2] https://wiki.haskell.org/Applications_and_libraries/Games
I recently hacked together an asteroids clone in Haskell with SDL2 and not much else. It’s not super pretty but it works.
I’ve talked to folks who’ve been using the newer effects libraries taking advantage of the new delimited continuation primops in GHC 9.6 for their game dev. Even with very high level libraries their reporting acceptable performance.
With enough dedication I’m certain anyone could make whatever game they wanted in an FP language.
> I've spent enough time with functional languages to have figured out how to implement non-trivial, interactive applications like video games. My plan is to cover this information in a short series of entries.
The biggest problem with the approach is that mainstream functional languages put most effort in the functional core rather than the imperative shell modeling.
Effect systems (ZIO for Scala, effect-ts) are the only places where fibers allow composing, scheduling and managing "processes" in a functional way.
I think questions like this are really getting at, is how do you update the 'huge-game-state' without mutating it, changing variables.
People have a very ingrained feeling that the game state is updated by updating variables.
But in functional languages, the state is immutable, so if you are new to functional languages you ask 'man how do I update this'.
Then, if this new person takes next step and is told 'you make a copy of the game state', they are like 'man that must be a big performance hit to copy the whole thing'.
So I think questions like this are really about how to handle a state with immutable data. How to make a copy efficiently, like in parts, or whatever. And then of course, you are down the rabbit whole of changing your brain thinking towards functional style.
I had the luck of having a whole semester doing just that with various functional and logic languages (Haskell and Prolog) with a great teacher.
You start by learning accumulator parameters and tail recursion, then you build and learn to use foldl and lazy functions; and then you may learn monads as a design pattern to reduce boilerplate for those techniques.
Basically, all you do is define a function (fun (arg1, arg...) -> result) and invoke it (the exact same way you define a callback with .map(() => result) in Javascript for example).
You can store the local state of the function you're currently in, into the callback and state can be passed around that way. (Say you are in a JS function where you defined a number or other variable above: you can use that number in any continuation/closure/function and pass it around, to .map() for example or another function.)
I've also heard of recursion as being a way to pass state around in a functional language. If you have a recursive function that takes a num parameter and calls itself with (num + 1), the number can be viewed as mutable state written in a not-so-mutable way.
This means any functional programming language can implement any imperative programming language with a factor of O(log(n)) overhead.
Where you represent the composite structure hanging from the point you want to mutate, so updating that point is merely dropping the header and appending the updated value as a new prefix.
Suppose you have array mutation, but the changes are only local.
Then as TuringTest pointed out, there are efficient ways to make those local changes in a functional programming language without the log(N) overhead.
[2] https://github.com/fable-compiler/repl/blob/master/public/sa...
It's not better than using C, but worked just fine when I wrote it in 2014. I'd say, unless you're planning to sell it, write the game in whatever language you enjoy.
Part 2: https://prog21.dadgum.com/24.html
Part 3: https://prog21.dadgum.com/25.html
Part 4: https://prog21.dadgum.com/26.html
Follow up: https://prog21.dadgum.com/37.html
- "Not being able to re-use names for values in functions". It's not a feature of functional languages, and thankfully many these days don't suffer from this limitation (see Erlang vs Elixir for example)
- "The lack of a simple dictionary type". Again, blame the terrible functional ecosystems of 2008.
I can’t say they were wrong
https://github.com/opyate/minild72/blob/master/src/pong/core...
The simple counterexample, take setpixel, which takes an image buffer, and returns an image buffer with one pixel changed. Now imagine having a large bitmap and using setpixel to draw a sprite to it. For every non-transparent pixel in the sprite you need a whole copy of the whole buffer. Obvs there are lots of improvements and cleverness but compared to the speed and simplicity of an imperative code just modifying an image buffer in-place it's vastly slower, more complicated or both.
However you can manage to write "most" of your game in a functional way and if you're careful about it, and use tricks like copy-on-write to make some things act functional, you can do it. Writing your game in a functional mindset, and keeping data immutable when you can has a lot of benefits.
- https://github.com/EgorDm/fp-pacman (https://www.youtube.com/watch?v=DlifcJ4cexw)
- https://github.com/Jcklly/PacMan
- https://github.com/ajkavanagh/pacman
and other games:
- https://wiki.haskell.org/Applications_and_libraries/Games
See also the #haskell-game chat on Matrix and Libera IRC.
See a few (small, demo) games built by the community in [2] .
Notice Elm has abandoned the FRP approach in favor of Model-View-Update [3].
[1] https://elm-lang.org/ [2] https://github.com/rofrol/elm-games [3] https://elm-lang.org/news/farewell-to-frp
Not pretty, but those CPUs were designed to be really cheap.
> Before I studied the art, a binding was just a binding.
After I learned the art, an assignment was an assignment but an application was an application.
Now that I've understood the art, a binding is just a binding.
16 bit addresses didn’t fit into registers, so you keep pointers in the zero page (bytes 0-255). There was no 16 bit increment either, so to increment the pointer you incremented the low byte and on overflow increment the high byte
Non-constant modulo can often be computed by checking for roll-over when incrementing (or just computed in a similar way to division)
If you think about it, 1.) most performance comes down to memory access. 2.) It is widely known that accessing memory off the stack is faster than allocating memory on the heap. 3.) calling a function pushes everything onto the stack, starts a new stack pointer, and when it’s done releases everything from the stack.
So my thinking is if by definition functional programming is about organizing your program as a pure composition of functions, doesn’t that also imply an ideal memory access pattern? Can someone tell me where I'm wrong?
I guess the one issue where this comes into doubt is with strings of unknown length, and other data types that must be heap allocated, but even in those cases it seems that problem would equally affect all other paradigms.
ps: of course FP shields you from having to manage correct state update seen in imperative programming, which might require a lot, a lot, of reads and writes to ensure things are not messed up.
This is even harder if you work with data structures like threaded trees, where there are so many links that even the zipper approach basically falls flat. (And the zipper already has performance questions if you do a deep edit, if I am not mistaken. What could be a single value mutation is now a rewiring of every link down to the value that you wanted to change.)
This is a very C-centric view of it.
In FP, sometimes they're functions, sometimes they're closures. Sometimes they're fully applied and sometimes they're partially applied.
You're probably on the heap, not the stack for two reasons: tail recursion, and variables very often outliving the return.
I mean like… what’s stopping you?