[Haskell-beginners] Question about FRP trigger based programming

Mathew de Detrich deteego at gmail.com
Mon May 10 02:01:44 EDT 2010

Basically Im designing an interface which will (eventually) be customizable
through scripts run through HSPlugin, and the scripting is going to be very
reactive based (its for a game). Im basically trying to make a Trigger based
API, so this example should show simply what I am trying to do (you can also
see it here http://pastebin.com/ZNCD75KN). Obviously some of the haskell
isn't legitimate (I just hope im conveying what I am trying to do)

data Event = OnPress | OnRelease
data Bindings = (a,b,..z) -- This isn't legal haskell, but basically a tuple
with any
-- amount of types, this would have to be done with generics or something
along those lines

onKey :: Event Char Bindings (Char → Bindings → ReturnVal)
onKeyPress = error "stub"

testFunc :: Char → Bindings → ReturnVal
testFunc a _ = do
     putStrLn [a]

testFunc2 :: Char → Bindings → ReturnVal
testFunc2 a _ = do
    putStrLn [z]
        where z = a++a

testFunc3 :: Char → Bindings → ReturnVal
testFunc3 a b = do
    putStrLn ([a] ++ show bX ++ bZ)
        bX = b.x -- x binding from the tuple (x,z)
        bZ = b.z -- z binding from the tuple (x,z)

main = do
    onKey Press 'A' Nothing testFunc
    onKey Release 'B' Nothing testFunc2
    onKey Press 'C' (x,z) testFunc3
        x = 2
        z = "cheese"

Basically the premise is that you make "Triggers" which are based on 2 data
types. One being the event (i.e. in this example a key being pressed down or
up) and the actual data type that is being triggered (in this case a key).
In the example above, the onKey is the actual function that the users
creating the scripts will use (so it has to be as clear as possible) and the
testFuncX are the functions that the users will create to bind onto the

What happens is you attach functions to those triggers, and those functions
execute (for now consecutively but it can be changed) whenever those events
happen. There also needs to be the ability of binding variables to those
functions if you need them (this is what happens for testFunc3 with (x,y)),
in case users need to send extra data to the functions being bounded onto
the triggers. The supposed above would output this

I have been reading through two FRP libraries (both Yampa and Reactive)
however the papers are very generic and there seems to be limited examples
(at least in the area of binding functions to triggers instead of the more
generic events and behaviors based on values that change over time). Would
the basic principle above be able to coded using either Reactive or Yampa or
would I have to create my own library to do such a thing, and more
importantly how would I be able to do it (and a working version of the
example above)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100510/742f8513/attachment.html

More information about the Beginners mailing list