Home > Blockchain >  How do I add libraries to a project in Haskell?
How do I add libraries to a project in Haskell?

Time:01-17

I'm trying to write a parter module and for that I want to connect parsec library. I put dependencies in package.yaml, but parsec is only visible in Main.hs.

My package.yaml

dependencies:
- base >= 4.7 && < 5
- parsec
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints

library:
  source-dirs: src
  dependencies :
    - parsec

executables:
  haskell-lab4-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - haskell-lab4
    - parsec

tests:
  haskell-lab4-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - haskell-lab4

The structure of the project is as follows (it was created using stack new)

|-app
|  \- Main.hs
|
|-src
|  \-Parser
|   |    \-Myfile.hs <-- I need parsec in this file
|   | 
|   |-Lib.hs
|
|-test

Error:

Could not find module ‘Text.Parsec.String.Combinator’
    Perhaps you meant Text.Parsec.Combinator (from parsec-3.1.15.0)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.

CodePudding user response:

Are you sure you're not just using the wrong module name?

Read this part of the error message:

Could not find module ‘Text.Parsec.String.Combinator’
    Perhaps you meant Text.Parsec.Combinator (from parsec-3.1.15.0)

This indicates that it is seeing the parsec package, it just can't find the module name Text.Parsec.String.Combinator. If it wasn't seeing the package at all it couldn't offer you any suggestions from that package, and if it was "aware" of the package while also being told not to depend on it it would say something like "from hidden package parsec-3.1.15.0".

And indeed the hackage listing for that version of parsec doesn't show any modules named Text.Parsec.String.Combinator. There's Text.Parsec.String, but no modules under that prefix. And there's Text.Parsec.Combinator (as the error message suggests).

What's in your Main.hs that you say is working?

  • Related