Synchronous Programming with Refinement Types

Jiawei Chen, José Luiz Vargas de Mendonça, Bereket Shimels Ayele, Bereket Ngussie Bekele, Shayan Jalili, Pranjal Sharma, Nicholas Wohlfeil, Yicheng Zhang, Jean-Baptiste Jeannin · DOI

Abstract

Cyber-Physical Systems (CPS) consist of software interacting with the physical world, such as robots, vehicles, and industrial processes. CPS are frequently responsible for the safety of lives, property, or the environment, and so software correctness must be determined with a high degree of certainty. To that end, simply testing a CPS is insufficient, as its interactions with the physical world may be difficult to predict, and unsafe conditions may not be immediately obvious. Formal verification can provide stronger safety guarantees but relies on the accuracy of the verified system in representing the real system. Bringing together verification and implementation can be challenging, as languages that are typically used to implement CPS are not easy to formally verify, and languages that lend themselves well to verification often abstract away low-level implementation details. Translation between verification and implementation languages is possible, but requires additional assurances in the translation process and increases software complexity; having both in a single language is desirable. This paper presents a formalization of MARVeLus, a CPS language which combines verification and implementation. We develop a metatheory for its synchronous refinement type system and demonstrate verified synchronous programs executing on real systems.

Introduction

In the development of Cyber-Physical Systems (CPS)—such as autonomous vehicles, surgical robotics, and industrial controllers—software errors can have catastrophic real-world consequences. Traditionally, safety is addressed through two distinct workflows: mathematical verification using abstract models (e.g., hybrid automata) and implementation using low-level executable code (e.g., C or Lustre).

The "gap" between these two worlds is a significant source of vulnerability. A safety proof for an abstract model may be invalidated by implementation bugs, timing discrepancies, or translation errors. MARVeLus aims to eliminate this gap by providing a single language where the type system itself performs formal verification at compile-time, ensuring that the executable code adheres to mathematically proven safety properties.

The MARVeLus Language and Formalism

MARVeLus is built upon the foundations of Lustre, a synchronous programming language where data is processed as continuous streams updated at discrete clock ticks. The core innovation of MARVeLus is the integration of two powerful formalisms into this synchronous framework:

  1. Refinement Types: These allow developers to constrain types with logical predicates. For example, a variable representing a positive velocity can be typed as {v: float | v > 0.}, rather than just a generic float.
  2. Temporal Logic (LTL): To handle properties that evolve over time, MARVeLus introduces temporal operators such as Always ($\Box$) and Next ($\bigcirc$). This allows the type system to express and verify invariants like "the distance to the leading vehicle will always exceed the minimum stopping distance."
The formal syntax of MARVeLus types and predicates, showing how base types are extended with refinement types and temporal trace predicates.

As illustrated in the syntax above, the language distinguishes between state predicates ($p, q$) and trace predicates ($\phi, \psi$). The latter enables the specification of safety properties across execution traces. The compiler utilizes the Z3 SMT solver to automatically verify that the program logic satisfies these refined type constraints. Furthermore, the language introduces a models keyword, allowing developers to link verifiable mathematical models of the physical environment with real-time sensor data during execution.

Experimental Evaluation

The effectiveness of MARVeLus was evaluated through both simulation and hardware deployment:

  • Industrial Control: A simulated tank system demonstrated how refinement types prevent overflows and maintain safety envelopes.
  • Robotic Collision Avoidance: A vehicle-following program was implemented in just 89 lines (52 lines of code and 37 lines of type specifications). The SMT solver verified the safety of this controller in less than 1.3 seconds.
  • Hardware Trials: The verified code was deployed on M-Bot Mini robots. Across thirty trials involving stationary obstacles and decelerating leaders, the MARVeLus-verified code successfully prevented all collisions, even when physical factors like friction introduced minor deviations from the ideal model.

Key Insights and Future Directions

A critical insight of the MARVeLus metatheory is the mapping of the synchronous fby (followed-by) operator to mathematical induction. By treating the initial value as the base case and the subsequent stream as the inductive step, the type system can automatically prove long-term safety properties. The authors also addressed the "Type Evolution" problem, developing a formal metatheory to ensure that types remain valid as time progresses and streams are consumed.

While MARVeLus represents a significant step forward, several areas for future work remain. Currently, complex safety properties may require manual "invariants" (hints) from the developer; future versions could incorporate automated invariant generation. Additionally, while the current discrete-time model is effective for many controllers, integrating differential invariants would better capture continuous physical dynamics. Finally, accounting for floating-point precision limits remains a goal for ensuring robustness against digital rounding errors.

Summary updated

BibTeX
@misc{chen2024synchronous,
  author = {Jiawei Chen and José Luiz Vargas de Mendonça and Bereket Shimels Ayele and Bereket Ngussie Bekele and Shayan Jalili and Pranjal Sharma and Nicholas Wohlfeil and Yicheng Zhang and Jean-Baptiste Jeannin},
  title = {{Synchronous Programming with Refinement Types}},
  year = {2024},
  eprint = {2406.06221},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.06221},
}
Figure