Home > OS >  Where is Haskell's or GHC's main defined?
Where is Haskell's or GHC's main defined?

Time:04-30

In Haskell, the execution of a compiled program starts with executing main in module Main. This function must be of type IO ().

Which standard or reference defines the above?

Compare C (references C11 standard (ISO/IEC 9899:2011) 5.1.2.2.1 Program startup (p: 13)):

Every C program [...] contains the definition [...] of a function called main, which is the designated start of the program.

Haskell 2010 and Haskell 98 don't define main formally (although there are several examples of functions called main), instead saying:

We leave as implementation dependent the ways in which Haskell programs are to be manipulated, interpreted, compiled, etc.

GHC User's Guide instructs the user to create a main function but never mentions its required type or that it is the start of the program execution. There are references to the Main module but not main function.

Compare C (references C11 standard (ISO/IEC 9899:2011) 5.1.2.2.1 Program startup (p: 13)):

Every C program [...] contains the definition [...] of a function called main, which is the designated start of the program.

Which standard or reference says that main is the start of execution of a Haskell program?

CodePudding user response:

A Haskell program is a collection of modules, one of which, by convention, must be called Main and must export the value main. The value of the program is the value of the identifier main in module Main, which must be a computation of type IO τ for some type τ (see Chapter 7). When the program is executed, the computation main is performed, and its result (of type τ) is discarded.

https://www.haskell.org/onlinereport/haskell2010/haskellch5.html#dx11-98001

  • Related