Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Custom operators in the revised syntax
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.

Of course, you can use the Haskell typeclass approach,
where you make the signature predefined as:

	(+): 'a -> 'a -> 'a

and then resolve to the specific type and thus specific
function (or error) after the usual inference/checking phase,
however this *does* require a extra code in the compiler,
possibly a complete extra phase that has to be done before
type information is erased, it introduces new errors:

	let f a b = a + b

will pass type checking, then fail during instantiation,
whereas at present the signature is inferred as

	f: int * int -> int

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

Felix also allows both overloading AND typeclasses,
but it works because it does not have type inference.

The only way I can see to extend Ocaml without breaking it
would require the introduction of a new kind of type,
which would at least allow finite sets of ground types,
for example:

	int \U int16 \U int32 \U float \U bigint \U matrix

Felix actually has these things, they're not types
but nouns of sugar used for constraints:

	'a constraint 'a = int | 'a = int16 ...

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.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net