Functional Programming – Using FP for Simulation and Modeling

cfunctional programminghaskellmodelingobject-oriented

I'm about to start a simulation/modelling project. I already know that OOP is used for this kind of projects. However, studying Haskell made me consider using the FP paradigm for modelling a system of components. Let me elaborate:

Let's say I have a component of type A, characterised by a set of data (a parameter like temperature or pressure,a PDE and some boundary conditions,etc.) and a component of type B, characterised by a different set of data(different or same parameter, different PDE and boundary conditions). Let's also assume that the functions/methods that are going to be applied on each component are the same (a Galerkin method for example). The object's mutable state would be used for non-constant parameters.

If I were to use an OOP approach, I would create two objects that would encapsulate each type's data, the methods for solving the PDE(inheritance would be used here for code reuse) and the solution to the PDE.

On the other hand, if I were to use an FP approach, each component would be broken down to data parts and the functions that would act upon the data in order to get the solution for the PDE. Non-constant parameters would be passed as functions of something else (time for example) or expressed by some kind of mutability(emulation of mutability,etc.) This approach seems simpler to me assuming that linear operations on data would be trivial.

To conclude, would implementing the FP approach be actually simpler and easier to manage (add a different type of component or new method to solve the pde) compared to the OOP one?

I come from a C++/Fortran background, plus I'm not a professional programmer, so correct me on anything that I've got wrong.

Best Answer

Good question, I've been thinking along similar lines. Historically, the OO paradigm arose from the need for computer simulation - see the history of Simula - and despite early OO languages like Smalltalk being made by people who knew what they were doing (i.e. Alan Kay), OO is now arguably over-used and brings in far too much accidental complexity.

Generally, FP style programs will be shorter, easier to test, and easier to modify than OO programs. As Rob Harrop put it in his talk, Is the Future Functional?, you can never get simpler than functions and data; the two compose infinitely, to build up whatever abstractions are needed. So one way to answer your question (or am I just restating it? :) is to ask, What's the highest level function, and the highest-level input-data --> output-data look like? Then you can start breaking down those "alpha" functions and data types into the next layer of abstractions, and repeat as necessary.

Another perspective (not quite answers) on your question is to look at this thread (disclaimer, I started it) on StackOverflow, some of the answers are very interesting: https://stackoverflow.com/questions/3431654/how-does-functional-programming-apply-to-simulations

My own opinion at this point is, unless you're modeling a situation where there really are discrete objects that only interact in definite ways (e.g. a model of a computer network) - and thus map directly to the capabilities of a clean, message-passing-paradigm OO language - it's simpler to go FP. Note that even in the games-programming community - where simulations are very prevalent and performance requirements are paramount - experienced developers are moving away from the OO-paradigm and/or using more FP, e.g. see this HN discussion or John Carmack's comments on FP

Related Topic