Re: imperative-to-functional compiler (Torben Ęgidius Mogensen)
Tue, 18 Jan 2011 09:49:52 +0100

          From comp.compilers

Related articles
imperative-to-functional compiler (nojb) (2011-01-17)
Re: imperative-to-functional compiler (Patryk Zadarnowski) (2011-01-18)
Re: imperative-to-functional compiler (2011-01-18)
Re: imperative-to-functional compiler (Alain Ketterlin) (2011-01-18)
Re: imperative-to-functional compiler (Martin Ward) (2011-01-18)
| List of all articles for this month |

From: (Torben Ęgidius Mogensen)
Newsgroups: comp.compilers
Date: Tue, 18 Jan 2011 09:49:52 +0100
Organization: - Supporting Open source
References: 11-01-074
Keywords: functional
Posted-Date: 18 Jan 2011 10:24:50 EST

nojb <> writes:

> Suppose you want to translate an imperative language (e.g. a suitable
> subset of Pascal) into a functional language that does not have
> mutable variables (e.g. ML).

ML does have mutable variables, but I will answer for the case where the
functional language doesn't.

> Is this possible?

Certainly. You can translate any Turing-complete language to any other.

> What would be the algorithms/theory that would be relevant to handle
> the mutability of the variables on the Pascal side?. Is SSA relevant?

SSA is certainly relevant, but it is not purely functional. There is a
variant of SSA called "functional intermediate form", which adds scoping
to SSA and replaces labels by function definitions and jumps by
tail-calls. This can be used to translate variable updates to parameter
passing. However, it only handles updates of scalar variables, not of
record fields, array elements and such. To handle that, you need to
represent data structures such as arrays and records as functional data
structures and be careful about sharing.

Functional languages that do not provide mutable variables often add
abstractions that make it easier to emulate mutable variables and data
structures. Haskell, for example, provides monads (and specifically
the state monad) that allow mutable variables to be implemented in a
way that is both fully functional and efficient. This can ease the
translation from an imperative language.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.