{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, Safe #-}
{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides a printer class that offers several channels.
module Text.Chatty.Channel.Printer (
  ChChannelPrinter (..),
  ArchiverT (..),
  IntArchiverT,
  BoolArchiverT,
  HandleArchiverT,
  runArchiverT,
  FilterT (..),
  IntFilterT,
  BoolFilterT,
  HandleFilterT,
  JoinerT (..)
  ) where

import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Text.Chatty.Printer
import System.IO

-- | Typeclass for all printers that offer several channels.
class (ChPrinter m,Eq c) => ChChannelPrinter c m where
  -- | Run the function with the given channel.
  cbracket :: c -> m a -> m a
  cbracket c
c m a
m = c -> m ()
forall c (m :: * -> *). ChChannelPrinter c m => c -> m ()
cstart c
c m () -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m a
m m a -> (a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> c -> m ()
forall c (m :: * -> *). ChChannelPrinter c m => c -> m ()
cfin c
c m () -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
  -- | Switch to the given channel
  cstart :: c -> m ()
  -- | Return to the previous channel. The argument is bogus (just for type inference).
  cfin :: c -> m ()
  -- | Print the string to the given channel.
  cprint :: c -> String -> m ()
  cprint c
c String
s = c -> m () -> m ()
forall c (m :: * -> *) a. ChChannelPrinter c m => c -> m a -> m a
cbracket c
c (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint String
s
  -- | Return the current channel.
  cthis :: m c

-- | Catches all output on multiple channels.
newtype ArchiverT c m a = Archiver { ArchiverT c m a
-> ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
runArchiverT' :: ([(c,[String])],[c]) -> m (a,([(c,[String])],[c])) }

instance Monad m => Monad (ArchiverT c m) where
  return :: a -> ArchiverT c m a
return a
a = (([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
 -> ArchiverT c m a)
-> (([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
forall a b. (a -> b) -> a -> b
$ \([(c, [String])], [c])
s -> (a, ([(c, [String])], [c])) -> m (a, ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,([(c, [String])], [c])
s)
  (Archiver ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
r) >>= :: ArchiverT c m a -> (a -> ArchiverT c m b) -> ArchiverT c m b
>>= a -> ArchiverT c m b
f = (([(c, [String])], [c]) -> m (b, ([(c, [String])], [c])))
-> ArchiverT c m b
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m (b, ([(c, [String])], [c])))
 -> ArchiverT c m b)
-> (([(c, [String])], [c]) -> m (b, ([(c, [String])], [c])))
-> ArchiverT c m b
forall a b. (a -> b) -> a -> b
$ \([(c, [String])], [c])
s -> do (a
a,([(c, [String])], [c])
s') <- ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
r ([(c, [String])], [c])
s; ArchiverT c m b
-> ([(c, [String])], [c]) -> m (b, ([(c, [String])], [c]))
forall c (m :: * -> *) a.
ArchiverT c m a
-> ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
runArchiverT' (a -> ArchiverT c m b
f a
a) ([(c, [String])], [c])
s'

instance MonadTrans (ArchiverT c) where
  lift :: m a -> ArchiverT c m a
lift m a
m = (([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
 -> ArchiverT c m a)
-> (([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
forall a b. (a -> b) -> a -> b
$ \([(c, [String])], [c])
s -> do a
a <- m a
m; (a, ([(c, [String])], [c])) -> m (a, ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,([(c, [String])], [c])
s)

instance MonadIO m => MonadIO (ArchiverT c m) where
  liftIO :: IO a -> ArchiverT c m a
liftIO = m a -> ArchiverT c m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ArchiverT c m a)
-> (IO a -> m a) -> IO a -> ArchiverT c m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance Monad m => Functor (ArchiverT c m) where
  fmap :: (a -> b) -> ArchiverT c m a -> ArchiverT c m b
fmap a -> b
f ArchiverT c m a
a = (a -> b) -> ArchiverT c m a -> ArchiverT c m b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> b
f ArchiverT c m a
a

instance Monad m => Applicative (ArchiverT c m) where
  <*> :: ArchiverT c m (a -> b) -> ArchiverT c m a -> ArchiverT c m b
(<*>) = ArchiverT c m (a -> b) -> ArchiverT c m a -> ArchiverT c m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
  pure :: a -> ArchiverT c m a
pure = a -> ArchiverT c m a
forall (m :: * -> *) a. Monad m => a -> m a
return

withAssoc :: Eq b => b -> a -> (a -> a) -> [(b,a)] -> [(b,a)]
withAssoc :: b -> a -> (a -> a) -> [(b, a)] -> [(b, a)]
withAssoc b
k a
n a -> a
f [] = [(b
k,a -> a
f a
n)]
withAssoc b
k a
n a -> a
f ((b
p,a
a):[(b, a)]
ps)
  | b
p b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
k = (b
p,a -> a
f a
a) (b, a) -> [(b, a)] -> [(b, a)]
forall a. a -> [a] -> [a]
: [(b, a)]
ps
  | Bool
otherwise = (b
p,a
a) (b, a) -> [(b, a)] -> [(b, a)]
forall a. a -> [a] -> [a]
: b -> a -> (a -> a) -> [(b, a)] -> [(b, a)]
forall b a. Eq b => b -> a -> (a -> a) -> [(b, a)] -> [(b, a)]
withAssoc b
k a
n a -> a
f [(b, a)]
ps

instance (Eq c,Monad m) => ChPrinter (ArchiverT c m) where
  mprint :: String -> ArchiverT c m ()
mprint String
s = (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
 -> ArchiverT c m ())
-> (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall a b. (a -> b) -> a -> b
$ \([(c, [String])]
r,c
c:[c]
cx) -> ((), ([(c, [String])], [c])) -> m ((), ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return ((),(c
-> [String]
-> ([String] -> [String])
-> [(c, [String])]
-> [(c, [String])]
forall b a. Eq b => b -> a -> (a -> a) -> [(b, a)] -> [(b, a)]
withAssoc c
c [] (String
sString -> [String] -> [String]
forall a. a -> [a] -> [a]
:) [(c, [String])]
r,c
cc -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cx))

instance (Eq c,Monad m) => ChChannelPrinter c (ArchiverT c m) where
  cbracket :: c -> ArchiverT c m a -> ArchiverT c m a
cbracket c
c ArchiverT c m a
m = (([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
 -> ArchiverT c m a)
-> (([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
forall a b. (a -> b) -> a -> b
$ \([(c, [String])]
r,[c]
c1) -> do
    (a
a,([(c, [String])]
r',[c]
_)) <- ArchiverT c m a
-> ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
forall c (m :: * -> *) a.
ArchiverT c m a
-> ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
runArchiverT' ArchiverT c m a
m ([(c, [String])]
r,c
cc -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
c1)
    (a, ([(c, [String])], [c])) -> m (a, ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,([(c, [String])]
r',[c]
c1))
  cstart :: c -> ArchiverT c m ()
cstart c
c = (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
 -> ArchiverT c m ())
-> (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall a b. (a -> b) -> a -> b
$ \([(c, [String])]
r,[c]
c1) -> ((), ([(c, [String])], [c])) -> m ((), ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return ((),([(c, [String])]
r,c
cc -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
c1))
  cfin :: c -> ArchiverT c m ()
cfin c
_ = (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
 -> ArchiverT c m ())
-> (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall a b. (a -> b) -> a -> b
$ \([(c, [String])]
r,c
_:[c]
cx) -> ((), ([(c, [String])], [c])) -> m ((), ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return ((),([(c, [String])]
r,[c]
cx))
  cprint :: c -> String -> ArchiverT c m ()
cprint c
c String
s = (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
 -> ArchiverT c m ())
-> (([(c, [String])], [c]) -> m ((), ([(c, [String])], [c])))
-> ArchiverT c m ()
forall a b. (a -> b) -> a -> b
$ \([(c, [String])]
r,[c]
c1) -> ((), ([(c, [String])], [c])) -> m ((), ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return ((),(c
-> [String]
-> ([String] -> [String])
-> [(c, [String])]
-> [(c, [String])]
forall b a. Eq b => b -> a -> (a -> a) -> [(b, a)] -> [(b, a)]
withAssoc c
c [] (String
sString -> [String] -> [String]
forall a. a -> [a] -> [a]
:) [(c, [String])]
r,[c]
c1))
  cthis :: ArchiverT c m c
cthis = (([(c, [String])], [c]) -> m (c, ([(c, [String])], [c])))
-> ArchiverT c m c
forall c (m :: * -> *) a.
(([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
Archiver ((([(c, [String])], [c]) -> m (c, ([(c, [String])], [c])))
 -> ArchiverT c m c)
-> (([(c, [String])], [c]) -> m (c, ([(c, [String])], [c])))
-> ArchiverT c m c
forall a b. (a -> b) -> a -> b
$ \([(c, [String])]
r,[c]
c) -> (c, ([(c, [String])], [c])) -> m (c, ([(c, [String])], [c]))
forall (m :: * -> *) a. Monad m => a -> m a
return ([c] -> c
forall a. [a] -> a
head [c]
c,([(c, [String])]
r,[c]
c))

runArchiverT :: (Eq c,Monad m) => c -> ArchiverT c m a -> m (a,[(c,Replayable)])
runArchiverT :: c -> ArchiverT c m a -> m (a, [(c, Replayable)])
runArchiverT c
c = ((a, ([(c, [String])], [c])) -> (a, [(c, Replayable)]))
-> m (a, ([(c, [String])], [c])) -> m (a, [(c, Replayable)])
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ((([(c, [String])], [c]) -> [(c, Replayable)])
-> (a, ([(c, [String])], [c])) -> (a, [(c, Replayable)])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ((([(c, [String])], [c]) -> [(c, Replayable)])
 -> (a, ([(c, [String])], [c])) -> (a, [(c, Replayable)]))
-> (([(c, [String])], [c]) -> [(c, Replayable)])
-> (a, ([(c, [String])], [c]))
-> (a, [(c, Replayable)])
forall a b. (a -> b) -> a -> b
$ ((c, [String]) -> (c, Replayable))
-> [(c, [String])] -> [(c, Replayable)]
forall a b. (a -> b) -> [a] -> [b]
map (([String] -> Replayable) -> (c, [String]) -> (c, Replayable)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second [String] -> Replayable
Replayable) ([(c, [String])] -> [(c, Replayable)])
-> (([(c, [String])], [c]) -> [(c, [String])])
-> ([(c, [String])], [c])
-> [(c, Replayable)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(c, [String])], [c]) -> [(c, [String])]
forall a b. (a, b) -> a
fst) (m (a, ([(c, [String])], [c])) -> m (a, [(c, Replayable)]))
-> (ArchiverT c m a -> m (a, ([(c, [String])], [c])))
-> ArchiverT c m a
-> m (a, [(c, Replayable)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ArchiverT c m a
 -> ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c])))
-> ([(c, [String])], [c])
-> ArchiverT c m a
-> m (a, ([(c, [String])], [c]))
forall a b c. (a -> b -> c) -> b -> a -> c
flip ArchiverT c m a
-> ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
forall c (m :: * -> *) a.
ArchiverT c m a
-> ([(c, [String])], [c]) -> m (a, ([(c, [String])], [c]))
runArchiverT' ([],[c
c])

type IntArchiverT = ArchiverT Int
type BoolArchiverT = ArchiverT Bool
type HandleArchiverT = ArchiverT Handle

-- | Forwards output only on a specific channel.
newtype FilterT c m a = Filter { FilterT c m a -> (c, [c]) -> m (a, [c])
runFilterT :: (c,[c]) -> m (a,[c]) }

instance Monad m => Monad (FilterT c m) where
  return :: a -> FilterT c m a
return a
a = ((c, [c]) -> m (a, [c])) -> FilterT c m a
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m (a, [c])) -> FilterT c m a)
-> ((c, [c]) -> m (a, [c])) -> FilterT c m a
forall a b. (a -> b) -> a -> b
$ \(c
c,[c]
s) -> (a, [c]) -> m (a, [c])
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,[c]
s)
  (Filter (c, [c]) -> m (a, [c])
g) >>= :: FilterT c m a -> (a -> FilterT c m b) -> FilterT c m b
>>= a -> FilterT c m b
f = ((c, [c]) -> m (b, [c])) -> FilterT c m b
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m (b, [c])) -> FilterT c m b)
-> ((c, [c]) -> m (b, [c])) -> FilterT c m b
forall a b. (a -> b) -> a -> b
$ \(c
c,[c]
s) -> do (a
a,[c]
s') <- (c, [c]) -> m (a, [c])
g (c
c,[c]
s); FilterT c m b -> (c, [c]) -> m (b, [c])
forall c (m :: * -> *) a. FilterT c m a -> (c, [c]) -> m (a, [c])
runFilterT (a -> FilterT c m b
f a
a) (c
c,[c]
s')

instance MonadTrans (FilterT c) where
  lift :: m a -> FilterT c m a
lift m a
m = ((c, [c]) -> m (a, [c])) -> FilterT c m a
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m (a, [c])) -> FilterT c m a)
-> ((c, [c]) -> m (a, [c])) -> FilterT c m a
forall a b. (a -> b) -> a -> b
$ \(c
c,[c]
s) -> do a
a <- m a
m; (a, [c]) -> m (a, [c])
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,[c]
s)

instance MonadIO m => MonadIO (FilterT c m) where
  liftIO :: IO a -> FilterT c m a
liftIO = m a -> FilterT c m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> FilterT c m a) -> (IO a -> m a) -> IO a -> FilterT c m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance Monad m => Functor (FilterT c m) where
  fmap :: (a -> b) -> FilterT c m a -> FilterT c m b
fmap a -> b
f FilterT c m a
a = (a -> b) -> FilterT c m a -> FilterT c m b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> b
f FilterT c m a
a

instance Monad m => Applicative (FilterT c m) where
  pure :: a -> FilterT c m a
pure = a -> FilterT c m a
forall (m :: * -> *) a. Monad m => a -> m a
return
  <*> :: FilterT c m (a -> b) -> FilterT c m a -> FilterT c m b
(<*>) = FilterT c m (a -> b) -> FilterT c m a -> FilterT c m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance (Eq c,ChPrinter m) => ChPrinter (FilterT c m) where
  mprint :: String -> FilterT c m ()
mprint String
str = ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m ((), [c])) -> FilterT c m ())
-> ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall a b. (a -> b) -> a -> b
$ \(c
c,c
c1:[c]
cx) -> if c
c c -> c -> Bool
forall a. Eq a => a -> a -> Bool
== c
c1 then String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint String
str m () -> m ((), [c]) -> m ((), [c])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),c
c1c -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cx) else ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),c
c1c -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cx)
  mnomask :: String -> FilterT c m ()
mnomask String
str = ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m ((), [c])) -> FilterT c m ())
-> ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall a b. (a -> b) -> a -> b
$ \(c
c,c
c1:[c]
cx) -> if c
c c -> c -> Bool
forall a. Eq a => a -> a -> Bool
== c
c1 then String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mnomask String
str m () -> m ((), [c]) -> m ((), [c])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),c
c1c -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cx) else ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),c
c1c -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cx)

instance (Eq c,ChPrinter m) => ChChannelPrinter c (FilterT c m) where
  cbracket :: c -> FilterT c m a -> FilterT c m a
cbracket c
c FilterT c m a
m = ((c, [c]) -> m (a, [c])) -> FilterT c m a
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m (a, [c])) -> FilterT c m a)
-> ((c, [c]) -> m (a, [c])) -> FilterT c m a
forall a b. (a -> b) -> a -> b
$ \(c
cf,[c]
cx) -> do
    (a
a,[c]
_) <- FilterT c m a -> (c, [c]) -> m (a, [c])
forall c (m :: * -> *) a. FilterT c m a -> (c, [c]) -> m (a, [c])
runFilterT FilterT c m a
m (c
cf,c
cc -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cx)
    (a, [c]) -> m (a, [c])
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,[c]
cx)
  cstart :: c -> FilterT c m ()
cstart c
c = ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m ((), [c])) -> FilterT c m ())
-> ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall a b. (a -> b) -> a -> b
$ \(c
cf,[c]
cx) -> ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),c
cc -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cx)
  cfin :: c -> FilterT c m ()
cfin c
_ = ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m ((), [c])) -> FilterT c m ())
-> ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall a b. (a -> b) -> a -> b
$ \(c
cf,c
_:[c]
cx) -> ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),[c]
cx)
  cprint :: c -> String -> FilterT c m ()
cprint c
c String
s = ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m ((), [c])) -> FilterT c m ())
-> ((c, [c]) -> m ((), [c])) -> FilterT c m ()
forall a b. (a -> b) -> a -> b
$ \(c
cf,[c]
cx) -> if c
c c -> c -> Bool
forall a. Eq a => a -> a -> Bool
== c
cf then String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint String
s m () -> m ((), [c]) -> m ((), [c])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),[c]
cx) else ((), [c]) -> m ((), [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),[c]
cx)
  cthis :: FilterT c m c
cthis = ((c, [c]) -> m (c, [c])) -> FilterT c m c
forall c (m :: * -> *) a. ((c, [c]) -> m (a, [c])) -> FilterT c m a
Filter (((c, [c]) -> m (c, [c])) -> FilterT c m c)
-> ((c, [c]) -> m (c, [c])) -> FilterT c m c
forall a b. (a -> b) -> a -> b
$ \(c
cf,[c]
cx) -> (c, [c]) -> m (c, [c])
forall (m :: * -> *) a. Monad m => a -> m a
return ([c] -> c
forall a. [a] -> a
head [c]
cx,[c]
cx)

type IntFilterT = FilterT Int
type BoolFilterT = FilterT Bool
type HandleFilterT = FilterT Handle

-- | Joins all output regardless of its channel.
newtype JoinerT m a = Joiner { JoinerT m a -> m a
runJoinerT :: m a }

instance Monad m => Monad (JoinerT m) where
  return :: a -> JoinerT m a
return a
a = m a -> JoinerT m a
forall (m :: * -> *) a. m a -> JoinerT m a
Joiner (m a -> JoinerT m a) -> m a -> JoinerT m a
forall a b. (a -> b) -> a -> b
$ a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
  (Joiner m a
j) >>= :: JoinerT m a -> (a -> JoinerT m b) -> JoinerT m b
>>= a -> JoinerT m b
f = m b -> JoinerT m b
forall (m :: * -> *) a. m a -> JoinerT m a
Joiner (m b -> JoinerT m b) -> m b -> JoinerT m b
forall a b. (a -> b) -> a -> b
$ do a
a <- m a
j; JoinerT m b -> m b
forall (m :: * -> *) a. JoinerT m a -> m a
runJoinerT (a -> JoinerT m b
f a
a)

instance MonadTrans JoinerT where
  lift :: m a -> JoinerT m a
lift = m a -> JoinerT m a
forall (m :: * -> *) a. m a -> JoinerT m a
Joiner

instance MonadIO m => MonadIO (JoinerT m) where
  liftIO :: IO a -> JoinerT m a
liftIO = m a -> JoinerT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> JoinerT m a) -> (IO a -> m a) -> IO a -> JoinerT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance Functor m => Functor (JoinerT m) where
  fmap :: (a -> b) -> JoinerT m a -> JoinerT m b
fmap a -> b
f (Joiner m a
j) = m b -> JoinerT m b
forall (m :: * -> *) a. m a -> JoinerT m a
Joiner (m b -> JoinerT m b) -> m b -> JoinerT m b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f m a
j

instance (Functor m, Monad m) => Applicative (JoinerT m) where
  <*> :: JoinerT m (a -> b) -> JoinerT m a -> JoinerT m b
(<*>) = JoinerT m (a -> b) -> JoinerT m a -> JoinerT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
  pure :: a -> JoinerT m a
pure = a -> JoinerT m a
forall (m :: * -> *) a. Monad m => a -> m a
return

instance ChPrinter m => ChPrinter (JoinerT m) where
  mprint :: String -> JoinerT m ()
mprint = m () -> JoinerT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> JoinerT m ())
-> (String -> m ()) -> String -> JoinerT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint
  mnomask :: String -> JoinerT m ()
mnomask = m () -> JoinerT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> JoinerT m ())
-> (String -> m ()) -> String -> JoinerT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mnomask

instance (Eq c,ChPrinter m) => ChChannelPrinter c (JoinerT m) where
  cstart :: c -> JoinerT m ()
cstart c
_ = () -> JoinerT m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  cfin :: c -> JoinerT m ()
cfin c
_ = () -> JoinerT m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  cbracket :: c -> JoinerT m a -> JoinerT m a
cbracket c
_ JoinerT m a
m = JoinerT m a
m
  cthis :: JoinerT m c
cthis = c -> JoinerT m c
forall (m :: * -> *) a. Monad m => a -> m a
return c
forall a. HasCallStack => a
undefined
  cprint :: c -> String -> JoinerT m ()
cprint c
_ String
s = String -> JoinerT m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint String
s