Clearly those values aren’t the same, just like List["fred"] and List["zombie"]
aren’t the same. This tells you that there’s no guarantee that getUserInput will always
return the same result when it’s called
The idea behind RT (and hence purity) is not that the same result would be returned when it is called...but the same result would be returned when called with the same input, plus the fact that when called, the function would not do anything else apart from returning the value.
In that case IO monad indeed makes a function RT and pure. So in your example, if the user enters "fred" into getUserInput, they would always get IO[String] (which in your example when actually run will yield "fred"), no matter how many times they call the function with "fred". And as long as getUserInput does not do anything observable outside of the function, like logging or writing to console, then yes getUserInput is pure
The program you then run, when you compose these IO values? Those are not pure because that is when the interaction with the outside world happens...and that is when you cannot be sure you will always get back the same results each time
Chapter 75, Introduction: The IO monad, page 599:
The idea behind RT (and hence purity) is not that the same result would be returned when it is called...but the same result would be returned when called with the same input, plus the fact that when called, the function would not do anything else apart from returning the value.
In that case IO monad indeed makes a function RT and pure. So in your example, if the user enters
"fred"
intogetUserInput
, they would always getIO[String]
(which in your example when actually run will yield "fred"), no matter how many times they call the function with"fred"
. And as long asgetUserInput
does not do anything observable outside of the function, like logging or writing to console, then yesgetUserInput
is pureThe program you then run, when you compose these IO values? Those are not pure because that is when the interaction with the outside world happens...and that is when you cannot be sure you will always get back the same results each time