Christian Marie | 14 Jan 06:48 2014

flip1 through flip9, useful?

I have defined a bunch of functions like this:

	-- | Move the fourth argument to the first place
	rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)

	-- | Reverse four arguments
	flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)

I decided to upload this as a library to hackage, as I personally find it
useful, especially for writing FFI bindings.

It seems like I can't be the first to write a library like this though, am I
missing something obvious? Is this useful or stupid? Does it exist already?

Full definition here:

https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Ivan Lazar Miljenovic | 14 Jan 07:06 2014
Picon

Re: flip1 through flip9, useful?

On 14 January 2014 16:48, Christian Marie <christian <at> ponies.io> wrote:
> I have defined a bunch of functions like this:
>
>         -- | Move the fourth argument to the first place
>         rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)
>
>         -- | Reverse four arguments
>         flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)
>
> I decided to upload this as a library to hackage, as I personally find it
> useful, especially for writing FFI bindings.
>
> It seems like I can't be the first to write a library like this though, am I
> missing something obvious? Is this useful or stupid? Does it exist already?
>
> Full definition here:
>
> https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs

Except for completeness, I don't see the point of rotate1 and flip1.

I'd also be tempted to possibly shift the module to
Data.Function.Flippers (as flip is re-exported by Data.Function in
base, and it seems like a more valid location to me).

That said, whilst I do use flip upon occasion, I think in the general
case it'd be too easy to abuse these kinds of functions and could be
more difficult to work any errors if you used the wrong one or had
some arguments in the wrong order.

(Continue reading)

Michael Orlitzky | 14 Jan 23:25 2014

Re: flip1 through flip9, useful?

On 01/14/2014 01:06 AM, Ivan Lazar Miljenovic wrote:
> On 14 January 2014 16:48, Christian Marie <christian <at> ponies.io> wrote:
>> I have defined a bunch of functions like this:
>>
>>         -- | Move the fourth argument to the first place
>>         rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)
>>
>>         -- | Reverse four arguments
>>         flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)
>>
>> I decided to upload this as a library to hackage, as I personally find it
>> useful, especially for writing FFI bindings.
>>
>> It seems like I can't be the first to write a library like this though, am I
>> missing something obvious? Is this useful or stupid? Does it exist already?
>>
>> Full definition here:
>>
>> https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs
> 
> Except for completeness, I don't see the point of rotate1 and flip1.
> 

It could be nice if you're attempting something insane with e.g.
template haskell and don't want n=1 to be a special case.
Tony Morris | 14 Jan 07:57 2014
Picon

Re: flip1 through flip9, useful?

On 14/01/14 15:48, Christian Marie wrote:
I have defined a bunch of functions like this: -- | Move the fourth argument to the first place rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e) -- | Reverse four arguments flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e) I decided to upload this as a library to hackage, as I personally find it useful, especially for writing FFI bindings. It seems like I can't be the first to write a library like this though, am I missing something obvious? Is this useful or stupid? Does it exist already? Full definition here: https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs

_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe <at> haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Why not generalise to any functor?

let flip f a = fmap ($a) f

-- Tony Morris http://tmorris.net/
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Christian Marie | 15 Jan 00:30 2014

Re: flip1 through flip9, useful?

On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>    Why not generalise to any functor?
>    let flip f a = fmap ($a) f

I didn't think of that, I suppose.

Now that I do, it seems to be a tradeoff between (arguably) less immediately
obvious type signatures and something.

I can't actually think of what that something is yet. Can you think of a real
world use for a functor flip where normal flip wouldn't do?
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
John Lato | 15 Jan 00:40 2014
Picon

Re: flip1 through flip9, useful?

On Tue, Jan 14, 2014 at 3:30 PM, Christian Marie <christian <at> ponies.io> wrote:
On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>    Why not generalise to any functor?
>    let flip f a = fmap ($a) f

I didn't think of that, I suppose.

Now that I do, it seems to be a tradeoff between (arguably) less immediately
obvious type signatures and something.

I can't actually think of what that something is yet. Can you think of a real
world use for a functor flip where normal flip wouldn't do?

I know I've written "fmap ($a) f" in the past, so I guess any of those would be a use case for a functor flip.  On the other hand, it's only a few characters longer than flip, and arguably more clear, so I'm not convinced that generalizing flip is useful.  YMMV.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Stijn van Drongelen | 15 Jan 00:42 2014
Picon

Re: flip1 through flip9, useful?

On Wed, Jan 15, 2014 at 12:30 AM, Christian Marie <christian <at> ponies.io> wrote:
On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>    Why not generalise to any functor?
>    let flip f a = fmap ($a) f

I didn't think of that, I suppose.

Now that I do, it seems to be a tradeoff between (arguably) less immediately
obvious type signatures and something.

I can't actually think of what that something is yet. Can you think of a real
world use for a functor flip where normal flip wouldn't do?


I think it's mostly a skeleton to use for generalizing flip. Right now, you have

    \f a -> fmap ($a) f :: Functor f => f (a -> b) -> a -> f b

so if you take (f ~ (->) c), you have

    \f a -> fmap ($a) f :: (c -> a -> b) -> (a -> c -> b)

For flips of higher arities, you would choose a different `f`, like (f ~ (->) d . (->) c):

    \f a -> fmap ($a) f :: (c -> d -> a -> b) -> (a -> c -> d -> b)
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Jason Dagit | 15 Jan 01:23 2014
Picon

Re: flip1 through flip9, useful?

While I don't think the overall idea is good*, I do wonder if the explicit numbers in the names could be removed.

Here are some approaches to writing N-ary zipWith that might give you inspiration:

Both have links to the original sources. The first one depends on closed type families and the other one still uses numerals but they are just names for special functions that makes it all hold together.

* I don't think we should encourage use of things like flip8 in 'real code' because it's very likely to make the code completely incomprehensible. I DO think it's a fun and interesting exercise to generalizing flip to N-ary.

Jason
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
João Cristóvão | 16 Jan 18:57 2014
Picon

Re: flip1 through flip9, useful?

For flips of higher arities, you would choose a different `f`, like (f ~ (->) d . (->) c):

    \f a -> fmap ($a) f :: (c -> d -> a -> b) -> (a -> c -> d -> b)

Hi,

Could you give a concrete example of this (for higher arities)?

Thanks,
Joao
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Alexander Solla | 15 Jan 00:38 2014
Picon

Re: flip1 through flip9, useful?




On Mon, Jan 13, 2014 at 10:57 PM, Tony Morris <tonymorris <at> gmail.com> wrote:
On 14/01/14 15:48, Christian Marie wrote:
I have defined a bunch of functions like this: -- | Move the fourth argument to the first place rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e) -- | Reverse four arguments flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e) I decided to upload this as a library to hackage, as I personally find it useful, especially for writing FFI bindings. It seems like I can't be the first to write a library like this though, am I missing something obvious? Is this useful or stupid? Does it exist already? Full definition here: https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs

_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe <at> haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Why not generalise to any functor?

let flip f a = fmap ($a) f

I don't see that operator as "flip-like", personally.  It might behave as flip in the case of the function functor , but that intuition does not carry over to things like

fmap ($a) [f, g, h] = [f a, g a, h a]

I'd call it "funder", personally.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Tom Ellis | 15 Jan 10:56 2014
Picon

Re: flip1 through flip9, useful?

On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
> Why not generalise to any functor?
> 
> let flip f a = fmap ($a) f

FYI, this is Control.Lens.??

    http://hackage.haskell.org/package/lens-3.9.2/docs/src/Control-Lens-Combinators.html#%3F%3F

Tom
Tony Morris | 15 Jan 11:48 2014
Picon

Re: flip1 through flip9, useful?

Indeed! I offered the question as a coaching exercise :)

On 15/01/14 19:56, Tom Ellis wrote:
> On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>> Why not generalise to any functor?
>>
>> let flip f a = fmap ($a) f
> FYI, this is Control.Lens.??
>
>     http://hackage.haskell.org/package/lens-3.9.2/docs/src/Control-Lens-Combinators.html#%3F%3F
>
> Tom
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

--

-- 
Tony Morris
http://tmorris.net/

Gmane