That's very interesting.
One problem is, if the set of event is closed, the set of possible data types is not (the user can choose any data type for a Message callback for example). I think this can be solved using a class instead of a GADT for "Type". One can also use a type witness?
On Tue, Sep 11, 2012 at 6:46 PM, David Menendez wrote:want to put viewEvent in the Event typeclass, but the class of eventsMixing GADTs and Typeable seems like a bad idea. If you really don't
is closed, you could use a GADT to witness the event type.On Tue, Sep 11, 2012 at 7:03 PM, Corentin Dupont wrote:unfortunately it seems that I will be obliged to maintain 2 parallel structures:
for each Event instance, I will have to add a ViewEvent element as well carrying the same information:That's why I like the all-GADT solution...Inspired by David's suggestion, here's another version without Typeable. In Corentin's version, the switching back and forth between explicit and forgetful typing bothered me. This version never forgets types. Also, viewEvent is really an instance of Show, as I would expect. I don't see the extra maintenance burden mentioned by Corentin.{-# LANGUAGE TypeFamilies, GADTs #-}data Player = Arrive | Leave deriving Shownewtype Message t = Message String deriving (Eq, Show)data Type :: * -> * whereInt :: Type (Message Int)String :: Type (Message String)Player :: Type Playerdata TEq :: * -> * -> * whereRefl :: TEq a ateq :: Type a -> Type b -> Maybe (TEq a b)teq Int Int = Just Reflteq String String = Just Reflteq Player Player = Just Reflteq _ _ = Nothingtype family Data t :: *type instance Data (Message t) = ttype instance Data Player = Intdata Event t = Event (Type t) tdata Handler whereHandler :: Event t -> (Data t -> IO ()) -> HandlerrunHandler :: Eq t => Event t -> Data t -> Handler -> IO ()runHandler (Event t e) d (Handler (Event u e') f) =case teq t u ofJust Refl | e == e' -> f d_ -> return ()runHandlers :: Eq t => Event t -> Data t -> [Handler] -> IO ()runHandlers e d hs = mapM_ (runHandler e d) hs-- Replacement for viewEventinstance Show (Event t) whereshow (Event ty e) =case ty ofInt -> show e ++ " of type Int"String -> show e ++ " of type String"Player -> "Player " ++ show e
messageEvent :: Type (Message t) -> String -> Event (Message t)messageEvent t s = Event t (Message s)playerEvent :: Player -> Event PlayerplayerEvent = Event Player-- Tests
event1 = messageEvent Int "give me a number" -- No type signature necessary!handler1 = Handler event1 (\n -> putStrLn $ "Your number is: " ++ show n)test1 = runHandlers event1 1 [handler1] -- Yields "Your number is: 1"Regards,Sean