144 points · by panic · 6y ago · 58 comments
Is there a main tail-recursive function which takes any inputs that have appeared since the last iteration and adds them to the arguments for the next recursive call? The global state would evolve as the arguments of that tail-recursive function over time?
Any state-evolution would need to be made by creating new data-structures out of existing ones, not by modifying the existing data-structures, to be "pure". So in essence the whole state of game-world would need to be copied for each iteration? What's the trick that makes it efficient enough to be usable?
It's also pretty common to still use mutability (the vector package in Haskell is hugely popular) but it tends to be principled and/or abstracted out because its use is explicitly tagged in type signatures. And those APIs can get even better once LinearTypes land.
In gamedev, both can be useful. Vector is useful for performance and GC relief (when Storable/Unboxed). Immutable structures are still useful when backtracking is needed since they make it trivial and performant in that setting. A useful property for debugging, "training mode", replays, netcode etc.
Recursion is used but not necessarily self tail recursion thanks to general TCE.
Persistent data structures (structural sharing) is not cheating. Persistent data structures are immutable just as if updates to the data structure had been implemented by wholesale copying of a data structure rather than just updating the part that needs updating.
Basically as I understand it functional languages must internally "cheat" all the time, because they must run on top of non-functional hardware abstractions. Tail-recursion-elimination happens by turning what looks like a recursive function into a while-loop internally, updating some mutable data.
I understand you can create a new car-cdr -list out of existing list and a new element, without having to modify anything. So would implementing the game-state functionally then mostly consist of creating new lists non-destructively?
Also still wondering about the 'main loop". Is it a tail-recursive function?
The only common immutable data structure that does not have a persistent/data sharing implementation is an immutable array (as opposed to a linked list or a tree structure approximating an array), since arrays must be contiguous sequences of memory and therefore parts cannot be shared (unless you count array slicing, where the new array is always a subarray). However, unless you have strict performance requirements, you generally can get away with not using an array, since arrays generally only offer performance benefits if they are primitive arrays (rather than arrays of pointers to other objects).
And if you do need a mutable array, there are ways of ensuring that its mutability doesn't spread out and "infect" other components of your system.
Generally speaking, yes the main loop ultimately is a tail recursive function. This does not turn out to be much of a constraint. This is because main generally returns Unit (what is called void in e.g. C or Java), and so generally does not make sense to put anywhere except in a tail position since it does not return a useful value to be used elsewhere.
The main loop is probably a tail recursive function.
case health <= 0 of
False => pure ()
True => Output $ Death target
This ought to just be: when (health <= 0) Output $ Death target
It takes years to learn all the general-purpose helper functions in these languages (I'm still discovering new ones after 10 years of Scala), but they can help make code so much clearer. And since they're just plain functions rather than keywords or anything, a reader who doesn't know can just jump through to the definition, which is a one-liner: https://github.com/idris-lang/Idris-dev/blob/master/libs/pre...This sounds quite promising. Large amounts of mandatory type level programming makes constructing useful programs hard. That it is opt-in is a good thing, and agrees with my own experiences with Idris, which are on a much smaller scale than the OP's.
Moreover in practice it's not very common to actually evaluate your functions on large literal inputs at compile time. Indeed it's not very common to evaluate the function on a single value as such at all. Instead it is better to think of your functions as abstract rewrite rules, that allow you to substitute one side with the other. Unlike at runtime, recursive functions do not always need to recurse until their base case.
Therefore it's not actually very common to e.g. have a recursive function recurse many times at compile time. However, when it does happen, yeah your compile times can shoot up, but it's usually fairly obvious what's causing it. Even then, in absolute terms the quantity of calculations involved generally isn't astronomical, it's rather the case that the compiler is far more inefficient at executing your code at compile time than your runtime system is at executing the compiled code.
That being said, dependently typed languages on the whole do tend to have abysmal compile times, but this seems like an implementation issue rather than a fundamental limitation. Efforts like https://github.com/AndrasKovacs/smalltt/blob/master/README.m... make me hopeful this is something that can be solved.
Typing an ML-like language is technically exponential in the length of the program, but really the way to look at it is that it's exponential in the depth of the most deeply nested block (for certain kinds of block). There's a certain symmetry with how program runtime is naturally exponential in the depth of the most deeply nested loop - and, in the same way, while deeply nested blocks are possible, they're generally bad code.
I've hit troublesome compile times in Scala a couple of times. One of them was due to a naively implemented type-level intersection operation and lead me to abandon that project (I was trying to write a compile-time dependency injection framework, so you'd have a set of types representing what you had already instantiated and a set of types representing what each service needed to instantiate itself, so you could only add a service if you already had all of its dependencies available. But it was all using linear search, since types (in Scala) are not hashable as far as I know, and even if they were, I don't know that I'm up to implementing a hashtable using type-level programming).
Yes, you can make compile times _extremely long_. They're not unbounded, as code you use in types needs to pass the _totality checker_, which proves it terminates. (The totality checker requires code to follow certain formats the checker knows how to solve for, otherwise it would solve the halting problem and therefore it could not exist)
But in my experience, it's possible but not likely in practice.
So:
- A year (started in 2019)
- With a new language
- A purely functional language that's not the easiest to learn and use
and ended up with:
- an actual playable game prototype
- a level editor
- a scripting engine
- etc. etc.
But.... how?! I would still be trying to read files from disk or some such.
Check out “Type-Driven Development with Idris” by Edwin Brady himself. The book is truly amazing.
It is also way less mature than Haskell, and someone trying to learn it will run into many issues as a result. I mean, read the linked blog post! Needing to regularly restart one's editor, long compile times for even short programs, and difficulties with installation are all issues I also ran into.
(Also, strong agree about the book. It's really well written, and all around excellent)
The only exception is if you use it purely as a companion to the Type-Driven Development book. As long as you don't stray too far from the book you generally won't run into any of these other issues and it's more plausible to think of it as a first foray into pure functional programming.
I quite like the book Type-Driven Development with Idris by Edwin Brady. It doesn't assume you already know Haskell. But it does teach you a nice mindset of thinking about types first and use that to drive the implementation.
And yeah, compile times are really a big problem if you're making games, because you need to constantly tweak your game logic and playtest). To be fair C++ also has this issue, but fortunately there are ways to alleviate this problem (minimize template usage, do not use libraries which use template metaprogramming like the notorious Eigen, divide your files into source/header files frequently, etc.)
On your second point, some people dynamically recompile C++ too! I guess that code maybe is designed for fast compiling. Many other people use an embedded language like Lua for fast iterations.
Give Idris 2 a try if you’re not afraid of the teething problems.
Ps. Brady btw.