4

CやPythonのような他のプログラミング言語で使用されるOCamlライブラリを書きたいです。

それが実現可能かどうかはわかりません。型の安全性を落とし、動的型付け言語のインターフェイスにランタイムチェックを追加する必要があると思います。

それは実行可能ですか?バインディングを自動生成するこの目標を達成するためのツールはありますか?CorbaのようなものはocamlABIにうまく適合しないと思いますが、私は間違っているかもしれません。

編集:ランタイム要件を削除し、llvmフロントエンドを持つ言語のみを使用することで、llvmを一般的なABIとして使用できますが、注意が必要です。

4

2 に答える 2

9

OCaml has a FFI to interact with C code. The code for the binding has to be written in C, not in OCaml (which has no direct representation of C values, while C has representations of OCaml values). My advice would be:

  1. On the C side, decide what would be the best interface to export that C programmers would like (or Python programmers writing Python bindings starting from your C interface)
  2. Define a "low-level layer" on the OCaml side that gets your OCaml value as close as possible from the C representation
  3. Write some C wrappers to convert from this low-level OCaml representation to your optimal C representation

The reason for step (2) is to have the step (3) as small as possible. Manipulating OCaml values from the C side is a bit painful, in particular you risk getting the interaction with the Garbage Collector wrong, which means segfaults -- plus you don't get any type safety. So the less work you have to do on the C side, the better.

There are some projects to do some of the wrapping work for you. CamlIDL for example, and I think Swig has some support for OCaml. I have never used those, though, so I can't comment.

If you know to which high-level language you wish to convert your interface to, there may be specialized bridge that don't need a C step. For example there are libraries to interact directly with Python representations (search for Pycaml, not sure how battle-tested their are) or with the Java runtime (the OCamlJava project). A C interface is still a safe bet that will allow other people to create bridges to their own languages.

于 2012-10-26T12:16:30.817 に答える
1

実行可能ですが、GCの動作など、関連するトピックを理解する必要があります。これを見てください:http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html#toc148

スタブコードの型に注意する必要がありますが、そうでない場合は型の安全性を保つことができます。

于 2012-10-26T12:15:31.127 に答える