Typed type variables (my Foo ::x)

2005-08-11 Thread Stuart Cook
Hi,

What's the current meaning of type annotations on type-variables?

For example, if I say...

my Foo ::x;

...which of these does it mean?

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?

I seem to recall Damian suggesting (a) in order to solve the what's
the invocant type of a class method problem[1], but I wasn't sure
whether it was already canon.

Also, can I do crazy stuff like this?

my $a = ::Foo;
my ::$a $obj;  # analogous to @$x, where $x is an arrayref


Thanks,

Stuart


[1] http://www.nntp.perl.org/group/perl.perl6.language/21914


Re: Typed type variables (my Foo ::x)

2005-08-11 Thread Autrijus Tang
On Thu, Aug 11, 2005 at 08:02:00PM +1000, Stuart Cook wrote:
 What's the current meaning of type annotations on type-variables?
 
 For example, if I say...
 
 my Foo ::x;
 
 ...which of these does it mean?
 
 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.

 Also, can I do crazy stuff like this?
 
 my $a = ::Foo;
 my ::$a $obj;  # analogous to @$x, where $x is an arrayref

Note that $a at compile time is unbound, so that automatically fails.
Now had you written this:

my $a ::= ::Foo;
my ::$a $obj;

Then I can see it working.

Thanks,
/Autrijus/


pgp1SkCTrbh4L.pgp
Description: PGP signature


Re: Typed type variables (my Foo ::x)

2005-08-11 Thread TSa

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. OTOH ++ might not be specialized
on Foo and thus only ++:(Int -- Int) is applied anyway. But
that implies that Foo $x = 3; $x++ warps $x to Int or weird
things like (4 of Int but 3 of Foo) or (4 of Int but 1 of Foo)
when Foo is considered cyclic.



Also, can I do crazy stuff like this?

   my $a = ::Foo;
   my ::$a $obj;  # analogous to @$x, where $x is an arrayref



Note that $a at compile time is unbound, so that automatically fails.
Now had you written this:

my $a ::= ::Foo;
my ::$a $obj;

Then I can see it working.


But wouldn't it be right away optimized to

   my ::Foo $obj;

Same if it comes from a dynamic scope:

   sub foo( ::Foo )
   {
  my $a ::= ::Foo;
  my ::$a $obj;

  return $obj;
   }
   say foo( Str );
   say foo( Int );
--
$TSa.greeting := HaloO; # mind the echo!


Re: Typed type variables (my Foo ::x)

2005-08-11 Thread Luke Palmer
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 KR 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