Why not the pure function getline :: [String] ?

Hello, I am learning Haskell and I am at the stage where IO comes into play.
I need help to clearly understand why this IO is used.

One can imagine a getline implementation returning a list which content is [“firt user input”, “second user input”, etc…].
Clearly, the values contained in this list as well as the list length are not known at compile time (lazy evaluation will know as needed). Also, values will change at each program execution.
Nevertheless, such a function would still be a pure function in the scope of one program execution.

The idea of a Haskell program being executed within a fully “pure world” seems thinkable to me. In such a world, external data are all immutable but exist in several “versions”. Each version remaining accessible to the program if needed.
The here above implementation of is an example for data input but the approach can be general.
For example, a data base can be implemented along that approach if each db modification is exposed as a full new db. Within the scope of a program execution, several db states should then remain accessible to the program. This is comparable to string modifications in Haskell but at the level of a db. The implementation may be a challenge regarding optimization but is probably doable.

Thanks in advance for taking the time to share your knowledge,


1 Like

There are several ways to do IO in a pure language such as Haskell. Each of these are possible in theory, but they all have advantages and disadvantages. In the end, Haskell decided to go with an IO type.

  • Mercury and Clean use uniqueness types which are a little bit like the “version” you described if you squint the right way
  • Haskell used to define main as a pure [Request] -> [Response] type, see page 69 and after that of the haskell 1.2 report.
  • Later, Haskell switched to monads, because they were found more practical and more importantly, easy to extend from the developer’s side. I think this publication highlights the advantages of monads well and answers your question.

Jaspervdj, thanks for your clear answer.
The publication is a great resource.
I’ll need some time to digest the document but already after a few pages I can see that the approach I was thinking off leads to many issues and dead ends.