VinylRecords/Vinyl

What is the significance of η in rmap?

Closed this issue · 2 comments

Admittedly I don't understand rmap too well, and the only instance of it I can come up with is not very useful:

λ> rmap (fmap id) (Identity "Text" :& Identity 1 :& RNil)
{Identity "Text", Identity 1}

My thinking is if I can understand the significance of η I might be able to come up with actually useful things that rmap can do.

EDIT: I see it's used with First as well to get the first non-nothing value in a Frames example as well:

holyRow :: Rec First '[MyString, MyInt, MyBool]
holyRow = rmap First $ pure (Col "joe") :& Nothing :& Nothing :& RNil

It is notionally a natural transformation, but all that is saying is that it is a function between functors. It is not able to inspect the inner value (i.e. the a in f a); all it can do is change the shape of the structure. To carry that analogy forward, if we have a type data V2 a = V2 a a, then we could write a function,

dup :: Identity a -> V2 a
dup (Identity x) = V2 x x

This is useful in vinyl to control things like aggregate plumbing, as in the example of transforming Maybe into First where the latter lets us accumulate all the values in a particular way (i.e. picking the first available value).

rmap is a somewhat misleading name as it suggests that it changes the leaves of the record structure as with map or fmap. The critical difference is that Rec is like a higher-order functor due to its first type parameter. This means that the "leaves" of a Rec f ts are values of type f t for some t in ts.

thanks @acowley !