Download OCaml 4 Reference Guide

Transcript
Chapter 19
Interfacing C with OCaml
This chapter describes how user-defined primitives, written in C, can be linked with OCaml code
and called from OCaml functions.
19.1
Overview and compilation information
19.1.1
Declaring primitives
User primitives are declared in an implementation file or struct. . . end module expression using
the external keyword:
external name : type = C-function-name
This defines the value name name as a function with type type that executes by calling the
given C function. For instance, here is how the input primitive is declared in the standard library
module Pervasives:
external input : in_channel -> string -> int -> int -> int
= "input"
Primitives with several arguments are always curried. The C function does not necessarily have
the same name as the ML function.
External functions thus defined can be specified in interface files or sig. . . end signatures either
as regular values
val name : type
thus hiding their implementation as a C function, or explicitly as “manifest” external functions
external name : type = C-function-name
The latter is slightly more efficient, as it allows clients of the module to call directly the C
function instead of going through the corresponding OCaml function.
The arity (number of arguments) of a primitive is automatically determined from its OCaml type
in the external declaration, by counting the number of function arrows in the type. For instance,
input above has arity 4, and the input C function is called with four arguments. Similarly,
267