cabal doesn't seem to register installed packages [duplicate] - haskell

I'm a Haskell newcomer and have Haskell installed on my (Mac) machine; I'm trying to use newsynth (https://www.mathstat.dal.ca/~selinger/newsynth/, http://hackage.haskell.org/package/newsynth). In my terminal in the same place where I installed Haskell (home directory) I ran the command cabal install newsynth as suggested by the package authors. However, I can't figure out how to actually access anything from the package from the command line, let alone within a particular file.
In GHCi Prelude, I tried running commands of the form import Quantum and import Quantum.Synthesis.Diophantine but always get an error message. (e.g. in contrast, import Data.Complex works just fine.)
(I'm sure I'm missing something pretty obvious, but I only began with Haskell on Monday, and need to spin up some newsynth code by next week, which is why I'm not starting from the ground up.) Any advice on (1) how to run newsynth's functions from GHCi and (2) how to incorporate them into .hs files would be greatly appreciated. Thanks!
Edit: cabal --version returns
cabal-install version 3.2.0.0 (newline) compiled using version 3.2.0.0 of the Cabal library

Quoting a comment:
[cabal --version] returns: cabal-install version 3.2.0.0 (newline) compiled using version 3.2.0.0 of the Cabal library
It seems the installation instructions in the project page you linked to haven't been updated for cabal-install 3+ yet (in fairness, cabal-install 3 is relatively recent). In any case:
If all you want is running ghci and trying those modules out, with no strings attached, use cabal install --lib newsynth. That will make the newsynth package available in GHC's global environment (see the cabal install entry in the Cabal User Guide for further information).
Since you ultimately want to use the package in the code you'll have to write, though, my recommendation is using cabal init to create a new project for your code. Then, edit the .cabal file of the project to add newsynth to its build-depends section, and that's it: the package will be installed (if it isn't already) and made available in the context of your project the next time you do a cabal build to build the project, or a cabal repl to run GHCi in the context of your project. In that case, there is no need to use the cabal install command at all.

Related

Update Intero flycheck after changing cabal file

I am using Intero under emacs to edit my new Haskell project. I added an import to a third-party library to my code to see if Intero would automatically add the necessary dependency, but it didn't. So I edited the .cabal file manually to add the necessary dependency. Now what do I do - short of restarting emacs?
I've tried running cabal install --dependencies-only; cabal configure at the command line and they ran successfully, but the flycheck buffer still shows an error.
All that is necessary is to run
M-x intero-restart
in emacs.
Intero uses stack which has its own private sandbox for each package you are developing, so cabal install --dependencies-only isn't needed or useful.

Cabal-installed Modules won't import

There are several things I'm confused about here, so I'll try to explain each of them as clearly as I can.
I've been trying to install the diagrams package for haskell, using cabal. I've seen it suggested to install packages using sandboxes, so that's what I did. Something that's not clear to me is exactly what a sandbox is - I understand that I can initialise one with cabal sandbox init and install packages inside of it with cabal install, but I don't see how to use those packages once they're installed.
I then tried to compile a test-script using ghc, which resulted in the following error:
diagramstutorial.lhs:3:10:
Could not find module 'Diagrams.Prelude'
Use -v to see a list of the files searched for.
With a similar error for another module that the script was supposed to load. These modules are definitely both included in the diagrams package, and cabal seems happy that the package is installed correctly. I expect there's something simple I just don't understand, but I don't know what it is.
I typed ghc --make diagramstutorial.lhs to compile it
That will make GHC use the regular user package database (that is, not the sandbox one). Use cabal exec -- ghc --make diagramstutorial.lhs instead, so that GHC runs in the context of your sandbox.
You can also use GHCi within the sandbox with cabal repl. And naturally, if/when you start preparing a cabal package, all cabal commands (cabal build, etc.) will use the sandbox if you are within its directory.
Something that's not clear to me is exactly what a sandbox is
A set of packages with an accompanying database local to the directory. Beyond the cabal.sandbox.config configuration file there is also a hidden directory .cabal-sandbox, in which diagrams and the other packages you installed lie.
Find the sandbox directory, and locate the packages.conf.d file.
For example, /home/user/.cabal-sandbox/x86_64-linux-ghc-7.8.4-packages.conf.d
Rerun your GHC commands with the package-db flag:
ghci -package-db /home/user/.cabal-sandbox/x86_64-linux-ghc-7.8.4-packages.conf.d --make diagramstutorial.lhs
Everything should now work

Possible to ensure that profiling libraries are installed when installing GHC 7.8?

I'm going to install GHC on a fresh copy of Ubuntu and I'm wondering: How can I ensure that profiling libraries are installed for the core libraries (e.g., text, unordered-containers)?
I'm aware of the changing the profiling option in cabal's .config file but my understanding is that this only ensures that profiling libraries are installed for those packages that I install AFTER setting up cabal (see italicized text in update below).
(I inadvertently blew up my Ubuntu vbox last night as a result of trying to retroactively install profiling libraries for installed GHC packages. It led to the existing packages not working, which led to me trying to uninstall GHC, which led to...KABOOM!)
UPDATE:
I've installed GHC and am now at the point where I'm about to install cabal. I've confirmed my suspicion that I'm facing a "chicken-and-an-egg" dilemma: In order to get the initial cabal config file (in which I can set profiling option as True), I need to install cabal. However, installing cabal results in the installation of core packages (e.g., text, unordered-containers) BEFORE I get a chance to make the change in the cabal config file.
SOLVED:
As per Daniel Wagner's suggestion (thanks!), I made a couple of modifications to the bootstrap.sh script file (I unfortunately didn't have my old cabal or I would've followed his other suggestion). As reference for future readers, the beginning of my bootstrap.sh file looked like this (after the changes):
#VERBOSE
DEFAULT_CONFIGURE_OPTS="--enable-library-profiling --enable-shared"
EXTRA_CONFIGURE_OPTS=${EXTRA_CONFIGURE_OPTS-$DEFAULT_CONFIGURE_OPTS}
#EXTRA_CONFIGURE_OPTS
#EXTRA_BUILD_OPTS
#EXTRA_INSTALL_OPTS
The preferred solution is to install cabal-install via your package manager. If you have an old version of cabal-install in your package manager, you can then use the old version to install the new version with a config in place, or even specify profiling directly on the command line via cabal install cabal-install --enable-library-profiling.
An alternate solution if you plan to install cabal-install via its bootstrap.sh script is to use the environment variables it provides for configuration. There are four, notated at the top of bootstrap.sh; the relevant one is EXTRA_CONFIGURE_OPTS, which contents get passed to the configure step of each package's Setup command. So something like this ought to do the trick (though I haven't tested it):
EXTRA_CONFIGURE_OPTS=--enable-library-profiling ./bootstrap.sh

NixOS and ghc-mod - Module not found

I'm experimenting a problem with the interaction between the ghc-mod plugin in emacs, and NixOS 14.04. Basically, once packages are installed via nix-env -i, they are visible from ghc and ghci, recognised by haskell-mode, but not found by ghc-mod.
To avoid information duplication, you can find all details, and the exact replication of the problem in a VM, in the bug ticket https://github.com/kazu-yamamoto/ghc-mod/issues/269
The current, default, package management set up for Haskell on NixOS does work will with packages that use the ghc-api, or similar (ghc-mod, hint, plugins, hell, ...) run time resources. It takes a little more work to create a Nix expression that integrates them well into the rest of the environment. It is called making a wrapper expression for the package, for an example look at how GHC is installed an operates on NixOS.
It is reasonable that this is difficult since you are trying to make a install procedure that is atomic, but interacts with an unknown number of other system packages with their own atomic installs and updates. It is doable, but there is a quicker work around.
Look at this example on the install page on the wiki. Instead of trying to create a ghc-mod package that works atomically you weld it on to ghc so ghc+ghc-mod is an atomic update.
I installed ghc+ghc-mod with the below install script added to my ~/.nixpkgs/nixpkgs.nix file.
hsEnv = haskellPackages.ghcWithPackages (self : [
self.ghc
self.ghcMod
# add more packages here
]);
Install package with something like:
nix-env -i hsEnv
or better most of the time:
nix-env -iA nixpkgs.haskellPackages.hsEnv
I have an alias for the above so I do not have to type it out every time. It is just:
nixh hsEnv
The down side of this method is that other Haskell packages installed with nix-env -i[A] will not work with the above installation. If I wanted to get everything working with the lens package then I would have to alter the install script to include lens like:
hsEnv = haskellPackages.ghcWithPackages (self : [
self.ghc
self.ghcMod
self.lens
# add more packages here
]);
and re-install. Nix does not seem to use a different installation for lens or ghc-mod in hsEnv and with the ghc from nix-env -i ghc so apparently only a little more needs to happen behind the scenes most of the time to combine existing packages in the above fashion.
ghc-mod installed fine with the above script but I have not tested out its integration with Emacs as of yet.
Additional notes added to the github thread
DanielG:
I'm having a bit of trouble working with this environment, I can't even get cabal install to behave properly :/ I'm just getting lots of errors like:
With Nix and NixOS you pretty much never use Cabal to install at the global level
Make sure to use sandboxes, if you are going to use cabal-install. You probably do not need it but its there and it works.
Use ghcWithPackages when installing packages like ghc-mod, hint, or anything needs heavy runtime awareness of existing package (They are hard to make atomic and ghcWithPackages gets around this for GHC).
If you are developing install the standard suite of posix tools with nix-env -i stdenv. NixOS does not force you to have your command line and PATH cultured with tools you do not necessarily need.
cabal assumes the existence a few standard tools such as ar, patch(I think), and a few others as well if memory services me right.
If you use the standard install method and/or ghcWithPackages when needed then NixOS will dedup, on a package level (If you plot a dependency tree they will point to the same package in /nix/store, nix-store --optimise can always dedup the store at a file level.), many packages automatically unlike cabal sandboxes.
Response to comment
[carlo#nixos:~]$ nix-env -iA nixos.pkgs.hsEnv
installing `haskell-env-ghc-7.6.3'
these derivations will be built:
/nix/store/39dn9h2gnp1pyv2zwwcq3bvck2ydyg28-haskell-env-ghc-7.6.3.drv
building path(s) `/nix/store/minf4s4libap8i02yhci83b54fvi1l2r-haskell-env-ghc-7.6.3'
building /nix/store/minf4s4libap8i02yhci83b54fvi1l2r-haskell-env-ghc-7.6.3
collision between `/nix/store/1jp3vsjcl8ydiy92lzyjclwr943vh5lx-ghc-7.6.3/bin/haddock' and `/nix/store/2dfv2pd0i5kcbbc3hb0ywdbik925c8p9-haskell-haddock-ghc7.6.3-2.13.2/bin/haddock' at /nix/store/9z6d76pz8rr7gci2n3igh5dqi7ac5xqj-builder.pl line 72.
builder for `/nix/store/39dn9h2gnp1pyv2zwwcq3bvck2ydyg28-haskell-env-ghc-7.6.3.drv' failed with exit code 2
error: build of `/nix/store/39dn9h2gnp1pyv2zwwcq3bvck2ydyg28-haskell-env-ghc-7.6.3.drv' failed
It is the line that starts with collision that tells you what is going wrong:
collision between `/nix/store/1jp3vsjcl8ydiy92lzyjclwr943vh5lx-ghc-7.6.3/bin/haddock' and `/nix/store/2dfv2pd0i5kcbbc3hb0ywdbik925c8p9-haskell-haddock-ghc7.6.3-2.13.2/bin/haddock' at /nix/store/9z6d76pz8rr7gci2n3igh5dqi7ac5xqj-builder.pl line 72.
It is a conflict between two different haddocks. Switch to a new profile and try again. Since this is a welding together ghc+packages it should not be installed in a profile with other Haskell packages. That does not stop you from running binaries and interrupters from both packages at once, they just need to be in their own name space so when you call haddock, cabal, ghc, there is only one choice per profile.
If you are not familiar with profiles yet you can use:
nix-env -S /nix/var/nix/profiles/per-user/<user>/<New profile name>
The default profile is either default or channels do not which one it will be for your set up. But check for it so you can switch back to it later. There are some tricks so that you do not have to use the /nix/var/nix/profiles/ directory to store you profiles to cut down on typing but that is the default location.

Help with running the Yesod Development server?

I'm currently trying out web development frameworks for haskell and I recently came across yesod. It seemed pretty interesting so I installed it using cabal, however I'm not able to run the development server. Following their getting started instructions here's the result:
$ yesod init
$ cd mysite
$ yesod devel
Configuring mysite-0.0.0...
Testing files...
Rebuilding app
yesod: bind: resource busy (Address already in use)
Preprocessing library mysite-0.0.0...
Preprocessing executables for mysite-0.0.0...
Building mysite-0.0.0...
Controller.hs:16:7:
Could not find module `Data.Dynamic':
It is a member of the hidden package `base'.
Perhaps you need to add `base' to the build-depends in your .cabal file.
It is a member of the hidden package `base-3.0.3.2'.
Perhaps you need to add `base' to the build-depends in your .cabal file.
Use -v to see a list of the files searched for.
Testing files...
Testing files...
^^ above line just keeps repeating...
I'm assuming it has something to do with the Data.Dynamic module but I don't know how to go about fixing it.
Additional Info
Running Ubuntu 10.10 Maverick
ghc version:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.1
I haven't run into this specific issue, but the error message looks like it's a simple question of GHC being unable to find version 3.0.3.2 the package "base." This version has been buildable since GHC 6.9, so you should have it. Try running the following command:
ghc-pkg check
This will tell you if there is something wrong with your packages. Cabal can be a bit of a nightmare for dependencies -- partly, it seems, because a lot of Haskell developers underestimate the extent to which their underlying libraries will shift in the future. So they will define a dependency as ">= [version of package x]" without limiting the max version to the one presently available. Or they just leave out version-limiting altogether.
Yesod, I'm happy to say, doesn't fall into this trap. But several of the libraries it depends on do. When you start developing in Haskell, learn this lesson: never assume that future versions of a library won't break your code. They will. A lot.
If ghc-pkg comes up with broken packages, you may need to clean up/uninstall/reinstall these packages until they are either cleaned up or hidden. (Just do ghc-pkg hide [package name] to tell ghc to ignore that package.
Your next problem is that hidden base package. Try the following:
ghc-pkg list | grep base
If you see brackets around the library, that means it's hidden. The package base-3.0.3.2 might show up as hidden (although that's a bit unlikely, as that's where the backward-compatible Prelude lives). If it is hidden, try to unhide it with the following command:
ghc-pkg expose base-3.0.3.2
Now try re-running yesod devel and see how it goes. Best case scenario is that it works. If not, let us know.
According to the Yesod in Five Minutes guide, you appear to be missing the a call to the command "cabal install" between your "cd mysite" and "yesod devel". It may need to install further packages based on what your responses were during "yesod init", such as which database you want to use.
Also, you may want to check that the port is not currently being used, as you have the "Address already in use" shown in your transcript.
First, it would be a good idea to resolve any broken packages reported by ghc-pkg check, by removing/reinstalling/upgrading them.
Next, can you manually cabal build the mysite app without trouble ? If your mysite.cabal actually does not specify base in the build-depends list, you should follow the suggestion to add that.
The repeating Testing Files message is normal for current yesod devel, it is polling your source files.

Resources