haskell packages: base versus haskel98 - haskell

I installed haskell-platform on my Ubuntu system. Now I need to run
ghci -package haskell98 -hide-package base
in order to try out simple things like:
map Char.toUpper "Hello World"
Is there a way to make this the default?

Well, you can just put those options in your ~/.ghci file:
:set -hide-package base
:set -package haskell98
But, I really recommend you don't do this. Just get used to the modern libraries.
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> map Data.Char.toUpper "Hello"
"HELLO"
Or
Prelude> :m +Data.Char
Prelude Data.Char> toUpper <$> "Hello"
"HELLO"

Related

Cant load Prelude in ghci interpreter in VS code

I have installed Haskell platform following the instructions on chocolatey and haskell.org.
I am using Windows 10.
My hello.hs program complies in command prompt, but when I try to do the same in VS code,
it won't load Prelude, which I assume is necessary for running Haskell programs.
I think it might be a configuration problem, but I cant find any useful documentation on it.
How could I fix this and turn on Prelude?
Are the problems that VS code shows relevant to this problem?
Nothing looks wrong in your screenshots. The prompt text is ghci> rather than Prelude> because GHCi no longer defaults to showing the loaded modules on the prompt (see the GHC 9.0.1 changelog. Prelude is being loaded regardless. The warning shown in the IDE is a style suggestion that is inconsequential for the purpose of getting your code to run. As chi suggests, :l hello.hs at the GHCi prompt should be enough to have it loaded.
If I type main into the console, then it works
ghci> main
"haskell"
name
"hi, name"
ghci>
This might work
$ ghci
ghci> import Prelude
Prelude> printStrLn "Hello World!"
Hello World!
Prelude>
You may need to just run import Prelude.

Haskell: Where to find debug trace output on Windows10

I am using trace for debugging on Windows 10, and the terminal states that Logs printed to console, but I have no idea where to find this output.
As far as I can tell, console in Windows means the standard output stream. You can easily see trance output if you're running GHCi in the Windows Command Prompt, as this session demonstrates:
C:\Users\mark>stack ghci
Configuring GHCi with the following packages:
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from C:\Users\mark\AppData\Local\Temp\ghci7560\ghci-script
Prelude> :m +Debug.Trace
Prelude Debug.Trace> foo x = trace "foo" x
Prelude Debug.Trace> foo 42
foo
42
Here, I'm using Stack to run Haskell, but IIRC, you can use the Haskell Platform for Windows in the same way.

Why does a Haskell script using putStrLn hang?

On my Windows 7 Home Premium box, why does the following Haskell program hang?
main = do
putStrLn "Hello, World"
The script is compiled (using GHC) like this:
C:\>ghc --make my_script
[1 of 1] Compiling Main ( my_script.hs, my_script.o )
Linking my_script.exe ...
The program is then executed like this:
C:\>my_script.exe
Even after several minutes, there is no output in the Command Prompt window.
GHC version is:
C:\>ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.0.2
(Haskell compiler obtained from https://www.haskell.org/platform/windows.html.)
Update Loading and executing in GHCi yields the following:
C:\>ghci my_script.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( my_script.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Hello, World
*Main> :quit
Leaving GHCi.

Load a new package in ghci using stack

Is there a way to load a package(s) using Stack in GHCI and play around with it ?
So, that when the ghci is loaded, we can import the modules and see it's type signature, etc.
For the packages present in Stackage,
$ stack ghci --package unix-time
And this will give you a repl with the package unix-time loaded in it:
Run from outside a project, using implicit global project config
Using resolver: lts-6.14 from implicit global project's config file: /home/sibi/.stack/global-project/stack.yaml
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
λ> import Data.UnixTime
λ> :t getUnixTime
getUnixTime :: IO UnixTime
For multiple packages:
$ stack ghci --package unix-time --package download
Run from outside a project, using implicit global project config
Using resolver: lts-6.14 from implicit global project's config file: /home/sibi/.stack/global-project/stack.yaml
tagsoup-0.13.10: using precompiled package
xml-1.3.14: using precompiled package
time-locale-compat-0.1.1.3: using precompiled package
feed-0.3.11.1: download
feed-0.3.11.1: configure
feed-0.3.11.1: build
feed-0.3.11.1: copy/register
download-0.3.2.4: download
download-0.3.2.4: configure
download-0.3.2.4: build
download-0.3.2.4: copy/register
Completed 5 action(s).
Configuring GHCi with the following packages:
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
Ok, modules loaded: none.
λ> import Network.Download
λ>

How can I use GHC with a cabal sandbox that's not in the current working directory?

If I create a cabal sandbox with cabal sandbox init, I can use cabal repl or cabal exec ghc(i) to work with those packages without creating a project:
$ mkdir /tmp/example && cd /tmp/example
$ cabal sandbox init
$ cabal install QuickCheck
$ cabal exec ghci
Prelude> :m Test.QuickCheck
Prelude Test.QuickCheck>
However, if I change the path to something else, even to a subdirectory, I cannot access the packages anymore:
$ mkdir -p /tmp/example/sub && cd /tmp/example/sub
$ cabal exec ghci
Prelude> :m Test.QuickCheck
<no location info>:
Could not find module ‘Test.QuickCheck’
It is not a module in the current program, or in any known package.
Is there any way to use the contents from the sandbox, without copying its content?
The problem is that cabal will only respect sandboxes in the current working directory. However, there are several options where you can specify a sandbox location for cabal or the package databse for GHC.
Using cabal features
You can use cabal's --sandbox-config-file option to specify a sandbox configuration, e.g.
$ cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config exec ghci
Prelude> :m Test.QuickCheck
Prelude Test.QuickCheck>
This also enables you to change the sandbox from other places, which comes in handy if you just want to install random stuff into a temporary place:
$ cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config install lens
$ cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config repl
Prelude> :m Control.Lens
Prelude Control.Lens> :m Test.QuickCheck
Prelude Control.Lens Test.QuickCheck>
Since this gets cumbersome after a while, you should probably add an alias
$ alias sandboxed-cabal="cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config"
$ sandboxed-cabal repl
Prelude>
Using ghc -package-db
Alternatively, you can directly specify the package database when you use GHC with -package-db:
$ ghci -package-db /tmp/example/.cabal-sandbox/<ARCH>-packages.conf.d
Prelude> :m Test.QuickCheck
Prelude Test.QuickCheck>
The <ARCH> depends on your system and the used GHC, e.g. on a 64bit Linux and GHC 7.10.3 it's x86_64-linux-ghc-7.10.3-packages.conf.d. You can then use all packages in that database:
$ ghci -package-db /tmp/example/.cabal-sandbox/<ARCH>-packages.conf.d
Prelude> :m Control.Lens
Prelude Control.Lens>
Again, an alias should come in handy.
Using GHC_PACKAGE_PATH
Last, but not least, you can adjust an environment variable. However, if the environment variable GHC_PACKAGE_PATH exists, it will overwrite GHC's usual package databases, so you either need to check ghc-pkg list and add them too
$ GHC_PACKAGE_PATH=/opt/ghc/7.10.3/lib/ghc-7.10.3/package.conf.d/:/tmp/example/.cabal-sandbox/x86_64-linux-ghc-7.10.3-packages.conf.d ghci
or use -global-package-db and -user-package-db to reenable them:
$ GHC_PACKAGE_PATH=/tmp/example/.cabal-sandbox/x86_64-linux-ghc-7.10.3-packages.conf.d ghci -global-package-db -user-package-db

Resources