Scryer Prolog

repository·master·Indexed Jul 30, 2026

https://github.com/mthom/scryer-prolog

An open-source, industrial-strength ISO Prolog implementation written mostly in Rust. Version 0.10.0 serves as both a production environment and a research testbed for logic and constraint programming. It features first instantiated argument indexing, efficient UTF-8 string representation, and support for tabling (SLG resolution). The project provides native compilation, WebAssembly targets via wasm-pack, and Docker images.

Tokens
7.9K
Snippets
21
Records
59
Agent score
68%

What's inside scryer-prolog

  1. How benchmarks are structured and executed

    master

    Benchmarks in Scryer Prolog are centered around running queries against a Prolog module file.

    Lifecycle:

    1. benchmark.setup() is called, which reads a specified .pl module file.
    2. A new scryer_prolog::machine::Machine is initialized.
    3. The query is executed against that machine.

    Execution differences:

    • Criterion: Runs the query many times to gather statistical data.
    • iai: Runs the query exactly once to count instructions.
  2. Use Scryer Prolog for standards-compliant applications

    master
    Scryer Prolog is designed with a strong commitment to the Prolog ISO standard. This makes it suitable for use in regulated environments (corporations, government agencies) where interoperability, standards compliance, and warranty are critical. It is also well-suited for teaching Prolog and testing the syntactic conformance and portability of existing Prolog programs.
  3. How first instantiated argument indexing works

    master

    Scryer Prolog uses a strategy called first instantiated argument indexing. It indexes on the leftmost argument of a predicate that is not a variable in all clauses of that predicate's definition.

    This is particularly useful for meta-predicates like maplist/N and foldl/N. Even if the first argument is a variable (the goal), Scryer will automatically use the second argument (the list) for indexing, preventing unnecessary choicepoints and ensuring deterministic goals.

  4. Efficient String and Partial String representation

    master

    Scryer Prolog uses a compact UTF-8 internal representation for strings, providing up to a 24-fold reduction in memory usage compared to traditional WAM implementations.

    Key Behaviors:

    • The default value for the double_quotes flag is chars. This means double-quoted strings like "abc" are treated as lists of characters: [a,b,c].
    • Partial Strings: You can use partial_string/3 from library(iso_ext) to construct partial strings that behave like partial lists of characters while maintaining the efficient internal encoding.
    ?- partial_string("abc", Ls0, Ls).
    Ls0 = [a,b,c|Ls].
  5. Install Scryer Prolog

    master

    Scryer Prolog can be installed using several methods depending on your environment and requirements:

    Binaries

    Pre-compiled binaries are available for quick installation.

    Native Compilation

    You can compile the project from source using the following methods:

    • From a local git checkout: Clone the repository and build it locally.
    • Via cargo install: Use the Rust package manager to install it directly.

    Building WebAssembly

    Scryer Prolog supports building for WebAssembly targets.

    Caveats for Windows

    Note that there are specific caveats when installing or running on Windows environments.

  6. Use Showterm to draw Prolog terms as trees in Emacs

    master

    Showterm is a tool that allows you to visualize Prolog terms as trees directly within Emacs.

    Prerequisites

    Ensure the following programs are installed on your system:

    • Scryer Prolog
    • dot (part of [Graphviz])
    • convert (part of [ImageMagick])

    Installation

    1. Copy showterm.el and showterm.pl to the same directory (e.g., ~/scryer-prolog/tools/).
    2. Add the following line to your .emacs configuration to load the tool:
      (load "~/scryer-prolog/tools/showterm.el")
    3. If the Scryer Prolog executable is not in your PATH, set the scryer-prolog-path variable in your .emacs:
      (setq scryer-prolog-path "/usr/local/bin/scryer-prolog")

    Usage

    To visualize a term, select the Prolog term in a buffer as a region and invoke the showterm function. You can do this via M-x showterm RET or by binding it to a key in your .emacs:

    (global-set-key [f12] 'showterm)
    (load "~/scryer-prolog/tools/showterm.el")
    (setq scryer-prolog-path "/usr/local/bin/scryer-prolog")
    (global-set-key [f12] 'showterm)
  7. Build Scryer Prolog from a local git checkout

    master

    Clone the repository and use cargo build with the --release flag to produce an optimized, faster executable. The resulting binary will be located in target/release/scryer-prolog.

    $> git clone https://github.com/mthom/scryer-prolog
    $> cd scryer-prolog
    $> cargo build --release
  8. Build Scryer Prolog for Windows

    master

    On Windows, it is recommended to build within an MSYS2 environment to handle native C compilation requirements. The resulting binary does not require MSYS2 to run. For execution, use an advanced shell like Windows Terminal instead of the default mintty.

    To create a Windows Installer (MSI) using the WiX Toolset, compile in release mode and then run:

    candle.exe scryer-prolog.wxs
    light.exe scryer-prolog.wixobj
  9. Install Scryer Prolog via Binaries

    master

    Precompiled binaries for various platforms can be downloaded directly from the GitHub releases page. This is the fastest way to get started without a local build environment.

    https://github.com/mthom/scryer-prolog/releases/latest
  10. Interact with the Prolog REPL

    master

    After consulting a program, you can query its predicates.

    Querying and Results:

    • To see further answers for a query, press SPACE.
    • To abort a search and return to the top-level prompt, press RETURN or ..
    • To see the next 5 answers, press f.
    • To see all answers, press a.
    • To show a help message, press h.

    Navigation and Completion:

    • Use TAB to complete atoms and predicate names. Pressing TAB repeatedly cycles through alternative completions.
  11. Debug Prolog code with library(debug)

    master

    For declarative debugging, use predicates from library(debug):

    • (*)/1: "Generalize away" a goal. Place * in front of a goal to ignore it during debugging to see if the rest of the predicate succeeds.
    • ($)/1: Emit a trace of the execution, showing when a goal is invoked and when it succeeds.