Restricting output of hoogle on ghci - haskell

I have hoogle installed on my box and I can use hoogle from within ghci. Is there a way to ask hoogle to search only within certain modules and packages?
I tried
:hoogle +System.Directory directory
but I get a lot of results that are not found in the System.Directory.

How is your :hoogle command is defined in your .ghci file?
I have:
:def hoogle \x -> return $ ":!hoogle \"" ++ x ++ "\""
and your example works for me.
To debug the problem further, try (note the !):
:!hoogle +System.Directory directory
This directly executes the hoogle function on your system - so if that doesn't work it's a problem with your hoogle executable.

Related

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

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.

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.

What's the right way to :reload and run :main as a single command in GHCi?

Is there a way to chain :reload/:r along with :main as a single command in GHCi?
The goal here is to avoid typing both every time I change something in my other terminal, but to just type ↑Enter.
:cmd seems to accept string with multiple lines.
Therefore you can do the following command.
:cmd return $ unlines [":reload",":main"]
also you can add following code to ~/.ghci
:def hoge const $ return $ unlines [":reload",":main"]
now you can execute :hoge in ghci

Get absolute path of current source file in Haskell

Is it possible to get the absolute path of the current source file in Haskell?
I could only find one relevant function: getCurrentDirectory from System.Directory, but it "returns an absolute path to the current directory of the calling process.", not the path of the current file.
(I need it to read sample inputs which are located in the same folder as the source file; If there's any better way to do it, that will be helpful too!)
You can use CPP. If you compile this file
{-# LANGUAGE CPP #-}
main = print __FILE__
it will print the path to the source as you passed it to ghc – which may or may not be the full path, though:
/tmp $ ghc --make mypath.hs
[1 of 1] Compiling Main ( mypath.hs, mypath.o )
Linking mypath ...
/tmp $ ./mypath
"mypath.hs"
/tmp $ ghc --make /tmp/mypath.hs
Linking /tmp/mypath ...
/tmp $ ./mypath
"/tmp/mypath.hs"
As an alternative, the file-embed package can be used here. It uses template haskell to embed files/directories.
This can be very useful to embed resources or configs in the executable. It may not be advisable to read the sample input this way though. data-files in cabal might be better alternative as already pointed out earlier in this thread.
The PseudoMacros package might be useful. According to the description, it provides C-like strings for the current file name etc.
UPDATE
The file name returned by PseudoMacros equals the path passed to ghc (same behaviour as #JoachimBreitner mentioned in his answer), so
import PseudoMacros
main :: IO ()
main = putStrLn ("Hello from " ++ $__FILE__ ++ ", line " ++ show $__LINE__ ++ "!")
will print
Hello from tmp.hs, line 5!
or
Hello from /tmp/tmp.hs, line 5!
depending on whether you provided a relative or absolute filename to ghc.

Resources