Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- parseSQL :: String -> Either ParseError SQL
- -- evalSQL :: SQL -> IO (Either EvalError Table)
- -- prettyPrintTable :: Table -> IO ()
- -- ParseError, EvalError and Table are instances of Show
- evalAndPrint :: String -> IO ()
- evalAndPrint x =
- case parseSQL x of
- (Left parseErr) ->
- print parseErr
- (Right sql) -> do
- result <- evalSQL sql
- case result of
- (Left err) ->
- print err
- (Right table) -> do
- prettyPrintTable table
- putStrLn $ "(" ++ show (length table) ++ " lines)n"
- parseSQL :: MonadError ParseError m => String -> m SQL
- evalSQL :: (MonadError EvalError m, MonadIO m) => SQL -> m Table
- -- if we were really doing this the mtl way, we'd introduce a new
- -- type class for changing error types instead of specializing to
- -- ExceptT, but that's another answer
- evalAndThrow :: String -> ExceptT String IO ()
- evalAndThrow s = do
- sql <- withExceptT show (parseSQL s)
- table <- withExceptT show (evalSQL sql)
- liftIO $ prettyPrintTable table
- liftIO . putStrLn $ "(" ++ show (length table) ++ " lines)n"
- evalAndPrint s = do
- v <- runExceptT (evalAndThrow s)
- case v of
- Left err -> putStrLn err
- Right _ -> return ()
- -- this is a generally useful combinator
- liftEither :: MonadError e m => Either e a -> m a
- liftEither = either throwError return
- -- this is a combinator specific to your question
- liftIOEither :: (MonadError e m, MonadIO m) => IO (Either e a) -> m a
- liftIOEither = join . liftIO . liftM liftEither
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement