module Control.Concurrent.SCC.Coercions
(
Coercible(..),
adaptSplitter
)
where
import Prelude hiding ((.))
import Control.Category ((.))
import Control.Monad (liftM)
import Data.Text (Text, pack, unpack)
import Control.Monad.Coroutine
import Control.Monad.Parallel (MonadParallel(..))
import Control.Concurrent.SCC.Streams
import Control.Concurrent.SCC.Types
class Coercible x y where
coerce :: Monad m => Transducer m x y
adaptConsumer :: Monad m => Consumer m y r -> Consumer m x r
adaptConsumer consumer = isolateConsumer $ \source-> liftM snd $ pipe (transduce coerce source) (consume consumer)
adaptProducer :: Monad m => Producer m x r -> Producer m y r
adaptProducer producer = isolateProducer $ \sink-> liftM fst $ pipe (produce producer) (flip (transduce coerce) sink)
instance Coercible x x where
coerce = Transducer pour
adaptConsumer = id
adaptProducer = id
instance Coercible Char Text where
coerce = Transducer (mapStreamChunks ((:[]) . pack))
instance Coercible Text Char where
coerce = statelessTransducer unpack
instance Coercible x y => Coercible [x] y where
coerce = coerce . statelessTransducer id
instance Coercible x y => Coercible (Markup b x) y where
coerce = coerce . statelessTransducer unmark
where unmark (Content x) = [x]
unmark (Markup _) = []
adaptSplitter :: forall m x y b. (Monad m, Coercible x y, Coercible y x) => Splitter m x b -> Splitter m y b
adaptSplitter sx =
isolateSplitter $ \source true false edge->
pipe
(transduce coerce source)
(\source'->
pipe
(\true'->
pipe
(\false'-> split sx source' true' false' edge)
(flip (transduce coerce) false))
(flip (transduce coerce) true))
>> return ()