Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can runhaskell pick up options from .ghci?

Many people include .ghci files in their haskell projects to include needed options to load modules into ghci. Here's an example:

:set -isrc -itest -iexamples -packagehspec2

However when trying to run a file containing main through runhaskell one has to repeat all these options, e.g.:

runhaskell -isrc -itest -iexamples -packagehspec2 test/Spec.hs

Is there a good way to let runhaskell pick up the options from the .ghci file?

like image 677
shahn Avatar asked Oct 31 '14 06:10

shahn


People also ask

What is the difference between GHC and Ghci?

GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.

What does Ghci mean in Haskell?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.

How do I run Haskell with Ghci?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

What is stack Ghci?

The stack ghci or stack repl commands, which are equivalent, allow you to load components and files of your project into GHCi. The command uses the same TARGET syntax as stack build . It can also take flags like --test and --bench and options like --flag .

How to use stack GHCi with multiple components?

Similarly to stack build, the default is to load up ghci with all libraries and executables in the project. In order to load multiple components, stack ghci combines all of the ghc options together. This doesn't work in the general case, but when the packages being loaded share similar conventions, it should work out.

How do I load GHCi with an additional package?

Sometimes you want to load ghci with an additional package, that isn't a direct dependency of your components. This can be achieved by using the --package flag. For example, if I want to experiment with the lens library, I can run stack ghci --package lens.

Why does GHCi fail when using noimplicitprelude in one component?

For example, specifying NoImplicitPrelude in one component but not another is quite likely to cause failures. ghci will be run with -XNoImplicitPrelude, but it is likely that modules in the other component assume that the Prelude is implicitly imported. When loading multiple packages, there may be multiple definitions for the Main module.

How do I skip loading all defined modules into GHCi?

--no-load, to skip loading all defined modules into ghci. You can then directly use :load MyModule to load a specific module in your project. By default, stack ghci loads and imports all of the modules in the package. This allows you to easily use anything exported by your package.


1 Answers

I don't know of any way to make runhaskell work. What I do is just pipe "main" to ghci:

$ echo main | ghci -v0 test/Spec.hs

If you want to pass command-line arguments, that works too:

$ echo ':main -m "behaves correct"' | ghci -v0 test/Spec.hs

Or you can wrap it up in a script:

#!/usr/bin/env runhaskell
>import System.IO
>import System.Environment
>import System.Exit
>import System.Process
>
>main :: IO ()
>main = do
>  source:args <- getArgs
>  (Just h, Nothing, Nothing, pid) <- createProcess (proc "ghci" ["-v0", source]) {std_in = CreatePipe}
>  hPutStr h ("import System.Environment\nSystem.Environment.withArgs " ++ show args ++ " main\n")
>  hClose h
>  waitForProcess pid >>= exitWith

Which can be used like so:

$ ./run.lhs test/Spec.hs -m "behaves correct"
like image 135
Simon Hengel Avatar answered Oct 15 '22 05:10

Simon Hengel