Search code examples
scalahaskellfunctional-programmingscalazcomonad

What is the difference between the Store Comonad and a Representable Store Comonad in functional programming?


The Representable Store Comonad and the Store Comonad offers similar features... When should we use one over the other, and what are the benefits?


Solution

  • For reference, here's a quick recap on what they are:

    class {- ... => -} Representable f where
        type Key f
        -- ...
    
    data RepStore f a = RepStore (Key f) (f    a)
    data    Store s a =    Store s       (s -> a)
    

    Note that in particular

    instance Representable (s -> a) where
        type Key (s -> a) = s
        -- ...
    

    and so we have directly that Store s and RepStore (s ->) are pretty much completely interchangeable. In the other direction, category theory teaches us that all Representable functors are isomorphic to functions (with their Key as the domain), hence RepStore f and Store (Key f ->) are isomorphic.

    In summary: in most cases, it doesn't really matter which you choose. If you plan to use it only on functions anyway, you might as well use Store and benefit from its syntactic lightness; if you wish to use some representable functor that isn't exactly functions (say, memoized functions or something like that), then RepStore is an appropriate generalization.