By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Embedding OCaml within Python

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Max Powers wrote:
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
Jul 18 '05 #2

P: n/a
Max Powers wrote:
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...
Thanks,

Err... Not sure I'm really helpful here :(

Bruno

Jul 18 '05 #3

P: n/a
Max Powers wrote:
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

Jul 18 '05 #4

P: n/a
ma***********@hotmail.com (Max Powers) writes:
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

--
... Windows proponents tell you that it will solve things that
your Unix system people keep telling you are hard. The Unix
people are right: they are hard, and Windows does not solve
them, ... -- Tim Bradshaw, comp.lang.lisp
Jul 18 '05 #5

P: n/a
Jakub Fast wrote:
Max Powers wrote:
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?

AFAIK, no, it's a stand-alone native code compiler.

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.