Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
how to implement generic operators
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: Ant: [Caml-list] how to implement generic operators
On Mon, 2005-10-24 at 20:26 +0200, Martin Chabr wrote:

> I was surprised at first when I found out how OCaml
> restricts lists.

> Now I got used to it and appreciate the clarity: types
> in mixed expressions must always be converted to be
> compatible with each other. No automatic conversion
> where you wonder what will happen.

> C# had collections of mixed types as well in the 1.x
> version. Now, in the version 2.0 they have generics
> and strong type checking and everybody seems to like
> it, not having to do any type detection and casting.

The idea is to provide 'manifest' heterogeneity
like this:

iter print (1,2.0,"Hello");

which of course is just sugar for

print 1; print 2.0; print "Hello";

This will work in Felix because it supports overloading.
There is no loss of type safety, since each of the 
3 generated terms is independently type checked.

Internally, the Ocaml representation would be something like

Iter (print, [1;2.0;"Hello"])

and the reduction rule would be something like:

| Iter (op, elts) -> fold_left 
  (fun (result, elt) -> seq (result, op elt)) nop elts

So basically this operation is just sugar which takes
advantage of 'ad hoc polymorphism' aka overloading to
allow the user to get 'free' nary operators.

Note that the second argument to 'iter', 
namely (1,2.0, "hello"), is a *tuple*. It is therefore
heterogenous automatically, but of fixed length.

In Ocaml the *representation* is a list of terms, all of
the same type, namely 'expression'.

What I wrote above using Iter variant will work just fine.
The problem is, I also need Compose, Ravel, ... and many MANY
others. There are far too many to encode all of them in the
compiler: a reasonable fixed subset will remain both incomplete
and a lot of work to encode.

So instead of encoding them all in the compiler, I want
to encode them in the standard library, parse them,
and write an interpreter in Ocaml to 'execute' the 
parsed encodings.

See Jacques Carette post and my response for more.

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: