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
[Caml-list] operator overloading
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-13 (21:35)
From: Johan_Georg_Granström <georg.g@h...>
Subject: Re: [Caml-list] operator overloading
> Looking at the Google results, operator overloading has come up
> as a frequently discuseed topic.  Still I didn't find anything
> very useful in the posts that Google brought up.  Can anyone
> recommend a way to work around OCaml's lack of operator overloading
> when dealing with matrices, vectors, and spinors?  Is there a
> way to implement this with ocamlp4?

I had a way of working around lack of overloading, which is
actually pretty nice, when working with LISP a couple of years
ago. You simply define a macro which creates local bindings
for the arithmetic functions: then you can write for example

(let ((A (make-van-der-monde 3))
      (B (make-matrix '((4 2 1) (7 4 1) (8 5 2)))))
    (* (+ B A) (- B A))))

where make-van-der-monde and make-matrix are made up functions
and with-matrix-arithmetic is the macro you define. In ocaml
I guess this would become something like:

let a = make_van_der_monde 3
and b = make_matrix [|...|] in
    (B + A) * (B - A)

By defining the proper macro (using camlp4 I guess), this would
expand to something like:

let a = make_van_der_monde 3
and b = make_matrix [|...|] in
  let (+) = (Matrix.+)
  and (-) = (Matrix.-)
  and (*) = (Matrix.*) in
    (B + A) * (B - A)

Similar macros could be defined for computation in whatever
ring/field/group or whatever, you like...

This is just an idea...


- Johan Granström

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: