Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does functional programming apply to simulations?

Tags:

Besides the general question in the title,

  • 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, e.g. Clojure's separation of identity and state makes a lot of sense (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; there are some articles/papers written about FP and games, more would be welcome.

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!

like image 331
limist Avatar asked Aug 07 '10 18:08

limist


People also ask

What programming language is used for simulations?

C++ is the most common programming language for building specialized process simulation software packages such as Hysys, ChemCAD etc.

What is functional programming used for?

Functional programming seeks to take advantage of language support in using functions as variables, arguments, and return values to create elegant code. Because first class functions are so flexible and useful, even strongly OOP languages like Java and C# have moved to incorporate first class function support.

Is functional programming used in AI?

AI programs consist of lots of matrix multiplications. Functional programming is good at this just like GPUs. You work with datasets in AI with some algorithms to make changes in the data to get modified data. A function on a value to get a new value is similar to what functional programming does.

Is functional programming used in games?

There are a lot of advantages to functional programming in terms of concurrency, using persistent data-structures makes it cheaper (as well as much simpler in terms of code) to maintain old copies of game state which is very important if your game is going to feature a multi-player component.


2 Answers

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!

like image 56
Isaac Avatar answered Sep 28 '22 23:09

Isaac


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.

like image 28
Michał Marczyk Avatar answered Sep 29 '22 00:09

Michał Marczyk