简体   繁体   English

如何从Java调用Haskell

[英]How to call Haskell from Java

Before someone points out, I've looked at the answer here: Best way to call Haskell functions from within Java and I'm totally lost reading those answers. 在有人指出之前,我在这里查看了答案: 从Java内调用Haskell函数的最佳方法,而我完全迷失了阅读这些答案的方法。

I have a set of Haskell functions in a file called "output.hs". 我在名为“ output.hs”的文件中有一组Haskell函数。 Normally to execute those functions, I would use an interpreter like Hugs and enter my commands from their prompt. 通常,要执行这些功能,我会使用类似Hugs的解释器,并在提示符下输入命令。

What I want to do is emulate that process from a Java application. 我要做的是从Java应用程序模拟该过程。 I have a Java swing textfield where the user would type in a command in Haskell, and what I want to do is have that command interpreted and have the output displayed in some nearby text area. 我有一个Java swing文本字段,用户可以在其中输入Haskell的命令,而我想做的是对该命令进行解释,并在附近的某个文本区域中显示输出。 Essentially, I'm making a front for a Haskell interpreter. 本质上,我正在为Haskell解释器做准备。

Does anyone have direct experience with this? 有人对此有直接经验吗?

How about just make a GHCi/Hugs process, send it the input, and redirect stdout and stderr to your application? 仅执行GHCi / Hugs进程,将其发送给输入,然后将stdout和stderr重定向到您的应用程序怎么样? The Java API already has what you need, eg java.lang.ProcessBuilder . Java API已经具有所需的内容,例如java.lang.ProcessBuilder

Simply exposing the functions won't give you the ability to interpret Haskell, just exposing those functions (whether by library call or service), will really just expose the functions, not the ability to evaluate Haskell input. 仅仅公开这些函数并不能使您具有解释Haskell的能力,而仅仅公开那些函数(无论是通过库调用还是通过服务),实际上只会公开这些函数,而不是评估Haskell输入的能力。

To evaluate Haskell input you would either write your own interpreter, use an existing one (I don't think one exists), or take the option I suggested which should be the most practical and easy: use an existing interpreter and invoke it as a process. 要评估Haskell输入,您可以编写自己的解释器,使用现有的解释器(我认为不存在),或者采用我建议的最实用,最简单的选项:使用现有的解释器并将其作为处理。

I've had some experience with this approach, it's very simple to get to work, but watch out for unexpected termination and naturally, decide what you want to do when that happens. 我对这种方法有一定的经验,上班很简单,但是要小心意外终止,并自然地决定发生这种情况时要做什么。 If this is too difficult for your domain, you may also consider invoking the interpreter per expression, (eg ghc -e "1 + 1" will output 2 . See Modes of operation in the GHC manual, at section 4.5.2 for more information). 如果这对您的域来说太困难了,您还可以考虑按每个表达式调用解释器(例如ghc -e "1 + 1"将输出2有关更多信息,请参见GHC手册中的操作模式 ,第4.5.2节)。 )。 This could be easier but I'd only use it for infrequent evaluations because it will be slower, and might be noticeable with rapid input. 这可能会更容易,但是我只会将其用于不经常进行的评估,因为它会比较慢,并且在快速输入时可能会很明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM