Why is it necessary to specify the name of the module when starting the source file?

GHC insists that the module name must match the file name. But if they are the same, then why do we need a Haskell compiler? Seems redundant to me. Is this just a language design error?

Besides the inconvenience, the problem also arises: if I want to use 2 libraries that accidentally have the same top-module name, then I can’t definitely rename it by renaming the folder of one of them. What is the idiomatic solution to this problem?

+5
source share
3 answers

The Haskell language specification requires that the modules start the module header and does not mention files - it leaves complete freedom for executing compilers regarding files. Thus, the Haskell language does not have the ability to express where the files containing the modules are located. Because of this, some compilers [including the most important: GHC] use a simple solution: the module name must match the path from the include directory to the file. This has led to redundancy.

To avoid redundancy, compilers could waive the requirement in the language specification to start each module with a header. However, they decided not to do this simply to confirm the specification. Perhaps the GHC language extension can do this, but there is currently no such extension.

Thus, the problem is a design error of the language and lives as a legacy.

To combat possible name collisions between independent libraries, the best option is the GHC "Import Packages" extension .

-3
source

The Haskell language specification does not talk about files. He talks only about modules and their syntax. Therefore, it is clearly not a mistake in the design of the language.

The GHC compiler (and many others) decided to follow the template of one module per file and search for modules in files with the corresponding names. For me, this seems like a decent strategy. Otherwise, you need to provide the compiler with some mapping from the module name to the file name or an explicit list of all the files used.

+8
source

I would say that one of the main reasons is that you do not always want the module name to be the path to the file added with the file name. This is the same as for Java, C #, and many other languages ​​that prefer an explicit declaration of a namespace in the source code, explicit better than implicit in many cases. This gives the programmer maximum control over the file names, not binding it only to the file name.

Imagine that I was a Japanese Haskell programmer and my OS used Japanese names for file names. I can write my source code using Japanese characters where possible, but I also want to export an API that uses ASCII characters. If the module name and the file name should be identical, this would be impossible, and it would be very difficult for people in other countries to use my library.

And as @chi pointed out, if you have two packages with conflicting module names (this is a very rare occurrence in my experience), you can always use package import.

+2
source

Source: https://habr.com/ru/post/1200976/


All Articles