I wanted to figure out how GHC decides what assembly instructions to emit when compiling the functions of the ST monad

Hello,

I am implementing the RGN monad discussed in Fluet, et al. I wanted to know how GHC figures out what assembly instructions to emit when compiling the ST Monad. I would also welcome suggestions on what files I should go through in the GHC source code to get an understanding of how the ST Monad has been implemented and how to use the State# runtime feature.

The ST Monad has very little special treatment inside the compiler. For the most part it gets desugared to Core, which eventually is compiled to Assembly like any other code after desugaring.

I would recommend starting by looking at how GHC desugars Haskell to Core, and then go from there. There are a number of flags allowing you to view the intermediate representations GHC uses internally: 5.13. Debugging the compiler — Glasgow Haskell Compiler 9.15.20251006 User's Guide

These might be a good place to start.

There is also a paper on ST which might give you a better idea on how ST works: Lazy Functional State Threads

I should go through in the GHC source code to get an understanding of how the ST Monad has been implemented and how to use the State# runtime feature.

Beyond the type level trickery explained in the Paper State# is what GHC calls a zero width argument. Those need some consideration in various places in the compiler. But there isn’t one place that explains those. I would start with reading some dumbs, reading the paper, and then you will probably have more narrow questions that are easier to answer.

5 Likes