From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q092ZYch008986 for ; Mon, 9 Jan 2012 03:35:35 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArYAAFFRCk+PtnwVkGdsb2JhbABDrEQiAQEBAQkJDQcUBSCBdAEEOj8SASoUQiYBBA4NvQqLLmMEp0I X-IronPort-AV: E=Sophos;i="4.71,477,1320620400"; d="scan'208";a="138347724" Received: from mga03.intel.com ([143.182.124.21]) by mail1-smtp-roc.national.inria.fr with ESMTP; 09 Jan 2012 03:35:29 +0100 Received: from azsmga002.ch.intel.com ([10.2.17.35]) by azsmga101.ch.intel.com with ESMTP; 08 Jan 2012 18:35:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.71,315,1320652800"; d="scan'208";a="54525191" Received: from orsmsx604.amr.corp.intel.com ([10.22.226.87]) by AZSMGA002.ch.intel.com with ESMTP; 08 Jan 2012 18:35:27 -0800 Received: from orsmsx152.amr.corp.intel.com (10.22.226.39) by orsmsx604.amr.corp.intel.com (10.22.226.87) with Microsoft SMTP Server (TLS) id 8.2.255.0; Sun, 8 Jan 2012 18:35:27 -0800 Received: from orsmsx101.amr.corp.intel.com ([169.254.8.57]) by ORSMSX152.amr.corp.intel.com ([169.254.8.220]) with mapi id 14.01.0355.002; Sun, 8 Jan 2012 18:35:26 -0800 From: "Harrison, John R" To: "caml-list@inria.fr" CC: "Harrison, John R" Thread-Topic: Non-toplevel C functions using OCaml values Thread-Index: AczOd1eibW59g9B9Rqa/KTxQRpkOzA== Date: Mon, 9 Jan 2012 02:35:25 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.22.254.138] Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q092ZYch008986 Subject: [Caml-list] Non-toplevel C functions using OCaml values I have a question about interfacing C and OCaml functions that I didn't see an answer to in the manual, though maybe I just didn't look carefully enough. I think I pretty well understand how to set up a C function that is designed to be called directly by OCaml. But what about a function that I want to use internally as a utility, callable by other C functions that are themselves called by OCaml? For example, if I want to implement a function designed to be called directly by OCaml and basically equivalent to the OCaml definition let cons h t = h::t;; then I think (correct me if I'm wrong) that I do the following: CAMLprim value caml_cons(value h,value t) { CAMLparam2(h,t); CAMLlocal1(newblock); newblock = caml_alloc(2,0); Store_field(newblock,0,h); Store_field(newblock,1,t); CAMLreturn(newblock); } But what if I want to set up a utility that I'm only going to call from other C functions, and which involves a mixture of standard C types and OCaml values among its arguments or local variables, and/or allocates OCaml values? For example, suppose I want to implement the special case of the above "cons" for int lists where the first argument is a C int (which I assume to be in range for an OCaml int). I might guess that I would do something like this: value int_cons(int h,value t) { CAMLparam1(t); CAMLlocal1(newblock); newblock = caml_alloc(2,0); Store_field(newblock,0,Val_int(h)); Store_field(newblock,1,t); CAMLreturn(newblock); } and then just call it like any other C function. However, I can imagine several other plausible alternatives, e.g. that I should skip some or all of the special CAMLsomething functions, or that I should just forget the idea and use OCaml values throughout, calling it as if it were any other OCaml function. Can someone enlighten me? John.