Specifying package name for module-related commands in ghci - haskell

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!

Related

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.

GHCI can't load module if both package-conf and source path contains it

I encounter a strange situation in GHCI, don't know if anyone observed similar case. For some module, when I have it in the search path both by -package-conf, and also by -i, GHCI fails when I try to import the module with 'module is not loaded: FooModule'.
:module loads it fine however
or I can do :load FooModule, :m to clear the import list, and then import FooModule
or I can remove the path from -i and then it imports fine
Tracked this to be the otherwise -> modNotLoadedError m loc case in GHC, where otherwise ~ modulePackageId = this_pkg (the meaning of which I don't know).
This is not entirely systematic, there are some module which are both in package and source path, but can be imported.
GHC only knows about packages that are installed. To see which packages are installed, use the ghc-pkg list command:
$ ghc-pkg list
/usr/lib/ghc-6.12.1/package.conf.d:
Cabal-1.7.4
array-0.2.0.1
base-3.0.3.0
base-4.2.0.0
bin-package-db-0.0.0.0
binary-0.5.0.1
bytestring-0.9.1.4
containers-0.2.0.1
directory-1.0.0.2
(dph-base-0.4.0)
(dph-par-0.4.0)
(dph-prim-interface-0.4.0)
(dph-prim-par-0.4.0)
(dph-prim-seq-0.4.0)
(dph-seq-0.4.0)
extensible-exceptions-0.1.1.0
ffi-1.0
filepath-1.1.0.1
(ghc-6.12.1)
ghc-prim-0.1.0.0
haskeline-0.6.2
haskell98-1.0.1.0
hpc-0.5.0.2
integer-gmp-0.1.0.0
mtl-1.1.0.2
old-locale-1.0.0.1
old-time-1.0.0.1
pretty-1.0.1.0
process-1.0.1.1
random-1.0.0.1
rts-1.0
syb-0.1.0.0
template-haskell-2.4.0.0
terminfo-0.3.1
time-1.1.4
unix-2.3.1.0
utf8-string-0.3.4

GHCi not seeing my module

A made a module Timeit. I can't import it to GHCi.
Module:
module Timeit (timeit, timeCatch) where
import Data.Time.Clock
timeit::IO ()->IO (Float)
timeit io=do
(time,())<-timeCatch io
return time
timeCatch::IO (a)->IO (Float,a)
timeCatch io=do
start <-getCurrentTime
result <-io
end <-getCurrentTime
return $! (realToFrac (diffUTCTime end start), result)
test=do
putStrLn "What is your name?"
name <- getLine
putStrLn $ "Your name is "++name++"."
GHCi:
theking#ChrisLaptopUbuntu1304:~/Desktop/Haskell$ cd ~/Desktop/Haskell/; ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Timeit
<no location info>:
Could not find module `Timeit'
Perhaps you meant Time (needs flag -package haskell98-2.0.0.2)
I am able to import it into my other programs, just not GHCi.
Note: Am I a haskell noob.
In order for a module to be imported by GHCi, you have to make sure a few things are true.
First, are you in the same directory? By default GHCi will only search the current directory for modules.
Second, have you added the module header? Your code should start with
module Timeit where
...
Third, your file must actually be named Timeit.hs (with that capitalization).
By default Haskell inserts module Main where, which is a problem if your module isn't main.
Last but not least, GHCi seems to require that you use :l Timeit at least once. I'm not sure why this is, but once loaded you can remove it from scope with :m and then import it to your hearts content.
If you've done these things it should import just fine.
To import modules from ghci don't use import, rather say
:m +TimeIt

How to quit one level of imported package

In haskell interactive mode, ghci.
If I have imported one package, such as: import Data.List
I went to Prelude Data.List>
My question is, how to get back to "Prelude"
Thanks,
GHCi "meta" commands begin with :. One of them, :modules (:m for short) lets you modify the currently loaded modules. :m by itself will reset all of the loaded modules returning you to Prelude>.
You can also use :m +MODULE to "add" the module MODULE or :m -MODULE to remove the module MODULE.

How to configure GHCi to automatically import modules

When I use GHCi, I almost always end up importing Control.Applicative, Data.List, etc. . Is there a way to configure GHCi to automatically import those modules.
Also, after importing them, how do I keep the prompt from being insanely long?
Prelude Control.Applicative Data.List Database.HDBC Database.HDBC.Sqlite3 System.Directory>
GHCi looks for its configuration file at
~/.ghc/ghci.conf on Unix-like systems.
%APPDATA%\ghc\ghci.conf on Windows.
The configuration file syntax is simple: it's a list of GHCi commands to execute on startup.
For example, your ghci.conf could contain:
import Control.Applicative
import Data.Char
import Data.List
:set prompt "> "
The last line sets the prompt to "> " so it won't show all the modules you imported on the command line.
Now you can get to work right away:
GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
> toLower <$> "Hello, world!"
"hello, world!"
>
Also, if you decide you don't want Data.Char in the middle of a GHCi session, you can remove it with:
:m -Data.Char
and if you decide you don't want anything but Prelude during a session:
:m
GHC will also load any .ghci file it finds in the current directory. It's very useful to do per-project configuration of GHCi.
This is an example from a project I work on:
:set -isrc:dist/build/autogen
:set -hide-package mtl
The first is there to make sure that the modules generated by Cabal are easy to import. The second hides mtl since this particular project uses transformers.

Resources