On 8/11/05, TSa <[EMAIL PROTECTED]> wrote:
> HaloO,
> 
> Autrijus Tang wrote:
> > On Thu, Aug 11, 2005 at 08:02:00PM +1000, Stuart Cook wrote:
> >>    my Foo ::x;
> >>a) ::x (<=) ::Foo (i.e. any type assigned to x must be covariant wrt. Foo)
> >>b) ::x is an object of type Foo, where Foo.does(Class)
> >>c) Something else?
> >
> > My current reading is a) -- but only if ::x stays implicitly
> > "is constant".  So your "assigned" above should read "bound".
> 
> Same reading here! ::x is declared as a subtype of Foo.
> 
> Note that the subtype relation is *not* identical to the
> subset relation, though. E.g. Foo (=) (1,2,3) gives
> Foo (<=) Int but Foo is not a subtype of Int if Int requires
> e.g. closedness under ++. That is forall Int $x the constraint
> $t = $x; $x++; $t + 1 == $x must hold. Foo $x = 3 violates
> it because 4 is not in Foo. 

That's perfectly okay.  The following is a valid subtype in Perl:

    subtype Odd of Int where { $_ % 2 == 1 };

Even though it doesn't obey the algebraic properties of Int.

The way to state algebraic properties of a type is the same way as in
Haskell: use type classes.

    macro instance (Type $type, Role $class) {
        $type does $role; ""
    }

    role Addable[::T] {
        multi sub infix:<+> (T, T --> T) {...}
    }

    instance Addable[Int], Int;

This would require the definition of a "multi sub infix:<+> (Int, Int
--> Int)", thus declaring that Ints are closed under addition. 
However, the subtype Odd above also does Addable, but it does
Addable[Int], not Addable[Odd].  That means that you can add two Odds
together and all you're guaranteed to get back is an Int.

Hmm, if we can have K&R C-style where clauses on subs, then we can
constrain certain parameters in the signature.  For instance:

    sub foo ($x, $y)
        where Int $x
        where Int $y
    {...}

Using this style, we can constrain types as well:

    sub foo (T $x, T $y)
        where Addable[T] ::T
    {...}

foo is now only valid on two types that are closed under addition.

Notationally this has a few problems.  First, you introduce the
variable ::T after you use it, which is parserly and cognitively
confusing.  Second, you say T twice.  Third, there is a conflict in
the returns clause:

    sub foo ($x) returns Int
        where Int $x
    {...}

The where there could go with Int as a subtype or with foo as a constraint.

Luke

Reply via email to