kure-2.16.8: Combinators for Strategic Programming

Copyright(c) 2012--2014 The University of Kansas
LicenseBSD3
MaintainerNeil Sculthorpe <neil@ittc.ku.edu>
Stabilitybeta
Portabilityghc
Safe HaskellSafe-Inferred
LanguageHaskell2010

Language.KURE.MonadCatch

Contents

Description

This module provides classes for catch-like operations on Monads.

Synopsis

Monads with a Catch

class Monad m => MonadCatch m where Source

Monads with a catch for fail. The following laws are expected to hold:

fail msg `catchM` f == f msg
return a `catchM` f == return a

Methods

catchM :: m a -> (String -> m a) -> m a Source

Catch a failing monadic computation.

Instances

MonadCatch IO

The String is generated by showing the exception.

MonadCatch KureM 
MonadCatch m => MonadCatch (OneR m) 
MonadCatch m => MonadCatch (AnyR m) 
MonadCatch m => MonadCatch (Transform c m a)

Lifting through a Reader transformer, where (c,a) is the read-only environment.

The KURE Monad

data KureM a Source

KureM is the minimal structure that can be an instance of MonadCatch. The KURE user is free to either use KureM or provide their own monad. KureM is essentially the same as Either String, except that it supports a MonadCatch instance which Either String does not (because its fail method calls error) A major advantage of this is that monadic pattern match failures are caught safely.

runKureM :: (a -> b) -> (String -> b) -> KureM a -> b Source

Eliminator for KureM.

fromKureM :: (String -> a) -> KureM a -> a Source

Get the value from a KureM, providing a function to handle the error case.

liftKureM :: Monad m => KureM a -> m a Source

Lift a KureM computation to any other monad.

The IO Monad

liftAndCatchIO :: (MonadCatch m, MonadIO m) => IO a -> m a Source

Lift a computation from the IO monad, catching failures in the target monad.

Combinators

(<+) :: MonadCatch m => m a -> m a -> m a infixl 3 Source

A monadic catch that ignores the error message.

catchesM :: (Foldable f, MonadCatch m) => f (m a) -> m a Source

Select the first monadic computation that succeeds, discarding any thereafter.

tryM :: MonadCatch m => a -> m a -> m a Source

Catch a failing monadic computation, making it succeed with a constant value.

mtryM :: (MonadCatch m, Monoid a) => m a -> m a Source

Catch a failing monadic computation, making it succeed with mempty.

attemptM :: MonadCatch m => m a -> m (Either String a) Source

Catch a failing monadic computation, making it succeed with an error message.

testM :: MonadCatch m => m a -> m Bool Source

Determine if a monadic computation succeeds.

notM :: MonadCatch m => m a -> m () Source

Fail if the monadic computation succeeds; succeed with () if it fails.

modFailMsg :: MonadCatch m => (String -> String) -> m a -> m a Source

Modify the error message of a failing monadic computation. Successful computations are unaffected.

setFailMsg :: MonadCatch m => String -> m a -> m a Source

Set the error message of a failing monadic computation. Successful computations are unaffected.

prefixFailMsg :: MonadCatch m => String -> m a -> m a Source

Add a prefix to the error message of a failing monadic computation. Successful computations are unaffected.

withPatFailMsg :: MonadCatch m => String -> m a -> m a Source

Use the given error message whenever a monadic pattern match failure occurs.