Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] [ANN] The Missing Library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jdh30@c...>
Subject: Re: [Caml-list] Re: [ANN] The Missing Library
On Wednesday 28 April 2004 6:14 pm, skaller wrote:
> I have *seen* a heat flow algorithm which can be
> tested on a 2D object, and the algorithm then
> applied to a 3D object -- without changing anything.

I designed and wrote C++ code like this for my PhD in Physics. It was 
partially specialised over the dimensionality of the Euclidean space and over 
the tupleness of particle interactions (i.e. n=2 for pair potential etc.). I 
used C++ templates to perform the partial specialisation. Short of finding 
and having to work around a bunch of bugs in gcc, the resulting code was 
reliable and considerably more efficient than unspecialised code.

> Just try to do that in C or Fortran or Ocaml.

I bet MetaCaml would do a much, much better job than C++. And I bet it 
wouldn't segfault the compiler. ;-)

> You can't.

That isn't true, of course. They're all Turing complete so, if the worst comes 
to the worst, you can write a Fish compiler in C...

> In C for example, a typical operation
> on a 2D object is a double loop:
>
> 	for(i=
> 	for(j=
>
> but for 3D you need:
>
> 	for(i=
> 	for(j=
> 	for(k=

The first step in writing such code correctly is to realise that this is a bad 
way of looking at the problem. You don't want nested loops, you just want a 
function to propagate computation, whether it be to the next element or to 
the first element on the next row. Incidentally, there is no performance loss 
in making this part of the generalisation.

> that is, you have no choice implementing generalised
> tensor mathematics than to rewrite the program
> for every value of n, the rank.

That is only true if you approach the problem wrongly. If you approach the 
problem correctly then you can do everything that you are asking for.

> Yet the (tensor) maths is identical and independent
> of the rank. I guess there are numerous other
> science problems where you do a calc for a certain
> set of generalised coordinates .. and then need
> to add more coordinates and recalculate ..
> only you have to rewrite the program every time.

Yes, the ability to write such code is imperative (ho, ho, ho) for scientific 
code. I found it very useful to debug my algorithms in 2D before using them 
for production stuff in 3D.

> For more general data stuctures of the form
>
> type 'a x = X of int * 'a | Y of 'a * 'a | Empty
>
> etc, it is clear how to write a map function.
> But each such data structure has a distinct map
> function with a distinct type.

Yes, for efficiency you want to use a tuple (2-tuple for 2D, 3-tuple for 3D 
etc.) but for generality you want to use a list. Partial specialisation gives 
you the best of both worlds.

> So clearly .. with functorial polymorphism
> you get a LOT more resuable code than at the moment.

Not if you do it properly.

Cheers,
Jon.

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