我怎样才能以编程方式发送haskell表达式到当前正在运行的ghci会话 [英] how can i send programmatically haskell expression to current running ghci session

查看:78
本文介绍了我怎样才能以编程方式发送haskell表达式到当前正在运行的ghci会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Haskell展示引擎Howerpoint 。它正在GHCi中运行。我想创建一个函数来输出一个语句给当前正在运行的GHCi会话。它必须在Linux和Mac上工作,Windows不是必需的。函数可能会有类型

  executeStatement :: String  - > IO()

我已经试过了:


  • getProcessID getParentProcessID 然后发送像

      echo'xxx'> / proc / 92856 / fd / 1 
    -bash:/ proc / 92856 / fd / 1:没有这样的文件或目录


  • 我也试过 runCommand ,但它在Bash中执行命令,而不是在GHCi中,所以我得到错误, / b>

  • xdotool 无法在mac上运行

  • 您可以使用
    > ghcid 项目从hackage评估表达式。它们不会在当前运行的同一会话中进行评估,但是您仍然可以发送表达式并在 a 会话中读取它们的输出。
    下面是一个例子:

      import Language.Haskell.Ghcid 

    main :: IO()
    main = do
    (g,_)< - startGhcighci(Just。)True
    let executeStatement = exec g
    executeStatementlet x = 33
    executeStatementx + 8>> = print。头
    executeStatement打印x>> =打印。头部
    stopGhci g

    输出为4133,g代表ghci会话。



    如果你确实需要在已经运行的ghci实例中执行表达式,你可以看看这个函数 - startGhci ,而不是创建一个新的进程,你将不得不挖掘现有的进程,然后设置std_in,std_out和std_err。


    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

    解决方案

    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.

    这篇关于我怎样才能以编程方式发送haskell表达式到当前正在运行的ghci会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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