Embedding OCaml within Python

Discussion in 'Python' started by Max Powers, Apr 28, 2004.

  1. Max Powers

    Max Powers Guest

    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
     
    Max Powers, Apr 28, 2004
    #1
    1. Advertising

  2. 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
     
    Peter Strempel, Apr 28, 2004
    #2
    1. Advertising

  3. 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
     
    Bruno Desthuilliers, Apr 28, 2004
    #3
  4. Max Powers

    Jakub Fast Guest

    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
     
    Jakub Fast, Apr 28, 2004
    #4
  5. (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
     
    Michael Hudson, Apr 28, 2004
    #5
  6. 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.
     
    Bruno Desthuilliers, May 1, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Rubin

    OT: Ocaml?

    Paul Rubin, Aug 23, 2003, in forum: Python
    Replies:
    6
    Views:
    526
    Donn Cave
    Aug 26, 2003
  2. Bryan

    ocaml extension

    Bryan, Jun 16, 2004, in forum: Python
    Replies:
    0
    Views:
    338
    Bryan
    Jun 16, 2004
  3. Jelle Feringa // EZCT / Paris

    OCAMl a more natural extension language for python?

    Jelle Feringa // EZCT / Paris, Jan 17, 2005, in forum: Python
    Replies:
    4
    Views:
    511
  4. Gigs_

    ocaml to python

    Gigs_, Feb 19, 2007, in forum: Python
    Replies:
    2
    Views:
    521
    Laurent Pointal
    Feb 20, 2007
  5. Replies:
    0
    Views:
    508
Loading...

Share This Page