From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 89E067EE25 for ; Fri, 7 Jun 2013 04:06:26 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of anthony.tavener@gmail.com) identity=pra; client-ip=74.125.83.42; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="anthony.tavener@gmail.com"; x-sender="anthony.tavener@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of anthony.tavener@gmail.com designates 74.125.83.42 as permitted sender) identity=mailfrom; client-ip=74.125.83.42; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="anthony.tavener@gmail.com"; x-sender="anthony.tavener@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ee0-f42.google.com) identity=helo; client-ip=74.125.83.42; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="anthony.tavener@gmail.com"; x-sender="postmaster@mail-ee0-f42.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvEAAKw+sVFKfVMqk2dsb2JhbABZgzmsQzKJNYhHdAgWDgEBAQEHCwsJFAQkgiMBAQQBJxkBGxILAQMBCwYFCw0NISIBEQEFAQoSBhMSh2gBAwkGDJ4OjEqCf4RcChknAwpYh3QBBQyPIgQHg1sDiSCOH4EpjjIWKYJaJIFWHQ X-IPAS-Result: AvEAAKw+sVFKfVMqk2dsb2JhbABZgzmsQzKJNYhHdAgWDgEBAQEHCwsJFAQkgiMBAQQBJxkBGxILAQMBCwYFCw0NISIBEQEFAQoSBhMSh2gBAwkGDJ4OjEqCf4RcChknAwpYh3QBBQyPIgQHg1sDiSCOH4EpjjIWKYJaJIFWHQ X-IronPort-AV: E=Sophos;i="4.87,818,1363129200"; d="scan'208";a="17070291" Received: from mail-ee0-f42.google.com ([74.125.83.42]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 07 Jun 2013 04:06:25 +0200 Received: by mail-ee0-f42.google.com with SMTP id c4so1493575eek.1 for ; Thu, 06 Jun 2013 19:06:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=O9wR0YEp5408s+95XDMVdD2nK1RPYIxpvACNitPei/4=; b=M7iHRJ8AYyGKhpI4B+ch3qJXAKwzIghkfB589It4bJfaSgOZOjbHnXhu3hTPPtOwmN 4GjuOlrHH+nv3DjsSnqcN6AGM9RM4ncLQwPjfxx+GKlq7/PQIF0GY2N89VQ6mWIotc1R r4g43VyzYRUbna6BRHrf0oAiQxf5gnbbSfsHBd/j5xr3zBr4/nr+aRD2VhQ51Sq811LJ zIN7GVc8FtHetU5IPwX6+W+j3ZaZKyq1erMMdgLo14xhk/l/3GzwesgE/V2QEisikC81 vROxq8iY47cXl/K46kc5Ix+XxE2ZJQXxtALSUC6kKKK86hnVub5J3k89ADz29TZ8MYEK tZ4Q== MIME-Version: 1.0 X-Received: by 10.15.41.196 with SMTP id s44mr8089997eev.138.1370570784968; Thu, 06 Jun 2013 19:06:24 -0700 (PDT) Received: by 10.14.246.199 with HTTP; Thu, 6 Jun 2013 19:06:24 -0700 (PDT) In-Reply-To: <51B13721.1070101@riken.jp> References: <51B13721.1070101@riken.jp> Date: Thu, 6 Jun 2013 20:06:24 -0600 Message-ID: From: Anthony Tavener To: Francois Berenger Cc: "caml-list@inria.fr" Content-Type: multipart/alternative; boundary=089e0163449cc7334a04de86e0b0 Subject: Re: [Caml-list] ANN: ocaml-ctypes, a library for calling C functions directly from OCaml --089e0163449cc7334a04de86e0b0 Content-Type: text/plain; charset=ISO-8859-1 "Too good to be true" is coming to mind now... because this looks very nice. :) My TODO list has been getting choked up with "make OCaml bindings for ", but it's so unpleasant to do (especially for libraries in development which you know will change). Let's see how well this works... Thanks, Jeremy! On Thu, Jun 6, 2013 at 7:28 PM, Francois Berenger wrote: > That looks very interesting!!! > > How about the cost of exchanging values between C and OCaml? > > Is there a trick in ocaml-ctypes like there is for bigarrays? > > Regards, > F. > > > On 06/07/2013 08:17 AM, Jeremy Yallop wrote: > >> I'm happy to announce the initial release of ocaml-ctypes. >> >> The ocaml-ctypes library makes it possible to call C functions >> directly from OCaml without writing or generating C code. The core of >> the library is a set of combinators for describing C types -- scalars, >> functions, structs, unions, arrays, and pointers to values and >> functions. Type descriptions can then be used to bind native >> functions and values. Here's a simple example: >> >> # let puts = foreign "puts" (string @-> returning int);; >> val puts : string -> int = >> # puts "Hello, world!";; >> Hello, world! >> >> Here's a more substantial example that shows how to describe a C >> structure type, map the type to an OCaml record, and call a function >> that returns the structure. >> >> (* Describe the C struct. There are two fields, both ints. *) >> let div_t = structure "div_t";; >> let q = div_t *:* int >> let r = div_t *:* int >> let () = seal div_t >> >> (* Define the OCaml record that we'll use to view the C structure. *) >> type div_result = { quot : int; rem: int } >> >> (* Define the conversions between the C struct and the OCaml record. >> *) >> let div_result_of_div_t d = { quot = getf d q; rem = getf d r } >> let div_t_of_div_result {quot; rem} = >> let d = make div_t in (setf d q quot; setf d r rem; d) >> >> (* Create a "view type" for that looks like div_result and behaves >> like div_t *) >> let div_result = view ~read:div_result_of_div_t >> ~write:div_t_of_div_result div_t >> >> (* Bind to the standard C `div' function *) >> let div = foreign "div" (int @-> int @-> returning div_result) >> >> (* Try it out *) >> # div 17 2;; >> - : div_result = {quot = 8; rem = 1} >> >> The distribution contains larger examples and a fairly extensive test >> suite, showing how to use other features of the library, such as >> binding to functions that accept callback arguments. Among the >> examples is Anil Madhavapeddy's port of the `curses' example from the >> OCaml documentation; it's instructive to compare the two >> implementations: >> >> OCaml manual curses example >> http://caml.inria.fr/pub/docs/**manual-ocaml/manual033.html#**toc147 >> >> ocaml-ctypes curses example >> https://github.com/ocamllabs/**ocaml-ctypes/blob/master/** >> examples/ncurses/ncurses.ml >> >> Detailed installation instructions for ocaml-ctypes can be found in >> the tutorial. (Briefly: ensure libffi is installed, then 'opam >> install ctypes'.) >> >> Comments, bug reports, and other feedback are most welcome. >> >> Tutorial: >> https://github.com/ocamllabs/**ocaml-ctypes/wiki/ctypes-**tutorial >> Examples: >> https://github.com/ocamllabs/**ocaml-ctypes/tree/master/**examples >> API documentation: http://ocamllabs.github.io/**ocaml-ctypes/ >> Github repository: https://github.com/ocamllabs/**ocaml-ctypes >> Direct download: >> https://github.com/ocamllabs/**ocaml-ctypes/archive/ocaml-** >> ctypes-0.1.tar.gz >> >> > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/**arc/caml-list > Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-**bugs > --089e0163449cc7334a04de86e0b0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
"Too good to be true" is coming to mind now... b= ecause this looks
very nice. :) My TODO list has been getting choked up= with "make
OCaml bindings for <some C lib>", but= it's so unpleasant to do
(especially for libraries in development which you know will cha= nge).

Let's see how well this works...
Thanks, Jeremy!


On Thu, Jun 6, 2013 at 7:28 PM, Francois Ber= enger <berenger@riken.jp> wrote:
That looks very interesting!!!

How about the cost of exchanging values between C and OCaml?

Is there a trick in ocaml-ctypes like there is for bigarrays?

Regards,
F.


On 06/07/2013 08:17 AM, Jeremy Yallop wrote:
I'm happy to announce the initial release of ocaml-ctypes.

The ocaml-ctypes library makes it possible to call C functions
directly from OCaml without writing or generating C code. =A0The core of
the library is a set of combinators for describing C types -- scalars,
functions, structs, unions, arrays, and pointers to values and
functions. =A0Type descriptions can then be used to bind native
functions and values. =A0Here's a simple example:

=A0 =A0 =A0# let puts =3D foreign "puts" (string @-> returning= int);;
=A0 =A0 =A0val puts : string -> int =3D <fun>
=A0 =A0 =A0# puts "Hello, world!";;
=A0 =A0 =A0Hello, world!

Here's a more substantial example that shows how to describe a C
structure type, map the type to an OCaml record, and call a function
that returns the structure.

=A0 =A0 =A0(* Describe the C struct. =A0There are two fields, both ints. *)=
=A0 =A0 =A0let div_t =3D structure "div_t";;
=A0 =A0 =A0let q =3D div_t *:* int
=A0 =A0 =A0let r =3D div_t *:* int
=A0 =A0 =A0let () =3D seal div_t

=A0 =A0 =A0(* Define the OCaml record that we'll use to view the C stru= cture. *)
=A0 =A0 =A0type div_result =3D { quot : int; rem: int }

=A0 =A0 =A0(* Define the conversions between the C struct and the OCaml rec= ord. *)
=A0 =A0 =A0let div_result_of_div_t d =3D { quot =3D getf d q; rem =3D getf = d r }
=A0 =A0 =A0let div_t_of_div_result {quot; rem} =3D
=A0 =A0 =A0 =A0 =A0let d =3D make div_t in (setf d q quot; setf d r rem; d)=

=A0 =A0 =A0(* Create a "view type" for that looks like div_result= and behaves
like div_t *)
=A0 =A0 =A0let div_result =3D view ~read:div_result_of_div_t
~write:div_t_of_div_result div_t

=A0 =A0 =A0(* Bind to the standard C `div' function *)
=A0 =A0 =A0let div =3D foreign "div" (int @-> int @-> retur= ning div_result)

=A0 =A0 =A0(* Try it out *)
=A0 =A0 =A0# div 17 2;;
=A0 =A0 =A0- : div_result =3D {quot =3D 8; rem =3D 1}

The distribution contains larger examples and a fairly extensive test
suite, showing how to use other features of the library, such as
binding to functions that accept callback arguments. =A0Among the
examples is Anil Madhavapeddy's port of the `curses' example from t= he
OCaml documentation; it's instructive to compare the two
implementations:

=A0 =A0 =A0OCaml manual curses example
=A0 =A0 =A0http://caml.inria.fr/pub/docs/manual-= ocaml/manual033.html#toc147

=A0 =A0 =A0ocaml-ctypes curses example
=A0 =A0 =A0https://github.com/ocamllab= s/ocaml-ctypes/blob/master/examples/ncurses/ncurses.ml

Detailed installation instructions for ocaml-ctypes can be found in
the tutorial. =A0(Briefly: ensure libffi is installed, then 'opam
install ctypes'.)

Comments, bug reports, and other feedback are most welcome.

Tutorial:
https://github.com/ocamllabs/ocaml-ctypes/wiki/cty= pes-tutorial
Examples:
https://github.com/ocamllabs/ocaml-ctypes/tree/mas= ter/examples
API documentation: http://ocamllabs.github.io/ocaml-ctypes/
Github repository: https://github.com/ocamllabs/ocaml-ctypes
Direct download:
https://github.com/ocamllabs/ocaml-ctyp= es/archive/ocaml-ctypes-0.1.tar.gz



--
Caml-list mailing list. =A0Subscription management and archives:
ht= tps://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners<= /a>
Bug reports:
http://caml.inria.fr/bin/caml-bugs

--089e0163449cc7334a04de86e0b0--