module Generics.Pointless.Observe.Functors where
import Generics.Pointless.Combinators
import Generics.Pointless.Functors
import Debug.Observe
import Data.Typeable
import Prelude hiding (Functor(..))
import Control.Monad hiding (Functor(..))
instance Typeable One where
typeOf _ = mkTyCon "One" `mkTyConApp` []
class FunctorO f where
functorOf :: Fix f -> String
watch :: Fix f -> x -> Rep f x -> String
fmapO :: Fix f -> (x -> ObserverM y) -> Rep f x -> ObserverM (Rep f y)
instance FunctorO Id where
functorOf _ = "Id"
watch _ _ _ = ""
fmapO _ f = f
instance (Typeable a,Observable a) => FunctorO (Const a) where
functorOf _ = "Const " ++ show (typeOf (_L::a))
watch _ _ _ = ""
fmapO _ f = thunk
instance (FunctorO f, FunctorO g) => FunctorO (f :+: g) where
functorOf _ = "(" ++ functorOf (_L::Fix f) ++ ":+:" ++ functorOf (_L::Fix g) ++ ")"
watch _ _ (Left _) = "Left"
watch _ _ (Right _) = "Right"
fmapO _ f (Left x) = liftM Left (fmapO (_L::Fix f) f x)
fmapO _ f (Right x) = liftM Right (fmapO (_L::Fix g) f x)
instance (FunctorO f, FunctorO g) => FunctorO (f :*: g) where
functorOf _ = "(" ++ functorOf (_L::Fix f) ++ ":*:" ++ functorOf (_L::Fix g) ++ ")"
watch _ _ _ = ""
fmapO _ f (x,y) = do x' <- fmapO (_L :: Fix f) f x
y' <- fmapO (_L::Fix g) f y
return (x',y')
instance (FunctorO g, FunctorO h) => FunctorO (g :@: h) where
functorOf _ = "(" ++ functorOf (_L::Fix g) ++ ":@:" ++ functorOf (_L::Fix h) ++ ")"
watch _ (x::x) = watch (_L::Fix g) (_L::Rep h x)
fmapO _ = fmapO (_L::Fix g) . fmapO (_L::Fix h)
omap :: FunctorO (PF a) => a -> (x -> ObserverM y) -> F a x -> ObserverM (F a y)
omap (_::a) = fmapO (_L::Fix (PF a))
instance Observable One where
observer = observeBase
instance Observable I where
observer FixId = send "" (fmapO (_L :: Fix Id) thunk FixId)
instance (Typeable a,Observable a) => Observable (K a) where
observer (FixConst a) = send "" (liftM FixConst (fmapO (_L::Fix (Const a)) thk a))
where thk = thunk :: a -> ObserverM a
instance (FunctorO (PF a),FunctorO (PF b)) => Observable (a :+!: b) where
observer (FixSum f) = send "" (liftM FixSum (fmapO (_L::Fix (PF a :+: PF b)) thk f))
where thk = thunk :: a :+!: b -> ObserverM (a :+!: b)
instance (FunctorO (PF a), FunctorO (PF b)) => Observable (a :*!: b) where
observer (FixProd f) = send "" (liftM FixProd (fmapO (_L::Fix (PF a :*: PF b)) thk f))
where thk = thunk :: a :*!: b -> ObserverM (a :*!: b)
instance (FunctorO (PF a), FunctorO (PF b)) => Observable (a :@!: b) where
observer (FixComp f) = send "" (liftM FixComp (fmapO (_L::Fix (PF a :@: PF b)) thk f))
where thk = thunk :: a :@!: b -> ObserverM (a :@!: b)
instance (Functor f, FunctorO f) => Observable (Fix f) where
observer (Fix x) = send (watch (_L::Fix f) (_L::Fix f) x) (liftM Fix (fmapO (_L :: Fix f) thk x))
where thk = thunk :: Fix f -> ObserverM (Fix f)