I have just changed the typeclass system to allow overloaded
virtual functions. This is necessary to allow this:
typeclass Complex[t,r] {
inherit Eq[t];
inherit FloatRing[t];
inherit Trig[t];
virtual fun real: t -> r;
virtual fun imag: t -> r;
virtual fun abs: t -> r;
virtual fun arg: t -> r;
virtual fun add: r * t -> t;
virtual fun add: t * r -> t;
virtual fun sub: r * t -> t;
virtual fun sub: t * r -> t;
virtual fun mul : t * r -> t;
virtual fun mul : r * t -> t;
virtual fun div : t * r -> t;
virtual fun div : r * t -> t;
}
In this we allow, for example, double precision floats to act
as double precision complex numbers: the overloading is
required at least to allow both 'add' versions (as well as
the complex addition itself, defined in FloatRing[t]).
This appears to work, and I even got an error matching instances
which turned out to be an actual error! The following hack
module is also added:
open module Imagination
{
type sqrt_minus_one = "__ERROR__";
const i : sqrt_minus_one = "__ERROR__";
}
and the 3 complex modules each get this (or equivalent):
fun apply(a:double, b:sqrt_minus_one)=>dcomplex(0.0,a);
and all the above features combined allow this to work:
println (1.0 + 2.0i);
We think it 'mazing!
NOTE: Erick suggested we should provide some reductions
such as, for example:
a i + b i => (a + b) i
a i * b i = - a * b
real ( a i ) => a
It would be interesting to check if such rules can really
speed up typical calculations. C does this with an
'_Imaginary' type. The Felix technique with Erick's suggestion
of using reductions is clearly superior!
[Because, the reductions are automatic and don't require
manually fiddling with silly typing .. :]
Although addition is commutative, this nice rule:
r1 + z1 + r2 => r1 + r2 + z1
won't fly because it requires associativity as well,
which doesn't hold. However this one is true:
r1 + z1 + r2 => z1 + r1 + r2
but probably doesn't give any performance improvement.
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language