如何发送文本到GHCi进程? [英] How to send text to GHCi process?

查看:66
本文介绍了如何发送文本到GHCi进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Haskell演示引擎Howerpoint .它正在GHCi中运行.我想创建一个函数,该函数将向当前正在运行的GHCi会话输出一条语句.它必须可以在Linux和Mac上运行,而Windows则不是必需的.函数可能具有类型

executeStatement :: String -> IO ()

我已经尝试过的东西:

  • getProcessIDgetParentProcessID,然后发送类似内容

    echo 'xxx' > /proc/92856/fd/1
    -bash: /proc/92856/fd/1: No such file or directory
    

  • 我也尝试过runCommand,但是它在Bash中而不是在GHCi中执行命令,所以我收到找不到该命令的错误

  • xdotool不能在Mac上运行

解决方案

您可以使用 ghcid 从黑客项目中评估表达式.它们不会在与您当前正在运行的会话中进行评估,但是您仍然可以在 a 会话中发送表达式并读取其输出. 这是一个示例:

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

输出为"41""33",并且g代表ghci会话.

如果您确实需要在已经运行的ghci实例中执行表达式,则可以查看此功能-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

解决方案

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.

这篇关于如何发送文本到GHCi进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆