Request for kp6 compiler architecture review

2007-06-19 Thread Flavio S. Glock

I'm working on the development plan for the next generation of the
Perl6-in-Perl6 compiler kp6.  kp6 is implemented in mp6, a basic
version of Perl 6 which is written in mp6 itself, and bootstrapped
on Perl 5.

kp6, while incomplete, runs today and can already parse and compile
a subset of Perl 6.


* Accessing kp6:


The kp6 directory (Pugs /v6/v6-KindaPerl6/) contains the tools you
need for bootstrapping the compiler and for running programs.

Compiler source code can be compiled with:

$ perl mp6.pl  lib/Some/File.pl  lib5/Some/File.pl

Programs written in Perl 6 can be compiled and run with:

# run a test
$ perl kp6-perl5.pl  t/kp6/01-tap.t | perl -Ilib5

# show the compiled code
$ perl kp6-perl5.pl  hello.pl | perltidy

# show the AST
$ perl kp6-perl5.pl --ast  hello.pl | perltidy

There are shortcut scripts for some common operations:

$ perl recompile.pl # recompiles all kp6 source

$ sh run_tests.sh # runs all kp6 tests

Only the Perl 5 backend is fully implemented; Parrot (through
Perl6-in-Parrot), JVM (through Groovy), and Python (possibly
through RPython) are experimental.


* Development Plan:


The most up-to-date document for the kp6 architecture is this
drawing:

http://svn.pugscode.org/pugs/v6/docs/kp6.jpg

# simplified version
http://svn.pugscode.org/pugs/v6/docs/kp6-overview.jpg

kp6 currently implements the compiler almost exactly as drawn.

The current development plan can be accessed from here:

http://pugs.blogs.com/pugs/2007/06/kindaperl6_proj.html

I'm looking for comments on how to improve the compiler internals.

For example:

- One of my basic design principles was to implement AST
transformations as pluggable modules, using a visitor pattern.
Do you see potential problems with this approach?

- BEGIN block side-effects are recorded in a hash. This adds an
overhead to all assignment operations. Do you see a better way to
check for side-effects?

I'd appreciate if I can get a compiler architecture review.

I'm available on #perl6 in freenode (my timezone is -03:00).
You can find me there as 'fglock'.
Please share your comments either here on the mailinglist or via IRC.
I look forward to your feedback

thanks!
- Flavio S. Glock (fglock)


Re: Motivation for /alpha+/ set Array not Match?

2006-09-22 Thread Flavio S. Glock

2006/9/22, Patrick R. Michaud [EMAIL PROTECTED]:


Out of curiosity, why not:

/foo bar bar $xyz:=(foo+)/

and then one can easily look at $xyz.from and $xyz.to, as well
as get to the arrayed elements?  (There are other possibilities as
well.)

I'm not arguing in favor of or against the proposal, just pointing
out that there are ways in the existing scheme to get at what is
wanted.


This aliasing would still work:

   /foo bar bar $xyz:=(foo+)/

 $foo[0]   - match
 $xyz[]  - array of match
 $xyz[0]  - match
 ~$xyz  - stringified capture

I've been using aliasing a few times, and mostly exactly in this situation,
so I thought it would be nice to have a way to express this idea with
fewer words:

/foo bar bar foo+/

 $foo[0]   - match
 $foo[1;*]  - array of match
 $foo[1;0]  - match
 ~$foo[1]  - stringified capture

OTOH, you lose ' @all_foo = $foo ', but maybe ' @all_foo = $foo[]
' could be overloaded to the old behaviour.

Anyway, it's just a thought.

thanks!
- Flavio S. Glock


Re: Using Rules Today

2006-07-07 Thread Flavio S. Glock

2006/7/5, Joshua Gatcomb [EMAIL PROTECTED]:


I have not had a chance to look at Flavio's links yet.  Since no one who
actually knows rules seemed to be inspired to write an example for me - I
will *eventually* figure it out on my own and post back to the list as an
FYI.


Here is a simple one that handles '+' and '*' - it could be simplified
a bit, but this one works for me:
---
use Data::Dumper;
use strict;
use Pugs::Compiler::Token;
use Pugs::Runtime::Match::Ratchet;
use base 'Pugs::Grammar::Base';

Pugs::Compiler::Token-install( 'mul','
   (alnum+)
   [   \\* mul  { return ( $/[0]()*$/{mul}() ) }
   |   \'\'
   ]
' );
Pugs::Compiler::Token-install( 'sum','
   mul
   [   \\+ sum  { return $/{mul}()+$/{sum}() }
   |   \'\' { return $/{mul}() }
   ]
' );

my $s = shift;
my $match = main-sum( $s );
print $match-();
---

- Flavio S. Glock


Re: Using Rules Today

2006-07-03 Thread Flavio S. Glock

2006/7/3, Joshua Gatcomb [EMAIL PROTECTED]:


I am specifically interested in examples that can be run in Perl 5 today
without needing Pugs or Parrot.


http://svn.openfoundry.org/pugs/perl5/Pugs-Compiler-Rule/compile_p6grammar.pl
- doesn't do exactly what you want, but you can see what the syntax
looks like for writing an evaluator using rules in p5.

http://svn.openfoundry.org/pugs/perl5/Pugs-Compiler-Rule/lib/Pugs/Grammar/Rule/Rule.pm
- this is the grammar for rules, written in rules.
The last rules have looser precedence; the rules at the start of the
file have tighter precedence.
This grammar is compiled to p5 using lrep.

- Flavio S. Glock


Re: Set Theory (Was: Do junctions support determining interesections of lists)

2006-04-04 Thread Flavio S. Glock
2006/4/4, Larry Wall [EMAIL PROTECTED]:
 But this is all based on enumerated sets.  Oddly missing are any
 Sets that are defined by rule.  That would presumably take closures,
 though I suppose one can attempt to enumerate the closures that have
 to hold true and autothread through the calls to those closures...

 Can Russel be far behind?  :-)

This is in ext/Recurrence:

use Recurrence;

# all integer numbers
$universe = Recurrence.new(
closure_next = sub { $_ + 1 },
closure_previous = sub { $_ - 1 },
:is_universe(1) );

# all even integers
$even_numbers = Recurrence.new(
closure_next = sub { 2 * int( $_ / 2 ) + 2 },
closure_previous = sub { 2 * int( ( $_ - 1 ) / 2 ) },
universe = $universe );

# all odd integers
$odd_numbers = $even_numbers.complement;

# all non-zero integers
$non_zero = Recurrence.new(
closure_next =sub ($x) { $x == -1 ??  1 !! $x + 1 },
closure_previous =sub ($x) { $x ==  1 ?? -1 !! $x - 1 },
complement_next = sub ($x) { $x  0   ??  0 !!Inf },
complement_previous = sub ($x) { $x  0   ??  0 !!   -Inf },
universe = $universe );

- Flavio S. Glock


Re: Do junctions support determining interesections of lists

2006-04-04 Thread Flavio S. Glock
2006/4/4, Luke Palmer [EMAIL PROTECTED]:
 I don't follow.  Why is that the representation of any(1,2,3)?  Is
 this a disjunctive normal form; i.e. is 2  any(1,2,3) equivalent to
 the test:

  2  1
   || 2  2
   || 2  3
   || 2  1  2  2
   || ...

  2  1
| 2  2
| 2  3

which ends up being the same thing after simplification

- Flavio S. Glock


Re: Binding of list slice elements

2005-11-24 Thread Flavio S. Glock
I'd like to add a few more cases:

(1, $a)[0]  = 42;  # dies ?
(1, $a)[0] := 42;  # dies ?
(1, $a)[1]  = 42;  # modify $a ?
(1, $a)[1] := 42;  # makes $a constant ?

Ingo Blechschmidt wrote:
 (These questions were motivated by Flavio's work in progress [2] (A
 draft on the runtime view of how Lazy things (like Arrays) work).)

Thanks for reading!

- Flavio S. Glock


Lazy lists in Str context

2005-11-23 Thread Flavio S. Glock
Can we have:

  say 1..Inf;

to output an infinite stream, instead of just looping forever?

OTOH, it would be nice if

  say substr( ~(1..Inf), 0, 10 )

printed 1 2 3 4 5.

Flattened lists would still loop forever (or fail):

  say **(1..Inf);

  $s = substr( ~( **(1..Inf) ), 0, 10 );

- Flavio S. Glock


Re: Lazy lists in Str context

2005-11-23 Thread Flavio S. Glock
Juerd:

2005/11/23, Juerd [EMAIL PROTECTED]:
 Flavio S. Glock skribis 2005-11-23 10:13 (-0200):
  Can we have:
say 1..Inf;

 It's important, I think, to note that this isn't item context, but list
 context. Str list context, but still list context. Which means 1..Inf
 isn't stringified as a whole. say will have an array that represents
 the lazy list. It should iterate over that rather than output it all at
 once, anyway, for reasons of conserving memory.

Ah, ok - but I believe that say() is slurpy, which means the list must
be instantiated first.

  to output an infinite stream, instead of just looping forever?

 How do you imagine anything outputs infinite stuff, without looping
 forever? I don't think stdout knows about our kind of laziness :)

There are some reasons for this - you can see what's happening, and
press ctrl-C; or you may wish for it to output until there is a
timeout; or maybe you are writing a daemon which is supposed to run
forever anyway.

  OTOH, it would be nice if
say substr( ~(1..Inf), 0, 10 )
  printed 1 2 3 4 5.

 Here, 1..Inf is stringified as a whole, while with say, each of the
 individual elements of the list are separately stringified. The question
 of lazy strings is an interesting one. It would be very useful, and
 would also allow GREAT things like

 my $revfoo := reverse $foo;
 $revfoo ~~ s/foo/bar/g;

 I wonder if it's doable, though...

I believe it is - I've come to this idea while trying to write down
the Array spec, and it seems pretty feasible.

 Juerd

Thanks!
- Flavio


Re: Lazy lists in Str context

2005-11-23 Thread Flavio S. Glock
2005/11/23, Larry Wall [EMAIL PROTECTED]:

 I think the last one is more feasible than the middle one, at least
 by default.  The problem is that stringification is considered a result
 of a kind of scalar context, and ordinary scalar context is not lazy
 in Perl 6.  So we'd probably need to set up some way of declaring
 this particular string is lazy.

 Basically, we're attaching the whole lazy/nonlazy mess to the
 list/scalar distincion, which I think is a really good default.
 We use ** and lazy() to violate those defaults.

How about allowing reduce() to return a scalar with the same laziness
as the list:

[EMAIL PROTECTED] - a lazy string if @list is lazy
[EMAIL PROTECTED] - a lazy number if @list is lazy

It would look like:

$foo = substr( [~](1..Inf), 10 );
my $revfoo := reverse $foo;
$revfoo ~~ s/foo/bar/g;

- Flavio S. Glock


Re: Test Case: Complex Numbers

2005-11-18 Thread Flavio S. Glock
2005/11/11, Larry Wall [EMAIL PROTECTED]:

 While you're there, also think about the gray area between arrays and hashes,
 and whether .[...] subscripts are just a specialized form of .{...} 
 subscripts.

By the way, are lazy hash slices allowed?

  %h{1...}

I asked this in #perl6 when I was implementing lazy arrays (in the p5
backend), and most people found it was useless. But I thought I would
just ask again :)

- Flavio S. Glock


Re: Classification syntax [Was: Renaming grep]

2005-11-18 Thread Flavio S. Glock
Larry Wall wrote:
 If we had some kind of partitioning operator, it'd probably be generalized
 to sorting into bins by number, where 0 and 1 are degenerate cases for
 booleans.

Cool!
This doesn't solve the general case, but how about a left-side zip:

  zip( @keys, @values ) = %hash;

  zip( @even, @odd ) = 0...;

- Flavio S. Glock


Re: Classification syntax [Was: Renaming grep]

2005-11-18 Thread Flavio S. Glock
2005/11/18, Brent 'Dax' Royal-Gordon [EMAIL PROTECTED]:
 Larry Wall [EMAIL PROTECTED] wrote:
  The name is relatively unimportant in the overall scheme of things.
  I'm more worried about the fact that it's difficult to partition a
  list into multiple lists in one pass without declaring temp arrays.

 Didn't the list agree long ago on a `part` builtin?  I certainly wrote
 List::Part based on that discussion...

In E06:

   ($cats, $chattels) = part is_feline, @animals;

How about a switch syntax instead?

part @animals {
when .is_feline() { @cats }
default { @chattels }
}

In the more general case:

part @a {
when $_  10 { @a }
when $_  20 { @b }
when $_  30 { @c }
@d
};

and:

@a.part:{
when $_  10 { @a }
when $_  20 { @b }
when $_  30 { @c }
@d
};

- Flavio S. Glock


Re: set questions -- Re: $object.meta.isa(?) redux

2005-08-10 Thread Flavio S. Glock
I wonder if infinite sets (recurrences) will be supported - then I'll
move all(ext/Recurrence, ext/Span, ext/Set-Infinite) to
Perl6::Container::Set::Ordered - cool.

- Flavio S. Glock

2005/8/10, Dave Whipp [EMAIL PROTECTED]:
 Luke Palmer wrote:
 
  A new development in perl 6 land that will make some folks very happy.
   There is now a Set role.  Among its operations are (including
  parentheses):


Re: lazy list syntax?

2005-07-29 Thread Flavio S. Glock
Just wondering - would 'reverse =$foo' call '$foo.previous()' ?

- Flavio

2005/7/29, Aankhen [EMAIL PROTECTED]:
 On 7/29/05, Flavio S. Glock [EMAIL PROTECTED] wrote:
  Is for = only for filehandles? I tried:
 
 No, it's for anything that supports iteration... `=$foo` ==
 `$foo.next()`, if I recall correctly.  It's probably not yet
 implemented.
 
 Aankhen


Re: lazy list syntax?

2005-07-28 Thread Flavio S. Glock
2005/7/28, Yuval Kogman [EMAIL PROTECTED]:

 I think unary = is what you want:
 
 my @a = $span.lazy;
 
 for [EMAIL PROTECTED] - $item {
 ...
 }
 
 Ofcourse, my @a = $span.lazy will have to be fixed, but what you
 tried should be working.

Is for = only for filehandles? I tried:

  pugs say for =1
  *** cannot cast from VInt 1 to Handle (VHandle)

- Flavio S. Glock


lazy list syntax?

2005-07-27 Thread Flavio S. Glock
How can I create a lazy list from an object?

I have an object representing the sequence 1..Inf.
I tried creating a Coroutine, and then assigning the Coroutine to an
Array, but it only yielded 1:

  my @a = $span.lazy;   # 1

The coroutine worked fine in a while loop, but it didn't work in a for loop.

This is the implementation (in ext/Span):

coro lazy ($self: ) {
my $iter = $self.iterator();
loop { 
my $n = $iter.next;
return unless defined $n;
yield $n;
}
}

I understand that this is not fully specified yet, but I'd like to
start writing some tests for it.

Thanks!
- Flavio S. Glock