Search code examples
programming-languagesfunctional-programmingimperative-programming

Is there a standardized way to transform functional code to imperative code?


I'm writing a small tool for generating php checks from javascript code, and I would like to know if anyone knows of a standard way of transforming functional code into imperative code?

I found this paper: Defunctionalization at Work it explains defunctionalization pretty well.

Lambdalifting and defunctionalization somewhat answered the question, but what about datastructures, we are still parsing lists as if they are all linkedlists. Would there be a way of transforming the linkedlists of functional languages into other high-level datastructures like c++ vectors or java arraylists?


Solution

  • Here are a few additions to the list of @Artyom:

    • you can convert tail recursion into loops and assignments
    • linear types can be used to introduce assignments, e.g. y = f x can be replaced with x := f x if x is linear and has the same type as y
    • at least two kinds of defunctionalization are possible: Reynolds-type defunctionalization when you replace a high-order application with a switch full of first-order applications, and inlining (however, recursive functions is not always possible to inline)