Folks,

I recently needed to write a series of codes on lambda calculus in perl. As MJD has shown Perl 5 can handle lambda calculus but I am beginning to get tired of whole bunch of 'my $x = shift' needed.


  our $ZERO =
      sub { my $f = shift;
          sub { my $x = shift; $x }};
  our $SUCC =
      sub { my $n = shift;
            sub { my $f = shift;
                  sub { my $x = shift;
                        $f->($n->($f)($x)) }}};
  our $ADD =
      sub{ my $m = shift;
           sub { my $n = shift;
                 sub { my $f = shift;
                       sub { my $x = shift;
                             $m->($f)($n->($f)($x)) }}}};
  our $MULT =
      sub { my $m = shift;
            sub { my $n = shift;
                  sub { my $f = shift;
                        $m->($n->($f)) }}};
  our $POW =
      sub { my $m = shift;
            sub { my $n = shift;
                  $n->($m) }};

And I found that these can be made much, much simpler and more intuitive with Perl 6, even more so than scheme!

  our $ZERO = sub($f){ sub($x){ $x }};
  our $SUCC = sub($n){ sub($f){ sub($x){ $f.($n.($f)($x)) }}};
our $ADD = sub($m){ sub($n){ sub($f){ sub($x){ $m.($f)($n.($f) ($x)) }}}};
  our $MULT = sub($m){ sub($n){ sub($f){ $m.($n.($f)) }}};
  our $POW  = sub($m){ sub($n){ $n.($m) }};

You can even make it simpler by removing dots but I leave it that way because it looks more like the original notation that way (i.e. zero := λf.λx.x).

Runs perfectly fine on Pugs 6.2.8. Add the code below and see it for yourself.

  my $one     = $SUCC.($ZERO);
  my $two     = $SUCC.($one);
  my $four    = $ADD.($two)($two);
  my $eight   = $MULT.($two)($four);
  my $sixteen = $POW.($four)($two);
  for($one, $two, $four, $eight, $sixteen) -> $n {
      $n.(sub($i){ 1 + $i})(0).say
  };

Maybe we can use this for advocacy.

Dan the Perl 6 User Now

P.S. I am surprised to find Pugs does not include this kind of sample scripts.
 

Reply via email to