How can I run a GHCi statement in cabal v2-repl directly from command line? - haskell

How can I replicate ghci -e "print 123" in cabal v2-repl?
I've searched for "expression" or "statement" in cabal v2-repl --help with no luck.

The simplest way is to use shell piping capabilities. See:
% cabal v2-repl <<< ':type zip'
...
λ zip :: [a] -> [b] -> [(a, b)]
λ Leaving GHCi.
This <<< notation, in sh script interpreter, means that the quoted string is sent to the standard input of the command, followed by newline, then end of file.
There are other ways. For example, if you wish to supply more lines, you can use the so-called "here-doc":
% cabal repl <<EOF
:type zip
:type fst
EOF
The <<< notation is a shorthand for a one-line "here-doc".
In general, a program may know whether its standard input is a terminal (assuming, live user) or a file (which heredoc pretends to be), and behave differently. But ordinarily it would work either way, and, if you can send things to its standard input, you can automate it.

Related

How to run GHCi in command line like a regular shell command

Is there any way I can run GHCi on command line like a regular command in shell?
For example: :browse in GHCi - list all the function for specific module.
but I want to run it on shell, e.g.: ghci --browse "MyModule"
which lists all the functions for the module
I know hoogle can run it on shell, e.g: hoogle Monad
The easiest way is to pipe in the commands via standard input. In Bash this can be done nicely with a “here-string”:
$ ghci <<< ':t reverse'
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/sagemuej/.ghci
Loaded GHCi configuration from /home/sagemuej/.ghc/ghci.conf
Prelude> reverse :: [a] -> [a]
Prelude> Leaving GHCi.
Use verbosity 0 to avoid all the greeting stuff:
$ ghci -v0 <<< ':t reverse'
reverse :: [a] -> [a]

Running shell commands from Haskell in NixOS

I'm fairly new to NixOS, and am trying to invoke emacs from a Haskell program using the following function:
ediff :: String -> String -> String -> IO ()
ediff testName a b = do
a' <- writeSystemTempFile (testName ++ ".expected") a
b' <- writeSystemTempFile (testName ++ ".received") b
let quote s = "\"" ++ s ++ "\""
callCommand $ "emacs --eval \'(ediff-files " ++ quote a' ++ quote b' ++ ")\'"
When I run the program that invokes this command using stack test, I get the following result (interspersed with unit test results):
/bin/sh: emacs: command not found
Exception: callCommand: emacs --eval '(ediff-files "/run/user/1000/ast1780695788709393584.expected" "/run/user/1000/ast4917054031918502651.received")'
When I run the command that failed to run above from my shell, it works flawlessly. How can I run processes from Haskell in NixOS, as though I had invoked them directly, so that they can access the same commands and configurations as my user?
Both your shell and callCommand use the PATH environment variable, so it seems like stack is changing that. It turns out that stack uses a pure nix shell by default, but you also want to access your user environment, which is 'impure'.
To quote the stack documenation
By default, stack will run the build in a pure Nix build environment (or shell), which means the build should fail if you haven't specified all the dependencies in the packages: section of the stack.yaml file, even if these dependencies are installed elsewhere on your system. This behaviour enforces a complete description of the build environment to facilitate reproducibility. To override this behaviour, add pure: false to your stack.yaml or pass the --no-nix-pure option to the command line.
Another solution is to add Emacs to nix.dependencies in stack.yaml (thanks #chepner). It has the benefit that some version of Emacs will always be available when a developer runs the tests, but that Emacs may not be the Emacs they want to use. You may be able to work around that using something like ~/.config/nixpkgs/config.nix, unless they have configured their Emacs elsewhere, like the system configuration or perhaps a home manager. I'd prefer the simple but impure $PATH solution.

Change shell used to run "shell commands" in GHCi

In GHCi, a shell command can be executed using :!<command>. By default, it seems that the sh shell is used:
Prelude> :!echo $0
/bin/sh
I have a number of aliases and custom functions defined for the shell I use generally on my system (fish), and I'd like to use these from GHCi but not have to translate them into sh-compatible versions.
Is there a way of changing the shell GHCi uses to execute :! commands? I haven't been able to find any :set options for this, nor any command-line arguments to GHCi to do this.
The :! command in current versions of ghci is a call to System.Process.system
-- | Entry point for execution a ':<command>' input from user
specialCommand :: String -> InputT GHCi Bool
specialCommand ('!':str) = lift $ shellEscape (dropWhile isSpace str)
-- [...]
shellEscape :: String -> GHCi Bool
shellEscape str = liftIO (system str >> return False)
I suspect system is hardcoded to /bin/sh, but there's no reason you can't define your own ghci command to do fish calls. I don't have fish installed, so I'll use bash as an example:
λ import System.Process (rawSystem)
λ :def bash \cmd -> rawSystem "/bin/bash" ["-c", cmd] >> return ""
λ :bash echo $0
/bin/bash

Is it possible to pass command line arguments when running Haskell code with `ghc -e`?

Intuitively, I've tried
$ ghc -e "import System.Environment" -e "getArgs" -- a b c
ghc: unrecognised flag: --
did you mean one of:
-n
-F
-v
Usage: For basic information, try the `--help' option.
$
...without success.
I was expecting the output to be along the lines of ["a","b","c"].
The docs here don't seem to mention any way to pass cli arguments through.
AFAIK piping in stdio works as expected.
And perhaps if it is not possible to pass in arguments with ghc -e, maybe it's still possible with ghci to supply both some code, some arguments to it, run, then exit?
To answer the second part of the question:
Prelude> :help :main
Commands available from the prompt:
…
:main [<arguments> ...] run the main function with the given arguments
…
Prelude> let main = System.Environment.getArgs >>= print
Prelude> :main foo bar
["foo","bar"]

Deleting items in stdin with haskell

I have a bit of code in my haskell program like so:
evaluate :: String -> IO ()
evaluate = ...
repl = forever $ do
putStr "> " >> hFlush stdout
getLine >>= evaluate
Problem is, when I press the delete key (backspace on windows), instead of deleting a character from the buffer, I get a ^? character instead. What's the canonical way of getting delete to delete a character when reading from stdin? Similarly, I'd like to be able to get the arrow keys to move a cursor around, etc.
Compile the program and then run the compiled executable. This will give the correct behavior for the Delete key. For some reason interpreting the program screws up the use of Delete.
To compile the program, just invoke ghc like this:
$ ghc -O2 myProgram.hs
This will generate a myProgram executable that you can run from the command line:
$ ./myProgram
That will then give the correct behavior for Delete.

Resources