develooper Front page | perl.perl6.language | Postings from September 2005

Hyper fmap

From:
Luke Palmer
Date:
September 10, 2005 15:37
Subject:
Hyper fmap
Message ID:
7ca3f01605091015376d8454ab@mail.gmail.com
I think we should generalize the hyper stuff a little bit more.  I
want hyper operators serve as "fmap", or "functor map", rather than
just list.  This is a popular concept, and a pretty obvious
generalization.

A functor is any object $x on which you can do "fmap" such that it
satisfies these laws:

    $x.fmap:{$_} eqv $x               # identity maps to identity

    $x.fmap:{ f($_) }.fmap:{ g($_) } eqv
    $x.fmap:{ g(f($_)) }              # transparent to composition

Another way to think of a functor is just some sort of aggregate data
structure, where "fmap" is what you use to do something to each of its
elements.

In order to do this, I want to desymmetricalize hyper operators again.
 That is, you put the arrows on the side of the operator you have the
list (now functor).  This is so that if you have a functor on either
side of a hyper operator, it knows which side to map.

Here's how it works:

    $x >>+ $y         #  $x.fmap:{ $_ + $y }
    $x +<< $y         #  $y.fmap:{ $x + $_ }
    +<< $x            #  $x.fmap:{ +$_ }
    foo(1, >>$x<<, 2) #  $x.fmap:{ foo(1, $_, 2) }

etc.

For the current hyper semantics where (1,2,3) >>+<< (4,5,6) eqv
(5,7,9), there is also a "binary functor".  I call the mapping
function fmap2 (but maybe we can find a better name).

    fmap2($a, $b, { $^x + $^y })

It takes a two argument function and two functors and returns the
appropriate combination.  Not all functors have to be binary functors
(it's not clear that binary functors have to be regular functors
either).

    $x >>+<< $y       # fmap2($x, $y, &infix:<+>)

Luke



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