Hacker News new | ask | show | jobs
by tome 2145 days ago
> I can't easily try it out in an online compiler to understand it better

Try this. It's a full working program. The packages it depends on are "profunctors" and "product-profunctors".

    {-# LANGUAGE FlexibleInstances     #-}
    {-# LANGUAGE DeriveFunctor         #-}
    {-# LANGUAGE FlexibleContexts      #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE TypeFamilies          #-}
    
    module MyExample where
    
    import qualified Data.Profunctor                 as P
    import qualified Data.Profunctor.Product         as PP
    import qualified Data.Profunctor.Product.Default as D
    
    newtype Zipper a b = Zipper { unZipper :: Traverse ZipList a b }
      deriving Functor
    
    instance a ~ b => D.Default Zipper [a] b where
      def = Zipper (P.dimap ZipList id D.def)
    
    instance P.Profunctor Zipper where
      dimap f g = Zipper . P.dimap f g . unZipper
    
    instance Applicative (Zipper a) where
      pure = Zipper . pure
      f <*> x = Zipper ((<*>) (unZipper f) (unZipper x))
    
    instance PP.ProductProfunctor Zipper where
      purePP = pure
      (****) = (<*>)
    
    cl_map :: D.Default Zipper a b => (b -> r) -> a -> [r]
    cl_map f = getZipList . fmap f . runTraverse (unZipper D.def)