Skip to content

A comprehensive workshop for learning Scryer Prolog with literate programming using Org-mode. Includes automated setup, examples, and LSP support for Emacs.

Notifications You must be signed in to change notification settings

aygp-dr/scryer-prolog-workshop

Repository files navigation

Scryer Prolog Workshop

Badges

https://img.shields.io/badge/License-BSD%203--Clause-blue.svg https://img.shields.io/badge/rust-%23000000.svg?style=flat&logo=rust&logoColor=white https://img.shields.io/badge/-FreeBSD-%23AB2B28?style=flat&logo=freebsd&logoColor=white https://img.shields.io/badge/Org-literate%20programming-77aa99?style=flat&logo=org&logoColor=white

A comprehensive workshop for learning Scryer Prolog, a modern ISO Prolog implementation written in Rust. This repository uses literate programming with Org-mode for an integrated learning and development experience.

Overview

Scryer Prolog is a free software ISO Prolog system that aims to be:

  • An industrial-strength production environment
  • A testbed for bleeding-edge research in logic and constraint programming
  • A modern implementation with advanced features like tabling, constraints, and FFI

This workshop provides:

  • Automated setup and build tools via Makefile
  • Comprehensive examples from basics to advanced topics
  • Literate programming approach with Org-mode
  • Full Emacs configuration for Rust and Prolog development with LSP

System Requirements

Tested on:

  • FreeBSD 14.3-RELEASE amd64
  • Also works on Linux, macOS, and Windows

Required:

  • Emacs (for org-mode tangling)
  • Rust toolchain (cargo)
  • Git

Quick Start

# Check dependencies
make test-deps

# Install dependencies (if needed)
make install-deps

# Option 1: Install via cargo (recommended)
make setup

# Option 2: Build from source in ~/opt
make build-from-source
make update-path

# Tangle org files to generate Prolog code
make tangle

# Start Scryer Prolog REPL
make run

# Or run a specific file
make run-file FILE=hello.pl

Repository Structure

.
├── Makefile                    # Build and setup automation
├── README.org                  # This file
├── setup.org                   # Main workshop content (literate programming)
├── scryer-emacs-config.el     # Emacs configuration for development
│
├── Generated files (from setup.org):
├── hello.pl                    # Hello World and basic examples
├── facts.pl                    # Facts and queries (family relationships)
├── lists.pl                    # List operations
├── recursion.pl                # Recursive predicates
├── scryer_features.pl          # Scryer-specific features (DCGs, format, etc.)
├── constraints.pl              # CLP(Z) constraint examples
├── tabling.pl                  # Tabling for memoization
└── setup.sh                    # Installation scripts

Makefile Targets

Basic Commands

  • make help - Display help message with all available targets
  • make test-deps - Test for required dependencies
  • make install-deps - Install required dependencies (emacs, rust)

Setup Commands

  • make setup - Complete setup (tangle + install scryer-prolog via cargo)
  • make build-from-source - Build Scryer Prolog from source in ~/opt
  • make update-path - Add Scryer Prolog to PATH in shell config

Development Commands

  • make tangle - Tangle all org-mode files in current directory
  • make tangle-file FILE=<file.org> - Tangle a specific org file
  • make run - Run Scryer Prolog REPL
  • make run-file FILE=<file.pl> - Run Scryer Prolog with a file

Cleanup Commands

  • make clean - Remove generated files (.pl, .sh, etc.)
  • make clean-build - Clean build artifacts in ~/opt/scryer-prolog

Workshop Content

The setup.org file contains the complete workshop material, organized as:

1. Introduction and Installation

  • What is Scryer Prolog?
  • System requirements
  • Installation methods (cargo, build from source)

2. Getting Started

  • Hello World example
  • Running your first program
  • Basic Prolog queries

3. Core Exercises

  • Exercise 1: Facts and queries (family relationships)
  • Exercise 2: Lists (member, length, append, reverse, sum)
  • Exercise 3: Recursion (factorial, fibonacci, ranges)
  • Exercise 4: Scryer-specific features (DCGs, format, libraries)

4. Advanced Topics

  • Constraint Logic Programming: CLP(Z) examples and N-Queens problem
  • Tabling: Efficient memoization with tabled predicates
  • Module system and library usage

5. Resources and References

  • Official documentation links
  • Learning resources (The Power of Prolog, Learn Prolog Now!)
  • Community information (GitHub, IRC)

Emacs Configuration

The scryer-emacs-config.el file provides a complete development environment:

Features

  • LSP Support: Full Language Server Protocol integration for Rust and Prolog
  • Rust Development: rust-mode, cargo integration, rust-analyzer
  • Prolog Development: prolog-mode configured for Scryer, sweeprolog integration
  • Literate Programming: Org-mode with babel for tangling
  • Productivity Tools: Company (auto-completion), Flycheck (syntax checking)
  • Project Management: Projectile, Magit (Git integration)
  • UI Improvements: which-key, helpful, rainbow-delimiters

Installation

Add to your ~/.emacs or ~/.emacs.d/init.el:

(load-file "/path/to/scryer-prolog-workshop/scryer-emacs-config.el")

Key Bindings

  • C-c w t - Tangle current org file and prepare to run
  • C-c w s - Check if required tools are installed
  • C-c w p - Run Prolog REPL
  • C-c l - LSP command prefix
  • C-x g - Magit status

Literate Programming Workflow

This workshop uses literate programming with Org-mode:

  1. Edit: Modify setup.org or create new .org files
  2. Tangle: Run make tangle to extract code blocks into .pl files
  3. Test: Run programs with make run-file FILE=hello.pl
  4. Iterate: Refine your code and documentation together

Org-Mode Tips

  • Use C-c C-v t in Emacs to tangle the current file
  • Use :tangle filename.pl to specify output file
  • Use :tangle no to prevent tangling a code block
  • Code blocks support syntax highlighting and editing in native mode

Installation Methods

Method 1: Cargo Install (Recommended)

cargo install --locked scryer-prolog

Method 2: Build from Source

# Clone and build in ~/opt
make build-from-source

# Add to PATH
make update-path

# Or manually:
export PATH="$HOME/opt/scryer-prolog/target/release:$PATH"

Method 3: Manual Build

git clone https://github.com/mthom/scryer-prolog.git
cd scryer-prolog
cargo build --release
# Binary: target/release/scryer-prolog

Example Usage

Interactive Mode

$ scryer-prolog hello.pl
?- say_hello.
Hello, World!
Welcome to Scryer Prolog!
Prolog is awesome!

?- greet('Alice').
Hello, Alice!

?- greeting(X).
X = "Hello, World!" ;
X = "Welcome to Scryer Prolog!" ;
X = "Prolog is awesome!".

Non-Interactive Mode

$ scryer-prolog -g main hello.pl
=== Scryer Prolog Workshop ===
Hello, World!
Available greetings:
  - Hello, World!
  - Welcome to Scryer Prolog!
  - Prolog is awesome!

Learning Resources

Official Documentation

Prolog Learning

Community

Advanced Features

Constraint Logic Programming (CLP)

Scryer includes built-in CLP(B) and CLP(Z) libraries:

:- use_module(library(clpz)).

?- X #> 0, X #< 10, X #= Y + 3, Y #= 5.
   X = 8, Y = 5.

Tabling for Performance

Automatic memoization for recursive predicates:

:- use_module(library(tabling)).
:- table fib/2.

fib(0, 0).
fib(1, 1).
fib(N, F) :- N > 1, N1 is N - 1, N2 is N - 2,
             fib(N1, F1), fib(N2, F2), F is F1 + F2.

Definite Clause Grammars (DCGs)

Built-in support for parsing:

sentence --> noun_phrase, verb_phrase.
noun_phrase --> determiner, noun.
verb_phrase --> verb, noun_phrase.

?- phrase(sentence, [the, cat, chases, a, dog]).
   true.

Troubleshooting

Emacs not found

# FreeBSD
sudo pkg install emacs

# macOS
brew install emacs

# Linux
sudo apt-get install emacs  # Debian/Ubuntu
sudo dnf install emacs      # Fedora

Rust/Cargo not found

# Install via rustup (recommended)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

# Or via package manager (may be outdated)
sudo pkg install rust  # FreeBSD
brew install rust      # macOS

Scryer Prolog build fails

# Update Rust to latest stable
rustup update stable

# Clean and rebuild
cd ~/opt/scryer-prolog
cargo clean
cargo build --release

Tangling fails

  • Ensure Emacs is installed: which emacs
  • Check org-mode is available: emacs --batch --eval "(require 'org)"
  • Verify .org file syntax is correct

Contributing

This is a workshop repository for learning. Feel free to:

  • Add your own exercises to setup.org
  • Create new .org files for specific topics
  • Share improvements to the Makefile or Emacs configuration
  • Submit issues or pull requests

License

This workshop material is provided as-is for educational purposes.

Scryer Prolog itself is licensed under BSD-3-Clause. See: https://github.com/mthom/scryer-prolog/blob/master/LICENSE

Acknowledgments

  • Scryer Prolog: Mark Thom and contributors
  • The Power of Prolog: Markus Triska for excellent learning resources
  • Emacs: For enabling literate programming with Org-mode
  • Rust: For providing a solid foundation for Scryer Prolog

Built with: Org-mode • Scryer Prolog • Rust • Emacs • FreeBSD

Happy Prologging! 🚀

About

A comprehensive workshop for learning Scryer Prolog with literate programming using Org-mode. Includes automated setup, examples, and LSP support for Emacs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published