Can not import Data.Stream in Haskell - haskell

Why can't I import Data.Stream in Haskell?
Prelude> import Data.Stream
<no location info>: error:
Could not find module ‘Data.Stream’
Perhaps you meant Data.String (from base-4.12.0.0)

The module Data.Stream is not part of the standard library. You need to install a package that provides it first, probably using Cabal or Stack.

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.

Error: "Failed to load interface for 'Data.Either.Utils'"

Does anyone know why this single, specific import statement is causing a problem? I'm using sandbox and cabal. My other imports work fine (Web.Scotty, Data.Text.Lazy, etc.). I'm running with "cabal exec runghc filename.hs". I don't have a cabal.config file, but I do have a cabal.sandbox.config file.
I'm attempting to use the forceEither function, which is in Data.Either.Utils. My import statements are normal as far as I can tell:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Control.Monad.Trans (liftIO)
import Data.Aeson (object, (.=))
import Network.HTTP.Types.Status
import Data.Text.Lazy
import Data.Text.Lazy.IO
import Data.Either.Utils
import Data.Monoid (mconcat)
Message:
filename.hs:8:1: error:
Failed to load interface for ‘Data.Either.Utils’
Use -v to see a list of the files searched for.
Running with -v shows:
Using a sandbox located at
/Users/myuser/Desktop/mydirectory/myotherdirectory/.cabal-sandbox
/usr/local/bin/ghc --print-global-package-db
/usr/local/bin/runghc filename.hs
The Data.Either.Utils module is not part of "base" Haskell; it's part of the MissingH package, and that package seems to be .. ha ha .. missing!
I'm not too familiar with Cabal sandboxes (I use Stack), but presumably you can run:
cabal install MissingH
in your sandbox, and you should be good to go.
If that doesn't work, just copy the code for forceEither from MissingH:
forceEither :: Show e => Either e a -> a
forceEither (Left x) = error (show x)
forceEither (Right x) = x

Unable to import lens library

I installed haskell lens library. In ghci I try to run
λ> import Control.Lens
and it says -
<no location info>:
Could not find module ‘Control.Lens’
Perhaps you meant Control.Seq (from parallel-3.2.0.4)
I ran ghc-pkg list to list the installed packages and it does show -
lens-4.6.0.1
I am using GHC 7.8.3 on Ubuntu 13.04.

How can I use module Control.Monad.Free in Haskell?

I try code in Haskell. I want to use module Control.Monad.Free. I written import Control.Monad.Free in my test.hs. But I have error:
How can I fix this error?
Control.Monad.Free is defined in the free package. You can install it with Cabal:
cabal install free

Could not find module, it is a member of the hidden package haskell98

When I try to compile a simple source file with import IO or import Random, the build fails with an error message like this:
Could not find module 'IO'
It is a member of the hidden package 'haskell98-2.0.0.1'
Use -v to see a list of the files searched for
The module names changed at some point. You probably want import System.IO and import System.Random instead.
Here is the module hierarchy for the standard libraries in GHC 7.6.1.

Resources