views:

274

answers:

5

How do functional programmers and functional languages approach the domain of simulations, which seem to be most naturally handled by object-oriented languages? Are there open-source examples of complex simulations written in a (mostly) functional style? What changes of perspective would an OO-programmer need, in order to approach simulations from a functional paradigm?

I'm asking this while learning how Clojure's creator Rich Hickey specifically sought to tame the "incidental complexity" of OO-programming and mutable state. I'm beginning to see how and why Clojure's approach to identity and state may well be superior (Hickey's ants.clj is on the study list). Another related area is using functional programming for games, which are often simulations with lots of stateful "things" all over the place, but there doesn't seem to be a lot written about fp and games that I could find.

Perhaps experienced functional programmers can provide additional background and advice on how to re-orient one's thinking to functional style, specifically for simulations. Thanks in advance!

+3  A: 

I'm not sure that I'm up to the challenge of writing up a comprehensive analysis of the problem posed in the question, but I can at least post some links interesting on the FP vs. games front:

Jörg W. Mittag provides a number of interesting examples in this answer to a question on "real world" Haskell programming (with links to some interesting write-ups -- the Purely Functional Retrogames series is really worth a read).

In Clojure land, Phil Hagelberg has implemented a text-based adventure game for his PeepCode screencast on Clojure programming; the code is available on GitHub. Then there's Brian Carper's RPG project; no code publicly released yet and just that one post from a while ago (it looked very cool, though, so let's all come together to pressure Brian to continue ;-)). Finally, here's an example of a simple game using Penumbra (for some reason -- possibly unrelated to Clojure -- I couldn't get it to work, but may you will, plus there's a write-up attached).

As for simulations, studying ants.clj is a great idea. Also, I remember seeing a series of SICP-based lectures from an introductory programming course at UC Berkeley (I think...?) available somewhere (90% it was on their YouTube channel); they've got three lectures on OOP in Scheme and I think they mention simulation as a domain providing good use cases for the approach. Note that I have a pretty vague memory of this one, so it's hard for me to say how useful it might be to you.

Michał Marczyk
Excellent answer! Mire was fun to read though, and the Peepcode cast was a nice way to review things (I watched it too late, alas, to get the nice introduction value it has). Also, Brian's still working on the RPG evidently. Get excited.
Isaac Hodes
Michal, thanks for the helpful links and information. Re. the SICP-based OOP lectures, perhaps you mean Lectures 16-18 here? http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/lecture-notes/
limist
@limist: No, I meant lectures 17-19 from the SICP course from UC Berkeley -- just rediscovered the link: http://www.youtube.com/ucberkeley#g/c/6879A8466C44A5D5 @Isaac Hodes: Thanks! Also, good to know that Brian's still on it. And thanks for your own cool collection of links. :-)
Michał Marczyk
+2  A: 

Michal's answer is excellent, but I thought I'd add a couple other neat examples I've personally found helpful/interesting.

The first is a post (and code) about functional fluid dynamics by Lau Jenson. Though he definitely goes the mutable route for speed here, the style is rather functional. I'd bet by Clojure 1.3 this could be done (mostly!) immutably with reasonable performance.

The next is a simple Snake game implemented in Clojure. Easy enough to read though in an hour or so, and the style is really pleasant and cohesive.

Also, some neat code to look at (I think!) is code modeling neural networks. Jeff Foster has some single layer perceptron code, and some more idiomatic revisions of the code. Worth looking at, even if you're not acquainted with NNs. He also has some more recent posts regarding fluid dynamics, though this time in Haskell. (Part I and Part II) Also fun, I think, is his implementation of the Game of Life (& Part II).

Finally, as Michal mentioned before me, Brian Carper is working on a RPG in Clojure. he recently posted some artwork for the game, so I'm betting it's still being worked on ;)

I love using the sequence libraries for working with tons of data; it feels more natural using abstractions like map and reduce, and fun, handy tools like juxt rather than simple imperative iterations. You do pay a tax, I've found, by using Clojure/functional langs in reimplementing well-known and well-implemented imperative algorithms.

Have fun!

Isaac Hodes
+3  A: 

Simulations are a form of interpreter -- which are easy to write in a functional style. They can be also designed as self-optimizing simulators, based on treating them as a compiler.

Don Stewart
@Don: thank you, could you share more examples/references to simulations as a "form of interpreter" please? I'm fairly new to non-OO styles of programming and just starting to escape that mode of thinking. I do want to eventually write a DSL for the simulation I'm working on, so any additional pointers in that direction would be appreciated, thanks.
limist
+2  A: 

Uncle Bob has been playing with Clojure lately and in particular writing an orbital simulator as his most public example.

Some links:

Alex Miller
+3  A: 

I'm writing a game in Clojure, using a mostly functional style. For example, the entire game state is modelled as an immutable data structure.

This has required some slightly convoluted coding. For example, you frequently end up creating functions with a lot of parameters to pass around various elements of game state and ensure that application of game state updates happens to the very latest game version.

But it has also thrown up some really nice advantages, for example concurrency has proved pretty trivial and you can do fun things like cloning the entire game state to run different simulations in the AI.

Overall, I'm delighted with Clojure as a language for simulations / games.

Based on this experience, the things I think would improve Clojure for games / simulations would be:

  • Better support for primitives, especially as function parameters and return values
  • Implementation of core language functions that are less hard on memory allocations (GC pressure is an issue for interactive games!)

You can see an early version of the game here: Ironclad - Generals of Steam. It's basically a steampunk themed strategy game.

mikera