4

For a simple Haskell program like

main = putStrLn "Hello"

I would like to know where it exits the functional world of Haskell and enters C. If I try to set a breakpoint in ghci at putStrLn I get an error:

ghci> :break putStrLn 
cannot set breakpoint on putStrLn: module System.IO is not interpreted

Is there some other way from inside ghci to set a breakpoint in the internal IO functions (maybe some special option or by compiling ghci in a special way ?). I would like to follow putStrLn into libraries/Base/System/IO.hs (putStrLn) or further up in libraries/Base/GHC/IO/Handle/Text.hs (hPutStrLn) to see where it enters C code.

Or maybe someone can sketch which paths is taken until the write() syscall is reached?

ja.
  • 4,245
  • 20
  • 22
Konrad Eisele
  • 3,088
  • 20
  • 35
  • 1
    I voted to close as "unclear" since it does not "enter C code". Haskell is compiled to machine code and does not "enter C". There are intermediate languages and the GHC RTS is itself written in C but this does not mean your Haskell primitives ever "enter C". – Thomas M. DuBuisson Jul 17 '17 at 04:08
  • I disagree: When you compile the main = putStrLn with ghc you can open it with gdb and set a breakpoint at "write" which is in my case symbol write@GLIBC_2.2.5 defined in libpthread-2.24.so (however I guess implemented in inline assembly in glibc). There is also a FFI mechanism in Haskell, where you end up in C. – Konrad Eisele Jul 17 '17 at 04:36
  • The real reason I want to know this is because I wonder weather the Monad construct in Haskell is just a convention or weather there is a implementation reason for it: Could you just FFI into libc instead to do IO and skip the Monad? Would it work anyway or is the outcome undeterministic because of Haskell evaluating lazy or something else, like that the GHC is pattern matching for Monad and then handling it in a special way... – Konrad Eisele Jul 17 '17 at 04:40
  • XY problem. If you want to know something about monadic IO, why not ask a question you want answered? Reading the source of `putStrLn` or even seeing it executed line by line won't really help with that question. – n. m. could be an AI Jul 17 '17 at 04:45
  • It is hard to state a question that covers the whole subject. For me it would be helpful if someone could answer how the lower implementation details work. I can also ask the question again from another angle... – Konrad Eisele Jul 17 '17 at 05:10
  • 2
    I rephrased it from the more general angle: https://stackoverflow.com/questions/45136398/is-the-monadic-io-construct-in-haskell-just-a-convention However I'm still interested in the implementaion details. – Konrad Eisele Jul 17 '17 at 05:16
  • @KonradEisele Related question: https://stackoverflow.com/questions/34492110/haskell-primputchar-definition – danidiaz Jul 17 '17 at 13:43
  • @danidaz That is the answer I was looking for. – Konrad Eisele Jul 17 '17 at 16:47
  • @KonradEisele and here I wondered why I got a random upvote :D. – Zeta Jul 17 '17 at 16:56
  • @Zeta your answer in the other thread was pretty much what I was interested in. Dont know how to close this thread, I just leave it like it is. – Konrad Eisele Jul 17 '17 at 17:20

0 Answers0