public
Created

Lift the inner monad for an enumerator

  • Download Gist
liftEnum.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
import Control.Monad.Trans.Error
import Control.Monad.Trans.Class
import Data.Enumerator
 
data In = In
data Out = Out
 
data OcrError = OcrError
instance Error OcrError
 
liftEnum :: Enumerator In IO (Either OcrError Out)
-> Enumerator In (ErrorT OcrError IO) Out
liftEnum enum step = unpeel $ enum $ peel step
 
peel :: Step In (ErrorT e IO) Out -> Step In IO (Either e Out)
peel (Yield b s) = Yield (Right b) s
peel (Error e) = Error e
peel (Continue k) = Continue $ \s -> Iteratee $ do
x <- runErrorT $ runIteratee $ k s
case x of
Left e -> return $ Yield (Left e) EOF
Right x -> return $ peel x
 
unpeel :: Iteratee In IO (Either OcrError Out)
-> Iteratee In (ErrorT OcrError IO) Out
unpeel (Iteratee mstep) = Iteratee $ do
step <- lift mstep
unpeel' step
 
unpeel' :: Step In IO (Either OcrError Out)
-> ErrorT OcrError IO (Step In (ErrorT OcrError IO) Out)
unpeel' (Yield (Left e) _) = ErrorT $ return $ Left e
unpeel' (Yield (Right b) s) = ErrorT $ return $ Right $ Yield b s
unpeel' (Error e) = return $ Error e
unpeel' (Continue k) = ErrorT $ return $ Right $ Continue $ \s -> Iteratee $ do
step <- lift $ runIteratee $ k s
unpeel' step

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.