Search code examples
haskellfrpnetwirearrow-abstraction

Netwire mutually dependant wires


To try out Netwire, I'm implementing Pong using the library. In the code I have a ball wire and a computer paddle wire, and since they depend on each other for some values I've been running into issues with infinite loops. Some pseudo-code to explain:

ball :: Wire () IO GameInput Ball
ball = (... define ball ...) . pcPaddle

pcPaddle :: Wire () IO GameInput Paddle
pcPaddle = (... define pcPaddle ...) . ball

The thing to notice is they take each other for inputs. I've tried to alleviate this by doing the following:

ball :: Wire () IO GameInput Ball
ball = ( ... ) . delay ( ... base paddle init ...) . pcPaddle

and other variations of using the delay function in these two wires, but I'm getting the <<loop>> runtime error regardless.

How do I initialize one of the wires so that this system can work?


Solution

  • Of course 5 minutes later I find the magic combination that seems to work. What I did was I altered the inputs the wires took in to be

    ball :: Wire () IO Paddle Ball
    ball = ...
    
    paddle :: Wire () IO Ball Paddle
    paddle = ...
    

    then when it came to creating my network of wires I did this:

    {-# LANGUAGE DoRec  #-}
    {-# LANGUAGE Arrows #-}
    system = proc g -> do
        rec b <- delay (... ball initial value ...) . ball -< p
            p <- paddle -< b
    
        returnA -< (b,p)
    

    This acknowlegdes their dependency, and gives the paddle the dummy initial value for the ball on it's first pass.