haskell / haskell/transformers

Add exception handler newtype

Open
#74 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
5
Forks
10
Avg merge
2h 21m
Merged PRs (30d)
1

Description

[original issue 65 by @turion]


catchE and throwE make ExceptT into a monad in the exception type:

newtype HandleExcept a m e = HandleExcept { runHandleExcept :: ExceptT e m a }

instance Functor (HandleExcept a m) where
fmap f = HandleExcept . withExceptT f . runHandleExcept

instance Monad (HandleExcept a m) where
return = HandleExcept . throwE
he >>= f = HandleExcept $ runHandleExcept he `catchE` (runHandleExcept . f)

try :: ExceptT e m a -> HandleExcept a m e
try = HandleExcept

safe :: m a -> HandleExcept a m Void
safe = HandleExcept . lift

safely :: HandleExcept a m Void -> m a
safely = fmap (fromLeft $ error "safely: Got Right value") . runExceptT . runHandleExcept

This monad interface is very useful. We can write programs like this:

finallyCalculateThatValue = safely $ do

reasonItMightNotHaveWorked <- try $ calculateValue
someBackupPlan <- handler reasonItMightNotHaveWorked
safe $ thisWillAlwaysWork someBackupPlan

It will execute all the statements after each other until they throw exceptions (or succeed in returning a value). For example:

rightToMaybe :: ExceptT m a e -> m (Maybe a)

rightToMaybe mae = safely $ do
e <- try mae
safe $ return Nothing

HandleExcept is also a functor in the category of monads (http://hackage.haskell.org/package/mmorph-1.1.2/docs/Control-Monad-Morph.html#t:MFunctor).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.