I have no problem with the IO Monad. But I want to understand the followings:
- In All/almost Haskell tutorials/ text books they keep saying that getChar is not a pure function, because it can give you a different result. My question is: Who said that this is a function in the first place. Unless you give me the implementation of this function, and I study that implementation, I can't guarantee it is pure. So, where is that implementation?
- In All/almost Haskell tutorials/ text books, it's said that, say
(IO String)
is an action that (When executed) it can give you back a value of type String. This is fine, but who/where this execution is taking place. Of course! The computer is doing this execution. This is OK too. but since I am only a beginner, I hope you forgive me to ask, where is the recipe for this "execution". I would guess it is not written in Haskell. Does this later idea mean that, after all, that a Haskell program is converted into a C-like program, which will eventually be converted into Assembly -> Machine code? If so, where one can find the implementation of the IO stuff in Haskell?
Many thanks
getChar
is not a function; it's a constant.IO
as assembling an imperative program (in e.g. C or assembler code). Or rather: Your Haskell program runs and creates one bigIO
value (an imperative program) bound tomain
. The Haskell runtime system then takes the value ofmain
and executes it.A -> B
for some types A and B. That is, a function takes exactly one argument. There is no->
ingetChar
's type.getChar
ultimately involves some OS syscalls to actually read the char. The "when executed" part you read in some documentation only means that, just because some IO action is returned, that does not mean it is executed. E.g.length [getChar,getChar]
does not perform any IO. Here you can pretend two IO actions are created but not executed (actually, doe to the laziness, they won't even be created, but that's another issue).IO
functions - it's there in plain text.