Abstract
Functional languages have provided major benefits to the verification community. Although features such as purity, a strong type system, and computational abstractions can help guide programmers away from costly errors, these can present challenges when used in a reactive system. Functional Reactive Programming (FRP) is a paradigm that allows users the benefits of functional languages and an easy interface to a reactive environment. We present a tool for building autonomous vehicle controllers in FRP using Haskell.
Introduction
Autonomous vehicles (AVs) are quintessential reactive systems, requiring reliable interaction with highly dynamic environments. Traditionally, these systems are developed using imperative languages like C++, which often rely on complex callback loops and shared state. As these systems scale, they become increasingly difficult to reason about, debug, and formally verify.
While functional programming offers benefits like purity and strong typing, applying it to cyber-physical systems is non-trivial. These systems require a tight integration of continuous signals (e.g., velocity) and discrete steps (e.g., digital controller logic). Existing tools often lack an explicit notion of time, complicating the modeling of temporal dependencies inherent in driving.
Methodology: Haskell-TORCS and Yampa
To bridge the gap between functional programming and physical simulation, we introduce Haskell-TORCS, a library providing Haskell bindings for The Open Racing Car Simulator (TORCS). TORCS is a sophisticated open-source physics engine widely used in AV research.
Our approach utilizes Arrowized Functional Reactive Programming via the Yampa library. In this paradigm, the core abstraction is a Signal (a value varying over time) and a Signal Function (SF) (a transformer converting one signal to another). To manage stateful logic—such as gear shifting or calculating velocity from consecutive frames—we employ the ArrowLoop abstraction. This allows for feedback loops without the "spaghetti" of global variables common in imperative implementations. By defining control logic (steering, acceleration) as pure functions, we isolate the logic from side effects, enhancing testability and verifiability.
Key Insights
Time as a First-Class Citizen
FRP treats time-varying values as primary objects. This is a natural fit for AVs, where the current state is inherently dependent on previous states.
Separation of Concerns
The "Arrow" syntax allows developers to pipe data through explicit transformations (e.g., Image -> Steering -> DriveState). This makes the dependency flow of the controller transparent and modular.
Concurrency for Platooning
Haskell’s native support for multi-threading facilitates the simulation of multiple vehicles. We implemented a communication layer using a thread-safe shared memory library (MVar), allowing vehicles to coordinate speed and avoid collisions through broadcasted requests.
Results
We successfully implemented a solo controller capable of navigating TORCS tracks with reasonable speed and finesse. The logic for gear shifting, steering, and acceleration is remarkably concise. Furthermore, we demonstrated a multi-vehicle platooning scenario where cars coordinate to avoid imminent collisions. While raw benchmarks were not the primary focus, the inherent parallelism of the FRP approach suggests potential performance gains on multicore processors compared to traditional imperative versions.
Limitations and Future Work
The current platooning simulation assumes idealized vehicle-to-vehicle (V2V) communication with zero packet loss. Real-world applications must account for noise and distance constraints. Additionally, while TORCS provides excellent physics, its graphics are dated; future work may explore higher-fidelity environments like Grand Theft Auto V for advanced image recognition. Finally, while FRP simplifies the path to formal verification, applying methods like model checking to these specific controllers remains a priority for future research.
Summary updated