{-# LANGUAGE ExistentialQuantification, DeriveDataTypeable, StandaloneDeriving #-} module Phi.Phi ( Phi , Message , DefaultMessage(..) , fromMessage , initPhi , dupPhi , sendMessage , receiveMessage ) where import Control.Concurrent.Chan import Control.Monad import Data.Typeable data Phi = Phi (Chan Message) data Message = forall a. (Typeable a, Show a) => Message a deriving instance Show Message data DefaultMessage = Repaint deriving (Typeable, Show) fromMessage :: (Typeable a, Show a) => Message -> Maybe a fromMessage (Message m) = cast m initPhi :: IO Phi initPhi = liftM Phi newChan dupPhi :: Phi -> IO Phi dupPhi (Phi chan) = liftM Phi $ dupChan chan sendMessage :: (Typeable a, Show a) => Phi -> a -> IO () sendMessage (Phi chan) = writeChan chan . Message receiveMessage :: Phi -> IO Message receiveMessage (Phi chan) = readChan chan