[Haskell-cafe] Distributing monadic(?) functions across dyadic functions

David Menendez zednenem at psualum.com
Sun Apr 2 16:09:12 EDT 2006


Jared Updike writes:

> Is there a common way (standard libs, higher order) to express the
> lambda part below? It's not particulary complicated but I think it is
> not higher-order enough
> 
> > unionBy (\x y -> fst x == fst y) listOfPairs1 listOfPairs2
> 
> Something like "distribute fst (==)" where
> 
> > distribute f op x y = f x `op` f y
> 
> would leave
> 
> > unionBy (distribute fst (==)) listOfPairs1 listOfPairs2
> 
> I imagine something involving Arrows and/or zip/curry/uncurry but I
> just can't see it. Is this a case of trying to make something more
> complicated than it is?

If you look at it in terms of folds over pairs,

    cata (&) (x,y) = x & y   -- corresponds to uncurry
    ana f g x = (f x, g x)   -- corresponds to (&&&)
    
Then you can de-forest:

    hylo (&) f g x = f x & g x
    
        -- hylo (&) f g == cata (&) . ana f g
        --              == uncurry (&) . f &&& g
        --
        -- cata (&) == hylo (&) fst snd
        -- ana f g  == hylo (,) f g

This seems remeniscent of pull-backs (or push-outs) in category theory,
but I don't know enough to say for certain.
-- 
David Menendez <zednenem at psualum.com> | "In this house, we obey the laws
<http://www.eyrie.org/~zednenem>      |        of thermodynamics!"


More information about the Haskell-Cafe mailing list