Applicatives and hlists - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Asad Saeeduddin

Does it make sense to say a functor is applicative iff it distributes over heterogeneous lists?

Asad Saeeduddin
type family (f :: a -> b) <$> (xs :: [a]) :: [b]
  where
  _ <$> []       = []
  f <$> (x : xs) = f x : f <$> xs

class Functor f => Applicative' f
  where
  distrib :: HList (f <$> xs) -> f (HList xs)
TheMatten

That's just generalization of Monoidal, isn't it?

Asad Saeeduddin

Well what I'm claiming is that it's neither a generalization nor a specialization, it's exactly equivalent. I'm not sure though. Do you think there's more Applicative's than Monoidal/Applicatives?

TheMatten

"Generalization" as in "it uses more general version of product, not just 2-tuple"

Asad Saeeduddin

Ah, I see. What I'm hoping is that the 2-tuple + 0-tuple version is nevertheless equivalent.

TheMatten

You can write

distrib' :: Applicative f => HList (f <$> xs) -> f (HList xs)
distrib' = \case
  HNil    -> pure HNil
  x :* xs -> (:*) <$> x <*> distrib' xs

pure' :: Applicative' f => a -> f a
pure' = (\(a :* HNil) -> a) <$> distrib (a :* HNil)

(<*|>) :: Applicative' f => f (a -> b) -> f a -> f b
f <*|> a = (\(f' :* a' :* HNil) -> f' a') <$> distrib (f :* a :* HNil)

so they should be I guess
(haven't typechecked this though)