My goal is to pipe some steps for ghci
to run from a bash script and then exit cleanly. The commentary online says to use runhaskell
for this.
This is the command I'm trying to run:
ghci> import System.Random
ghci> random (mkStdGen 100) :: (Int, StdGen)
With expected result similar to:
(-3633736515773289454,693699796 2103410263)
When I drop this into a file randomtest.hs
and execute it with runhaskell
I get the following error:.
randomtest.hs:3:1: error:
Invalid type signature: random (mkStdGen 100) :: ...
Should be of form <variable> :: <type>
It seems I can't use the runhaskell
method to blindly execute ghci
inputs.
Now the way to work around this is to add extra commands to the file that is passed to runhaskell
:
main = do print (random (mkStdGen 100) :: (Int, StdGen))
My goal is to automate the running of ghci work for a haskell course I'm using. I want to be able to run the ghci command from a bash script - in the format that ghci expects, and have it cleanly exit from ghci (or whatever runs it).
My question is: Is there a way to script a ghci session?
If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu. And you are presented with a prompt. The Haskell system now attentively awaits your input.
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.
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.)
Quits GHCi. You can also quit by typing control-D at the prompt. Attempts to reload the current target set (see :load ) if any of the modules in the set, or any dependent module, has changed. Note that this may entail loading new modules, or dropping modules which are no longer indirectly required by the target.
You'll want to use expect for that, it allows you to interactively control a REPL with simple commands. This script does what you want:
#!/usr/bin/env expect
log_user 0
spawn ghci
log_user 1
expect ".*> "
send ":set prompt \"ghci> \"\n"
expect "ghci> "
send "import System.Random\n"
expect "ghci> "
send "random (mkStdGen 100) :: (Int, StdGen)\n"
interact
Running this gives you the following:
$ ./ghci-interactive
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Prelude> :set prompt "ghci> "
ghci> import System.Random
ghci> random (mkStdGen 100) :: (Int, StdGen)
(-3633736515773289454,693699796 2103410263)
ghci>
Note: You might need to adjust this a bit to be resistant to users setting the prompt in ~/.ghci
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With