[Solved]Question about GHC's runtime system and executable file

I’m sorry I’m unfamiliar with computer science,so which I got confused while I reading the docs of GHC’s runtime system.It seemed that Haskell with GHC has a runtime system like JRE for Java,CLR for C#,Nodejs for JavaScript.Is that mean GHC complie the rts into the executable file when I compiling a Haskell program?

If so,the diagram Block Diagram made me stuck.As a program has been compiled into the machine code,why the diagram show the rts is a bridge between complied haskell code and the c client code?I have found generated code for my confusion,but it disoriented me again.😵‍💫

Could someone please told me what the GHC’s runtime system is?And I would be intensely appreciate if someone could clearly tell me the outline of GHC’s working step.:sob::sob::sob:I’m shamed of my ignorant.

2 Likes

You’re right that the RTS is bundled in every executable that GHC generates.
But note that the “C client code” and the “compiled Haskell code” are also part of that same executable.
So the diagram is not showing communication between different processes or executables, but it is showing function calls within the same executable.

2 Likes

You’ve asked several questions, so lets answer them one by one.

Is that mean GHC complie the rts into the executable file when I compiling a Haskell program?

Yes, the RTS is compiled into the executable file when compiling a Haskell program. However, Haskell can also interpret code, using GHCi, or by running the source code as a script. In those cases, the we are using the RTS that is bundled with GHC.

why the diagram show the rts is a bridge between complied haskell code and the c client code? / Could someone please told me what the GHC’s runtime system is?

@jaror already covers this but I’ll add to it: Haskell is based on lambda calculus, which has no concept of input / output, threading, memory, filesystems, etc. In order to get those things, we have bind Haskell to the operating system, because that is what the operating system is responsible for - managing programs and their resources. The RTS acts as a bridge and provides ‘functions’ to Haskell that cannot otherwise be written in it, because they are dependent on the OS (and so in some sense, the RTS acts as a portability layer, too).

I have found generated code for my confusion,but it disoriented me again.

Haskell source code undergoes transformation into several intermediate representations as it is being compiled to binary. STG stands for Spineless Tagless Graph-reduction machine and is one of those representations. Naive methods of performing lambda calculus can be very slow, and the STG machine is used for efficiency.

Compiling down to STG is a different part of the pipeline from the RTS.

3 Likes

:sob:Thank you all for your comprehensive replies.Your answers saved me from stuck mind.

1 Like