Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Thomas Fischbacher <tf@functionality.de>
To: Andrej.Bauer@andrej.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] caml and python (was: Announcement: ocaml-based magnetism simulation package)
Date: Wed, 21 Nov 2007 23:33:54 +0000	[thread overview]
Message-ID: <4744C062.1020605@functionality.de> (raw)
In-Reply-To: <4744AAFB.8090703@fmf.uni-lj.si>


Andrej Bauer wrote:

>>(The user interface is python, though.) 
> 
> I have suspected for a while that writing the computational part in
> ocaml and the user interface in python is a good combination. What is
> your experience? Are there any drawbacks? Would you do it again?

We use a modified variant of Art (arty) Yerkes' old "pycaml" module.

Essentially, I had to re-write quite a bit of it, as both reference
counting on the Python side and Gc management on the OCaml side were
broken. One impotant issue is that the pycaml module (which also is
part of Debian) does not recognize properly that python library
primitives often differ in their behaviour concerning whether they
increase the reference count of a return value or not.

Also, we added a lot of extra stuff and goodies which allows us to
interface ML to python quite smoothly. When I call a ML-function from
python, dynamic type-checking of all the arguments is automatic and
generates appropriate python exceptions. Here is an example for a
python-wrapped ML function that uses our extended pycaml module:

let _py_raw_cvode_advance =
   python_pre_interfaced_function
     [|CamlpillType; (* the cvode *)
       CamlpillType; (* the result vector *)
       FloatType;    (* target time *)
       IntType;      (* max nr_steps, -1 for "no limit" *)
     |]
     (fun args ->
        let cvode = cvode_from_ocamlpill args.(0) in
        let FEM_field (_,_,v_result) = field_from_ocamlpill args.(1) in
        let target_time = pyfloat_asdouble args.(2) in
        let max_steps = pyint_asint args.(3) in
        let result_time =
	 Sundials.cvode_advance cvode target_time v_result max_steps
	 in
	 pyfloat_fromdouble result_time)
;;

It is nice to be able to implement other high-level python/caml
interface abstractions through functional tricks -- such as a caml
function constructing and accessing a higher-rank nested-list python
tensor.

How does it feel? If I had a need for some specific fast low-level
extension to python, I would prefer using python+ocaml a thousand
times over python+C. It is just so much nicer not having to worry
about reference counting issues. Even if you feel you can do it
right, there is always a bit of doubt. Doubly so if you are not
the sole developer. I would not let our PhD students write C
extensions to python, but I have no problems whatsoever letting
them extend python via ocaml functions.

One sort-of limitation is that we use our own python interpreter.
(I.e. the main program is written in OCaml and eventually provides
a python prompt or runs a python script.)

This is necessary in our situation as we also want to support running
in parallel under MPI control, and the MPI libraries do some nasty
arg-passing things that upset an unmodified python interpreter. So, we
do not load some object-code module containing all the ML runtime
environment system into a standard python interpreter; technically, that
should be possible as well on many platforms, but it is not relevant in
our particular case here.


On the other hand, of course, if one can completely avoid such
inter-language interfaces, the better. In principle, both Python
and OCaml are widely loved because of flexibility they both derive
from LISP. So, some situations, one may be better off using e.g. the
Gambit Scheme Compiler instead (which has, by the way, a terrific
C interface!)

It depends a lot on your constraints. If you want to build something
that can be easily integrated with lots of data processing libraries
that are readily available and accessible to casual programmers
(engineers, physicists, etc.), providing python support seems to be a
very good choice to me. It is rather popular among engineers these
days, which I consider mostly a good thing.

Also, if you want your PhD students to be productive at writing code
that has to run fast, and if they are spoilt enough from previous
exposure to imperative languages that you cannot really teach them
Lisp/Scheme within a short amount of time ;-), then ocaml certainly
is a very good choice. (In terms of what we teach our students about
caml, this is not much more than the core ML language plus a bit
about the C interface. They usually pick that up in less than 3 months.
With C++, they never could be productive after such a short amount
of time.)

So, I would say: in an ideal computing world, we would all be expert
Scheme hackers, but in the real world, python+ocaml can be an extremely
productive and powerful combination. I have used and combined many
systems in the past, but I would have difficulty naming anything
else I played with that appeared even remotely as powerful.

I know that in the longer run, we certainly should split nmag and
ship our new and improved pycaml module separately (after brushing
it up a bit) -- and a few other modules as well. But for now,
we are busy dealing with other issues. (We are, after all,
physicists studying real physical systems, so all this symbolic
mumbo-jumbo in nmag/nsim is just a tool to us.)

-- 
best regards,
Thomas Fischbacher
tf@functionality.de


  reply	other threads:[~2007-11-21 23:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-21 19:06 Announcement: ocaml-based magnetism simulation package Thomas Fischbacher
2007-11-21 19:47 ` [Caml-list] " Jon Harrop
2007-11-21 20:03   ` Thomas Fischbacher
2007-11-21 22:02 ` Andrej Bauer
2007-11-21 23:33   ` Thomas Fischbacher [this message]
2007-11-22 15:21     ` [Caml-list] caml and python Andrej Bauer
2007-11-22 16:21       ` Thomas Fischbacher
2007-11-22 16:32       ` Jon Harrop
2007-11-22 16:19     ` [Caml-list] caml and python (was: Announcement: ocaml-based magnetism simulation package) Stefano Zacchiroli
2007-11-22 18:06       ` [Caml-list] caml and python Thomas Fischbacher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4744C062.1020605@functionality.de \
    --to=tf@functionality.de \
    --cc=Andrej.Bauer@andrej.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox