Skip to content

Instantly share code, notes, and snippets.

@echatav
Last active July 27, 2025 19:42
Show Gist options
  • Select an option

  • Save echatav/59ccd43ff400d75ecd0fce821524e618 to your computer and use it in GitHub Desktop.

Select an option

Save echatav/59ccd43ff400d75ecd0fce821524e618 to your computer and use it in GitHub Desktop.
profunctorial container constraint characterizations
module Profunctorial where
import Control.Applicative
import Data.Function
import Data.Functor
import Data.Functor.Contravariant
import Data.Profunctor
import Witherable
class Foldable f where
foldP ::
( Strong p, Choice p
, forall x. Applicative (p x)
, forall x. Contravariant (p x)
) => p a b -> p (f a) (f b)
class (Functor f, Foldable f) => Traversable f where
-- prop> fmap = traverseP
-- prop> foldP = traverseP
traverseP ::
( Strong p, Choice p
, forall x. Applicative (p x)
) => p a b -> p (f a) (f b)
class Applicative f => Cotraversable f where
cotraverseP ::
( Closed p
, forall x. Applicative (p x)
) => p a b -> p (f a) (f b)
replicateP ::
( Traversable f
, Cotraversable f
, Profunctor p
, forall x. Applicative (p x)
) => p a b -> p (f a) (f b)
replicateP p =
traverse (\f -> lmap f p) (cotraverse id id)
where
cotraverse = runCostar . cotraverseP . Costar
traverse = runStar . traverseP . Star
witherP ::
( Traversable f
, Filterable f
, Strong p, Choice p
, forall x. Alternative (p x)
) => p a b -> p (f a) (f b)
witherP = rmap catMaybes . traverseP . optional
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment