thierry-martinez / ocaml-in-python

Effortless Python bindings for OCaml modules
BSD 2-Clause "Simplified" License
50 stars 2 forks source link

first use impression #2

Closed nilsbecker closed 2 years ago

nilsbecker commented 2 years ago

I'm impressed and excited for this package.

When going through the README example, I noticed that somevalue. works on the module m compiled on-the-fly, but does not work for the builtin ocaml module (i.e. ocaml. or ocaml.List. gives no options in ipython). Could this be added?

Importing the ocaml csv package did not work for me.

import ocaml
ocaml.require("csv")
from ocaml import Csv
--> ImportError: Py.Err(10, "Py.Err(10, \"Unbound value ocaml_of_python6\\nHint: Did you mean ocaml_of_python8?\")")
nilsbecker commented 2 years ago

ocaml 4.13.0, anaconda python 3.7 (maybe I should add that I did not install the python@3 opam package, as I prefer not to maintain more than one python distribution on my system -- could this be a problem here?)

thierry-martinez commented 2 years ago

Thank you for reporting and for your interest in this package! The two problems you reported should be fixed now.

nilsbecker commented 2 years ago

Great. One more impression, if I may: In reading the conversion rules I found a lot of info how ocaml values are converted to python ones. But not the other way around. It appears to me that already in the first example:

import ocaml
print(ocaml.List.map((lambda x : x + 1), [1, 2, 3]))
# => output: [2;3;4]

a python anonymous function is mapped over an ocaml list? At least that's what I think happens: ocaml.List.map is the python callable that in turn calls List.map in ocaml. List.map expects an ocaml function, which must be the ocaml version of lambda x: x + 1. So this lambda has to be converted from python to ocaml. correct? My point is just that this backwards conversion is not mentioned in the docs.

thierry-martinez commented 2 years ago

All conversions should be bidirectional, and I agree I should put more emphasis on this in the documentation. When the documentation says that "OCaml type x is mapped to Python type y", that means that OCaml values of type x are converted to Python values of type y and that Python values of type y are converted to OCaml values of type x.