caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Infix operators
@ 2004-08-14 12:59 Erik de Castro Lopo
  2004-08-14 13:57 ` Richard Jones
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Erik de Castro Lopo @ 2004-08-14 12:59 UTC (permalink / raw)
  To: caml-list

Hi all,

I've got a number of defined types for which I'd like to define
infix operators for addition, subtraction, multiplication etc.

Unfortunately since each type needs is own infix operator I'm
running out of ideas unique operator versions for each type.

Why is it not possible to do something like:

    let ( ++ ) (a:float) (b:float) = a +. b ;;

    let ( ++ ) (a:int) (b:int) = a + b ;;

which with the full type specification should allow the compiler
to figure out which version to use in a given situation.

Regards,
Erik
-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo  nospam@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"An older MS internal whitepaper from August 2000 on switching
Hotmail, which MS acquired in 1997, from front-end servers
running FreeBSD and back-end database servers running Solaris
to a whole farm running Win2K, reads like a veritable sales
brochure for UNIX"
-- http://www.theregister.co.uk/content/4/28226.html

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Infix operators
  2004-08-14 12:59 [Caml-list] Infix operators Erik de Castro Lopo
@ 2004-08-14 13:57 ` Richard Jones
  2004-08-14 17:10 ` skaller
  2004-08-14 19:04 ` [Caml-list] " Julian Brown
  2 siblings, 0 replies; 5+ messages in thread
From: Richard Jones @ 2004-08-14 13:57 UTC (permalink / raw)
  Cc: caml-list

On Sat, Aug 14, 2004 at 10:59:19PM +1000, Erik de Castro Lopo wrote:
> Hi all,
> 
> I've got a number of defined types for which I'd like to define
> infix operators for addition, subtraction, multiplication etc.
> 
> Unfortunately since each type needs is own infix operator I'm
> running out of ideas unique operator versions for each type.
> 
> Why is it not possible to do something like:
> 
>     let ( ++ ) (a:float) (b:float) = a +. b ;;
> 
>     let ( ++ ) (a:int) (b:int) = a + b ;;
> 
> which with the full type specification should allow the compiler
> to figure out which version to use in a given situation.

GCaml can do this:

http://pauillac.inria.fr/~furuse/generics/

# generic plus =
  | int -> int -> int => (+)
  | float -> float -> float => (+.)
  ;;

(Actually, I'm not sure whether or not it can do the infix ops part).

What is the status of GCaml w.r.t it becoming a part of standard
OCaml?

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
MOD_CAML lets you run type-safe Objective CAML programs inside the Apache
webserver. http://www.merjis.com/developers/mod_caml/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Infix operators
  2004-08-14 12:59 [Caml-list] Infix operators Erik de Castro Lopo
  2004-08-14 13:57 ` Richard Jones
@ 2004-08-14 17:10 ` skaller
  2004-08-14 19:04 ` [Caml-list] " Julian Brown
  2 siblings, 0 replies; 5+ messages in thread
From: skaller @ 2004-08-14 17:10 UTC (permalink / raw)
  To: Erik de Castro Lopo; +Cc: caml-list

On Sat, 2004-08-14 at 22:59, Erik de Castro Lopo wrote:
> Hi all,
> 
> I've got a number of defined types for which I'd like to define
> infix operators for addition, subtraction, multiplication etc.
> 
> Unfortunately since each type needs is own infix operator I'm
> running out of ideas unique operator versions for each type.
> 
> Why is it not possible to do something like:
> 
>     let ( ++ ) (a:float) (b:float) = a +. b ;;
> 
>     let ( ++ ) (a:int) (b:int) = a + b ;;

There reason, roughly, is that it is very hard to combine
type inference with overloading. It can be done (there's
a paper on Citeseer describing an algorithm). It isn't
clear what the error message quality would be though.

The GCaml solution Richard Jones mentions:

# generic plus =
  | int -> int -> int => (+)
  | float -> float -> float => (+.)
  ;;

When you use this:

plus 1 1
plus 1.0 1.0

there is only a *single* plus function,
with several explicitly given typings --
thus avoiding the problem of combining
inference with overloading neatly.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Re: Infix operators
  2004-08-14 12:59 [Caml-list] Infix operators Erik de Castro Lopo
  2004-08-14 13:57 ` Richard Jones
  2004-08-14 17:10 ` skaller
@ 2004-08-14 19:04 ` Julian Brown
  2004-08-14 19:52   ` [Caml-list] " Jean-Baptiste Rouquier
  2 siblings, 1 reply; 5+ messages in thread
From: Julian Brown @ 2004-08-14 19:04 UTC (permalink / raw)
  To: caml-list

On 2004-08-14, Erik de Castro Lopo <ocaml-erikd@mega-nerd.com> wrote:
> Hi all,
>
> I've got a number of defined types for which I'd like to define
> infix operators for addition, subtraction, multiplication etc.
>
> Unfortunately since each type needs is own infix operator I'm
> running out of ideas unique operator versions for each type.

I came up with a totally rubbish solution to this: define an "infix-ising"
operator like so which lets you use "long" operator names made of plain
characters:

  let (%) a b = b a

  let add a b = a + b
  let addf a b = a +. b

  5 %add 6
  10. %addf 12.

This is not a serious suggestion though, since it totally breaks under
lots of situations, mostly involving precedence. Still, I suppose there
might be a use for it somewhere.

Julian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Infix operators
  2004-08-14 19:04 ` [Caml-list] " Julian Brown
@ 2004-08-14 19:52   ` Jean-Baptiste Rouquier
  0 siblings, 0 replies; 5+ messages in thread
From: Jean-Baptiste Rouquier @ 2004-08-14 19:52 UTC (permalink / raw)
  To: caml-list

>>I'd like to define [many] infix operators

>I came up with a totally rubbish solution to this: define an "infix-ising"
>operator like so which lets you use "long" operator names made of plain
>characters:
>
>  let (%) a b = b a
>
>  let add a b = a + b
>  let addf a b = a +. b
>
>  5 %add 6
>  10. %addf 12.


I sometimes use it as a "pipe" operator :

let (|>) x f = f x
let () = input_line stdin |> lexer |> parser' |> computation |> pretty_print

instead of

let input = input_line stdin in
let temp = lexer input in
let temp = parser' temp in
let temp = computation temp in
pretty_print temp;;

-- 
Jean-Baptiste Rouquier

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-08-14 19:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-14 12:59 [Caml-list] Infix operators Erik de Castro Lopo
2004-08-14 13:57 ` Richard Jones
2004-08-14 17:10 ` skaller
2004-08-14 19:04 ` [Caml-list] " Julian Brown
2004-08-14 19:52   ` [Caml-list] " Jean-Baptiste Rouquier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).