Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Manuel Fahndrich <maf@microsoft.com>
To: "'caml-list@inria.fr'" <caml-list@inria.fr>
Subject: external C primitives and documentation
Date: Thu, 18 Feb 1999 11:03:26 -0800	[thread overview]
Message-ID: <25983782061AD111B0800000F86310FE1026CB1B@RED-MSG-42> (raw)


I just got bitten by a misuse of the external facility to call C from CAML.
I think the documentation should alert people to following issue.

	The form of type expressions in "external" declarations is
important: beware of function type abbreviations.

Consider the following scenario:

type a
type b
type c

external foo : a -> b -> c = "CFoo"

The ML compiler assumes CFoo is a two argument function C function, thus a
call

	foo a b

will pass both arguments a and b to the C function CFoo and expect a value
of type c back.

Now I was stupid and I did the following:

type a
type b
type c
type b_to_c = b -> c

external foo : a -> b_to_c = "CFoo"

Of course, the ML compiler now assumes that CFoo is a one argument C
function and a call

	foo a b

will call CFoo with a single argument a, expect a closure back, which will
then be applied to b.

I haven't tried to see what happens when you say"

external foo : a -> (b -> c) = "CFoo"

but I expect this would be the second case as well.

Of course, this is exactly the intended meaning of the external declaration.
But when you have function type abbreviations like   'a iterator = ('a ->
unit) -> unit, it is too tempting to use them in the external declarations.

Thus I would suggest that the OCAML documentation on the C Interface
contains a warning to this effect. The documentation doesn't describe
exactly how it determines the number of arguments to be passed to the C
function.

An alternative would be to extend the external declarations with a parameter
count:

external foo[2] : a -> (b -> c) = "CFoo"

would then unambiguously state that foo is a function which calls into C
when applied to 2 arguments, and not to one.


-Manuel




                 reply	other threads:[~1999-02-19  8:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=25983782061AD111B0800000F86310FE1026CB1B@RED-MSG-42 \
    --to=maf@microsoft.com \
    --cc=caml-list@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