Load a module in GHCi by module name when module name doesn't match file name

邮差的信 提交于 2019-12-06 14:54:01

You can't where the name contains a dot, as per the documentation

For each of these directories, it tries appending basename.extension to the directory, and checks whether the file exists. The value of basename is the module name with dots replaced by the directory separator ('/' or '\', depending on the system), and extension is a source extension (hs, lhs)...

The key part being

The value of basename is the module name with dots replaced by the directory separator ('/' or '\', depending on the system)

So your module name of My.Module will be searched for as My/Module.hs. You would need to have a directory structure like

project/
    My/
        Module.hs
    project.cabal

And from the folder project you could run

$ cabal repl
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
> import My.Module

You can do this if your file is named MyModule.hs and your module name is MyModule, but it's just a special case of the rule above.

There are good reasons for this, namely that it enforces a structure to simplify your project structure and GHC's search algorithm. If this rule wasn't in place, what would stop me from having

project/
    MyModule1.hs
    MyModule2.hs

where both .hs files had the module declaration My.Module? Which one would be correct to load in GHCi if I ran import My.Module? By specifying what the filename and path is, you immediately know that the module X.Y.Z.W.Q.R.S.T is at the path X/Y/Z/W/Q/R/S/T.hs, no searching required. It reduces a lot of the ambiguity that could occur with looser module name specifications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!