2

I've understood how C code is compiled to Assembly, from Computer Architecture class. It was not that difficult for me, possibly because these two languages are both imperative language.

In Programming Language class, I've learned how functional language can be composed via AST, and tried to make my own small interpreted language.

The thing is, how this functional language is compiled to assembly code? What I've learned is, functional languages are based on lambda calculus, of which the minimal unit of computation is "expression", not the same as the unit of imperative languages, which is "statement" with semicolon.

I tried to analyze GHC code on my own, but it was way too hard and time consuming for me. All I want is some intuition about how lambda calculus-based languages can be compiled to assembly code.

MyBug18
  • 2,135
  • 2
  • 11
  • 25
  • 2
    You may start here : https://stackoverflow.com/questions/6808867/understanding-ghc-assembly-output . – schaiba Mar 16 '20 at 08:11
  • 2
    It's a much harder job, and there are many possible ways to do it. There are tons of scientific papers on PLs describing "abstract machines" for the lambda calculus, i.e. algorithms and data structures to implement functional programs as a state-based machine, so that FP can be used on our computers. GHC is based on the so-called STG machine, which is too complex to describe here. You can try reading Peyton-Jones papers. – chi Mar 16 '20 at 08:53
  • 2
    I've asked basically [the same question](https://stackoverflow.com/q/4385160/417501) a while ago. Maybe the resources linked there might be helpful to you. – fuz Mar 16 '20 at 10:03
  • 1
    There's not as big a difference as you might think. Many statements in imperative languages are just expressions (namely, function calls). Do you understand how a function like `foo () { return bar(3, baz("hi")); }` would be compiled in C? – chepner Mar 16 '20 at 12:34
  • To supplement chi's comment: ... or [one of Peyton Jones' books](https://www.microsoft.com/en-us/research/publication/the-implementation-of-functional-programming-languages/). – phipsgabler Mar 16 '20 at 14:22

0 Answers0