Version française
Home     About     Download     Resources     Contact us    
Browse thread
How do you declare infix in Ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Rogoff <bpr@b...>
Subject: Re: How do you declare infix in Ocaml
If you take a look at, for instance, the Num library, or the types of 
the infixes in Pervasives, you'll notice that the infix is parenthesized. 
That is also how you declare new ones, or redefine the existing ones. 
For example, 

# let (+@) x y = x + y - 1;;
val ( +@ ) : int -> int -> int = <fun>
# 1 +@ 0;;
- : int = 0

and the new (+@) gets its precedence from the first character of the
lexeme, the "+".

Prefixes are defined similarly, 

# let (~@) = fun x -> String.uppercase x ;;
val ( ~@ ) : string -> string = <fun>
# ~@ "hi there";;
- : string = "HI THERE"

You can also redefine the existing infixes and prefixes, including the
ones like mod, not, and or, but that would be very nasty. 

I wish that there was some mechanism like in Haskell to make infixes out 
of identifiers, but of course we would be back to having directives again 
to resolve ambiguities in precedence and associativity if this were the
case. Camlp4 helps here.

-- Brian

On Tue, 30 May 2000, Steve Stevenson wrote:

> In caml there are directives to do this. The obvious doesn't work in
> Ocaml.
> 
> best regards
> 
> steve
> 
>