On Mon, Mar 8, 2010 at 12:40 PM, Darren Duncan <dar...@darrenduncan.net> wrote:
> Starting with the context of this piece of Synopsis 2:
>
>  These types do (at least) the following roles:
>
>    Class       Roles
>    =====       =====
>    Str         Stringy
>    Bit         Numeric Boolean Integral
>    Int         Numeric Integral
>    Num         Numeric Real
>    Rat         Numeric Real Rational
>    FatRat      Numeric Real Rational
>    Complex     Numeric
>    Bool        Boolean
> <snip>
>
> So I'm wondering how the existing and anticipated built-in
> routines/operators/methods/etc are supposed to stratify between all these
> layers of roles and composing classes.
>
> Looking at Synopsis 32 seems to provide some answers, although it looks
> rather outdated in some respects, I think predating the above table.
>
> For example, since you've got the Numeric role, which is composed by
> integers, including routines that aren't closed over integers such as "log",
> then what routines are left that are just for Integral or Real or Rational?

For Integral?  There might be "remainder"-based routines, a concept
which only exists for integer math.  Also, such things as "greatest
common factor", or even just "factors".  But these are all just
extensions of the same basic principle: Division is not closed over
integers; but instead of banning integer division because the result
won't always be an integer, we instead allow it with a "best match"
approximation when the result isn't Integral, and add routines that
let us determine which numbers will produce Integrals (e.g., factors),
or that produce some sort of Integral representation of the "rounding
error" (e.g., remainders).

Similar routines could theoretically exist for Rationals and Reals: if
I raise Rational $x to the power of Rational $y, will the result be
Rational?  If not, what's the nearest Rational value to $x where the
result _will_ be Rational?  Such routines are not as standard for
Rational and Real as they are for Integral (and thus probably aren't
suitable for implementation in the Perl library); but the principle
remains.

As well, all three of Real, Rational, and Integral have a set of
operations that Numeric lacks: the comparison operators.  Note that
Complex does Numeric; but there is no "before" or "after" for Complex,
so there cannot be a "before" or "after" for Numeric.  I believe that
infix:«<before after cmp> are supplied by Ordered?  (I could be wrong
about that.)  Whatever it's called, infix:«('<', '<=', '>', '>=',
'<=>') are defined in Real, Rational, and Integral, but not in Numeric
or Ordered.  Likewise, Stringy defines infix:«<lt le gt ge leg>, but
Ordered does not.

> Or actually, there is just one main thing I want to know right now ...
>
> You have roles that look like they're supposed to match one specific class
> each in particular, such as Boolean for Bool, Integral for Int, etc,
> ostensibly in case users want to declare their own classes like them.
>
> So, would Int actually have any of its own methods, or would they *all* be
> provided by Integral?  Likewise with Bool and Boolean?  And so on.

My expectation is that all of Int's methods are supplied by Integral;
all of Bool's methods are supplied by Boolean; all of Complex's
methods are supplied by Numeric; all of Num's methods are supplied by
Real; all of Rat's and FatRat's methods are supplied by Rational; and
all of Str's methods are supplied by Stringy.  Conversely, Bit's
methods are supplied by both Integral and Boolean.

Mind you, this is only in terms of "which methods must the class
implement?" - which, ultimately, is what role composition is all
about.  FatRat implements the Rational methods differently than Rat
does, and Bit might implement the Boolean methods differently than
Bool does.  I expect that Stringy, Integral, and Real supply the
appropriate default implementations for Str, Int, and Num,
respectively.  Rational might be a parametric role, with the default
implementations handed to Rat and FatRat differing only in terms of
which data types they work on; but I could be wrong about this.  I
expect that Boolean supplies the default implementations for Bool, and
I suspect that those implementations _might_ be similar enough to what
Bit needs that they can be used there, too.  OTOH, I expect that
Numeric does not provide the default implementations that are used by
Complex.

-- 
Jonathan "Dataweaver" Lang

Reply via email to