[Haskell-cafe] better way to do this?

Peter Verswyvelen bugfact at gmail.com
Wed Oct 7 04:05:12 EDT 2009


Well, I have to add to this, that when I coded my first games in assembler
in the eighties, I did exactly the same thing: just recording the input of
the joystick was enough to get full replays and make auto playing demos.
 But on the old computers, this was all so easy, since you had full control
over every bit of the system (it was even easy to count exactly how many
cycles a routine would take :-), so it was just a matter of starting the
system in the same initial state, which was very easy.
As Ryan says, this was a blessing for debugging; after a bug was found (but
of course when writing in assembler one did not make bugs ;-), the testers
just returned a small log file of the input and version number, and this
allowed reproducing the problem *before* the bug occurred (aka known as
reverse debugging?).

Also since the mutable part of the game was just a couple of kilobytes (most
of the memory was used for immutable graphics, sounds and code), taking a
snapshot at regular intervals and embedding this into the log-file made it
easy to quickly go backwards and forwards in time.

To me, that was the joy of imperative programming: I could reason about
those systems as a whole, they behaved in a very predictable way, and since
all home computers (and game console) where basically identical, you knew
that if it behaved correctly on your system, it would also work on the
millions of other systems out there. Now IMO imperative programming is more
like playing Russian roulette...


But I find it so much harder or impossible on modern systems to do this, or
as Ryan says, it requires a high discipline from coders...

So yes, without using IO, Haskell forces you into this safe spot (there's of
course the unsafePerformIO function as backdoor to break all that :). But we
still need to see the first commercial games written in Haskell; hope to see
those soon, without using IO everywhere of course :)

Actually, it might be interesting to make a special mailing list for Haskell
and games? Or even a broader list for applying FP to games and simulations?
Or maybe that already exists?

On Sun, Oct 4, 2009 at 7:16 PM, Ryan Ingram <ryani.spam at gmail.com> wrote:

> And, to go further, once you embrace "determinism" in your randomness, you
> can do all sorts of really cool things.
> From the perspective of a games programmer:
> You can run the same simulation code on two different network nodes, and
> reliably get the same result, allowing you to just transfer user inputs
> between the nodes instead of game state.  This has applications in reducing
> latency as well, as you only need to transfer the input one way across the
> network.
> You can save off the user inputs and initial into a tiny "replay" buffer,
> allowing you to re-run the game from the beginning without much memory
> cost.  This is not only a cool end-user feature, but it aids *tremendously*
> in debugging.  When something goes wrong, you can always just rewind as many
> times as you want while you narrow down the cause of the problem.
> However, we always had problems with determinism failures, where somebody
> would use the wrong random-number generator, or forget that they aren't
> allowed to have the simulation depend on something that came from the
> graphics RNG.  In Haskell you can encode the purity of the simulation into
> its type and it won't break!
>   -- ryan
> On Sun, Oct 4, 2009 at 6:20 AM, Duncan Coutts <
> duncan.coutts at googlemail.com> wrote:
>> On Sun, 2009-10-04 at 05:11 -0700, Michael Mossey wrote:
>> > Duncan Coutts wrote:
>> > > Others have already answered but I'd like to suggest that you avoid
>> > > using IO here. There's no need for this to be impure.
>> > Can you point me to a tutorial that covers the basics of randomness in
>> > Hasell? I find it very confusing.
>> http://en.wikibooks.org/wiki/Haskell/Hierarchical_libraries/Randoms
>> http://learnyouahaskell.com/input-and-output#randomness
>> The main thing to realise is that random number generators are pure and
>> predictable. Given the state of a random number generator, if you ask
>> for a random number, it always gives the same answer. It has to, because
>> it is pure.
>> Let's make one, and seed it with the starting state 12345
>> ghci> :module System.Random
>> ghci> let g0 = mkStdGen 12345
>> Now we can ask for the next random number in the sequence:
>> ghci> let (n1, g1) = next g0
>> ghci> n1
>> 493972152
>> Now of course if we asked for the random number from g0 again then we
>> must get the same result. But notice that when we use 'next' it also
>> gives us back g1 which is the next state of the random number generator.
>> ghci> let (n2, g2) = next g1
>> ghci> n2
>> 335387100
>> So this is the basic way that random number generators work in a pure
>> language. The generator has to be passed around the pure function, for
>> example from one recursion to the next.
>> So you end up with pure functions like:
>> shuffle :: RandomGen g => g -> [x] -> [x]
>> Another approach is to hide the 'g' inside a monad. That's what
>> MonadRandom is all about. eg:
>> shuffle :: [x] -> Rand [x]
>> The tutorials above explain about the other random functions, for
>> getting values of different types (not just Int) and restricted ranges
>> of number etc.
>> Of course at some point you want to seed the random number generator
>> with some initial genuinely random value (not like the 12345 we used
>> above). That is the only place in your random-handling code that needs
>> to do IO. All the rest of it can be pure.
>> Duncan
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091007/fae54e57/attachment-0001.html

More information about the Haskell-Cafe mailing list