[Haskell-cafe] Re: Looking for pointfree version
George Pollard
porges at porg.es
Thu Feb 12 06:05:13 EST 2009
On Thu, 2009-02-12 at 11:08 +0100, Heinrich Apfelmus wrote:
> Benja Fallenstein wrote:
> > Kim-Ee Yeoh wrote:
> >>
> >> On the same note, does anyone have ideas for the following snippet? Tried the
> >> pointfree package but the output was useless.
> >>
> >> pointwise op (x0,y0) (x1,y1) = (x0 `op` x1, y0 `op` y1)
> >
> > import Control.Monad.Reader -- for the (Monad (a ->)) instance
> > import Control.Bifunctor -- package category-extras
> >
> > dup = join (,)
> > mapPair = uncurry bimap
> > pointfree = (mapPair .) . mapPair . dup
> >
> > Or if you're not afraid of *some* points, and want to avoid the imports:
> >
> > dup x = (x,x)
> > mapPair (f,g) (x,y) = (f x, g y)
> > pointfree op = mapPair . mapPair (dup op)
> >
> > That what you're looking for? :-)
>
> The pairs are of course an applicative functor
>
> (<*>) = uncurry (***) -- from Control.Arrow
> pure x = (x,x)
>
> pointwise op x y = pure op <*> x <*> y
>
>
> Regards,
> apfelmus
Concretely (this might do with a few laziness notations):
> import Control.Applicative
>
> data Pair a = a :*: a
>
> instance Functor Pair where
> f `fmap` (x :*: y) = f x :*: f y
>
> instance Applicative Pair where
> (f :*: g) <*> (x :*: y) = f x :*: f y
> pure x = x :*: x
>
> pointfree :: (a -> b -> c) -> Pair a -> Pair b -> Pair c
> --pointfree o x y = pure o <*> x <*> y
> pointfree = ((<*>) .) . (<*>) . pure
> -- in the applicative paper notation:
> --pointfree o x y = [| o x y |]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090212/441adb32/attachment.bin
More information about the Haskell-Cafe
mailing list