From 25779212dca36503765c2353da08cd659b0ca8de Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Sun, 26 Aug 2012 18:18:59 +0200
Subject: [PATCH] Add an Applicative instance for our Result type

This will allow us to run generic computations in an Applicative
context, if a monadic one is not needed (or not applicable due to
class constraints).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/BasicTypes.hs | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/htools/Ganeti/BasicTypes.hs b/htools/Ganeti/BasicTypes.hs
index d68387e9e..61a7e566f 100644
--- a/htools/Ganeti/BasicTypes.hs
+++ b/htools/Ganeti/BasicTypes.hs
@@ -36,6 +36,7 @@ module Ganeti.BasicTypes
   , compareNameComponent
   ) where
 
+import Control.Applicative
 import Control.Monad
 import Data.Function
 import Data.List
@@ -68,6 +69,12 @@ instance MonadPlus Result where
   (Bad _) `mplus` x = x
   x@(Ok _) `mplus` _ = x
 
+instance Applicative Result where
+  pure = Ok
+  (Bad f) <*> _       = Bad f
+  _       <*> (Bad x) = Bad x
+  (Ok f)  <*> (Ok x)  = Ok $ f x
+
 -- | Simple checker for whether a 'Result' is OK.
 isOk :: Result a -> Bool
 isOk (Ok _) = True
-- 
GitLab