Embedding OCaml within Python

M

Max Powers

I've spent an interesting day searching the web for help with calling
OCaml functions from a python program and have come to the newsgroup
for some advice (unfortunately I'm learning both languages at the same
time, so maybe I've missed something obvious!)

The situation is this; I've got a large legacy program that is written
in ocaml, within which there's a parser. I'd like to be able to call
that parser from my python program (actually it's really a GUI using
wxPython), rather than reimplement it (and then have to maintain two
parsers into the future).

I've come across Viper, which looks like a python compiler/interpreter
implemented in OCaml, so I imagine that I could get it to do some
magic tricks to call a ocaml library from python, but I think that's
doomed to failure because of the C extensions of wxPython.

I've stumbled across Pycaml, but don't quite understand how to use it
to call ocaml from python. Alternatively I think I can wrap the ocaml
library in a C wrapper and then call that from python.

Can anyone give advice/share experiences? Is combining OCaml with
python relatively straightforward with a bit more effort, or am I in
for a world of pain?

Thanks,
Max
 
P

Peter Strempel

Max said:
I've spent an interesting day searching the web for help with calling
OCaml functions from a python program and have come to the newsgroup
for some advice

I stumbled over a similar task few months ago. I found no direct way to call
OCaml functions from Python directly (maybe there is and I didn't find it),
but I solved it by calling the OCaml code from C which itself is an
extension loaded by Python.

Python -> C extension -> OCaml -> callback to C -> callback to Python

This is sort of ugly, but works. Actually Python is supposed to be the glue
language, not C. :)

The passed values are simple types, not classes or arrays, so this isn't
overly complex, but can probably get if you need to pass non-trivial values
because C needs to translate between OCaml and Python types then.

I implemented the above without SWIG or similar tools, simply because I
prefer to write the boilerplate code at least once to understand how it
works. Plans are to use SWIG later.

I approached the task by writing some C program which calls the OCaml stuff
and implements required callbacks first. Then later adding the interface to
Python. It actually isn't that complex if you try to treat the Python <-> C
and the C <-> OCaml parts individually. Both Python and OCaml have excellent
documentation covering interfacing with C.

In reply to your last remark, I think you are not in a world of pain. But
it's probably not the most trivial thing to do either.

I would appreciate pointers how to do this in a more elegant way, though.
I am in no way an OCaml expert, so chances are high I missed something here.

Peter
 
B

Bruno Desthuilliers

Max said:
I've spent an interesting day searching the web for help with calling
OCaml functions from a python program and have come to the newsgroup
for some advice (unfortunately I'm learning both languages at the same
time, so maybe I've missed something obvious!)

The situation is this; I've got a large legacy program that is written
in ocaml, within which there's a parser. I'd like to be able to call
that parser from my python program (actually it's really a GUI using
wxPython), rather than reimplement it (and then have to maintain two
parsers into the future).

I may say something stupid but... I suppose this OCaml program has a CLI
? If so, can't you just call the OCaml program from your wxPython GUI
and get result back via a pipe ?
I've come across Viper, which looks like a python compiler/interpreter
implemented in OCaml, so I imagine that I could get it to do some
magic tricks to call a ocaml library from python, but I think that's
doomed to failure because of the C extensions of wxPython.

AFAIK, wxWidget is C++, not C...
I've stumbled across Pycaml, but don't quite understand how to use it
to call ocaml from python. Alternatively I think I can wrap the ocaml
library in a C wrapper and then call that from python.

For what I saw (30' fast scan of the Pycaml page), it seems you would
need to write OCaml wrappers for your legacy OCaml code. But I dont have
any working experience in this kind of stuff, so I may have it all wrong.
Can anyone give advice/share experiences? Is combining OCaml with
python relatively straightforward with a bit more effort, or am I in
for a world of pain?

I guess you could ask help from the OCaml community too...
Err... Not sure I'm really helpful here :(

Bruno
 
J

Jakub Fast

Max said:
The situation is this; I've got a large legacy program that is written
in ocaml, within which there's a parser. I'd like to be able to call
that parser from my python program

Doesn't the O'Caml compiler translate the code into C first?

If you got it to generate C code for your your library, chances are you
could dig into it and wrap whatever you got there with SWIG or BPL, and
then compile both the wrapper and the lib in C.

Then,
> Python -> C extension -> OCaml -> callback to C -> callback to Python

becomes

Python -> C extension (O'Caml translation + callback to Python)


Of course, it might be the case that the O'Caml C code is too
complicated or too far removed from the O'Caml object interface...

hth

kuba
 
M

Michael Hudson

Alternatively I think I can wrap the ocaml library in a C wrapper
and then call that from python.

Not knowing much about the situation, this is probably the simplest
thing to do... depends on how complicated the data you want to pass
between the two parts of the program is.

Cheers,
mwh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top