Re: [Haskell-cafe] inv f g = f . g . f

2013-08-19 Thread Nikita Danilenko
Hi,

as for the nomenclature - mathematically the pattern

f^{-1} . g . f

is sometimes called "conjugation" [1]. One (trivial) type of occurrence is

data Foo a = Foo { unFoo :: a }
deriving Show

instance Functor Foo where

fmap f = Foo . f . unFoo

The under function from the lens library [2] allows expressing this as
follows:

instance Functor Foo where

fmap = under (iso Foo unFoo)

which is a very elegant way of capturing the essence of the pattern.

Best regards,

Nikita

[1] http://en.wikipedia.org/wiki/Conjugacy_class
[2]
http://hackage.haskell.org/packages/archive/lens/3.9.0.2/doc/html/Control-Lens-Iso.html#v:under

On 17/08/13 22:57, Dan Burton wrote:
> The lens docs even have an example of another helper function,
> "involuted" for functions which are their own inverse.
>
> >>> "live" & involuted reverse %~ ('d':)
> "lived"
>
> inv f g = involuted f %~ g
>
> http://hackage.haskell.org/packages/archive/lens/3.9.0.2/doc/html/Control-Lens-Iso.html#v:involuted
>
> -- Dan Burton
>
>
> On Sat, Aug 17, 2013 at 1:43 PM, Dan Burton  > wrote:
>
> This is indeed a job for lens, particularly, the Iso type, and the
> "under" function. Lens conveniently comes with a typeclassed
> isomorphism called "reversed", which of course has a list instance.
>
> >>> under reversed (take 10) ['a'.. 'z']
> "qrstuvwxyz"
>
> -- Dan Burton
>
> On Aug 17, 2013 10:23 AM, "Anton Nikishaev"  > wrote:
>
> Christopher Done  > writes:
>
> > Anyone ever needed this? Me and John Wiegley were discussing
> a decent
> > name for it, John suggested inv as in involution. E.g.
> >
> > inv reverse (take 10)
> > inv reverse (dropWhile isDigit)
> > trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
> >
> > That seems to be the only use-case I've ever come across.
>
> And it's here only because reverse^-1 ≡ reverse, is not it?
> I only can see how f ∘ g ∘ f^-1 can be a pattern.
>
> > There's also this one:
> >
> > co f g = f g . g
> >
> > which means you can write
> >
> > trim = co (inv reverse) (dropWhile isSpace)
> >
> > but that's optimizing an ever rarer use-case.
>
>
> --
> lelf
>
>
>
> ___
> Haskell-Cafe mailing list
> [email protected] 
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
>
> ___
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe




signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread John Wiegley
> Dan Burton  writes:

 under reversed (take 10) ['a'.. 'z']
> "qrstuvwxyz"

Excellent, thanks!

-- 
John Wiegley
FP Complete Haskell tools, training and consulting
http://fpcomplete.com   johnw on #haskell/irc.freenode.net

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


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Dan Burton
The lens docs even have an example of another helper function, "involuted"
for functions which are their own inverse.

>>> "live" & involuted reverse %~ ('d':)
"lived"

inv f g = involuted f %~ g

http://hackage.haskell.org/packages/archive/lens/3.9.0.2/doc/html/Control-Lens-Iso.html#v:involuted

-- Dan Burton


On Sat, Aug 17, 2013 at 1:43 PM, Dan Burton wrote:

> This is indeed a job for lens, particularly, the Iso type, and the "under"
> function. Lens conveniently comes with a typeclassed isomorphism called
> "reversed", which of course has a list instance.
>
> >>> under reversed (take 10) ['a'.. 'z']
> "qrstuvwxyz"
>
> -- Dan Burton
> On Aug 17, 2013 10:23 AM, "Anton Nikishaev"  wrote:
>
>> Christopher Done  writes:
>>
>> > Anyone ever needed this? Me and John Wiegley were discussing a decent
>> > name for it, John suggested inv as in involution. E.g.
>> >
>> > inv reverse (take 10)
>> > inv reverse (dropWhile isDigit)
>> > trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
>> >
>> > That seems to be the only use-case I've ever come across.
>>
>> And it's here only because reverse^-1 ≡ reverse, is not it?
>> I only can see how f ∘ g ∘ f^-1 can be a pattern.
>>
>> > There's also this one:
>> >
>> > co f g = f g . g
>> >
>> > which means you can write
>> >
>> > trim = co (inv reverse) (dropWhile isSpace)
>> >
>> > but that's optimizing an ever rarer use-case.
>>
>>
>> --
>> lelf
>>
>>
>>
>> ___
>> Haskell-Cafe mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Dan Burton
This is indeed a job for lens, particularly, the Iso type, and the "under"
function. Lens conveniently comes with a typeclassed isomorphism called
"reversed", which of course has a list instance.

>>> under reversed (take 10) ['a'.. 'z']
"qrstuvwxyz"

-- Dan Burton
On Aug 17, 2013 10:23 AM, "Anton Nikishaev"  wrote:

> Christopher Done  writes:
>
> > Anyone ever needed this? Me and John Wiegley were discussing a decent
> > name for it, John suggested inv as in involution. E.g.
> >
> > inv reverse (take 10)
> > inv reverse (dropWhile isDigit)
> > trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
> >
> > That seems to be the only use-case I've ever come across.
>
> And it's here only because reverse^-1 ≡ reverse, is not it?
> I only can see how f ∘ g ∘ f^-1 can be a pattern.
>
> > There's also this one:
> >
> > co f g = f g . g
> >
> > which means you can write
> >
> > trim = co (inv reverse) (dropWhile isSpace)
> >
> > but that's optimizing an ever rarer use-case.
>
>
> --
> lelf
>
>
>
> ___
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Anton Nikishaev
Christopher Done  writes:

> Anyone ever needed this? Me and John Wiegley were discussing a decent
> name for it, John suggested inv as in involution. E.g.
>
> inv reverse (take 10)
> inv reverse (dropWhile isDigit)
> trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
>
> That seems to be the only use-case I've ever come across.

And it's here only because reverse^-1 ≡ reverse, is not it?
I only can see how f ∘ g ∘ f^-1 can be a pattern.

> There's also this one:
>
> co f g = f g . g
>
> which means you can write
>
> trim = co (inv reverse) (dropWhile isSpace)
>
> but that's optimizing an ever rarer use-case.


-- 
lelf



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


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Joachim Breitner
Hi,

Am Samstag, den 17.08.2013, 11:11 +0200 schrieb Christopher Done:
> inv reverse (take 10)

if you want that fast and lazy, check out
http://www.joachim-breitner.de/blog/archives/600-On-taking-the-last-n-elements-of-a-list.html

Greetings,
Joachim

-- 
Joachim “nomeata” Breitner
  [email protected] • http://www.joachim-breitner.de/
  Jabber: [email protected]  • GPG-Key: 0x4743206C
  Debian Developer: [email protected]


signature.asc
Description: This is a digitally signed message part
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Tobias Dammers
Note that at least for the dropWhile example, there is a specialized
function, dropWhileEnd, which is most likely more efficient than reversing
the list twice.
On Aug 17, 2013 3:35 PM, "Tom Ellis" <
[email protected]> wrote:

> On Sat, Aug 17, 2013 at 11:11:07AM +0200, Christopher Done wrote:
> > Anyone ever needed this? Me and John Wiegley were discussing a decent
> > name for it, John suggested inv as in involution. E.g.
> >
> > inv reverse (take 10)
> > inv reverse (dropWhile isDigit)
> > trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
>
> This sounds like a job for a lens, or similar.
>
> Tom
>
>
> ___
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Tom Ellis
On Sat, Aug 17, 2013 at 11:11:07AM +0200, Christopher Done wrote:
> Anyone ever needed this? Me and John Wiegley were discussing a decent
> name for it, John suggested inv as in involution. E.g.
> 
> inv reverse (take 10)
> inv reverse (dropWhile isDigit)
> trim = inv reverse (dropWhile isSpace) . dropWhile isSpace

This sounds like a job for a lens, or similar.

Tom


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


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Ian Ross
In J (a sort of dialect of APL), there's a thing called "under", written
"&.".  The expression "(f &. g) x" is equivalent to "(g^:_1) (f (g x))"
where "g^:_1" is J's "obverse" of g, which in cases where it exists is
usually the inverse of g (
http://www.jsoftware.com/help/dictionary/intro26.htm).  Abusing notation
with some weird mixture of Haskell and J, this means that "((+) &. log)"
multiplies numbers by taking logs, adding and exponentiating.  You "inv" is
"under" for cases where g == g^-1 (reverse being a good example).  In cases
where g /= g^-1, it's obviously a useful operation, but the case where g ==
g^-1 seems a bit specialised.  Can you think of any other useful cases than
g == reverse?  I guess "inv (1/) sum" is the harmonic mean, but that's
another special case.


On 17 August 2013 11:40, Mateusz Kowalczyk  wrote:

> On 17/08/13 10:11, Christopher Done wrote:
> > Anyone ever needed this? Me and John Wiegley were discussing a decent
> > name for it, John suggested inv as in involution. E.g.
> First thing I thought was ‘inverse’…
> >
> > inv reverse (take 10)
> > inv reverse (dropWhile isDigit)
> > trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
> >
> > That seems to be the only use-case I've ever come across.
> >
> I do this a lot as well. Why not skip the ‘g’ all together and have ‘f .
> reverse . f’ if that's all we're doing? You could even call it fromEnd
> at that point and we end up with a rather intuitive ‘fromEnd (drop 10)’.
> Maybe even just have an operator.
> > There's also this one:
> >
> > co f g = f g . g
> >
> > which means you can write
> >
> > trim = co (inv reverse) (dropWhile isSpace)
> >
> > but that's optimizing an ever rarer use-case.
> >
>
>
> Is this a proposal for addition to something or is it just general
> discussion?
>
>
> --
> Mateusz K.
>
> ___
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


-- 
Ian Ross   Tel: +43(0)6804451378   [email protected]
www.skybluetrades.net
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Mateusz Kowalczyk
On 17/08/13 10:11, Christopher Done wrote:
> Anyone ever needed this? Me and John Wiegley were discussing a decent
> name for it, John suggested inv as in involution. E.g.
First thing I thought was ‘inverse’…
> 
> inv reverse (take 10)
> inv reverse (dropWhile isDigit)
> trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
> 
> That seems to be the only use-case I've ever come across.
> 
I do this a lot as well. Why not skip the ‘g’ all together and have ‘f .
reverse . f’ if that's all we're doing? You could even call it fromEnd
at that point and we end up with a rather intuitive ‘fromEnd (drop 10)’.
Maybe even just have an operator.
> There's also this one:
> 
> co f g = f g . g
> 
> which means you can write
> 
> trim = co (inv reverse) (dropWhile isSpace)
> 
> but that's optimizing an ever rarer use-case.
> 


Is this a proposal for addition to something or is it just general
discussion?


-- 
Mateusz K.


0x2ADA9A97.asc
Description: application/pgp-keys
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] inv f g = f . g . f

2013-08-17 Thread Ivan Lazar Miljenovic
On 17 August 2013 19:11, Christopher Done  wrote:
> Anyone ever needed this? Me and John Wiegley were discussing a decent
> name for it, John suggested inv as in involution. E.g.

In terms of a decent name: as soon as I saw the subject, I thought you
were somehow inverting a function :/

In terms of how useful it is, I don't think I tend to use such an idiom.

>
> inv reverse (take 10)
> inv reverse (dropWhile isDigit)
> trim = inv reverse (dropWhile isSpace) . dropWhile isSpace
>
> That seems to be the only use-case I've ever come across.
>
> There's also this one:
>
> co f g = f g . g
>
> which means you can write
>
> trim = co (inv reverse) (dropWhile isSpace)
>
> but that's optimizing an ever rarer use-case.
>
> ___
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe



-- 
Ivan Lazar Miljenovic
[email protected]
http://IvanMiljenovic.wordpress.com

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