Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lingering questions about Junctions.

4 views
Skip to first unread message

Rod Adams

unread,
Feb 19, 2005, 4:51:47 PM2/19/05
to Perl6 Language List
Okay,

Now that I've largely accepted junctions (except implicit autothreading,
which is Bad.), I see some corners that need to be poked at in terms of
how they fit into the language as a whole.

All of these examples assume an appropriate level of "use junctions;" is
in effect.

- Can junctions be used as array/hash subscripts?

In an rvalue context, this makes sense, in that you can simply return a
junction of the deferences. But in an lvalue context, this gets dubious
for everything except all() junctions. Consider:

@x = 1..10;
@x[any(4,3)] = 7;

What does @x look like now?

@x[all(4,3,2)] = 7;

makes sense, as long as it means:

@x[4,3,2] »=« 7;

I don't want to even think about what:

@x[none(1,2)] = 7;

does.

- Can you have non-scalar junctions?

As the discussions surrounding C< .. > demonstrated, it's not that hard
for someone to create a situation where a junction of lists might come
into existence. But let's consider something a step beyond.

%x = (a => 1, b => 2, c => 3) | (d => 4, e => 5, f => 6);
@y = %x.keys;

Does this explode, or does @y have something useful in it now?

Before dismissing the concept completely, it's perfectly possible to
create something much like a junctive hash via references:

%x = (a => 1, b => 2, c => 3);
%y = (d => 4, e => 5, f => 6);
$z = one(\%x, \%y);
@w = $z.keys;

- What does it mean to sort a list of junctions?

@x = any(1,6), all(2,5), one(3,4);
@x = @x.sort;

Does sort() carp on junctions, or is it just one of the weird things you
have to live with if you're playing with junctions?


-- Rod Adams


Brent 'Dax' Royal-Gordon

unread,
Feb 19, 2005, 7:24:43 PM2/19/05
to Rod Adams, Perl6 Language List
Rod Adams <r...@rodadams.net> wrote:
> - Can junctions be used as array/hash subscripts?
>
> In an rvalue context, this makes sense, in that you can simply return a
> junction of the deferences. But in an lvalue context, this gets dubious
> for everything except all() junctions. Consider:
>
> @x = 1..10;
> @x[any(4,3)] = 7;
>
> What does @x look like now?
>
> @x[all(4,3,2)] = 7;
>
> makes sense, as long as it means:
>
> @x[4,3,2] »=« 7;
>
> I don't want to even think about what:
>
> @x[none(1,2)] = 7;
>
> does.

The naive meaning of all of these would be::

any(map { @x[$_] = 7 } 4,3)
all(map { @x[$_] = 7 } 4,3,2)
one(map { @x[$_] = 7 } 1,2)
none(map { @x[$_] = 7 } 1,2)

But I'm not sure the naive interpretation is correct.

> - Can you have non-scalar junctions?
>
> As the discussions surrounding C< .. > demonstrated, it's not that hard
> for someone to create a situation where a junction of lists might come
> into existence. But let's consider something a step beyond.
>
> %x = (a => 1, b => 2, c => 3) | (d => 4, e => 5, f => 6);
> @y = %x.keys;
>
> Does this explode, or does @y have something useful in it now?

Do junctive operators force scalar context on their arguments? If so,
we know what happens (you get a junction of arrayrefs); if not, I
suppose it's up for negotiation.

> - What does it mean to sort a list of junctions?
>
> @x = any(1,6), all(2,5), one(3,4);
> @x = @x.sort;
>
> Does sort() carp on junctions, or is it just one of the weird things you
> have to live with if you're playing with junctions?

Good question. Spaceship and cmp aren't quite like the normal boolean
ops, unfortunately; I'm not quite sure what to do about that.

...actually...

Pretend for a moment that cmp is implemented exactly as:

multi sub infix:<cmp> (Any|Junction $lhs, Any|Junction $rhs) {
return -1 if $lhs lt $rhs;
return 0 if $lhs eq $rhs;
return 1 if $lhs gt $rhs;
}

Then things compare this way:

any(1,6) cmp all(2,5) = -1 (1 is less than both 2 and 5)
all(2,5) cmp any(1,6) = 1 (both 2 and 5 are greater than 1)
all(2,5) cmp one(3,4) = undef (no conditions match)
one(3,4) cmp all(2,5) = undef (no conditions match)
one(3,4) cmp any(1,6) = undef (no conditions match)
any(1,6) cmp one(3,4) = undef (no conditions match)

Happily, all of these are commutative (is this generally true?), and
the C<undef>s would be treated as 0s. So this actually would work,
although it would sort in an...interesting...order.

--
Brent 'Dax' Royal-Gordon <br...@brentdax.com>
Perl and Parrot hacker

"I used to have a life, but I liked mail-reading so much better."

Damian Conway

unread,
Feb 19, 2005, 7:52:09 PM2/19/05
to Perl6 Language List
Brent 'Dax' Royal-Gordon wrote:

> The naive meaning of all of these would be::
>
> any(map { @x[$_] = 7 } 4,3)
> all(map { @x[$_] = 7 } 4,3,2)
> one(map { @x[$_] = 7 } 1,2)
> none(map { @x[$_] = 7 } 1,2)
>
> But I'm not sure the naive interpretation is correct.

A junction as an array index or hash key returns a junction of the
corresponding elements/entries. So:

@x[4|3] = 7

is the same as:

any(@x[4], @x[3]) = 7

But I've repeated stated my strong belief that junctions are scalar values,
*not* lvalues. So the result of that assignment ought to be:

Can't modify constant item in scalar assignment at demo.pl line 1


>> - Can you have non-scalar junctions?
>

> Do junctive operators force scalar context on their arguments?

Yes.

> If so, we know what happens (you get a junction of arrayrefs);

Yes.

> if not, I suppose it's up for negotiation.

No. ;-)

>> - What does it mean to sort a list of junctions?

Brent's analysis is spot on, assuming that C<cmp> on junctions works as he
envisaged. Personally, I think I'd implement C<cmp> as:

multi sub infix:<cmp> (Any|Junction $lhs, Any|Junction $rhs) {
return -1 if $lhs lt $rhs;

return 1 if $lhs gt $rhs;

return 0
}

instead, but the differences are marginal: Brent's version treats junctions
that can be equal or greater than as being equal; mine favours the inequality
instead.

Ultimately, of course, a particular list of junctions may not be well-ordered
at all. For example:

sort 1|10, 5|6, 11|0

in which *any* permutation of the list is a plausible sort. So sorting
junctions isn't necessarily always meaningful.

Damian

Autrijus Tang

unread,
Feb 19, 2005, 10:23:58 PM2/19/05
to Damian Conway, Perl6 Language List
On Sun, Feb 20, 2005 at 11:52:09AM +1100, Damian Conway wrote:
> But I've repeated stated my strong belief that junctions are scalar values,
> *not* lvalues. So the result of that assignment ought to be:
>
> Can't modify constant item in scalar assignment at demo.pl line 1

Yay for sanity. :) It's how pugs implement it, too.

> Brent's analysis is spot on, assuming that C<cmp> on junctions works as he
> envisaged. Personally, I think I'd implement C<cmp> as:
>
> multi sub infix:<cmp> (Any|Junction $lhs, Any|Junction $rhs) {
> return -1 if $lhs lt $rhs;
> return 1 if $lhs gt $rhs;
> return 0
> }

Hrm, so Junction.isa("Any") == False? I need to retype print(), say()
and other primitives, then.

Thanks,
/Autrijus/

Stefan Lidman

unread,
Feb 19, 2005, 8:45:36 PM2/19/05
to perl6-l...@perl.org
Junctions should be on or off by default, I prefer on.
Having them half-on is bad.

Because if it is half-on people(me) is going to write
C< if $x == 3 | 5 | 7 { > in N places then have to
change it and remember to change it in N-1 places. Oops.

ON or OFF. On please.

/Stefan Lidman

Larry Wall

unread,
Feb 21, 2005, 2:18:46 PM2/21/05
to perl6-l...@perl.org
On Sun, Feb 20, 2005 at 02:45:36AM +0100, Stefan Lidman wrote:
: Junctions should be on or off by default, I prefer on.

Well, I'm certainly biased in favor of ON, but any "training wheels"
we put on (if we put any on at all) will not have the effect of
changing semantics. They would only have the effect of making certain
activities illegal. The semantics of alcohol don't change when you
reach drinking age. Only the pragmatics change.

Larry

0 new messages