Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decomposition (modularity) in functional languages

Got an idea: functions (in FP) could be composed similar ways as components in OOP. For components in OOP we use interfaces. For functions we could use delegates. Goal is to achieve decomposition, modularity and interchangeability. We could employ dependency injection to make it easier.

I tried to find something about the topic. No luck. Probably because there are no functional programs big enough to need this? While searching for enterprise scale applications written in FP I found this list. Functional Programming in the Real World and this paper. I hope I just missed the killer applications for FP, which would be big enough to deserve decomposition.

Question: Could you show decent real-world FP application (preferably open source), which uses decomposition into modules?

Bonus chatter: What is the usual pattern used? What kind of functions are usually decomposed into separate modules? Are the implementations ever mocked for testing purposes?

like image 825
Pavel Savara Avatar asked Sep 22 '10 22:09

Pavel Savara


2 Answers

Some time ago I was learning F# and wondering about the same topics, so I asked about quality open source projects to learn from.

The reason why you're not seeing anything similar to dependency injection in functional programming is that it's just "natural", because you "inject dependencies" just by passing or composing functions. Or as this article puts it, "Functional dependency injection == currying", but that's just one mechanism.

Mocking frameworks are not necessary. If you need to mock something, you just pass a "stub" function.

See also this question about real-world Scala applications.

like image 62
Mauricio Scheffer Avatar answered Sep 18 '22 09:09

Mauricio Scheffer


Either we're talking at cross-purposes (it's possible: I'm rather unfamiliar with OOP terminology) or you're missing a lot about functional programming. Modules and abstraction (i.e. interchangability) were basically invented in the functional language CLU. The seminal papers on abstract types are James Morris's Protection in programming languages and Types are not sets. Later, most improvements in module systems and abstraction have come from the functional programming world, through ML-like languages.

The killer application for functional programming is often said to be symbolic manipulation. Most compilers for functional languages are written in the language itself, so you could look up the source of your favorite functional language implementation. But pretty much any nontrivial program (functional or not) is written in a modular way to some extent — maybe I'm missing something about what you mean by “decomposition”? The modularity will be more visible and use more advanced concepts in strongly typed languages with an advanced module system, such as Standard ML and Objective Caml.

like image 21
Gilles 'SO- stop being evil' Avatar answered Sep 22 '22 09:09

Gilles 'SO- stop being evil'