2

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.

I have a set of Haskell functions in a file called "output.hs". Normally to execute those functions, I would use an interpreter like Hugs and enter my commands from their prompt.

What I want to do is emulate that process from a Java application. 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. Essentially, I'm making a front for a Haskell interpreter.

Does anyone have direct experience with this?

Community
  • 1
  • 1
maxematician
  • 199
  • 7
  • 5
    Shouldn't this be called, how to call Haskell from java? – Robert Moskal Mar 21 '16 at 23:30
  • Yeah, I think I switched the title by accident. – maxematician Mar 21 '16 at 23:47
  • 1
    @MaxLipton the fact that you're lost reading those answers don't mean that they don't answer the question. It just means that you need to learn more to understand them. – sclv Mar 22 '16 at 00:58
  • This answer (linked in the comments of the other thread) may be of service: http://stackoverflow.com/a/10370902/371753 – sclv Mar 22 '16 at 00:59
  • I don't know where to start learning. I was hoping somebody could answer the question in a way that doesn't require a multiple-hour detour to understand certain minutia. – maxematician Mar 22 '16 at 01:02

1 Answers1

4

How about just make a GHCi/Hugs process, send it the input, and redirect stdout and stderr to your application? The Java API already has what you need, e.g. 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.

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.

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, (e.g. ghc -e "1 + 1" will output 2. See Modes of operation in the GHC manual, at section 4.5.2 for more information). 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.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90