Re: junctions and conditionals

2009-04-02 Thread Jon Lang
Martin Kealey wrote:
> On Tue, 31 Mar 2009, Jon Lang wrote:
>> Another issue: what happens if conditional code mutates a junction
>> that it filtered?  For example:
>>
>>     $x = any (-5 .. 5);
>>     if $x > 0 { $x++ };
>>
>> At this point, which of the following does $x equal?
>>
>>     any(-4 .. 6) # the original junction gets mutated
>>     any(-5 .. 0, 2 .. 6) # the filtered part of the original junction
>> gets mutated; the rest is untouched
>>     any(2 .. 6) # the filtered part of the original junction gets
>> mutated; the rest is lost
>
> I choose #3.
>
> Reality can only take one path through a conditional; which one depends
> on the one/any/all/none binding of the junction.
>
> Once you've passed the conditional, you have:
>
>        one -> single matching value (no longer a junction)
>        any -> filtered list
>        all -> original junction
>        none -> empty (*1)

I'm a bit worried about 'none', for reasons that I'll get to in a moment.

> The "threading" is an implementation detail; the important thing is a
> junction is collection of values and a smart way of rewriting
> expressions that contain them, with special treatment for comparison
> operators (or indeed anything that forces Boolean content):
>
>        $x CMP all($y,$z)       $x CMP $y && $x CMP $z
>        $x CMP one($y,$z)       $x CMP $y ^^ $x CMP $z  (*2)
>        $x CMP none($y,$z)      all($x !CMP $y, $x !CMP $z)
>        $x CMP any($y,$z)       $x !CMP none($y, $x)
>
>        $x OP all($y,$z)        all( $x OP $y, $x OP $z)
>        $x OP any($y,$z)        any( $x OP $y, $x OP $z)
>        $x OP one($y,$z)        one( $x OP $y, $x OP $z)
>        $x OP none($y,$z)       none($x OP $y, $x OP $z)
>
> -Martin
>
> (*1: An argument could be made that "none" should leave the junction alone,
> the same as "all".)
>
> (*2: I would like to suggest that the semantics of "one" be changed to mean
> "pick one" (randomly) rather than "exactly one". In this respect it
> would be the same as "any" except it wouldn't have the overhead of
> filtering *every* match, just at least one.)

A few thoughts here.  First: should autothreading be lazy or eager?
As things stand, it seems to be eager: the moment you pass a junction
into a routine that doesn't ask for a junction in its signature, you
autothread that routine.  This has the nice benefit of being easy to
implement (autothread if the parameter doesn't specify 'junction';
don't autothread if it does); but as has been mentioned before, making
life easy on the implementor isn't a primary design goal for Perl.  I
submit that autothreading should be lazy.

As I see it, there are currently three things that can be done with a junction:

  1. Nothing.  Using the "junction as threads" paradigm, you bundle
the threads together and route them through a given part of the
program as a single "rope".

  2. Autothreading.  You feed each thread through individually, and
rebundle the results on the other side.

  3. Sieving.  You feed only some of the threads through a decision
point into its guarded region, and rebundle at the end of the guarded
region.

Option 3 raises some additional questions.  What do you rebundle at
the end of the guarded region?  Just the threads that were passed
through it, or all of the original threads?  And what happens if the
program flow gets rerouted inside the guarded region such that it
never comes out the other side?

If I say:

$x = any(-5 .. 5)
if $x > 0 { $x++ }
if $x ~~ (-1 .. 1) { $x *= 2 }

What is $x just before and just after '$x++'?  What is $x just before
and just after 'if $x > 0 { $x++ }'?  Ditto with '$x *= 2' and 'if $x
< 0 { $x *= 2 }'?

If I say:

$x = any(-5 .. 5)
if $x > 0 { return }
if $x ~~ (-1 .. 1) { doit($x) }

What junction should doit be receiving?

Second:

sub sayit (Str $s) { say $s }
sayit 'S' | 5;

What should happen?  Should the junction be autothreaded, and then one
of the threads fail due to a type mismatch?  Or does parameter-passing
count as a kind of decision-point, such that you only get the "sayit
'S'" thread?

And what happens with the following?

sayit none('S');

-- 
Jonathan "Dataweaver" Lang


Re: simultaneous conditions in junctions

2009-04-02 Thread Richard Hainsworth

Larry Wall wrote:

On Wed, Apr 01, 2009 at 08:40:12AM -0700, Dave Whipp wrote:
  
That said, the semantics of a chained relop really should work correctly  
for this. If you only reference a junction once in an expression, then  
it should behave as such: {a


Yes, that is the intent.  I consider a chained "1 < $x < 2" to be a
single function from the standpoint of autothreading, something like

chained(1, &infix:«<», $x, &infix:«<», 2);

and the semantics would fall out of that naturally, more or less.

Something similar could also work for the blocks of conditionals
if we were to treat the conditional as a function that autothreads
consistently over both its condition and its blocks; this implies the
junction autothreader must be given enough access to the scope of the
block in question to temporarily override the value of $x within the
block to its autothreaded value somehow.  The tricky bit is that
autothreading is setting up extra scopes around variant captures
without explicit blocks.  That alone is reason enough to keep
the mechanism all internal.
  

Do I understand correctly from this that
my $x = 1|5; say ?(2 < $x < $4) ; # False
my $x = 1|3|5; say ?(2 < $x < $4); # True

If so, then the current implementation of rakudo is generating an error, 
viz.,

$ perl6
> my $x=1|5; say ?( 2 < $x < 4)
1

May be something extra needs to be added to the specs?

If we do give access to universe of values the junction is interested
in, it should probably just be via a coercion to Set.
This would give the necessary extra syntax to ensure that the programmer 
knows what s/he is doing. It means that junctions could be threshed and 
sieved when desired.

  There would have
to be some caveats about using that as the universe of values, however,
since none() junctions define a set outside of the eigenstates.
  
Interesting. Do I understand this correctly: If the junction is finite, 
then none() defines an infinite set, and vice versa.
But then if perl6 is lazy, an infinite set would also be possible 
With access being lazily evaluated. Just as 0 .. inf is lazily evaluated?

For that reason I'd still prefer people to track their universe of
values outside the junctions rather than rely on junctions for that.

And of course, a Set in list context is its members, so any(%set) isn't
a problem going the other direction.

Larry
  


r26063 - docs/Perl6/Spec/S32-setting-library

2009-04-02 Thread pugs-commits
Author: moritz
Date: 2009-04-03 00:48:07 +0200 (Fri, 03 Apr 2009)
New Revision: 26063

Modified:
   docs/Perl6/Spec/S32-setting-library/Str.pod
Log:
[S32/Str] spec Str.trim()


Modified: docs/Perl6/Spec/S32-setting-library/Str.pod
===
--- docs/Perl6/Spec/S32-setting-library/Str.pod 2009-04-02 21:25:39 UTC (rev 
26062)
+++ docs/Perl6/Spec/S32-setting-library/Str.pod 2009-04-02 22:48:07 UTC (rev 
26063)
@@ -501,12 +501,20 @@
 If the replacement string is longer or shorter than the matched sub-string,
 then the original string will be dynamically resized.
 
+=item trim
+
+  multi method trim() is export;
+
+Returns a copy of the string, with leading and trailing whitespaces removed.
+
 =item unpack
 
 =item match
 
  method Match match(Str $self: Regex $search);
 
+See L
+
 =item subst
 
  method Str subst(Str $self: Regex $search, Str $replacement);