[Haskell-cafe] Make a DSL serializable

Daniel Trstenjak daniel.trstenjak at gmail.com
Sun Mar 24 18:25:43 CET 2013


Hi Corentin,

> I have a DSL like this:
> data Exp where
>     OnEvent      :: EventName -> (Int -> Exp) -> Exp
> (...)
> 
> The "OnEvent" element carries a function (the handler to be called when the
> event happens), and that makes my DSL non showable/serializable.
> How could I fix that? This is a real handicap not to be able to serialize
> the state of my whole application because of that :)

You could have a unique id for your handlers, which might be
an Int or a String and have some kind of registration for the
handlers.

data Exp where
    OnEvent :: EventName -> HandlerId -> Exp

type HandlerId = String
type Handler   = (Int -> Exp)
type Handlers  = HashMap HandlerId Handler

registerHandler :: Handlers -> (HandlerId, Handler) -> Handlers
getHandler      :: Handlers -> HandlerId -> Maybe Handler

But you have to ensure, that for each application run the same
HandlerId also gets the same Handler.


Less flexible but more secure is an ADT for you Handler.

data Handler = DoThat
             | DoSometingElse

You can than just pattern match on your handler and don't need any kind
of registration.


But you can go further and define your own little Handler DSL.


Greetings,
Daniel



More information about the Haskell-Cafe mailing list