$ show dtonhofer/prolog_notes
This repository is a personal knowledge base and learning collection focused on SWI-Prolog and logic programming. It contains extensive notes, tutorials, code snippets, and reusable utility modules covering a wide range of Prolog topics: language internals (data types, operators, dicts, exceptions), programming patterns (DCGs, difference lists, delimited continuations, foldl/r), puzzles, exercises, and integration with Java via JPL. The code is well-documented and includes unit tests (plunit) for many components. While not a cohesive application, the repository offers several self-contained, reusable Prolog predicates and patterns that demonstrate good Prolog craftsmanship. The primary value is as a reference and source of reusable utility code for SWI-Prolog developers.
The LLM identified 5 files worth highlighting for reuse:
Provides `partition_freely/4` which partitions a list into a dict using a key-extracting predicate. Uses `foldl/4` with a dict accumulator and `inc_for_key/3` to group elements. Self-contained, well-tested (`.plt` file), and demonstrates a clean functional pattern that can be reused in any Prolog project needing grouping.
Provides predicates for working with difference list lengths: `difflist_length/3` (determine/verify length), `openlist_length/2` (length of open list), and `probe_length/3` (classify list type and length). Demonstrates careful handling of open vs closed lists and uses `var/1` and `nonvar/1` correctly. Reusable in any Prolog code that manipulates difference lists.
Extends the standard `between/3` to support a step argument (`between_with_step/4`). Handles both positive and negative steps, and works with any numeric type. Includes unit tests. A straightforward, well-documented utility that fills a common gap in Prolog's standard library.
Provides a set of meta-predicates: `switch/1` (multi-way branching), `unless/2` (execute unless condition holds), `reify/2` (convert success/failure to boolean), and `if_then_else/3`. These replace verbose `-> ;` cascades and improve code readability. Self-contained and can be used in any Prolog project.
Implements an effect handler pattern using SWI-Prolog's `reset/3` and `shift/1` delimited continuations. Provides `run_counter/2` and `run_markov/2` as examples of stateful computations with implicit state passing. Demonstrates a non-trivial use of continuations that can be adapted for other coroutine-based patterns.