Certified Context-Free Parsing: A formalisation of Valiant's Algorithm in Agda

Jean-Philippe Bernardy, Patrik Jansson

TL;DR

This paper presents a fully verified, formal implementation of Valiant's efficient context-free parsing algorithm using the Agda proof assistant. By formalizing the algorithm and its correctness proof in Agda, the authors provide a highly trustworthy and educational resource for understanding both advanced parsing techniques and formal verification methods.

What Problem Does This Paper Address?

Parsing context-free grammars is fundamental to understanding the structure of programming languages and natural language. While many parsing algorithms exist, Valiant's algorithm, developed in 1975, remains the most asymptotically efficient for this task. However, implementing and formally proving the correctness of such complex algorithms can be challenging. This paper addresses the need for a rigorously verified implementation of Valiant's algorithm, making it accessible and trustworthy for both theoretical study and potential practical use. The authors also aim to provide a tutorial on using the Agda proof assistant for formalizing algorithms and proofs.

Methods

The core of this work is the formalization of Valiant's algorithm and its correctness proof in Agda, a functional programming language with dependent types that serves as a proof assistant. The authors follow a "calculational refinement" approach, deriving the algorithm from its specification rather than implementing it first and then proving it correct.

Key methodological aspects include:

  • Algebraic Specification: The paper defines a series of algebraic structures (SemiNearRing, SemiNearRing2, ClosedSemiNearRing) that capture the necessary properties for matrix operations and the transitive closure computation. These structures are built incrementally, adding complexity as needed for the algorithm and its proof.
  • Type-Theory Based Proofs: Agda's propositions-as-types paradigm is used, where proving a statement is equivalent to constructing a program of that statement's type. This ensures that the implementation and its proof are checked by Agda's type checker.
  • Recursive Structure: The algorithm is formalized using a recursive definition of matrices, mirroring the structure of Valiant's algorithm which breaks down the problem into smaller subproblems.
  • Literate Programming: The Agda code is presented in a literate programming style, interweaving code with explanatory text, making it easier to follow for readers unfamiliar with Agda.
  • Equational Reasoning: Agda's equational reasoning features are employed to structure proofs in a way that resembles pen-and-paper mathematical reasoning, enhancing readability.

Datasets

Not discussed in the paper. The paper focuses on the formalization of an algorithm rather than empirical evaluation on specific datasets.

Key Insights and Ideas

  • Valiant's Algorithm as Transitive Closure: A central insight is the elegant reduction of context-free parsing to computing the transitive closure of a matrix representing grammar rules and input symbols. This abstract view highlights the algorithm's generality.
  • Formal Verification for Trust: The use of Agda provides a high degree of confidence in the correctness of the implementation. The entire development is type-checked, ensuring that the algorithm adheres to its specification.
  • Agda as a Tutorial: The paper serves as a practical guide to using Agda for algorithm specification and proof, demonstrating how to define algebraic structures, express propositions as types, and construct proofs.
  • Incremental Development: The structured, layered approach to defining algebraic structures in Agda makes the complex formalization more manageable and understandable.
  • "Smallest" Solution Guarantee: The proof rigorously establishes that the computed closure is indeed the smallest solution, which is crucial for ensuring that only valid parses are found.

Key Results

  • Fully Certified Implementation: The paper presents a complete, formally verified implementation of Valiant's algorithm in Agda.
  • Correctness Proof: A formal proof of the algorithm's correctness is provided, checked by the Agda proof assistant.
  • Educational Resource: The paper and its accompanying Agda code serve as a valuable tutorial for learning about formal verification, dependent types, and the Agda proof assistant.
  • Algebraic Framework: A robust algebraic framework is developed to support the algorithm and its proof, demonstrating how to model complex structures in a formal system.

Limitations and Future Work

  • Performance: The authors acknowledge that the Agda program extraction mechanism is not yet mature, and thus the practical performance of their implementation is expected to be poor. This is a limitation of the current Agda ecosystem rather than the algorithm itself.
  • General Matrix Sizes: The current implementation assumes matrix dimensions are powers of two for simplicity. Extending this to handle arbitrary matrix sizes is suggested as future work.
  • Boolean Grammars: The authors believe their framework can be extended to handle Boolean grammars, which allow for intersection and complement in production rules.
  • Sparse Matrices: The proof does not currently deal with sparse matrices, which are important for average-case efficiency. Supporting sparseness is identified as a potential extension.
  • Arbitrary Semirings: The formalization could be generalized to work with arbitrary closed semirings, potentially leading to broader applications beyond context-free parsing.

Summary updated

Abstract

Valiant (1975) has developed an algorithm for recognition of context free languages. As of today, it remains the algorithm with the best asymptotic complexity for this purpose. In this paper, we present an algebraic specification, implementation, and proof of correctness of a generalisation of Valiant's algorithm. The generalisation can be used for recognition, parsing or generic calculation of the transitive closure of upper triangular matrices. The proof is certified by the Agda proof assistant. The certification is representative of state-of-the-art methods for specification and proofs in proof assistants based on type-theory. As such, this paper can be read as a tutorial for the Agda system.
BibTeX
@misc{bernardy2016certified,
  author = {Jean-Philippe Bernardy and Patrik Jansson},
  title = {{Certified Context-Free Parsing: A formalisation of Valiant's Algorithm in Agda}},
  year = {2016},
  eprint = {1601.07724},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1601.07724},
}
Figure