Is this a smart look at Haskell IO?

Is this a smart look at Haskell IO?

When defining a program, Haskell runtime does the following:

  • main call to return "I / O calculation"
  • Then it performs or “starts” this calculation, thereby performing all the side effects that the calculation contains.

This two-step approach allows main to remain a pure function.

The calculation of IO in this case looks like a special version of Haskell with explicit sequencing - or is there perhaps a better way to describe this?

+7
source share
2 answers

Yes, this is a decent semantic model of how programs are executed. Of course, the implementation does not work, but you can still use this model to parse programs.

But overall, what IO does is allow you to view imperative programs as pure values. Monad operations then allow you to compose imperative programs from small imperative programs (or use the usual term in this context, actions) and pure functions. Thus, a purely functional model, although it cannot execute imperative programs, can still describe them as expressions like IO a , and the compiler can translate these descriptions into imperative code.

Or you could say the following:

  • The compiler (and not the runtime) evaluates main .
  • The result of this assessment is a mandatory program.
  • This program is saved in the target executable file.
  • Then you execute the target program.

Ie "Evaluate the main " part of your model is pushed to the compiler and is not in the runtime when you first describe it.

+12
source

Your look at IO is good, but I have a problem with this line

The primary calls to return "I / O calculations"

The best way to think about Haskell is that functions do nothing. Rather, you are declaratively describing which values. A program consists of a description of an IO value called main . The only meaning to which he “calls the main” is that the main declaration comes down to the normal form of a weak head (or something like that).

IO is a type of arbitrary computing with full effect. A pure subset of Haskell is a purely declarative description of values ​​that allows for unsolvable descriptions. Think of Haskell as a mathematical language, such as set theory. Statements in set theory do nothing, but they can include complex calculations, such as "the smallest set containing the Ackerman function (30)." They may also contain unsolvable statements like "S = the set of all sets that do not contain themselves"

@amindfv has half the right: main not a "pure function". This is not a function at all. This value is determined by the net reduction, the encoding of fuzzy calculations.

+6
source

All Articles