Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Functional language are different from the language implementation point of view

There is the whole new paradigm of "functional programming", which needs a total change of thought patterns compared to procedural programming. It uses higher order functions, purity, monads, etc., which we don't usually see in imperative and object oriented languages.

My question is how the implementation of these languages differs from imperative or object oriented languages, with respect to, for example, memory management or internals like pointers etc..

There are functional languages that run on top of the JVM. Does this mean that these languages internally work like the other languages on the JVM?

like image 556
dinsim Avatar asked Dec 06 '09 23:12

dinsim


2 Answers

Code resulting from functional languages uses many features you see to varying degrees in non-functional languages. Garbage collection has passed into general usage. Tail-call optimization is done in GCC and VC++.

Closures, however, are a hallmark of functional programming. You don't see one without the other. If you define "functional languages" to refer only to pure functional languages, the two aren't synonymous as you find closures in imperative languages that support functional programming (e.g. Javascript and Scheme (which is technically imperative, though the functional paradigm is what's mostly used)). Closures might be implemented with a spaghetti stack for the call stack, or by copying out local variables when exiting a stack frame, or by allocating local variables on the heap and letting garbage collection take care of them.

Once you have closures, anonymous functions are relatively easy (with an interpreter, they're really easy). With a compiler, the function is converted to bytecode at compile time, and the bytecode (rather, the address of the entry point) is associated at runtime with the current environment.

Function composition can rely on anonymous function. When a compiler encounters a function composition operator f . g, it creates an anonymous function that calls the two arguments f and g, passing the result of one as the argument to the other.

Monads can be implemented in OO languages, they're just not as necessary as they are in pure functional languages. I/O monads aren't anything too special, they just rely on the fact that he underlying platform allows side effects.

like image 179
outis Avatar answered Sep 28 '22 18:09

outis


Implementations of Functional Programming languages are using a wide range of implementation techniques. An excellent introduction into the implementation of Scheme (a Lisp dialect) gives this book: Lisp in Small Pieces by Christian Queinnec.

like image 24
Rainer Joswig Avatar answered Sep 28 '22 18:09

Rainer Joswig