on master help sign in
Replace ReaderT with MyFailureT to avoid orphan instance
commitf27016c069fe53b6cd0e245704c64e0053e35b17 authorMichael Snoyman <michael@snoyman.com> dateThu 14 Jan 2010, 13:48 (17 years ago) parent4892302
1 file changed+17−9
Text/Libyaml.hs+17−9
Text/Libyaml.hs+17−9
@@ -5,7 +5,7 @@
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE MultiParamTypeClasses #-}
77{-# LANGUAGE PackageImports #-}
8−{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
99
1010module Text.Libyaml
1111 ( -- * The event stream
@@ -42,6 +42,7 @@ import Foreign.Marshal.Alloc
4242import "transformers" Control.Monad.Trans
4343import Control.Failure
4444import Control.Monad.Trans.Reader
45+import Control.Applicative
4546
4647import Control.Exception (throwIO, Exception, SomeException)
4748import Data.Typeable (Typeable)
@@ -414,7 +415,7 @@ emitEvent :: (MonadIO m, MonadFailure YamlException m)
414415 => Event
415416 -> YamlEncoder m ()
416417emitEvent e = do
417− emitter <- ask
418+ emitter <- MyReaderT ask
418419 res <- liftIO $ toEventRaw e $ c_yaml_emitter_emit emitter
419420 when (res == 0) $ do
420421 problem <- liftIO $ makeString c_get_emitter_error emitter
@@ -422,16 +423,23 @@ emitEvent e = do
422423
423424parseEvent :: (With m, MonadFailure YamlException m)
424425 => YamlDecoder m Event
425−parseEvent = ask >>= parserParseOne
426+parseEvent = MyReaderT ask >>= parserParseOne
426427
427−type YamlDecoder = ReaderT Parser
428−type YamlEncoder = ReaderT Emitter
428+runMyReaderT :: MyReaderT r m a -> r -> m a
429+runMyReaderT (MyReaderT (ReaderT f)) = f
429430
430431instance With m => With (ReaderT r m) where
431432 with orig f = ReaderT $ \r -> do
432433 let f' a = (runReaderT $ f a) r
433434 with orig f'
434−instance MonadFailure e m => Failure e (ReaderT r m) where
435+
436+newtype MyReaderT r m a = MyReaderT (ReaderT r m a)
437+ deriving (MonadTrans, MonadIO, Functor, Applicative,
438+ Alternative, Monad, MonadPlus, With)
439+type YamlDecoder = MyReaderT Parser
440+type YamlEncoder = MyReaderT Emitter
441+
442+instance MonadFailure e m => Failure e (MyReaderT r m) where
435443 failure = lift . failure
436444
437445foreign import ccall unsafe "yaml_stream_start_event_initialize"
@@ -550,13 +558,13 @@ data EnumResult a b = More a | Done b
550558encode :: (With m, MonadFailure YamlException m)
551559 => YamlEncoder m ()
552560 -> m B.ByteString
553−encode = withEmitter . runReaderT
561+encode = withEmitter . runMyReaderT
554562
555563encodeFile :: (With m, MonadFailure YamlException m)
556564 => FilePath
557565 -> YamlEncoder m ()
558566 -> m ()
559−encodeFile filePath = withEmitterFile filePath . runReaderT
567+encodeFile filePath = withEmitterFile filePath . runMyReaderT
560568
561569decode :: (With m, MonadFailure YamlException m)
562570 => B.ByteString
@@ -576,4 +584,4 @@ decodeFile' :: (With m, MonadFailure YamlException m)
576584 => FilePath
577585 -> YamlDecoder m a
578586 -> m a
579−decodeFile' fp dec = withFileParser fp $ runReaderT dec
587+decodeFile' fp dec = withFileParser fp $ runMyReaderT dec