Question

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?

Was it helpful?

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)

OTHER TIPS

Perhaps you are interested in removing some language elements (such as higher-order functions), right?

For eliminating HOFs from a program, there are techniques such as defunctionalization. For removing closures, you can use lambda-lifting (aka closure conversion). Is this something you are interested in?

I think you need to provide a concrete example of code you have, and the target code you intend to produce, so that others may propose solutions.

Added:

Would there be a way of transforming the linkedlists of functional languages into other high-level datastructures like c++ vectors or java arraylists?

Yes. Linked lists are represented with pointers in C++ (a structure "node" with two fields: one for the "payload", another for the "next" pointer; empty list is then represented as a NULL pointer, but sometimes people prefer to use special "sentinel values"). Note that, if the code in the source language does not rely on the representation of singly linked lists (in the source language implementation), you can also implement the "cons"/"nil" operations using a vector in the target language (not sure if this suits your needs, though). The idea here is to give an alternative implementations for the familiar operations.

No, there is not.

The reason is that there is no such concrete and well defined thing like functional code or imperative code.

Such transformations exist only for concrete instances of your abstraction: for example, there are transformations from Haskell code to LLVM bytecode, F# code to CLI bytecode or Frege code to Java code.

(I doubt if there is one from Javascript to PHP.)

Depends on what you need. The usual answer is "there is no such tool", because the result will not be usable. However look at this from this standpoint:

The set of Assembler instructions in a computer defines an imperative machine. Hence the compiler needs to do such a translation. However I assume you do not want to have assembler code but something more readable.

Usually these kinds of heavy program transformations are done manually, if one is interested in the result, or automatically if the result will never be looked at by a human.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top