OCaml: Effective Path to GUI Programming?

9codeMan9 picture 9codeMan9 · Mar 27, 2013 · Viewed 9.4k times · Source

I have seen a few threads that touch on GUI programming in OCaml but I don't feel they clearly lead to a clear-cut solution when a GUI interface is needed.

My question, to be more specific, is as follows: What is the most effective (and easy to pick up) approach in programming a GUI for OCaml software? Has anyone come into contact with simple and effective GUI modules in OCaml itself or found an effective language or free software package in which this can be done and that communicates/plays nicely with OCaml?

I have written an interpreter in OCaml, so my lexer, parser, core interpreter functions, etc. are OCaml modules. Currently, I have a command line solution (a "main.ml") that allows the user to interact with the interpreter by typing in expressions into the command line and receiving printed terminal output that shows the parsed and reduced expression, etc. However, the command line solution is only for testing purposes. I want users to interact via a GUI, it can be simple (Java frames comes to mind from eons ago), but needs to somehow interface with the OCaml modules I have coded. There is one library in OCaml I have found so far: http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual042.html. Does anyone know if this is effective and useful? (I think I've caught negative comments about this library)

If I chose to program the GUI in a more optimal language, would the software interaction be: write GUI in suited language (perhaps C++, Python, etc.), then compile the OCaml written interpreter into an executable, then connect the GUI somehow to the executable? I am not interested in some loosely connected or strange solution, via pipes (I keep thinking of inter-process communication for these, such what is concerned in operating system design) or sockets (I tend to think of these for network programming), I imagine there must some way to "house" my OCaml-coded interpreter in another language's GUI code if not OCaml itself. Any thoughts, guidance, or suggestions?

EDIT: I would be happy if I could get a GUI for a Linux-like operating system (i.e. Linux RedHat). If I could get the GUI to work on Windows that would be great, but at minimum I'm aiming for Linux.

EDIT 2: Just found this, does anyone have thoughts on "OCaml-Java"? http://ocamljava.x9c.fr/ It sounds pretty interesting, as it has, "...the ability to run Objective Caml sources that have been compiled using ocamlc; second, the ability to compile Objective Caml sources into executable jar files." My concern is that it never struck me that Java would be the best way to go to get a quick but useful GUI...

CURRENT SOLUTION: After examining the various options in the solution post that @Jeffrey Scofield put down below, I have chosen to currently look deeper into LablGtk (which would allow me to stay within OCaml). The next promising option for those looking at this post would be to look into foreign language interfacing with C, as C and OCaml already have a relationship to start with. There appears to be ways of calling C code within OCaml and OCaml within C (though this can be really difficult, as you essentially end up wrapping OCaml function calls with somewhat complex wrapper functions that will be more specific to the type of functions you call from within OCaml-->i.e. you will have to deal with the "mapping" of each OCaml function and its arguments within C). Take a look at: http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/calling_ocaml.html, for more info. OCaml-Java originally seeemed like a great idea, given I was comfortable with Java GUI programming, but the interaction between the two languages was not as direct as with C and OCaml, plus, the documentation for this appeared to be slim (and using OCaml-Java was not something you just pick up and get to the Java GUI stuff...). OCaml-JavaScript looked interesting, but keep in mind you will most likely have to invest time in good HTML 5 coding setup in addition to some JavaScript if you choose this path. Alternatively, there are several posts here in SO that talk about pipes and sockets, which are valid methods of creating a GUI-back-end system. However, this is a good idea if you don't mind that your program system/product will be "loosely coupled". I will update this solution once I figure out LablGtk and ensure that it yields an acceptable GUI front-end for my OCaml back-end code.

Answer

Jeffrey Scofield picture Jeffrey Scofield · Mar 27, 2013

I think the LablGtk toolkit is reasonably up to date and maintained currently. So that would be one option. I've not used it myself, however.

It's not too difficult to link a set of OCaml modules into a main program written in C, C++, or Objective C. You can make more or less direct calls to your OCaml functions and get back results. For this you need to learn the foreign function interface, which isn't so difficult once you get in the groove. Then you could use whatever GUI library you like from the C family side. If you're interested in this approach, you can start with Chapter 19 of the OCaml manual.

You might link other languages using a C intermediary, depending on the language. For an interpreted language you could probably add your OCaml functions as new primitives. (Interpreters are usually written in C or C++ in my experience.)

For Java, there is OCaml-Java (as you mention). I haven't used it myself, but it is intriguing. I believe there are a few limitations imposed by the JVM, so you might need to be a little extra careful in your coding.

For a web app, there is js-of-ocaml, which compiles OCaml to JavaScript.