Re: Functional-style pattern matching

2010-03-10 Thread Moritz Lenz
Hi,

Little Walker wrote:
> I've been looking around to see if there's been any discussion of
> introducing functional programming-style pattern matching for method/
> function dispatch.  Could someone point me to any such discussions?

It's done multi dispatch in Perl 6, and you can find an introduction in
a book chapter which we're writing these days:

http://github.com/perl6/book/blob/master/src/multi-dispatch.pod

> I've seen that perl6 is taking on a lot of ideas from the functional
> world (lazy evaluation for instance).  With multi-method dispatch on
> the agenda pattern matching seems inevitable but I haven't been able
> to find information about this or any related discussions.
> 
> This is a contrived example of what I'm referring to:
> 
> sub traverse([Leaf $a]) {
>   # do something
> }
> 
> sub traverse([Tree $left, Tree $right]) {
>   traverse($left);
>   traverse($right);
> }
> 
> my $t = Tree(...);
> traverse($t);

You got it almost right. The simples approach is to use "normal" matching:

multi traverse(Leaf $a) {
   # do something
}

multi traverse(Tree $t) {
   traverse($t.left);
   traverse($t.right);
}

The latter uses a bit of additional code for unpacking the $t parameter;
you can do that in a signature too (which is not yet covered in the book
chapter; you can find some documentation here:
)

multi traverse (Tree $t ( $left, $right) ) {
   traverse($left);
   traverse($right);
}

Since you don't actually access $t anywhere, there's no reason to give
it a name, so you can write this actually as


multi traverse (Tree $ ( $left, $right) ) {
   traverse($left);
   traverse($right);
}

Hope that helps,
Moritz
-- 
Moritz Lenz
http://perlgeek.de/ |  http://perl-6.de/ | http://sudokugarden.de/


Re: Functional-style pattern matching

2010-03-10 Thread Little Walker
> That's almost exactly the example from:
>
>    http://perlcabal.org/syn/S06.html#Unpacking_tree_node_parameters

1. I feel incredibly embarrassed to have missed this
2. This is awesome!



Re: Functional-style pattern matching

2010-03-10 Thread Little Walker
> Which is pretty powerful, really.

Absolutely - I think you're referring to the 'type subset' stuff which
is great.

> This is where Perl 6 is not the same as functional
> languages, since it's got an imperative OO element as well.

True, there can be friction between the functional style and OO, but
look at how Scala manages it with case classes.  When you look at the
implementation, really it boils down to syntactic sugar but then so do
many of the cool new features in Perl 6!

I bring this up because when thinking of what will be possible with
lazy evaluation, junctions, named parameter shorthand, closures, etc.,
etc., somehow pattern matching screams out at me.  It can be concise,
expressive and unambiguous, and very complementary to Perl 6's
existing feature set.  I know a lot of work went into bringing
functional and OO together to make Scala happen, so certainly there
may be impracticalities in just 'adding pattern matching'.

Scala case classes: http://www.scala-lang.org/node/107 or
http://programming-scala.labs.oreilly.com/ch06.html#CaseClasses



Re: Functional-style pattern matching

2010-03-10 Thread Mark J. Reed
Does the unpacking participate in dispatch?  If a Hash comes in as $t
with no 'left' key, will it fail to match?


On Tuesday, March 9, 2010, Little Walker  wrote:
>> Which is pretty powerful, really.
>
> Absolutely - I think you're referring to the 'type subset' stuff which
> is great.
>
>> This is where Perl 6 is not the same as functional
>> languages, since it's got an imperative OO element as well.
>
> True, there can be friction between the functional style and OO, but
> look at how Scala manages it with case classes.  When you look at the
> implementation, really it boils down to syntactic sugar but then so do
> many of the cool new features in Perl 6!
>
> I bring this up because when thinking of what will be possible with
> lazy evaluation, junctions, named parameter shorthand, closures, etc.,
> etc., somehow pattern matching screams out at me.  It can be concise,
> expressive and unambiguous, and very complementary to Perl 6's
> existing feature set.  I know a lot of work went into bringing
> functional and OO together to make Scala happen, so certainly there
> may be impracticalities in just 'adding pattern matching'.
>
> Scala case classes: http://www.scala-lang.org/node/107 or
> http://programming-scala.labs.oreilly.com/ch06.html#CaseClasses
>
>

-- 
Mark J. Reed 


Re: Functional-style pattern matching

2010-03-10 Thread Carl Mäsak
Mark (>):
> Does the unpacking participate in dispatch?  If a Hash comes in as $t
> with no 'left' key, will it fail to match?

Yes.

$ perl6 -e 'sub foo(%h($left)) { say $left }; foo({ left => "OH HAI" })'
OH HAI

$ perl6 -e 'sub foo(%h($left)) {}; foo({ no => "left key" })'
Not enough positional parameters passed; got 0 but expected 1
[...]

The error message is perhaps slightly LTA, but at least Rakudo
(correctly) fails to match.

// Carl


Re: Functional-style pattern matching

2010-03-10 Thread Mark J. Reed
Oh, wow.  I was just asking about the spec; didn't know this stuff
already worked.  Rakudos to the team! :)

On Wed, Mar 10, 2010 at 9:18 AM, Carl Mäsak  wrote:
> Mark (>):
>> Does the unpacking participate in dispatch?  If a Hash comes in as $t
>> with no 'left' key, will it fail to match?
>
> Yes.
>
> $ perl6 -e 'sub foo(%h($left)) { say $left }; foo({ left => "OH HAI" })'
> OH HAI
>
> $ perl6 -e 'sub foo(%h($left)) {}; foo({ no => "left key" })'
> Not enough positional parameters passed; got 0 but expected 1
> [...]
>
> The error message is perhaps slightly LTA, but at least Rakudo
> (correctly) fails to match.
>
> // Carl
>



-- 
Mark J. Reed