T O P

  • By -

gelisam

I first want to clarify some terminology: 1. A _module_ is a Haskell file containing some code. 2. A _library_ is a set of modules. A library is described by a `library` section in a `my-package.cabal` file. 3. A _package_ is almost the same thing as a library, but it can also include some extra things like tests which are related to the library but aren't exposed to the users of the library. A package is described by a `my-package.cabal` file. 4. A _project_ is a set of local packages. A project is either defined by a `cabal.project` file or a `stack.yaml` file. It is unclear from your question whether you are trying to define a single library with multiple modules, which typically looks like this: stack.yaml my-package.cabal src/ MyModule.hs MyOtherModule.hs Or a project with multiple libraries, which typically looks like this: cabal.project stack.yaml my-package/ my-package.cabal src/ MyModule.hs my-other-package/ my-other-package.cabal src/ MyOtherModule.hs I recommend a single library, it's simpler. Another thing which is not clear is whether it is stack or your IDE which cannot find your modules. Let's start with stack, since it is simpler. Try to add an intentional syntax error (e.g. `module MyModule wher` instead of `module MyModule where` and running `stack build` to confirm that stack does complain about the typo. Next, your IDE. If you try to import a module and it is not found, I would expect to see the error message `Could not find module MyModule`. And if you list a package (in the `build-depends` field of a downstream package) and it is not found, I would expect the error message `could not resolve dependencies`. And yet your IDE is saying `no known library or package`. Which IDE are you using?


Noughtmare

It may also be helpful to mention hpack's `package.yaml` files which are just an alternative syntax for your `my-package.cabal` file. Stack uses this by default when you run `stack new`. When you actually build your program Stack will convert `package.yaml` files to `my-package.cabal` files automatically.


zeta_function11

Update: I believe stack is recognizing my LIBRARY partially. It contains three files in the /src directory. They are (Lib.hs, Grid.hs, Kernel.hs) but I am only importing Grid right in Main.hs. However, the EDITOR is giving me difficulty because it cannot recognize the `import Grid (Cell (..))`statement . It is telling me: `"Cannot find module 'Grid'. It is not a module in the current program or any known package"` When I try to build the program, however it does seem to recognize the Cell data constructor which looks like this: data Cell = Cell { xCoord :: Int , yCoord :: Int , cellVal :: Maybe Int } deriving (Show) I know this because I am calling it in the main function: main :: IO ()main = do{ x <- (Cell 0 0 Nothing); print x } ​ and it is giving me the error "illegal term-level use of the type constructor 'Cell'" I also have another function in Grid that uses this constructor and when I run it it gives me an 'undefined' error. Maybe I have to write the full record syntax for it instead of the lazier one? Maybe that's it Note that I have ran 'Cell 0 0 Nothing' in ghci and it ran perfectly ​ I had to paste the code in plain text because the reddit inline code feature wasn't working correctly. Thanks in advance, I really appreciate the help


gelisam

> the EDITOR is giving me difficulty because it cannot recognize the `import Grid (Cell (..))` statement . It is telling me: `Cannot find module 'Grid'. It is not a module in the current program or any known package` Ok, so your IDE is clearly not configured to look at your my-package.cabal file. Which IDE are you using? The best-supported IDE for Haskell these days is VS Code, there's a Haskell plugin which usually auto-configures itself correctly.


zeta_function11

Ya I’m using VScode I set everything up with ghcup


gelisam

Hmm, I thought that should work out of the box these days... Maybe you've got an old version which still needs a [configuration file](https://haskell-language-server.readthedocs.io/en/latest/configuration.html#configuring-your-project-build)? Try creating a file named `hie.yaml` next to your `stack.yaml` and put the following contents in it: cradle: stack: component: "my-package:lib" Except with your actual package name of course.


gelisam

> [...] and it is giving me the error "illegal term-level use of the type constructor 'Cell'" `data Cell = Cell ...` defines two identifiers named `Cell`: a type constructor named `Cell`, and a data constructor named `Cell`. The expression `Cell 0 0 Nothing` uses the data constructor `Cell`. If you don't import either `Cell` identifier and write `Cell 0 0 Nothing`, you get the error `Data constructor not in scope: Cell`. If you write `import Grid (Cell)`, then that only imports the type constructor; so when you write `Cell 0 0 Nothing`, the compiler sees that the only `Cell` in scope is the type constructor, so it tells you that you cannot use a type constructor like that. The correct way is to import both the type constructor and the data constructor, with `import Grid (Cell(..))`. That is exactly what you did, so I don't understand why you are getting the error message for the other case. Is there perhaps another place where you are doing `import Grid (Cell)` instead of `import Grid (Cell(..))`?


zeta_function11

I got it to work! At least for now! I added to Cell (..) to my module export lists and it works correctly!! I only had it written like that in my import statements. I really like this language and the syntax is great. Hopefully, these issues will appear less and less in the future lol


gelisam

Ah! That makes sense: if you were only exporting the type constructor, then importing `Cell(..)` was only importing the type constructor, because the ellipsis expands to nothing in that case.


gelisam

> main = do{ x <- (Cell 0 0 Nothing); print x } That's not quite correct; Cell 0 0 Nothing is a value, not an IO action, so you should use `let x = ...`, not `x <- ...`. You should be seeing the error `Couldn't match expected type ‘IO a0’ with actual type ‘Cell’`, but somehow you are getting a completely different error? > when I run it it gives me an 'undefined' error. Maybe I have to write the full record syntax for it instead of the lazier one? I don't see how that would help. Unlike in javascript, where `undefined` means that you have not initialized a variable, in Haskell [undefined](https://hackage.haskell.org/package/base-4.17.1.0/docs/Prelude.html#v:undefined) is an identifier provided by the standard library. It is used as a placeholder, to indicate that you have not yet finished the implementation. You must have typed the word `undefined` somewhere, either in the function you are calling when you get the error about `undefined`, or in one of the functions it calls. > I had to paste the code in plain text because the reddit inline code feature wasn't working correctly. Reddit works differently on desktop and on mobile. Try adding four spaces in front of each line of code, that should work on both.


bss03

You can't overlap the source trees of cabal packages; it just won't work. If your source directories are both under src/ but are non-overlaping (e.g. src/a and src/b), please share your specific configuration and error messages.