Saturday, September 5, 2009

Thoughts about numeric roles in Perl 6

There was a big discussion about potential numeric classes/roles in Perl 6 on #perl6 a few days ago. Most of it went right over my head, I fear. But I've kept on thinking about it in the days since, and it seems to me there is a pretty simple and mathematically sound layout for the roles. (Instead of worrying about coming up with beautiful names, I'm just going to stick "Role" onto the name of each mathematical concept.) This is a quick high level sketch which I have made no attempt to implement.

The biggest role would be ComplexRole, capturing the idea of numbers with real and imaginary parts (either or both of which might be zero), and implementing a slew of standard mathematical functions that work on complex numbers.

Then there would be a subset RealRole of ComplexRole where { $^z.im == 0.0 }. The most notable added method would be Num, but there would probably also be a lot of RealRole-specific mathematical functions, as they can be a lot simpler than the complex equivalents.

I don't know how to express is as a where, but the RationalRole subset of RealRole would mostly just add numerator and denominator methods. And finally we would have subset IntegerRole of RationalRole where { $^q.denominator == 1 }.

The idea would be that the classes would be built up in the opposite order than this (or not related at all), using the logical computer programming approach, with these roles applied on top to give a mathematical structure. So you'd define a rational class as two integers, then define numerator and denominator methods for the RationalRole, a Num method for the RealRole, and an imaginary method for the ComplexRole.

I'm not sure how practical this is to actually implement in Perl 6, but it seems like it would be a nice way to capture these notions. I'm presuming most of the mathematical functions would be implemented on ComplexRole and RealRole, so this would provide a simple way of plugging any new math types you defined into a full library of math functions.

Update: I've started work on implementing B-spline knot vectors and basis functions in Perl 6, but it will be a few more days before I have something worth posting.

No comments:

Post a Comment