jcristovao / newtype-generics

A typeclass and set of functions for working with newtypes.
10 stars 11 forks source link

How can ala (or ala') implement under2? #20

Closed kozross closed 6 years ago

kozross commented 6 years ago

I read the following in the documentation for ala:

The reason for the signature of the hof is due to ala not caring about structure. To illustrate why this is important, another function in this package is under. It is not extremely useful; under2 might be more useful (with e.g., mappend), but then we already digging the trench of "What about under3? under4?". The solution utilized here is to just hand off the "packer" to the hof. That way your structure can be imposed in the hof, whatever you may want it to be (e.g., List, Traversable).

However, I am not seeing how you could implement

under2 :: (Newtype n, Newtype n', o' ~ O n', o ~ O n) => (o -> n) -> (n -> n -> n') -> o -> o -> o' 

using either ala or ala'. Am I misunderstanding what the documentation means by under2, the ala and ala' functions, or both?

sjakobi commented 6 years ago

Well, here's a rather terrible implementation of under2 via ala:

under2 :: (Newtype n, Newtype n', o' ~ O n', o ~ O n)
       => (o -> n) -> (n -> n -> n') -> (o -> o -> o')
under2 pa f o0 o1 = ala pa (\p -> uncurry f . bimap p p) (o0, o1)

I do agree though that that paragraph is rather confusing. Suggestions for improving it are very welcome! :)

sjakobi commented 6 years ago

I took a stab at the docs for ala: https://github.com/jcristovao/newtype-generics/commit/34328307da691d4734b47a67583e9b3a40f49c50

I hope this is better.

sjakobi commented 6 years ago

The documentation changes have since been released.

Feel free to reopen if you want to discuss this further! :)