Matching logic explained
Introduction
Matching logic is a unifying logic for specifying and reasoning about static structure and dynamic behavior of programs. It was recently proposed in [1] and further developed in [2], [3]. There exist several equivalent variants of matching logic. In this paper we consider the functional variant, which has a minimal presentation among the others. We refer to this variant as matching logic throughout the paper, abbreviating it as ML.
The key concept of ML is its patterns. Patterns are ML formulas, which are built from variables, constant symbols, a binary application construct, standard FOL constructs ⊥, →, ∃, and a least fixpoint construct μ. In terms of semantics, patterns are interpreted as the sets of elements that match them, which gives ML a pattern matching semantics. For example, is a pattern matched by the natural number 0; is a pattern matched by 1; is then a disjunctive1 pattern matched by 0 and 1, or, to put it another way, an element a matches iff a matches or a matches . Complex patterns can be built this way to match elements that are of particular structure, have certain dynamic behavior, or satisfy certain logic constraints. We discuss various examples in Sections 3 to 9.
We can use patterns to constrain models, by enforcing the models to match a set of given patterns, called axioms. This set of axioms yields a specification, also called a logical theory. In this paper we will define a variety of specifications, some of them capturing relevant mathematical domains, others datatypes, and others capturing transition systems. We will also show how to build a complex specification in a modular way by importing existing specifications. To present ML specifications rigorously and compactly, we propose a specification syntax in Section 3 that allows us to write specifications in a compact and human-readable way. All ML specifications presented in this paper are written using this syntax.
Our main technical contribution is a collection of complete ML specifications of important datatypes and data structures (including parameterized types, function types, and dependent types), a basic process algebra and its dynamic reduction relation, and the higher-order reasoning about functors and monads in category theory. For each specification, we derive several nontrivial properties using the matching logic proof system; some of these properties require inductive/coinductive reasoning, also supported by ML.
We organize the rest of the paper as follows:
- –
In Section 2 we define the syntax and semantics of ML.
- –
In Section 3 we introduce the specification syntax and define the specifications of several important mathematical instruments such as equality, membership, sorts, and functions.
- –
In Section 4 we explain how patterns are interpreted in ML models.
- –
In Section 5 we review the Hilbert-style proof system of ML and its soundness theorem.
- –
In Section 6 we discuss the general principle of induction and coinduction in ML and compare it with the classical principle of (co)induction in complete lattices.
- –
In Section 7 we give specifications for examples of main data types used in programming languages: simple datatypes (booleans and naturals), parametric types (product, sum, functions, lists, and streams), dependent types (vectors, dependent product, and dependent sum). For each example we present and prove illustrative (co)inductive properties.
- –
In Section 8 we define a basic process algebra in ML.
- –
In Section 9 we use ML for higher-order reasoning in category theory and define functors, monads, and comonads as ML specifications.
- –
In Section 10 we conclude the paper.
Section snippets
Matching logic syntax and semantics
We introduce the syntax and semantics of matching logic (ML). We refer the reader to [1], [2], [3] for full technical details. The ML variant that we introduce in this paper is called the functional variant and is firstly proposed in [3], but there fixpoints are not considered. Therefore, the syntax and semantics definitions that we will give in this section are an extension of the work in [3] by fixpoints.
Specification examples: important mathematical instruments
We define several important mathematical instruments such as equality, membership, sorts, functions, predicates, and constructors, as ML specifications. We will start with equality and membership, both of which are defined using the definedness symbol. Then, we will axiomatically define sorts in unsorted ML and show that the sorted functions and predicates can be defined as ML symbols in a direct and easy way. Our last example is to axiomatize a set of functions as the constructors of a given
Understanding models and interpretation of patterns
In this section we explain, based on an example, the flexibility to define models for ML specifications and how various patterns are interpreted in a model. Let us consider the following specification (with loose semantics) of natural numbers (we present the complete specification for clarity):
Matching logic proof system
In this section we review the Hilbert-style proof system for matching logic given in [2]. The proof system is shown in Fig. 1. We write to mean that φ can be proved by the proof system using the axioms in . The following theorem shows that the proof system is sound. Theorem 5.1 implies .[2]
In this paper we will use the proof system to simplify our reasoning about ML validity and semantics.
The following derived rules are useful for coinductive reasoning:
Explaining the general principles of induction and coinduction
In this section we explain how the (Knaster-Tarski) proof rule supplies a (co)induction proof principle in ML. The explanation is based on the well-known (co)induction principle expressed in lattice theory.
Defining types as matching logic specifications
In this section, we show how to define types as matching logic specifications. We discuss the basic types such as Booleans and numbers in Section 7.1. Then, we discuss parameterized types in Section 7.2, fixed-length vector types in Section 7.3, parametric finite sets in Section 7.4, product dependent types in Section 7.5, sum dependent types in Section 7.6. We give some discussion on binding in Section 7.7.
Defining basic process algebra as matching logic specifications
Process algebra is the field where the behavior of distributed or parallel systems is studied by algebraic means. The most known theories include calculus of communicating systems [15], communicating sequential process [16], π-calculus [17], and algebra of communicating processes [18], [19]. In this paper we consider a simple fragment of the algebra of communicating processes called the basic process algebra (BPA). BPA introduces simple operators together with their axioms that enable to
Defining functors and (co)monads as matching logic specifications
In this section we show how higher-order reasoning in category theory can be internalized in ML. We give specifications for functors, monads, and comonads as they are defined in functional languages, like Haskell (see, e.g., [20], [21]).
Conclusion
In this paper we gave an example-driven, yet comprehensive introduction to matching logic. We showed how to use matching logic specifications to capture various mathematical domains and data types, and we proposed matching logic notations to define domain-specific languages. We explained technical details when writing matching logic specifications and reasoning about matching logic semantics. In particular we discussed how to carry out inductive and coinductive reasoning using matching logic.
Declaration of Competing Interest
We declare that there are no conflicts of interest.
References (26)
- et al.
Modal mu-calculi
- et al.
Stream processing coalgebraically
Sci. Comput. Program.
(2013) Nominal logic, a first order theory of names and binding
Inf. Comput.
(2003)- et al.
Comonadic notions of computation
Electron. Notes Theor. Comput. Sci.
(2008) Matching logic
Log. Methods Comput. Sci.
(2017)- et al.
Matching μ-logic
- et al.
A general approach to define binders using matching logic
- et al.
Unification in matching logic
A lattice-theoretical fixpoint theorem and its applications
Pac. J. Math.
(1955)An initial algebra approach to the specification, correctness and implementation of abstract data types
(1976)
Initial Algebra Semantics in Matching Logic
Concepts: The Future of Generic Programming or How to Design Good Concepts and Use Them Well
Defining concepts
Overload J.
Cited by (16)
Capturing constrained constructor patterns in matching logic
2023, Journal of Logical and Algebraic Methods in ProgrammingCitation Excerpt :Matching logic proof system includes Knaster-Tarski proof rule [8,22] dedicated to fixpoint reasoning. This rule, combined with the least fixpoint definitions leads to matching logic proof strategies (see [22] for more details on (co)inductive reasoning in matching logic). Here we exemplify the inductive strategies for natural numbers, lists and multisets.
An integrated tool set for verifying CafeOBJ specifications
2022, Journal of Systems and SoftwareCitation Excerpt :The K framework (Rosu and Serbanuta, 2010), which was initially implemented in Maude (the current implementation language is Java), is a semantic framework for the specification and analysis of programming languages. K provides theorem proving via matching logic (Chen et al., 2021), a logic proposed to reason about the static structure and dynamic behavior of programs. The main difficulty to adapt these techniques to our tool is the specific target of the K framework, which is not designed for general systems but for the semantics of programming languages.
Matching-Logic-Based Understanding of Polynomial Functors and their Initial/Final Models
2023, Electronic Proceedings in Theoretical Computer Science, EPTCSGenerating Proof Certificates for a Language-Agnostic Deductive Program Verifier
2023, Proceedings of the ACM on Programming LanguagesMatching Logic for Concurrent Programs Based on Rely/Guarantee and Abstract Patterns
2023, International Journal of Software Engineering and Knowledge Engineering