Monadic constraint programming

Tom Schrijvers, Peter J. Stuckey, Philip Wadler

Abstract

Constraint programming (CP) has traditionally suffered from a divide between declarative constraint modeling and imperative, stateful search implementations. This paper introduces a monadic framework in Haskell that bridges this gap. By encapsulating solver state within monads and representing the search space as a lazy, monadic tree, search strategies become first-class, composable objects. This approach allows for the modular construction of complex search heuristics through "search transformers," significantly improving the expressiveness and flexibility of CP systems.

Introduction

Constraint Programming (CP) is a powerful paradigm for solving complex combinatorial problems such as scheduling, routing, and logistics. In a typical CP workflow, a user declares constraints (the "what"), and a solver explores a search tree to find solutions (the "how"). While the modeling phase is often beautifully declarative, the search process is frequently monolithic and imperative. Developers wishing to customize search strategies—for instance, by adding node limits or implementing branch-and-bound—often face significant complexity or are limited by the solver's hard-coded options.

This research addresses the lack of a clean, functional abstraction for search strategies. By leveraging Haskell’s monads and lazy evaluation, the authors propose a system where search strategies are modular, plug-and-play components.

Methodology

The core of the proposed framework is the decoupling of the search tree definition from the exploration strategy. This is achieved through three primary abstractions:

  1. The Monadic Solver and Search Tree: A generic Solver type class encapsulates the stateful nature of the constraint solver. The search space is represented as a Tree data type, implemented as a free monad. This tree structure includes nodes for constraint addition, alternative branches (Try), failure (Fail), and dynamic subtree generation (Dynamic). The Dynamic node is particularly powerful, as it allows the search tree to inspect the solver's state at runtime to prune branches.
  2. Queuing Strategies: Base search orders, such as Depth-First Search (DFS) or Breadth-First Search (BFS), are abstracted into a Queue type class. This allows the underlying data structure (stack, FIFO queue, etc.) to define the fundamental traversal order.
  3. Composable Search Transformers: The framework's most significant innovation is the use of search transformers. These are modular wrappers that modify search behavior—such as imposing depth bounds, node limits, or implementing limited-discrepancy search. Using continuation-passing style, these transformers can be stacked (e.g., combining a node bound with branch-and-bound) to create highly customized search heuristics.

Evaluation and Results

The framework was evaluated using classic combinatorial problems, most notably the N-Queens problem and graph-based shortest path routing.

An 8-Queens solution, a classic combinatorial problem used to evaluate the framework's search efficiency.

The results demonstrate that modular search strategies can lead to dramatic improvements in efficiency. For the 16-Queens problem, a naive "in-order" search required visiting 9,738 nodes. By simply swapping the strategy for a dynamic "first-fail + middle-out" strategy—achieved through simple composition—the node count dropped to 518. Similarly, in shortest path problems, applying a bisection branch-and-bound transformer reduced the search space from over 87,000 nodes to fewer than 6,000.

Key Insights

  • Lazy Evaluation: Haskell's lazy evaluation ensures that the massive search space is only constructed as it is explored, preventing memory exhaustion.
  • Compositional Order: The authors noted that search transformers do not always commute; the order of composition (e.g., applying randomization before or after a discrepancy limit) can significantly impact performance.
  • Separation of Concerns: By isolating the model, the queuing strategy, and the search limits, the framework makes it trivial to experiment with new heuristics.

Conclusion and Future Work

Monadic constraint programming offers a path toward more flexible and maintainable CP systems. While the current focus is on expressiveness and node-visit efficiency, future work will address computational overhead by integrating with high-performance C++ solvers like Gecode via the Foreign Function Interface (FFI). Additionally, the authors plan to incorporate Constraint Handling Rules (CHR) to provide programmable solving alongside programmable search.

Summary updated

BibTeX
@article{schrijvers2009monadic,
  author = {Tom Schrijvers and Peter J. Stuckey and Philip Wadler},
  title = {{Monadic constraint programming}},
  journal = {J. Funct. Program.},
  year = {2009},
  doi = {10.1017/s0956796809990086},
}
Figure