haskellghcghcihaskell-platformwinghci

Is it not possible to import modules from another module if they are in the same dir? (Haskell)


To demonstrate my question, I've created a test project like this ->

enter image description here

The directory "Data" has only 2 modules, namely "Test3.hs" and "Test4.hs". They are empty modules for test purposes. (implementation: "module Data.Test3 where" )

"Test1.hs" imports all the modules like this ->

enter image description here

which results in an error ->

enter image description here

I am using WinGHCi to import the modules, which automatically changes the directory to "cd: ~\.hs".

I also tried to import the modules by using GHCi and by manually changing the dir. But ended up with the same result as above.

So I come to conclusion that there is no way of importing your own modules from the same directory and you have to always create sub dirs only for this purpose.

Is that right?


Solution

  • You need to decide where your root directory is, run GHCi from that directory, and then consistently name all your modules relative to that same directory.

    You need to name your modules consistently both in the module declaration (module Foo where...) and in the import statements (import Foo).

    So you need either:

    module Test.Test1 where
    
    import Test.Data.Test3
    import Test.Data.Test4
    import Test.Test2
    

    or run GHCi from inside Test and remove all the Test prefixes. But you can't have the prefix on some but not others. You have to be consistent everywhere. Each module name is basically a file path from the current directory to where the source file is.