I have decided to use my own Prelude for a larger project (containing some libraries and some executables). The Prelude doesn't export some partial functions and exports some common functions (i.e. from Control.Monad etc.). However, I am fighting with the way how to do it. I have tried:
use base-noprelude. Create Prelude.hs in module my-common-module.
Same as above, but in the my-common-module create My.Prelude instead. In every other module create a directory 'prelude', put it into hs-source-dirs cabal section, create a file prelude/Prelude.hs with import My.Prelude
The problem is that in 1) I cannot just run ghci, as I get conflicting base and my-common-module. In 2) ghci works, cabal repl somehow doesn't as it fails mysteriously with 'attempting to use module ‘Prelude’ (prelude/Prelude.hs) which is not loaded'. Additionally, base-noprelude doesn't seem to like ghcjs, which I want to use for part of the project (code sharing).
It seems to me the only way currently is to start each and every file with:
import Prelude ()
import My.Prelude
or
{-# LANGUAGE NoImplicitPrelude #-} -- or extensions: NoImplicitPrelude in .cabal
...
import My.Prelude
The 'extensions: NoImplicitPrelude' option seems to me best as it requires every file to import My.Prelude otherwise it won't work. Am I missing some obvious way that would achieve custom Prelude and at the same time work with cabal repl and ghcjs?
Update: base-noprelude works with GHCJS when I manually remove the reexport of GHC.Event.
Update: Ok, I spent some time with this and I should have spent more. It seems to me that 1) is the right way to go. cabal repl works (thanks Yuras), ghci must be loaded with ghci -hide-package base and works too.
I ended up with this setup that seems to work:
Create a special package my-prelude. This package exports the Prelude, can contain other modules, it can depend on base. You may need to use {-# LANGUAGE NoImplicitPrelude #-} in some modules to avoid circular dependencies. E.g. you may want to have some orphan instances defined and exported by your custom Prelude in separate files (e.g. Orphans.Lib_aeson), these files need the NoImplicitPrelude.
In your main project, libraries etc. change the dependencies in cabal from base to base-noprelude, my-prelude.
What works:
cabal repl
ghci/runghc works, but you have to start it with ghci -hide-package base; otherwise there will be conflict between base and my-prelude
What does not work:
cabal repl in the my-prelude package.
Related
Trying to import CI as described in the docs but getting an error:
Module ‘Data.CaseInsensitive’ does not export ‘CI’
https://hackage.haskell.org/package/case-insensitive-1.2.1.0/docs/Data-CaseInsensitive.html
What is going on?
As your answer indicates there was a collision between the case-insensitive package, and a custom package.
You can make use of package-qualified imports by enable the PackageImports language pragma, and then specify the name of the package as a string literal, so:
{-# LANGUAGE PackageImports #-}
import "case-insensitive" Data.CaseInsensitive (CI)
import qualified "case-insensitive" Data.CaseInsensitive as CI
This thus allows working with two or more packages that export a module with the same name and thus avoid clashes. It will also give more helpful message if it turns out you did not expose that package (in your .cabal file for example).
Embarrassed to admit this but I was using another library which doesn't expose CI from its module but has the same module name. Thanks everyone for your valuable time.
As stated in the title. There is a codebase, where I have seen the following syntax
import "cryptonite" Crypto.Hash
(Context, Digest, SHA256, hash, hashFinalize, hashInit,
hashUpdate)
This syntax doesn't seem to be mentioned on haskell wiki on imports.
What does the "cryptonite" string do here?
Where does this syntax come from?
Is is part of Haskell2010 and if it is so where in the language report is it mentioned?
This is extra syntax that is supported when using the PackageImports extension:
With the PackageImports extension, GHC allows import declarations to
be qualified by the package name that the module is intended to be
imported from. For example:
import "network" Network.Socket
would import the module Network.Socket
from the package network (any version). This may be used to
disambiguate an import when the same module is available from multiple
packages, or is present in both the current package being built and an
external package.
The special package name this can be used to refer to the current
package being built.
It occasionally happens that two packages export a module with the same name. For example both hashmap and unordered-containers export Data.HashSet. If both packages are installed, we want to disambiguate between the different packages. With this type of import, the author thus specifies that the Crypto.Hash module of the cryptonite needs to be used.
This is to to the best of my knowledge not standard Haskell (in the sense that other Haskell compilers do not have to support this, it seems not specified in the import statement section of the Haskell 2010 report), but a Glasgow Haskell compiler extension. Of course other compilers can support this as well, but a compiler without this extension, can still rightfully call itself a "Haskell compiler". In order to activate this extension, you need to compile with the -XPackageImports extension:
ghc -XPackageImports main.hs
This is a dynamic flag, and thus can be specified in the "pragma" of a Haskell source file as well.
I am under the impression that until recently cabal repl automatically imported all the imports from the currently loaded file. That is if I had a file src/Main.hs:
module Main where
import qualified Data.Vector as V
main = putStrLn "foo"
And I would load it in cabal repl with :l src/Main.hs, V would be available without any further work.
I believe this behavior changed during the change to ghc/ghci 7.10, but I am not sure. Is there any option that would let me change that back to the way it was before?
edit: As it happens I have actually compiled the modules, which was part of the problem. See Ørjan Johansens comment below.
From the GHC User's Guide:
The syntax in the prompt *module indicates that it is the full top-level scope of module that is contributing to the scope for expressions typed at the prompt. Without the *, just the exports of the module are visible.
NOTE: for technical reasons, GHCi can only support the *-form for modules that are interpreted. Compiled modules and package modules can only contribute their exports to the current scope. To ensure that GHCi loads the interpreted version of a module, add the * when loading the module, e.g. :load *M.
In other words, add a * before the module name when loading.
I was fairly sure that a while back GHC added the ability to explicitly set the character encoding on a Handle. However, when I look in System.IO, I don't see anything relating to character encodings. (I have Haskell Platform 2012.4.0.0)
Am I blind, or simply mistaken?
I investigated where the function is hiding.
Summary: Make sure to use System.IO from package base, not from package haskell2010.
Details: Hoogle tells me that there is System.IO.hSetEncoding in the latest base package.
http://www.haskell.org/hoogle/?hoogle=hSetEncoding
Checking the documentation about the Haskell platform 2012.4.0.0, I see a System.IO module from the haskell2010 package. And that module doesn't seem to contain hSetEncoding.
http://lambda.haskell.org/platform/doc/2012.4.0.0/ghc-doc/libraries/haskell2010-1.1.0.1/System-IO.html
But do not despair, there seems to also be the System.IO from base which contains hSetEncoding.
http://lambda.haskell.org/platform/doc/2012.4.0.0/ghc-doc/libraries/base-4.5.1.0/System-IO.html#v:hSetEncoding
So I guess you just have to make sure that you use the System.IO from base and not from haskell2010.
Oh my God!
OK, I just figured this out.
It appears that there are two packages that both export System.IO - the base package and the haskell2010 package.
The two versions of the module are different. Specifically, only the module from base has all the character encoding stuff in it.
When you go to the locally-installed module index, it only shows you the version of System.IO that's included in haskell2010 - without all the character encoding stuff.
It appears the only way to see the version from base is to click on some other module exported from base, then click "Contents", then navigate to System.IO from there. Then it shows you the correct module!
Counter-intuitive, much? o_O
OK, so I've found my function now, but man, Haddock should probably do a better job of handling this obscure edge-case...
Does it is possible to use the equivalent of "import qualified" in GHCI with our OWN module? Something like :m + qualified Data.List which of course doesn't work.
Thanks.
Just type import qualified Data.Text or import qualified Data.Text as T inside ghci, just as you would do inside your code.
I don't know of a pretty way to do what you want, but you could fake it with something like this:
First, use :load My/Module.hs to load your module.
Use :module - My.Module to bring it out of scope.
(Optional.) Use import qualified My.Module as MM to bring it into scope qualified.
Every module that GHCi knows about is automatically available fully qualified, so after step 2, My.Module.value will work fine. Step 3 is only necessary if you want to use a shorter prefix.
If you want to load multiple files at once, :load can do that too;
:load My/Module1.hs My/Module2.hs ... My/ModuleN.hs
will work fine. It will put you in the scope of *My.Module1, and then My.Module2 … My.ModuleN will all be available fully qualified as mentioned above.
For more about GHCi, you can always check the GHC User's Guide, Ch. 2: "Using GHCi"; particularly relevant sections are §2.2, "Loading source files" and §2.4.5, "What's really in scope at the prompt?".