English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Custom operators in the revised syntax
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-05-12 (04:52)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Custom operators in the revised syntax
On Saturday 12 May 2007 05:40, you wrote:
> On Sat, 2007-05-12 at 03:48 +0100, Jon Harrop wrote:
> > Using, +, +., +| and +|| is better than add, add_float, add_vector,
> > add_matrix but allowing + to be used to all such types ('a -> 'a -> 'a)
> > is much better still. It isn't even that hard to add to the language.
> Oh? It changes the way type inference works.


> I have only seen one algorithm for this an it was
> extremely complicated. If you know a better way I'd
> sure like to know what it is.

AFAIK, you just change the unify to intersect sets of types. I assume the 
implementation of polymorphic variants already does this, so take that and 
make it enforce a single type contructor at function boundaries (maybe 
defaulting in some cases). If you "polymorphic variant" has more than one 
constructor then there is an ambiguity and you flag an error asking for a 
type annotation.

> 	let f a b = a + b
> will pass type checking,

The F# approach is to default + to int, to maintain compatibility with OCaml.

> I know F# supports overloaded operators, but I have no idea
> how it can work.

.NET provides run-time type information so I assume the compiler specializes 
when types are static and resorts to run-time dispatch otherwise. I was 
certainly advised against using generics to implement a float/float32 FFT 
because they incur run-time tests for many simple arithmetic operations, 
killing performance.

> and this kind of constraint, unfortunately, doesn't propagate
> (i.e. the above isn't really a type). Propagating ground
> type sets is actually easy, but once you have higher orders
> it is probably undecidable.

I hadn't thought of that. I just discovered that you cannot add vectors of 
vectors in F#, so it is probably 1st order only.

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal