The following is an attempt to put a number of Perl6 concepts into
practice, in order to see how useful and intuitive they actually are.

Complex numbers come in two representations: rectilinear coordinates
and polar coordinates:

class complexRectilinear {
  has $.x, $.y;
  method infix:+ ($a is complexRectlinear, $b is complexRectilinear)
returns complexRectilinear {
    return new complexRectilinear($a.x + $b.x, $a.y + $b.y);
  }
  ...
  method coerce:complexPolar () returns complexPolar {
    return new complexPolar ($.x * $.x + $.y * $.y, atn($.y / $.x) *
(sgn($.x) || sgn($.y)));
  }
  ...
}

... and a similar definition for class polar.

(Technically, coerce:complexPolar and infix:+ are "generators" in the
sense that they create new instances of the class; but ISTR something
about generators not being allowed in classes.)

You should then be able to say:

union complex {
  class complexRectilinear;
  class complexPolar;
}

...or something of the sort.  At this point, you have the ability to
freely represent complex numbers in either coordinate system and to
switch between them at will.  Am I right so far?

Next:

multi exp($a is complex, $b is complex) returns complex { ... }

This defines a function that raises one complex number to the power of
another complex number and returns a third complex number.  One
problem is that if $b's real component isn't an integer, there's no
guarantee that there will be a unique answer.  To address this, we
could provide a second version of this function:

multi exp($a is complex, $b is complex) returns list of complex { ... }

However, if $b's real component isn't a rational number, you won't
have a finite number of elements in the list.  Here's where I get a
little fuzzy: IIRC, there's some means of defining a list by providing
an element generator:

generator element($index) returns complex { ... }

Is there a way of setting things up so that an attempt to ascertain
the list's length would call a separate generator for that purpose?

generator length() returns complex { ... }

At any point above, am I abusing the concept of theories, or failing
to use them to their fullest extent?

With this example, it would be useful if the list of results could
extend in both directions, so that one could advance through them in a
clockwise or counterclockwise direction.  If $b's real component is
rational, it makes some sense to treat [-1] as the last element in the
list, [-2] as the second-to-last, and so on; but if $b's real
component is irrational, there _is_ no positive end to the list, and
it would make sense if [-1] referred to the first result that you
reach by rotating in the clockwise direction from the primary result. 
Can an "infinitely long" list be set up so that [-1] still has
meaning?

Also, even if $b's real component is rational, there are times that
one might want to access a version of the result that is rotated some
multiple of 2*pi radians from one of the main results.  As an example,
let's say that $b = 0.5.  This means that the first result is rotated
zero radians counterclockwise from $a's direction, and corresponds to
[0].  [1] would correspond to being rotated pi radians, and the length
generator would say that there are only two elements in the list. 
Could you set things up so that you could nonetheless access [2]
(which is rotated by 2*pi radians), [3] (rotated by 3*pi radians), or
possibly even have [-1] represent a rotation by -pi radians?

--
Jonathan "Dataweaver" Lang

Reply via email to