develooper Front page | perl.perl6.language | Postings from March 2009

junctions and conditionals

Thread Next
From:
Jon Lang
Date:
March 31, 2009 08:58
Subject:
junctions and conditionals
Message ID:
ef30550b0903310858s46b4d39buae91bfcdb9ac7b60@mail.gmail.com
In "Junction Algebra", Martin Kealey wrote:
> On Mon, 30 Mar 2009, Mark J. Reed wrote:
>> >        ( $a <= any(-1,+1) <= $b ) == ( $a <= any(-1,+1) && any(-1,+1) <= $b )
>>
>> Clearly, the RHS is true for $a == $b == 0, but I'm not sure the LHS
>> shouldn't also be.  Isn't it just syntactic sugar for the RHS?
>
> I suspect not. Rather I think that
>
>        $a <= any(-1,+1) <= $b
>
> corresponds to
>
>        $tmp = any(-1,+1);
>        $a <= $tmp <= $b
>
> and thence to
>
>        $tmp = any(-1,+1);
>        $a <= $tmp && $tmp <= $b
>
> The more I think about this, the more I come to the conclusion that a
> junction should appear to have a uniform (single) value in each
> "eigenthread".

Ugh.  Let me float another problem, and a possible solution to both,
that doesn't require persistent threading environments:

    $x = -1 | +1;
    if $x > 0 { say $x }

As I understand junctions right now, the result of this code is identical to:

   say -1 | +1;

In both cases, the problem arises when you apply a predicate to a
junction, and then use the junction within code that only gets
executed according to the truth of the predicate.

The possible solution to both of these: junctions collapse when they
hit conditional statements - but only within the "conditioned scope".
So: within the code block for the "if $x > 0" statement above, $x only
equals +1, since -1 didn't pass the test.  Thus, "say $x" is the same
as "say 1".  In the case of the range test, the code on the RHS of the
&& only executes if the code on the LHS evaluated to true; therefore,
when evaluating the RHS, "$tmp" doesn't include any of the
possibilities that would have failed on the LHS.  That means that:

    0 <= -1 | +1 <= 0

...becomes:

    0 <= -1 | +1 && +1 <= 0

...which, obviously, is false.

Extending this further:

    $x = +1 | -1;
    if $x > 0 { say "$x is positive." }
    else { say "$x is negative." }

I suspect that both codeblocks would be executed; but within the first
block, $x == +1, and within the second codeblock, $x == -1.

    $x = +1 | -1;
    say $x > 0 ?? $x + 2 :: $x * 2;

In this case, $x is +1 when evaluating "$x + 2", and it is -1 when
evaluating "$x * 2".  After this expression is fully evaluated, they
get recombined into a single junction: +3 | -2.

This last one might be easier to manage simply by having $x autothread
through the "?? ::" operator, since you need to be able to reassemble
a junction on the other end.

-- 
Jonathan "Dataweaver" Lang

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About