summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libraries/base/Control/Applicative.hs17
1 files changed, 17 insertions, 0 deletions
diff --git a/libraries/base/Control/Applicative.hs b/libraries/base/Control/Applicative.hs
index 87394e078b..e898bf8c04 100644
--- a/libraries/base/Control/Applicative.hs
+++ b/libraries/base/Control/Applicative.hs
@@ -141,5 +141,22 @@ instance Alternative ZipList where
-- extra functions
-- | One or none.
+--
+-- It is useful for modelling any computation that is allowed to fail.
+--
+-- ==== __Examples__
+--
+-- Using the Control.Monad.Except as an Alternative, a couple of functions:
+--
+-- >>> canFail = throwError "it failed" :: Except String Int
+-- >>> final = return 42 :: Except String Int
+--
+-- Can be combined by allowing the first function to fail:
+--
+-- >>> runExcept $ canFail *> final
+-- Left "it failed"
+-- >>> runExcept $ optional canFail *> final
+-- Right 42
+
optional :: Alternative f => f a -> f (Maybe a)
optional v = Just <$> v <|> pure Nothing