Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To write or not to write `module Main where` in Haskell

Tags:

Haskell 98 specification says that the entry point of a program, namely, function main, should reside in the module called Main, by convention. However, even if you don't write module Main where at the top of the file you write main in, the source code compiles and seems working correct when you're using GHC.

The question is:

  1. What's the difference between writing module Main where and not writing it?
  2. Which one is preferred?
like image 210
Pteromys Avatar asked Jun 20 '12 03:06

Pteromys


People also ask

What is module Main where in Haskell?

A Haskell module is a collection of related functions, types and typeclasses. A Haskell program is a collection of modules where the main module loads up the other modules and then uses the functions defined in them to do something. Having code split up into several modules has quite a lot of advantages.

How do you define a module in Haskell?

Haskell's module design is relatively conservative: the name-space of modules is completely flat, and modules are in no way "first-class." Module names are alphanumeric and must begin with an uppercase letter. There is no formal connection between a Haskell module and the file system that would (typically) support it.


1 Answers

There isn't really a difference, module Main (main) where would be the implicit definition when you don't specify a header yourself. From the Haskell 98 Report:

An abbreviated form of module, consisting only of the module body, is permitted. If this is used, the header is assumed to be module Main(main) where.

I would prefer an explicit definition to an implicit one but, for a Main.hs it's a minor preference.

like image 113
jdeseno Avatar answered Oct 05 '22 15:10

jdeseno