All switch functions are ways to change the signal function to behave like another signal function. I personally think that the Yampa diagrams are somewhat difficult to parse, but the type signatures of the various switches give a good idea of how to understand them. When you understand type signatures, diagrams become much clearer. switch itself is the most basic:
switch :: SF a (b, Event c) -> (c -> SF ab) -> SF ab
If we look at the type, it will tell you exactly what it does: it requires SF sf and the SF sfg generator. sf creates values of type (b, Event c) , and the input for the signal function generator also has type c . Thus, whenever the sf event occurs, SF switches to the result of the SF generator. Until an event occurs, the received SF will return the value of the original SF.
This idea is also used in rswitch and kswitch , but in a slightly different way.
rswitch : This is called an “external switch,” which means the SF will switch without any analysis of its input or output. Let's look at a signature like:
rswitch :: SF ab -> SF (a, Event (SF ab)) b
It takes one SF, which takes type a as input and outputs values of type b . rswitch creates a new SF, which also produces output b , but accepts additional input of type Event (SF ab) . Note that the type of event value matches the input type. This means that whenever an event occurs, this SF switches to this event value. However, the type SF remains SF (a, Event (SF ab)) b . This means that SF can receive additional events with new SFs, which will affect the behavior of the general SF. To do this, you can use the AI behavior in the game:
moveFollowTarget :: SF TargetPosition Velocity moveShootTarget :: SF TargetPosition Velocity moveIdle :: SF TargetPosition Velocity aiMovement :: SF (TargetPosition, Event (SF TargetPosition Velocity)) Velocity aiMovement = rswitch moveIdle
Here, aiMovementManager will fire an event whenever the behavior of the AI movement should change, and the value of the event will be SF, which the movement should change.
kswitch : This is called an intrinsic switch , as the contents of the SF are parsed to find out what the correct switch should be. Let's move on to the type signature
kswitch :: SF ab -> SF (a, b) (Event c) -> (SF ab -> c -> SF ab) -> SF ab
Here kswitch takes three arguments, sf , analyzer and mapping . sf is just standard SF with inputs of type a and outputs of type b . sf is how the signal behaves initially. analyzer is an SF that accepts sf input and output and may or may not trigger some kind of event whose value is of type c . If it does not fire the event, then nothing happens, and SF continues to behave like sf . If it fires an event, then both sf and the value of the event are passed to mapping , which defines the new SF to switch to. kswitch is useful in changing the way systems behave based on their outputs.
One example where this is useful is the algorithm for avoiding TCP congestion . Here we look at whether we are losing network packets or increasing or decreasing the speed with which we request data.