How to include hackage packages into Leksah - haskell

I wanted to start playing with hExpat for Xml I/O with Haskell.
However I didn't manage to find where to express to Leksah that I want to import that package into my current module.
Could you tell me how to achieve this ?
EDIT: still searching. There is some uselful info with this Q&A but it is only about Data.Time Data.Directory.
In my case, it is a downloaded, unzipped, Hackage package.

The first thing is, that you have to install the package. Just run cabal and type:
cabal install YOUR PACKAGE NAME HERE
Than, open Leksah, open the project's dependencies (package -> edit package -> dependencies) and enter your new package. Hit "save" afterwards. Now you have to reconfigure the package and afterwards it should work.

Related

How to install an library in Haskell?

I try to use
Control.Monad.Extra.andM
import Control.Monad.Extra (andM)
but has an error:
Could not find module ‘Control.Monad.Extra’
Perhaps you meant
Control.Monad.Catch (needs flag -package-key exceptions-0.10.4)
Control.Monad.Error (needs flag -package-key mtl-2.2.2)
Control.Monad.Except (needs flag -package-key mtl-2.2.2)not found
This error does not make sense.
According to
https://cabal.readthedocs.io/en/3.6/installing-packages.html#installing-packages-from-hackage
3.2.1. Installing packages from Hackage
The cabal tool also can download, configure, build and install a Hackage package and all of its dependencies in a single step. To do this, run:
$ cabal install [PACKAGE...]
To browse the list of available packages, visit the Hackage web site.
Which says "in a single step", but in my experience this is too complicated and actually I have no idea how to install Control.Monad.Extra .
Usually, when a Haskeller want to install a specific library/package like this, how do you do this? There's no adequate documentations, it seems.
How to install an library in Haskell?
You don't. You should just depend on them, and then let Cabal worry about any installations that may need to be done. I.e., as you wrote
Go to the Hackage page and study which exact library to be used.
*.cabal file -> build-depends: extra >=1.7.10
This is the crucial step. Your own Cabal file is the way to both specify what libraries are needed right now, as well as ensuring that everything will still work in the future. This file needs to specify both the packages you require, as well as the modules you're defining yourself.
Probably you don't actually need >=1.7.10, but it can't hurt much to be specific there. Standard practice is to add both lower and upper bounds on the x.y level, i.e. you'd use extra >=1.7 && <1.8, and then push the upper boundary as new versions come out. Arguably this is a bit overcautious; if you only use some simple tools from a package that is unlikely to have breaking changes in the future then it may be less trouble to just leave the upper boundary out.
$ cabal install extra
You've already specified that extra is needed for your project, no need to state that again. Instead, simply build your own project now.$ cabal buildor, to get a GHCi prompt with your modules,$ cabal replor, if you have an executable with a main that you wish to execute$ cabal runor if you want to install that executable (and only then)$ cabal installCabal will then automatically figure out that extra-1.7.10 should be installed first.
(In old Cabal it would still have been necessary to run cabal install --dependencies-only first.)
Ok Self-Answered. Comment me if the information is wrong.
1. Go to the Hackage page and study which exact library to be used.
In this case, it seems extra-1.7.10.
2. *.cabal file -> build-depends: extra >=1.7.10 (among other libs)
3. $ cabal install extra
Error:
cabal: Cannot build the executables in the package extra because it does not contain any executables. Check the .cabal file for the package and make sure that it properly declares the components that you expect.
Well, I did for sure.
4. cabal install extra --lib
Finally works, but the documentation never said this.
Am I correct?
`
Is this the only method? or any other smarter way?

Cabal - add build dependency with Cabal instead of manually mangling with the file

Do I need to manually edit the *.cabal file's build-depends section to add package as a project dependency?
Or perhaps there is a more convenient way that is not as error prone as manually mangling with build files is.
Thinking about functionality that pretty much any package manager I used has, namely
apt install
npm i
nuget install
Install Package
and so on. Does such functionality exist in Cabal?
There is no better way at the moment. The answer #danidiaz gave is essentially correct -- cabal-edit will automatically update cabal files for you. The plan is to import similar functionality into cabal directly. This was remains blocked on an exactprinter that can parse and emit cabal files precisely -- and work on that exactprinter is now underway.

How to completly remove packages installed by cabal?

I am trying to learn cabal, and have tested several my own little projects, now I want to clean them up.
Basically, if I am working without a sandbox, my workflow is:
run cabal init
edit src/Mylib.hs, and then edit mylibname.cabal file
run cabal build
run cabal repl and test my code
run cabal install
Now, I see my own project:
installed into ~/.cabal/lib/x86-64-linux-ghc-7.10.1
registered in ~/.ghc/package.conf.d
I can write import Mylib in my other haskell source code, so I think the package is successfully installed.
Then I want to uninstall the package, as the package itself is just meaningless experiment code.
I read this article, who says that:
There is no "cabal uninstall" command. You can only unregister
packages with ghc-pkg:
ghc-pkg unregister
so I run
ghc-pkg unregister mylibname
Now, it seems that the package is unregistered in ~/ghc/package.conf.d, however, there is still a compiled library in ~/.cabal/lib/x86-64-linux-ghc-7.10.1.
So, how could I completly remove my project, could I just rm -rf the library in ~/.cabal?
You can delete the files yourself from the packages directory. However, the reason no command to do so is provided is there's in general no guarantee something may not have linked against them elsewhere, and so such deletions may cause breakages. That said, there's also a tool that goes and does the deletion for you if you really want it.
http://hackage.haskell.org/package/cabal-uninstall
And there's a tool with a bit more functionality that also lets you figure out what packages have no reverse deps, so at least no other packages break:
https://github.com/iquiw/cabal-delete

How are Hackage package names mapped to 'cabal install' names?

I'm using cabal to download Haskell packages.
The following works:
> cabal install JSON
It gets Text.JSON
However, this fails:
> cabal install Data.List.Key
cabal: "Data.List.Key" is not valid syntax for a package name or package
dependency.
What is the syntax problem here? How do I make cabal get Data.List.Key? In general, for a package of name X.Y, what name does cabal install need in order to find the package? (I'm confused why cabal install JSON gets Text.JSON, and not Foobarbaz.JSON)
The cabal install command uses package names. Package names are different from module names. If you look on the hackage page for the text package, you'll see that the package name is "text", but it exports a module called Data.Text (amongst others). Packages can export any number of modules and there does not have to be any relationship between the name of the package and the name of the modules it exports.
If you know a package you want, but you don't know the exported modules, look on the hackage page for that package. To do this, I go to "http://hackage.haskell.org/package/" in my browser. I've gone there so many times, it auto-completes very quickly, then I add the package name to the end of that url. If I don't know the exact package name, then I just go to that page and search the package list for what I want.
The converse situation where you know what module you want but don't know what package provides it is a little more difficult. In this case, I rely on the wonderful Haskell search engine Hoogle.(Another one Hayoo, has been offline for a while.)
Packages can include more than one module. There is no rule about how module names map to package names.
If you know the module name and want to find the package it is in, browse it's hackage documentation.
The url of the module description contains the package name after the package part, e.g. the url of Linear.Quaterion is
http://hackage.haskell.org/package/ linear-1.21.1 /docs/Linear-Quaternion.html
On that page the package name is also shown in the top left end.
If you know the package name, you can query which modules are included with
cabal info <package name>.

Could not find module `Control.Monad.Reader'

Today when I tried to compile my code in Geany I got this error
Could not find module `Control.Monad.Reader':
it was found in multiple packages: monads-fd-0.1.0.1 mtl-1.1.0.2
Compilation failed.
I get the same for the Writer monad; I thought I should remove one of those packages, but I do not want to break other packages, so now what should I do, yesterday everything worked without any problem.
It looks like you have recently installed monads-fd, perhaps as a dependency of something else you installed. Both monads-fd and mtl packages contain the module Control.Monad.Reader, so GHC doesn't know which one to use when you compile some code that imports it. You need to resolve the ambiguity somehow:
If you are using GHC or GHCi directly
either use a -hide-package <package> flag on the command line to hide one of the packages, or
hide the package by default using ghc-pkg hide <package>. You may need to use ghc-pkg --user hide <package> if the package was installed in your home directory (the default on some platforms).
You can use Cabal, and say exactly which one of the conflicting packages you depend on using the build-depends field in your .cabal file
I encountered a similar problem recently, and it was suggested that I run ghc-pkg hide {x} where '{x}' is the name of one of those packages. It worked in my situation.

Resources