2

Recently, I've been wanting to create a small (educational) functional, optimizing compiler. For the optimizing portion, I want to use SSA. The thing is that (most, as far as I know) functional programming languages have immutable variables (by default), so every variable is assigned only once like in SSA. Would SSA be needed? Is a functional program (in Haskell for example) already in SSA form?

xilpex
  • 3,097
  • 2
  • 14
  • 45
  • "Functional programming languages have immutable variables" just means that, as part of optimizing, you will have to do extra work to add mutable variables (while doing things like turning recursion into loops, etc). The end result will be "SSA with Phi function for mutable variables' (the same as you would've had for imperative languages). – Brendan Oct 15 '20 at 09:24
  • There is one more step to get an exact equivalent of an SSA from a pure functional program - a simple CPS transformation. – SK-logic Oct 15 '20 at 13:08
  • @SK-logic -- Could you please expand on that? – xilpex Oct 16 '20 at 16:37

1 Answers1

5

Yes, programs written in purely functional languages (eg. Haskell) are in SSA form. You can find more explanation in this research paper.

Note that this is not true for all functional languages. For example, OCaml allows programmers to mutate variables and write imperative blocks (which makes OCaml not a pure functional language), breaking the SSA form.

Camelid
  • 1,535
  • 8
  • 21
Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59