Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created December 10, 2021 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonymorris/756583eb141f495e893e6709cb297c77 to your computer and use it in GitHub Desktop.
Save tonymorris/756583eb141f495e893e6709cb297c77 to your computer and use it in GitHub Desktop.
import Control.Applicative(Alternative(..), liftA2)
newtype ValidationT f a b =
ValidationT (f (Either a b))
instance Functor f => Functor (ValidationT f a) where
fmap f (ValidationT x) =
ValidationT (fmap (fmap f) x)
instance Applicative f => Applicative (ValidationT f a) where
pure =
ValidationT . pure . pure
ValidationT f <*> ValidationT x =
ValidationT (liftA2 (<*>) f x)
-- Monad required: second effect will not run if first succeeds
instance (Alternative f, Monad f) => Alternative (ValidationT f a) where
ValidationT a <|> ValidationT b =
ValidationT (a >>= either (const b) (pure . pure))
empty =
ValidationT (Right <$> empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment