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
Re: What about infix operator?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xleroy@p...>
Subject: Re: What about infix operator?
Caml Light allows you to define infix operators (that is,
functions), but not infix constructors. Constructors are not
functions. Concerning the :: infix constructor is built-in at the
level of the parser. What you propose is the best way to circumvent
this limitation:

        type rational = Frac of int * int;;
        let frac x y = Frac(x,y);;
        #infix "frac";;
        1 frac 2;;

However, you won't be able to use "frac" in left-hand sides of

As you have noticed, you are allowed to do:

        let Frac x y = Frac(x,y);;

though this changes the status of "Frac" from constructor to function.
Actually, this is a bug in the current implementation. The phrase
above should be rejected, just as the semantically equivalent fomr
below is rejected:

        let Frac = fun x y -> Frac(x,y);;

This might be fixed some day. So, don't rely on this behavior and name
differently the constructor "Frac" and the infix operator "frac".

> There is any possibility to define as
> prefix operators any combination of symbols, not only identifiers?.

Presently, no. Operators are restricted to be identifiers. Allowing more
operators (e.g. ++ or */ ) would be nice, but requires some changes in
the lexical conventions of Caml Light. For instance: 1+-2 without
blanks is currently parsed as 1 + (-2), but would probably have to be
parsed as prefix +- 1 2 with this extension.


- Xavier Leroy