A Coq Library of Sets for Teaching Denotational Semantics

Qinxiang Cao, Xiwei Wu, Yalun LiangarXiv 2024 · DOI

TL;DR

The authors present a lightweight Coq library that leverages type classes to introduce standard mathematical notations (like $\in$, $\cup$, $\subseteq$, and $\circ$) for sets and relations. Designed specifically for undergraduate courses in denotational semantics, the library drastically improves readability and simplifies proof automation without introducing any proof-term overhead.

What Problem Does This Paper Address?

Interactive theorem provers like Coq are powerful tools for teaching programming language theory, providing students with immediate, machine-checked feedback on homework. However, Coq's native formalization of sets can be incredibly unintuitive for beginners.

Typically, Coq represents a subset as a curried function to Prop (e.g., A -> Prop for a set, or A -> B -> Prop for a binary relation). This leads to two major issues in an educational setting:

  1. Poor Readability: Set membership is written as function application (X a or X a b) rather than the familiar $a \in X$ or $(a,b) \in X$.
  2. Duplicated Definitions: Standard operations like union ($\cup$) or composition ($\circ$) must be defined separately for every relation arity (e.g., union1 for sets, union2 for binary relations). This forces students to memorize multiple versions of the same semantic theorems (e.g., commutativity and associativity).

Methods

To bridge the gap between formal logic and standard mathematical notation, the authors developed a Coq library based on two core components:

  • Type Class Overloading: The library defines type classes (SETS, RELS, and SETS_ELE) to overload standard notations. This allows a single symbol like $\cup$ or $\circ$ to work seamlessly across sets, binary relations, ternary relations, and labeled relations.
  • Zero-Cost Unfolding Tactic (Sets_unfold): When proving set properties, standard Coq unfolding tactics often reduce goals into messy, low-level lambda expressions. The authors designed a custom Ltac tactic, Sets_unfold, which uses a helper function called set_transfer to safely transform variables into intuitive propositions containing the $\in$ symbol. Crucially, this transformation relies on $\beta\iota\delta$-equivalence, meaning it introduces absolutely no extra rewrite steps or proof-term bloat.

Datasets

While this paper does not use machine learning datasets, the library was evaluated empirically across three undergraduate computer science courses at Shanghai Jiao Tong University.

Student demographics across the three evaluated courses

The student cohorts varied significantly in their prior exposure to discrete math and programming experience, providing a robust test bed for the library's educational clarity.

Key Insights and Ideas

  • Notation is a Cognitive Load: In education, forcing students to translate X a b to $(a,b) \in X$ distracts from the actual course content (learning language semantics). Standard mathematical notation acts as a user interface that lowers this cognitive barrier.
  • Arity-Agnostic Unification: By treating a unary relation (A -> Prop) as a binary relation to a unit type (A -> unit -> Prop), the authors demonstrate how type classes can elegantly unify relation composition across different dimensions. This helps students visualize properties like the distribution of composition over union using intuitive diagrams.
Figure 1: Diagrams for binary relation composition
  • The "Zero-Cost" Sweet Spot: Abstracting notations for the sake of readability usually comes at the cost of performance or proof complexity. This library proves that with clever tactic design, you can have human-readable goals during interactive proofs while maintaining identical underlying proof terms.

Key Results

  • Comprehensive Lemma Library: The library provides 61 pre-proven lemmas detailing the commutativity, associativity, and distributivity of set operations, eliminating the need for students to hunt for type-specific theorem names.
  • Seamless Automation: The library's operators are proven to preserve set equivalence and inclusion using Coq's standard morphism type classes, allowing students to use Coq's built-in rewrite tactic directly on sets.
  • Identical Proof Terms: The proof terms generated using Sets_unfold match manual, low-level proofs exactly, validating the "zero-cost" claim.

Limitations and Future Work

Despite minimizing the friction of set notation, integrating a theorem prover into a course still requires dedicating valuable lecture time to teaching the tool itself. To keep the course manageable, the instructors had to intentionally omit advanced Coq concepts like the Curry-Howard correspondence and inductively defined propositions.

Currently, the library is optimized for simple imperative languages and textbook denotational semantics. Extending this user-friendly notation system to handle highly complex, concurrent language features remains an open avenue for future development.

Summary updated

BibTeX
@misc{cao2024coq,
  author = {Qinxiang Cao and Xiwei Wu and Yalun Liang},
  title = {{A Coq Library of Sets for Teaching Denotational Semantics}},
  year = {2024},
  eprint = {2404.05459},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2404.05459},
}
Figure