Is there a ghci colon-command to hide a package? - haskell

At present I'm using ihaskell to learn about some libraries. IHaskell is still hazardous, and I can not do:
-- This doesn't work: ":ext PackageImports", the kernel hangs
-- This doesn't work: "{-# LANGUAGE PackageImports #-}", the kernel hangs
import Codec.Crypto.RSA.Pure
import qualified "crypto-api" Crypto.Random as CR
import Control.Monad.CryptoRandom
Without "PackageImports", I get an error message:
Ambiguous interface for ‘Crypto.Random’: it was found in multiple packages: crypto-api-0.13.2 cryptonite-0.7
My question is, is there a colon command inside ghci that would allow me to hide the cryptonite package?

Yes.
:set -hide-package cryptonite

Related

Haskell Language Server in VS code shows 'could not find module' Error

I have the following imports in my file
import Control.Applicative (Applicative(liftA2))
import Control.Monad ( guard )
import qualified Data.Vector as V
and I get the error that reads could not find module 'Data.Vector'
The module is installed. I have vector in dependencies and the program builds fine. I just can't get rid of the error in VS Code.
What extra installations do I have to do to fix this?
I have tried:
cabal install vector --lib
Have you tried?
cabal install vector --lib

Finding explicit imports for ghc's -Wmissing-import-lists

GHC generates warnings when there are implicit imports (see below). The goal is to replace these with explicit imports. Is there an automated way to generated the list (instead of manually finding it in code)?
/Users/srid/code/slownews/backend/src/SlowNews/Main.hs:10:1: warning: [-Wmissing-import-lists]
The module ‘Control.Exception’ does not have an explicit import list
|
10 | import Control.Exception
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/Users/srid/code/slownews/backend/src/SlowNews/Main.hs:13:1: warning: [-Wmissing-import-lists]
The module ‘Control.Monad.IO.Class’ does not have an explicit import list
|
13 | import Control.Monad.IO.Class
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
GHC has a -ddump-minimal-imports flag which will do the trick.
There's an open pull request un importify tool which I'm working on. After this is done you will be able to convert implicit imports to explicit automatically:
https://github.com/serokell/importify/pull/82
The Haskell Tools project has some nice looking tooling for that. Apparently it works in Atom but I couldn't get it to work with atom or atom-beta on macOS. However, it does work at the command line. I have a simple example stack project set up. The Main.hs uses an explicit import list:
module Main where
import System.Environment
doMain = print =<< getEnvironment
main = doMain
BTW, I installed the Haskell Tools with:
$ stack install haskell-tools-daemon haskell-tools-cli fswatch
Then execute:
$ ht-refact -e 'ProjectOrganizeImports' .
Now git tells me that I have the following diff:
-import System.Environment
+import System.Environment (getEnvironment)
Check out the Haskell Tools website for an interactive example of how it should work in an editor. Looks like a very promising set of refactoring tools.

How do I use PackageImports properly in ghci?

I want to do testing in ghci and I'm running into problems with similarly named modules in different packages (this is basically a continuation of my previous question: What should I do if two modules share the same name?)
The packages in question are crypto-api, cryptonite, and DRBG. They all provides modules involving Crypto.Random. I'm using PackageImports in ghci to work fix this issue.
However, it seems that the order of using PackageImports is important.
This works (in ghci):
:set -XPackageImports
import Crypto.Random.DRBG
import "crypto-api" Crypto.Random
This does not work:
:set -XPackageImports
import "crypto-api" Crypto.Random
import Crypto.Random.DRBG -- Error!
Also this does not work:
:set -XPackageImports
import "crypto-api" Crypto.Random
import "DRBG" Crypto.Random.DRBG -- Error!
The error that appears is:
<interactive>:1:1: error:
Ambiguous interface for ‘Crypto.Random’:
it was found in multiple packages:
crypto-api-0.13.2 cryptonite-0.23
Am I doing something wrong or is this a bug?
Perhaps the easiest way is to avoid PackageImports and instead rename the packages when starting ghci, using the -package option:
ghci -package "crypto-api (Crypto.Random as A)" -package "cryptonite (Crypto.Random as B)"
Once in ghci, you could import the renamed modules:
Prelude> import A
Prelude A> import B
Prelude A B>
The syntax -package "crypto-api (Crypto.Random as A)" makes only the Crypto.Random module available to ghci, but not the other modules in the package.
According to the Thinning and renaming modules section of the GHC user guide:
We also support renaming of modules, in case you need to refer to both
modules simultaneously; this is supporting by writing OldModName as
NewModName, e.g. -package "base (Data.Bool as Bool). You can also
write -package "base with (Data.Bool as Bool) to include all of the
original bindings (e.g. the renaming is strictly additive).
So perhaps it's better to write the options like -package "crypto-api with (Crypto.Random as A)" to maintain access to all the modules.
Using PackageImports instead of thinning and renaming modules has the problem that it makes the source code dependent on the precise packages in which imported modules live. If a module changes packages that breaks the program.
Why would you expect that to work at all?
:set -XPackageImports
import "crypto-api" Crypto.Random
import Crypto.Random.DRBG -- Error!
import "DRBG" Crypto.Random.DRBG -- Error!
You have imported the DRBG module twice and from the same package. From your question I thought you'd want to get the Crypto.Random modules from crypto-api and cryptonite but I don't see any attempt to use cryptonite at all here.
Just so we're clear, I can reproduce the error I think you were getting via GHCi:
Prelude> :set -XPackageImports
Prelude> import "crypto-api" Crypto.Random as OriginalRandom
Prelude OriginalRandom> import "cryptonite" Crypto.Random as ConflictRandom
<interactive>:1:1: error:
Ambiguous module name ‘Crypto.Random’:
it was found in multiple packages:
crypto-api-0.13.2 cryptonite-0.24
But it all works when you load it from a file so this is just a bug and can be avoided:
% cat t.hs
{-# LANGUAGE PackageImports #-}
import "crypto-api" Crypto.Random as OriginalRandom
import "cryptonite" Crypto.Random as ConflictRandom
import Crypto.Random.DRBG -- This only appears in one package
% ghci t.hs
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /Users/tommd/.ghci
[1 of 1] Compiling Main ( t.hs, interpreted )
Ok, 1 module loaded.
*Main>
Notice that DRBG is not part of this discussion. It doesn't have any conflicting modules names with anything on hackage as far as I know.

Haskell Import MissingH

I'm trying to import the Data.String.Utils module in my Haskell code, this is part of the MissingH package. I've installed cabal and used it to acquire MissingH, which is properly installed. However, wehn I try adding
import Data.String.Utils
to my code, WinHugs returns
ERROR file:.\ex.hs - Can't find imported module "Data.String.Utils"
Any feedback?
You already have MissingH installed with Cabal.
Using Cabal packages with WinHugs is hit and miss.
Use ghci and import Data.String.Utils should work.

Specifying package name for module-related commands in ghci

Is there a way to specify the package name for a module for the :browse, :load or :module commands in ghci (version 6.12.1) ?
Some module names are ambiguous:
Prelude> :module Control.Monad.Cont
<no location info>:
Ambiguous module name `Control.Monad.Cont':
it was found in multiple packages: mtl-1.1.0.2 monads-fd-0.1.0.2
Is setting the -hide-package option the only thing I can do to avoid the ambiguity?
As far as I know, yes. But it doesn't have to be a big deal, you can do this inside ghci:
Prelude Data.List> :set -hide-package mtl
package flags have changed, resetting and loading new packages...
Prelude> import Control.Monad.Cont
Prelude Control.Monad.Cont>
There was also a line-item on GHC-7 change log that made me think package imports would work on the command line, but it doesn't seem to yet (see below). The change comment said something like "full import syntax supported in GHCi", which must exclude extensions I suppose.
$ ghci-7.0.0.20100924 -XPackageImports
GHCi, version 7.0.0.20100924: http://www.haskell.org/ghc/ :? for help
...
Prelude Data.List> import "mtl" Control.Monad.Cont
<no location info>:
Ambiguous module name `Control.Monad.Cont':
it was found in multiple packages: mtl-1.1.1.0 monads-fd-0.1.0.2
Is setting the -hide-package option the only thing I can do to avoid the ambiguity?
You can use ghc-pkg, e.g.
$ ghc-pkg hide monads-fd
This is like setting -hide-package on every subsequent ghc invocation. Packages explicitly depending on monads-fd via Cabal will not be affected, but everything else is. Watch out!

Resources