alternative-io (empty) → 0.0.0
raw patch · 5 files changed
+144/−0 lines, 5 filesdep +basedep +lifted-basedep +monad-controlsetup-changed
Dependencies added: base, lifted-base, monad-control, transformers, transformers-base
Files
- Data/Alternative/IO.hs +47/−0
- Data/Alternative/IO/Lifted.hs +38/−0
- LICENSE +29/−0
- Setup.hs +2/−0
- alternative-io.cabal +28/−0
+ Data/Alternative/IO.hs view
@@ -0,0 +1,47 @@+{-# LANGUAGE ScopedTypeVariables, DeriveDataTypeable #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++{-|+ 'IO' as 'Alternative' instance.++ If the left 'IO' monad of ('<|>') causes an error or 'goNext' is used,+ the right 'IO' monad is executed.++ Of course, side effects cannot be rolled back. This means+ that this 'Alternative' instance breaks the 'Alternative' laws.+ But it's common in parsers.++-}++module Data.Alternative.IO where++import Control.Applicative+import Control.Exception+import Data.Typeable+import Prelude hiding (catch)++----------------------------------------------------------------++instance Alternative IO where+ empty = goNext+ x <|> y = x `catch` (\(_ :: SomeException) -> y)++----------------------------------------------------------------++{-| Go to the next 'IO' monad by throwing 'AltIOgoNext'.+-}+goNext :: IO a+goNext = throwIO AltIOgoNext++{-| Run any one 'IO' monad.+-}+runAnyOne :: [IO a] -> IO a+runAnyOne = foldr (<|>) goNext++----------------------------------------------------------------++{-| Exception to control 'Alternative' 'IO'.+-}+data AltIOgoNext = AltIOgoNext deriving (Show, Typeable)++instance Exception AltIOgoNext
+ Data/Alternative/IO/Lifted.hs view
@@ -0,0 +1,38 @@+{-# LANGUAGE ScopedTypeVariables, DeriveDataTypeable #-}+{-# LANGUAGE FlexibleContexts #-}++{-|+ Lifted 'IO' like 'Alternative' instance. Due to technical limitation,+ Lifted 'IO' is not an instance of 'Alternative'. ('<||>') is provided+ instead.+-}++module Data.Alternative.IO.Lifted where++import Control.Exception (Exception, SomeException)+import Control.Exception.Lifted (catch, throwIO)+import Control.Monad.Base (MonadBase)+import Control.Monad.IO.Class+import Control.Monad.Trans.Control (MonadBaseControl)+import Data.Typeable+import Prelude hiding (catch)++{-| If the left 'IO' monad of ('<||>') causes an error or 'goNext' is used,+ the right 'IO' monad is executed.+-}+(<||>) :: MonadBaseControl IO m => m a -> m a -> m a+x <||> y = x `catch` (\(_ :: SomeException) -> y)++{-| Go to the next 'IO' monad by throwing 'AltIOLiftedGoNext'.+-}+goNext :: (MonadIO m, MonadBase IO m) => m a+goNext = throwIO AltIOLiftedGoNext++{-| Run any one lifted 'IO' monad.+-}+runAnyOne :: (MonadIO m, MonadBaseControl IO m) => [m a] -> m a+runAnyOne = foldr (<||>) goNext++data AltIOLiftedGoNext = AltIOLiftedGoNext deriving (Show, Typeable)++instance Exception AltIOLiftedGoNext
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2012, IIJ Innovation Institute Inc.+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in+ the documentation and/or other materials provided with the+ distribution.+ * Neither the name of the copyright holders nor the names of its+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN+ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE+POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ alternative-io.cabal view
@@ -0,0 +1,28 @@+Name: alternative-io+Version: 0.0.0+Author: Kazu Yamamoto <kazu@iij.ad.jp>+Maintainer: Kazu Yamamoto <kazu@iij.ad.jp>+License: BSD3+License-File: LICENSE+Synopsis: IO as Alternative instance+Description: IO as Alternative instance+Category: Data+Cabal-Version: >= 1.6+Build-Type: Simple++Library+ if impl(ghc >= 6.12)+ GHC-Options: -Wall -fno-warn-unused-do-bind+ else+ GHC-Options: -Wall+ Exposed-Modules: Data.Alternative.IO+ Data.Alternative.IO.Lifted+ Build-Depends: base >= 4 && < 5+ , lifted-base+ , monad-control+ , transformers+ , transformers-base++Source-Repository head+ Type: git+ Location: git clone git://github.com/kazu-yamamoto/alternative-io