Previous week Up Next week

Hello

Here is the latest Caml Weekly News, for the week of 06 to 13 April, 2004.

  1. Release of the NAB network simulator
  2. Dynamically evaluating OCaml code
  3. New release of CamlImages
  4. Threading: Using and Building

Release of the NAB network simulator

Henri Dubois-Ferriere announced:
It's a pleasure to announce the first public release of NAB (Network in a
Box). NAB is a network simulator targeted at wireless ad hoc and sensor
networks. It is developped within the MICS project at EPFL.

Downloads and documentation are available at http://nab.epfl.ch

NAB is written in ocaml, a high-level yet highly efficient programming
language which supports both object-oriented and functional programming
styles.

NAB places a particular emphasisis on scalability and visualization.

Routing protocols currently implemented are AODV, EASE, and FRESH.
Various MAC layers are available including a null MAC and a CSMA MAC.
A 802.11b MAC is under development.

As always, questions, comments, and feedback are most welcome.
    

Dynamically evaluating OCaml code

John Goerzen asked and Basile Starynkevitch answered:
> I am moving from Python to OCaml and one of the things I miss is
> Python's eval() call.  It takes a string representing a bit of Python
> source code, evaluates it, and returns the result.  I would like to be
> able to do similar things with OCaml.

As observed by others, there is a big typing issue around this (what
would be the type of the eval function)?

If you really need something, you could hack the toplevel (at your own
risk). I don't think it is a good idea, unless you did very well
understood what you really want to do.

However, the functional values are usually enough to avoid the "eval",
and an "eval" won't be really safe.

Also, you could use metaocaml, which provide constructs (inspired by
eval) to typefully meta-program, ie generate programs at runtime in a
rather safe manner.

See http://www.cs.rice.edu/~taha/MetaOCaml/ for more.

dynamics (like old work from Leroy & Mauny - see X.Leroy's publication
pages on http://cristal.inria.fr/~xleroy ) and runtime type
information (like in Jun Furse GCaml) might also help - and are in
some remote way a bit related to eval's typing.

In addition to typing issues, there is also a runtime issue: dynamic
code generation really requires garbage collection of executable code,
which is not available in Ocaml (and would be terrible to implement -
it would mean rewrite most of the system)

________________


However, for the beginner, the good answer (at least as given by Ocaml
gurus here) to the usual "I want eval" request is simply "no you don't
really need it"
    
Issac Trotts asked and Basile Starynkevitch answered:
Issac Trotts wrote:
> Basile Starynkevitch wrote:
> > However, for the beginner, the good answer (at least as given by Ocaml
> > gurus here) to the usual "I want eval" request is simply "no you don't
> > really need it"
>
> That being so, how would you use OCaml as an extension language for a C
> program?

I'm not sure to understand your point. Many applications coded in C
embed Ocaml inside. The simplest way is to give the application a
compiled ocaml bytecode file (which can be choosen at runtime) invoked
thru ocaml_main

See section 18.7.4 Main program in C of
http://caml.inria.fr/ocaml/htmlman/manual032.html

An alternative is to have the application being a custum ocaml
program, with lots of C primitives. This means that the ocaml runtime
system has the control and invoke appropriately the C primitives
provided by the application.

If you ask about embedding the ocaml toplevel into your application,
it is a different question. I agree that extending or embedding or
customizing the toplevel is not very well documented.
    
John Goerzen said and Kenneth Knowles answered:
> I've been toying with the
> idea of writing a generic build system for OCaml to address this point
> though.

If you haven't already, take a look at the OCamlMakefile here:
http://www.ai.univie.ac.at/~markus/home/ocaml_sources.html

and ocamlconf (by yours truly) here:
http://kenn.frap.net/ocamlconf/

One of them may your needs, but if not I'm sure the authors would appreciate a
description of their insufficiencies.
    
Nicolas Cannasse added:
Don't forget about OCamake, which does not need any other tool than itself
(and ocaml compilers), and is working very well on both Windows and Unix'es:

http://tech.motion-twin.com/ocamake
    
John Goerzen said and Benjamin Geer answered:
>I'm looking at being able to let
>people insert OCaml expressions directly into otherwise plain-text
>config files.

One option you might not have considered is using Schoca, Christoph
Bauer's implementation of the Scheme language in Caml:

http://sourceforge.net/project/showfiles.php?group_id=2598

Schoca contains an example Caml program that reads a configuration file
written in Scheme.  Scheme has a lot going for it as an extension
language; for one thing, it's very easy to learn.
    
John Goerzen asked and Clément Capel answered:
> I am moving from Python to OCaml and one of the things I miss is
> Python's eval() call.  It takes a string representing a bit of Python
> source code, evaluates it, and returns the result.  I would like to be
> able to do similar things with OCaml.
>
> I have observed that /usr/bin/ocaml, the interactive top-level, is
> itself written in OCaml, which suggests that this should be possible.
> Although I have tried to study the source for this, it seems extremely
> complex and I can't figure out a way to do the simple evaluation
> described above.
>
> Can anyone help me out here?

Try this simple example:

Toploop.initialize_toplevel_env();;

let eval txt = let lb = (Lexing.from_string txt) in
  let phr = !Toploop.parse_toplevel_phrase lb in
  Toploop.execute_phrase true Format.std_formatter phr;;

eval "let add1 x = x +1;;";;
eval "add1 2;;";;

(compile with toplevellib.cma)

But be careful, it can break the typing system.
if you use the Toploop module in the
"string parameter" of the function eval or if you
evaluate it in the toplevel.
But it seems there's a guard with the new version (3.07+2).
    

New release of CamlImages

Pierre Weis announced:
The new version 2.12 of the CamlImages library, by Jun Furuse,
François Pessaux, and Pierre Weis, is now available.

Version 2.12 adds support for lablgtk2 and is also a bug fix release.

CamlImages is an image processing library, which provides to the
objective Caml programmer:

  ** basic functions for image processing and loading/saving various image
  file formats (hence providing a translation facility from format to format),

  ** an interface with the Caml graphics library allows to display
  images in the Graphics module screen and to mix them with Caml
  drawings,

  ** a freetype interface, integrated into the library:
  you can draw texts into images using any truetype fonts,

  ** a set of example programs, including gif animation, slide show, postscript
  converter to print images.

Sources and documentation are available from:

http://pauillac.inria.fr/camlimages/eng.htm

ftp://ftp.inria.fr/INRIA/caml-light/bazar-ocaml/camlimages-2.12.tgz

Contributions and comments are welcome.
    

Threading: Using and Building

John Goerzen asked and Xavier Leroy answered:
> I am looking at using multi-threaded programs in OCaml, but have some
> questions:
>
> *** Regarding the thread support itself
>
> Chapter 24 of the OCaml documentaion says that "The threads library is
> implemented by time-sharing on a single processor.  It will not take
> advantage of multi-processor machines."  That's bad.
>
> But then later on I notice that there are two threading options: system
> threads and VM-level threads.  The introductory paragraph does not seem
> to apply to system threads which, in other languages at least, do not
> behave that way.

Even with system threads, the OCaml runtime system and GC isn't
thread-safe, so only one Caml thread can execute at any given time
(this is achieved via a "master mutex" on the runtime system).  So,
the introductory sentence holds (mostly) for system threads as well:
you will not get parallel execution of Caml threads.  Although on a
multiprocessor with system threads, it is possible to have one Caml
thread running in parallel with one or several C threads.

> *** Regarding building programs and libraries with threading support
>
> My next concern is building programs and libraries to support threading.
> Chapter 24 also mentions that programs must be linked with -thread, and
> all object files compiled with -thread, if the final result is to
> support threads.  Alternatively, -vmthread could be substituted.

These requirements are a bit more stringent than what is actually needed.
The real requirements are:
- The -thread or -vmthread option must be given at link-time
  (so that different versions of the standard library and Unix library
   can be picked up)
- The -thread or -vmthread option must be given when compiling a
  source file that uses modules from the thread library (e.g. Thread, Mutex,
  Event, etc).

Actually, if you do not meet one of these requirements, you'll get
"file not found" errors when compiling or linking.  So, don't worry
about forgetting -thread or -vmthread, the compiler will remind you.

> So my questions are:
>
>  1. Few libraries out there build themselves with -thread or -vmthread.
>     Is that to be considered a bug?  Is there a workaround short
>     of recompiling them?

As a consequence of the requirements above, you can use a library not
compiled with -thread in a program that uses threads.  So, it's not a bug
and you shouldn't worry about the library not being compiled with -thread.
(But beware about non-thread safe libraries, see below.)

>  2. Can a library or object file built with -thread or -vmthread be used
>     in a non-threaded program (one that does not use -thread or
>     -vmthread?)  Can it be used in a threaded program that uses the
>     *other* option?  (-thread vs. -vmthread)  Does the answer to this
>     question vary depending on whether C code is used, or are there
>     other things that can be done in the code to increase compatibility?

Yes.  Yes.  No.  No.

>  3. I am assuming that -thread and -vmthread are not universally
>     supported across OCaml platforms.  Would it be correct to assume,
>     then, that one should check for the presence of -thread or -vmthread
>     at build time?  Do there exist platforms for which neither are
>     supported?

My experience is that -vmthread works on basically all variants of Unix,
-thread works on Windows (with the MS and MinGW ports) and most of the
recent Unix derivatives.

I'm not sure about Windows with the Cygwin port.  It could be that
neither -thread nor -vmthread works on this platform due to
insufficient emulation of Unix syscalls and POSIX threads, but maybe not.

Oh, yes, Mac OS 9 and earlier used to support neither -thread nor
-vmthread, but they now rest in peace.

>  4. If I am developing an application...  what can I do if it is
>     multi-threaded but depends on libraries that are not built in a
>     multi-threaded fashion on the user's system?  What if the libraries are
>     built with the wrong type of threading (-thread vs. -vmthread)?  What
>     if some libraries are built with one type and some with another?

See question 2.  There should be no problems.

>  5. If I am developing a library... what must I do to make it maximally
>     compatible with non-threaded applications and both types of
>     threaded applications the user may be developing?

Give it a thread-safe (reentrant) API.  That is, avoid global storage.
Developers of threaded applications will bless you.

>  6. What considerations must one take into account when developing C
>     interfaces that will be used in multithreaded OCaml programs?

By default, the "master mutex" that ensures single-threaded execution
of Caml code also ensures single-threaded execution of the C code
called from Caml.  That makes it mostly safe to use non-thread-aware C
code in threaded Caml applications.

It is possible for C/Caml interface code to explicitly relinquish the
master mutex using the enter_blocking_section() and leave_blocking_section()
functions.  (Google for these names, you should find earlier posts of
mine describing how to use them.)  This enables another Caml thread to
execute concurrently.

>  7. Do any of the standard build systems (OCamlMake, configure.in, etc)
>     take into account the above answers in a useful way for an
>     application or library developer?

No idea.

>  8. How do I know which, if any, standard or third-party libraries
>     installed on my system are threadsafe, and which threading model
>     they support?

As in most other languages: their documentation should say so clearly,
but in general one has to read their source code to figure this out.
    

Using folding to read the cwn in vim 6+

Here is a quick trick to help you read this CWN if you are viewing it using vim (version 6 or greater).

:set foldmethod=expr
:set foldexpr=getline(v:lnum)=~'^=\\{78}$'?'<1':1
zM

If you know of a better way, please let me know.


Old cwn

If you happen to miss a CWN, you can send me a message and I'll mail it to you, or go take a look at the archive or the RSS feed of the archives.

If you also wish to receive it every week by mail, you may subscribe online.


Alan Schmitt