Traversable Instance of the Constant type

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Traversable Instance of the Constant type

Pretty sure I'm getting bogged down on something simple, but how does the traverse function actually embed the Constant type in the f context passed to it? Or is it the pure defined in the f context that's being called?

Thank you,

newtype Constant a b = Constant { getConstant :: a } deriving (Eq, Show)

instance Functor (Constant a) where
    fmap f (Constant a) = Constant a

instance Monoid a => Applicative (Constant a) where
    pure a = Constant { getConstant = mempty }
    Constant a <*> Constant b = Constant { getConstant = a `mappend` b }

instance Foldable (Constant a) where
    foldMap f (Constant a) = mempty

instance Traversable (Constant a) where
  traverse f (Constant a) = pure $ Constant a

λ> traverse (\x -> Just x) (Constant 1)
Just (Constant {getConstant = 1})

Sent with ProtonMail Secure Email.

Beginners mailing list
[hidden email]