Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send text to GHCi process?

Tags:

haskell

ghci

I'm working on Haskell presentation engine Howerpoint. It is running in GHCi. I would like to create a function which would output a statement to current running GHCi session. It must work on Linux and Mac, Windows is not necessary. Function probably will have type

executeStatement :: String -> IO ()

What I tried already:

  • getProcessID and getParentProcessID and then sending something like

    echo 'xxx' > /proc/92856/fd/1
    -bash: /proc/92856/fd/1: No such file or directory
    
  • I also tried runCommand but it executes command in the Bash and not in GHCi so I got error that the command was not found

  • xdotool does not run on mac

like image 550
user1698641 Avatar asked Feb 11 '16 05:02

user1698641


People also ask

How do I use the “type-AT” command in GHCi?

This command is useful when integrating GHCi with text editors and IDEs for providing a show-type-under-point facility. The last string parameter is useful for when the span is out of date, i.e. the file changed and the code has moved. In which case :type-at falls back to a general :type like lookup. The :type-at command requires :set +c to be set.

How do I invoke GHCi from GHCi?

Invoking GHCi ¶ GHCi is invoked with the command ghci or ghc --interactive. One or more modules or filenames can also be specified on the command line; this instructs GHCi to load the specified modules or filenames (and all the modules they depend on), just as if you had said :load modules at the GHCi prompt (see GHCi commands ).

Is there a way to read GHCi files?

With this macro defined in your .ghci file, you can use :source file to read GHCi commands from file. You can find (and contribute!-) other suggestions for .ghci files on this Haskell wiki page: GHC/GHCi Additionally, any files specified with -ghci-script flags will be read after the standard files, allowing the use of custom .ghci files.

How do I control the context of an expression in GHCi?

GHCi provides a flexible way to control exactly how the context for an expression is constructed: The :load, :add, and :reload commands ( The effect of :load on what is in scope ). The import declaration ( Controlling what is in scope with import ). The :module command ( Controlling what is in scope with the :module command ).


1 Answers

You could use the ghcid project from hackage to evaluate expressions. They would not be evaluated in the same session as your are currently running, but you can send expressions and read their output in a session nonetheless. Here is an example:

import Language.Haskell.Ghcid

main :: IO ()
main = do 
    (g, _) <- startGhci "ghci" (Just ".") True 
    let executeStatement = exec g
    executeStatement "let x = 33"
    executeStatement "x + 8" >>= print . head
    executeStatement "print x" >>= print . head
    stopGhci g

The output is "41" "33" and g represents a ghci session.

If you really need to execute expressions in an already running ghci instance you can have a look at this function - startGhci and instead of creating a new process you would have to tap into the existing process and then set std_in, std_out and std_err.

like image 86
Jackie Avatar answered Sep 19 '22 17:09

Jackie