Previous week Up Next week


Here is the latest Caml Weekly News, for the week of 02 to 09 August, 2005.

  1. ocamlsdl natively for Windows?
  2. ocamllex problem
  3. right-associating infix application operator camlp4 extension

ocamlsdl natively for Windows?


Tobias Elze said:
I succeeded in compiling them with MingW. Special thanks to Sami Mäkelä 
for his invaluable help and suggestions!

In order to spare others hours of annoying work I provide a mini Howto here:

How to compile ocamlsdl for MingW

1) Download the following archives:

a) ocamlsdl ( (e.g.
b) SDL for MingW (precompiled:
c) SDL image (source: , 
the precompiled version for VC6 works for MingW as well:
d) SDL ttf (source: , 
e) SDL mixer 
( . 

(If you need GL support, you may want to download lablgl as well. It has 
to be installed before compiling ocamlsdl!)

2) Install SDL

a) SDL main package

If you downloaded the compiled version for MingW (see above) then unpack 
it to your Cygwin home directory.

- go to cygwin/usr/include/Sdl, edit the file "SDL_main.h", and remove 
the line "#define main ...".
- look for a file named "i386-mingw32msvc-sdl-config" in your /bin 
directory and rename it to "sdl-config".
- edit this file,
        - change the prefix to "/usr",
        - remove all references to SDL_main and SDLmain
        - add "-mno-cygwin"  to "--cflags)"
        - remove all "-lmingw32" flags

b) SDL image, SDL ttf, SDL mixer

- If you use the precompiled packages, just unarchive. I put the *.h 
files into the directory where SDL.h was located and the libraries into 
the directory where the SDL libraries were.

3) Install ocamlsdl

- just ./configure, make, make install -- that's all!

(However, there is no visible standard output of the generated binaries 
-- I don't know if it's a bug or a feature;) ...)

ocamllex problem


Jonathan Roewen asked:
Here's my .mll file, to match IRC strings. The problem is that it's
including spaces, which I assume it shouldn't.

let letter = [^' ']

rule token = parse
        | ':'((letter|' ')* as s)       { STRING s }
        | letter+ as s                  { STRING s }
        | [' ']+                        { token lexbuf }
        | eof                           { EOL }

(BTW, this is for matching rule 2). Maybe I'm just retarded, but this
should work, correct?
He later said, then skaller asked:
> Yes, I'm retarded. Ignore me ;-)

No way .. you picked something I didn't know:

> >        | ':'((letter|' ')* as s)       { STRING s }

I check the manual .. yup, you can match
subgroups like that..

How does that work??? The algorithm for handling
this for a DFA is non-trivial. Any pointers to
the algorithm used?

Alain Frisch pointed me at some nasty papers on
this, one with a regexp -> NFA conversion and the
other with a NFA-> DFA conversion, but I couldn't
figure out how to do the direct regexp->DFA conversion,
I'd sure like to find an algorithm for that..
Alain Frisch answered:
As you can find in the source code of ocamllex:

(* To generate directly a NFA from a regular expression.
     Confer Aho-Sethi-Ullman, dragon book, chap. 3
   Extension to tagged automata.
       Ville Larikari
      ``NFAs with Tagged Transitions, their Conversion to Deterministic
        Automata and Application to Regular Expressions''.
       Symposium on String Processing and Information Retrieval (SPIRE
(See also)
James Woodyatt also answered:
In my OCaml NAE Core Foundation, there is a something you may find  
interesting.  See the [Cf_lex] module and its subordinate [Cf_dfa].   
Since it isn't trying to be a multi-stage programming tool like  
[ocamllex], it produces a parser monad that executes a Lazy-DFA,  
instead of a fully space-time optimized DFA.  At some point, I may  
implement a [study] function that fully evaluates the Lazy-DFA and  
optimizes it, but I don't yet see a compelling need for that.

One thing: the pattern [':'((letter|' ')* as s)] is interesting.   
You're definitely right that something non-trivial is happening  
inside the DFA.  My [Cf_dfa] module does not keep a stack of  
backtracking sequences because I did something else to resolve the  
problem.  Look at the ( $@ ) operators, which allow you to use a  
parser monad on the recognized input sequence to obtain the result of  
a lexical rule.  Using this, you can implement something like the  
feature you're interested in by defining a nested hierarchy of parsers.

I know.  This is probably not what you're looking for.  To get what  
you're looking for, I'd have to extend [Cf_dfa] to handle marker  
nodes in the NFA.  I thought that would be more appropriate for  
[ocamllex] and similar tools, so I didn't do it.  Nice to see  
[ocamllex] did.
Berke Durak then said:
You may also wish to have a look at :

  Thomas Reps, "Maximal-Munch" Tokenization in Linear Time, ACM Trans.
  Program. Lang. Syst., vol. 20, num. 2, 1998, pp.259-273
Jonathan Bryant suggested and David Mentre said:
> Maybe also "Modern Compiler Implementation In Java".  It's in Java, but
> the examples are written in a functional style (as far as is possible).
> Unfortunately, I can't remember the author's name off hand, but it is
> published by Cambridge Press...

The book exists in 3 languages, Java, ML and C :

right-associating infix application operator camlp4 extension


Quôc Peyrot asked and Christian Lindig answered:
> Is there any existing camlp4 extensions to define a $ haskell-like  
> operator?
> example:
> f x $ y z would be equivalent to f x (y z)
> or
> print_endline $ string_of_int x would be equivalent to print_endline  
> (string_of_int x)

You don't need CamlP4 for this. You could define in OCaml:

        let (@@) f x = f x

Now you can write:
         print_endline @@ string_of_int x

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

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