From High to Low: Simulating Nondeterminism and State with State

Wenhao Tang,Tom Schrijvers
2023-12-05
Abstract:Some effects are considered to be higher-level than others. High-level effects provide expressive and succinct abstraction of programming concepts, while low-level effects allow more fine-grained control over program execution and resources. Yet, often it is desirable to write programs using the convenient abstraction offered by high-level effects, and meanwhile still benefit from the optimisations enabled by low-level effects. One solution is to translate high-level effects to low-level ones. This paper studies how algebraic effects and handlers allow us to simulate high-level effects in terms of low-level effects. In particular, we focus on the interaction between state and nondeterminism known as the local state, as provided by Prolog. We map this high-level semantics in successive steps onto a low-level composite state effect, similar to that managed by Prolog's Warren Abstract Machine. We first give a translation from the high-level local-state semantics to the low-level global-state semantics, by explicitly restoring state updates on backtracking. Next, we eliminate nondeterminsm altogether in favor of a lower-level state containing a choicepoint stack. Then we avoid copying the state by restricting ourselves to incremental, reversible state updates. We show how these updates can be stored on a trail stack with another state effect. We prove the correctness of all our steps using program calculation where the fusion laws of effect handlers play a central role.
Programming Languages
What problem does this paper attempt to address?
### What problems does this paper attempt to solve? This paper aims to study how to use low - level effects to simulate high - level effects, especially through algebraic effects and handlers. Specifically, the author focuses on the interaction between state and nondeterminism, namely the so - called local state, and explores how to map it to the low - level compound state effects managed by the Prolog - like Warren Abstract Machine (WAM). #### Main problems 1. **Conversion between high - level and low - level effects**: - The core problem of the paper is to explore how to convert high - level effects (such as local state) into low - level effects (such as global state). High - level effects provide a concise abstraction of programming concepts, while low - level effects allow for more fine - grained control of program execution and resource utilization. - The author hopes that through this conversion, they can both enjoy the convenient abstraction brought by high - level effects and benefit from the optimizations provided by low - level effects. 2. **Semantic differences between local and global states**: - Local state means that each nondeterministic branch has its own copy of the state, which is very common in search problem - solving systems such as Prolog. - Global state means that all nondeterministic branches share a single state. This method has performance advantages and can reduce memory usage and improve locality. 3. **Correctness and optimization**: - The paper also focuses on how to prove the correctness of these conversions and explores how to avoid holding multiple copies of the state through incremental and reversible state updates, thereby improving efficiency. #### Overview of solutions - **Conversion from local state to global state**: First, convert the local state semantics to global state semantics by explicitly restoring state updates during backtracking. - **Elimination of nondeterminism**: Introduce a low - level state containing a choicepoint stack to replace nondeterminism. - **Incremental state update**: Avoid copying the state by restricting oneself to only incremental and reversible state updates. - **Trail stack to store updates**: Store incremental updates on a trail stack so that they can be restored in batches during backtracking. #### Proof of correctness The author uses equational reasoning techniques and the fusion law for handlers to prove the correctness of all steps. ### Summary This paper, through the study of algebraic effects and handlers, provides a method for converting high - level effects into low - level effects, with a particular focus on the interaction between state and nondeterminism. This method not only helps to understand the relationship between these two effects but also provides theoretical support for optimization in practical programming.