Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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)))))
 (with-matrix-arithmetic
    (* (+ 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
  matrix_arithmetic
    (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...

Yours,

- Johan Granström

-------------------
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