Distributing monadic(?) functions across dyadic functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Distributing monadic(?) functions across dyadic functions

Jared Updike
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?

  Jared.
--
http://www.updike.org/~jared/
reverse ")-:"
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Distributing monadic(?) functions across dyadic functions

David Menendez
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 <[hidden email]> | "In this house, we obey the laws
<http://www.eyrie.org/~zednenem>      |        of thermodynamics!"
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Distributing monadic(?) functions across dyadic functions

Dean Herington
In reply to this post by Jared Updike
At 11:58 AM -0700 4/2/06, Jared Updike wrote:

>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?
>
>   Jared.

I think you've reached sufficient higher-order-ness.  Others on the
list have offered your function in a slightly different form:

>  f `on` g = \x y -> g x `f` g y
>  unionBy ((==) `on` fst) listOfPairs1 listOfPairs2

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Off-topic [was: Distributing monadic(?) functions across dyadic functions]

Daniel McAllansmith
In reply to this post by David Menendez
On Monday 03 April 2006 08:09, David Menendez wrote:

> 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.
I especially like the above code after it has been automatically transformed
to use the puppy operator by my email client.

Actually, the more I look at it... maybe the new crop of haskell editors
should define some 'haskicon' replacements. :)

Daniel

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

puppy_op.jpg (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Distributing monadic(?) functions across dyadic functions

Nils Anders Danielsson
In reply to this post by Jared Updike
On Sun, 02 Apr 2006, "Jared Updike" <[hidden email]> wrote:

> Something like "distribute fst (==)" where
>
>> distribute f op x y = f x `op` f y

A function like this has been suggested for the standard libraries a
couple of times before. Someone suggested the name "on", which I quite
like:

  (*) `on` f = \x y -> f x * f y

>> unionBy (distribute fst (==)) listOfPairs1 listOfPairs2

unionBy ((==) `on` fst) xs ys

I think on makes the code rather readable: union by equality on first.

--
/NAD

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe