Haskell - What makes the โ€œcoreโ€ unique?

With this code:

main :: FilePath -> FilePath -> IO () main wrPath rdPath = do x <- readFile rdPath writeFile wrPath x 

I got the following error:

 Couldn't match expected type 'IO t0' with actual type 'FilePath -> FilePath -> IO() 

But the file compiles correctly when I change the name "main" to something else.

What is unique in the main and why should its type be IO t0 ?

+7
source share
4 answers

Since the language specification says so .

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

+22
source

As GolezTrol said, all programs should know which character should be run when the function is called. Many scripting languages โ€‹โ€‹do not require (or simply do not need) the main routine, as they may have instructions located at the top level. This does not apply to Haskell, C, and many others - these languages โ€‹โ€‹need an initial location and by convention, which is the main function (according to the Haskell specification - see Cat's answer).

The Haskell notification main does not accept any parameters that correspond to the program arguments - they are received through System.Environment.getArgs .

+4
source

As in C, Java, or C #, main is a special identifier in certain contexts that indicates where the program should run.

Haskell main defines the type of IO a . You must either give your function a different name, or if you really want it to be a starting point, change its signature and read its arguments from the command line using getArgs

Although you did not specify it specifically, main also a feature in that it is the only function in the Haskell program that can (safely) trigger IO actions. The Haskell runtime treats main as special.

+3
source

By definition, main is a function that takes no arguments and returns a value of type IO a , which is discarded by the runtime. Your error message means that your main does not meet these requirements. This is true since your main receives two parameters.

To access command line options, use System.Environment.getArgs .

+1
source

All Articles