Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ST monad have special compiler support?

Does ST monad have special compiler support in GHC?

like image 484
nponeccop Avatar asked Nov 17 '11 15:11

nponeccop


2 Answers

You can see the code for STRefs here: http://haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-STRef.html

It uses, clearly, MutVar# primitives. The runST code similarly uses the RealWorld# primitive and unboxed tuples. However, given simply IORefs and unsafePerformIO you could build your own ST monad with the same big-O properties, although probably less overall efficiency.

like image 120
sclv Avatar answered Nov 01 '22 18:11

sclv


Section 9 of State in Haskell might be of interest. I didn't read closely enough to tell whether what they described there actually requires compiler support, but it did sound like implementing some of the primitives in the compiler was the path of least resistance.

like image 42
Daniel Wagner Avatar answered Nov 01 '22 19:11

Daniel Wagner