adding Applicative & Alternative.

This commit is contained in:
Kazu Yamamoto 2012-02-15 14:58:30 +09:00
parent 96cbf68e16
commit f353e89d11

42
AA.hs Normal file
View File

@ -0,0 +1,42 @@
{-# LANGUAGE ScopedTypeVariables, DeriveDataTypeable #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module AA where
import Control.Applicative
import Control.Exception
import Control.Monad
import CoreMonad
import Data.Typeable
import Exception
import GHC
----------------------------------------------------------------
instance Applicative Ghc where
pure = return
(<*>) = ap
instance Alternative Ghc where
empty = goNext
x <|> y = x `gcatch` (\(_ :: SomeException) -> y)
----------------------------------------------------------------
{-| Go to the next 'Ghc' monad by throwing 'AltGhcgoNext'.
-}
goNext :: Ghc a
goNext = liftIO $ throwIO AltGhcgoNext
{-| Run any one 'Ghc' monad.
-}
runAnyOne :: [Ghc a] -> Ghc a
runAnyOne = foldr (<|>) goNext
----------------------------------------------------------------
{-| Exception to control 'Alternative' 'Ghc'.
-}
data AltGhcgoNext = AltGhcgoNext deriving (Show, Typeable)
instance Exception AltGhcgoNext