SWI-Prolog (swipl-devel)
repository·master·Indexed 22 days ago
https://github.com/swi-prolog/swipl-develAn open-source (BSD-2) implementation of the Prolog language with extensive extensions. It supports applications in NLP, robotics, machine learning, and web development via WASM. The documentation covers CLI application creation using library(main), environment personalization via init.pl, XSB dialect support, and manual generation using LaTeX and PlDoc.
What's inside SWI-Prolog
- CLP(B) is a library for Constraint Logic Programming over Boolean variables. It is designed to model and solve combinatorial problems such as verification, allocation, and covering tasks. The implementation uses reduced and ordered Binary Decision Diagrams (BDDs) to perform reasoning over the Boolean domain.
MacOS build and bundle helpers
masterSpecific scripts are provided for MacOS-related build tasks:
macos-deps.sh: Builds MacOS dependencies required for releases.macosx_bundle_fixup.sh: A helper used byCPackto finalize dependencies within a MacOS bundle.
Use the SICStus compatibility library
masterThelibrary(sicstus)provides a compatibility layer for users migrating from or working with SICStus Prolog. It allows you to run code written for SICStus within the SWI-Prolog environment by providing equivalent predicates and behaviors.Explore SWI-Prolog GUI and Editor tools
masterFor an enhanced development experience, you can use several GUI and editor-integrated tools:
- XPCE: SWI-Prolog's native GUI toolkit. Comprehensive builds include the
swipl-winexecutable, which opens a Prolog terminal. XPCE also provides PceEmacs, an Emacs clone featuring a rich Prolog mode, a source-level debugger, and profiling tools. XPCE is included in default binaries for Windows, MacOS, and as a flatpak for Linux. - GNU-Emacs: Supports rich mode for (SWI-)Prolog via the sweep package.
- Online Editors: Use SWISH or SWI-Tinker for quick testing without local setup.
- XPCE: SWI-Prolog's native GUI toolkit. Comprehensive builds include the
Release management scripts overview
masterThe following scripts are used for managing SWI-Prolog releases:
newversion: Updates version files and GIT tags.mkchangelog [--no-date] [version]: Generates the changelog.make-distribution: Must be sourced. Defines functions to build and upload a release. This script may require manual editing when used on external build machines.make-src-tape: Creates a.tar.gzfile from the GIT repo and its submodules. Typically invoked viamake-distribution.
Perform search using enumeration predicates
masterAfter defining constraints and domains, you typically need to perform an actual search for solutions. This is done using enumeration predicates. The most common way to trigger the search is vialabeling/2orlabel/1.Use CLP(FD) for declarative integer arithmetic
masterThe CLP(FD) library allows you to use constraints instead of low-level arithmetic predicates like
is/2or=:=/2. This provides greater flexibility, allowing you to reorder goals and improve termination properties. For example, using#=/2allows a predicate to work even when some arguments are not yet instantiated, which is not possible with standard arithmetic.n_factorial(0, 1). n_factorial(N, F) :- N #> 0, N1 #= N - 1, F #= N * F1, n_factorial(N1, F1).Syntax for Boolean expressions in CLP(B)
masterIn CLP(B), you can construct Boolean expressions using the following syntax:
Operator Meaning 0false 1true _variable_unknown truth value _atom_universally quantified variable ~ Exprlogical NOT Expr + Exprlogical OR Expr * Exprlogical AND Expr # Exprexclusive OR (XOR) Var ^ Exprexistential quantification Expr =:= Exprequality Expr \= Exprdisequality (same as #)Expr =< Exprimplication (less or equal) Expr >= Exprgreater or equal Expr < Exprless than Expr > Exprgreater than card(Is, Exprs)cardinality constraint: true if the number of true expressions in Exprsis a member of the listIs(which can contain integers or ranges likeFrom-To)+(Exprs)n-fold disjunction (OR of all elements) *(Exprs)n-fold conjunction (AND of all elements) Atoms represent parametric values that are universally quantified. In residual goals, these typically appear on the right-hand side of equations to express functional dependencies on input variables.
What are association lists in library(assoc)?
masterAn association list is a collection of unique keys associated with values.
Key constraints and properties:
- Keys must be ground (fully instantiated).
- Values do not need to be ground.
- Ordering: Elements can be enumerated in ascending order of their keys.
- Implementation: The library uses AVL trees, ensuring that inserting a key, changing an association, or fetching a single element are all $O(\log(N))$ worst-case and expected time operations, where $N$ is the number of elements.
Advantages:
- Portability: Written entirely in Prolog.
- Declarative: Predicates avoid destructive updates to terms, fitting Prolog's nature.
- Efficiency: Scales predictably and can represent sparse arrays efficiently.
How CLP(FD) constraints differ from low-level arithmetic
masterThe primary advantage of CLP(FD) constraints is their relational nature. Low-level predicates like
is/2or=:=/2require all arguments to be sufficiently instantiated, whereas CLP(FD) constraints can be used to solve for unknown variables in any direction.Example: Solving for a variable
Using CLP(FD):
?- 3 #= Y+2. Y = 1.Using low-level arithmetic (fails):
?- 3 is Y+2. ERROR: is/2: Arguments are not sufficiently instantiated ?- 3 =:= Y+2. ERROR: =:=/2: Arguments are not sufficiently instantiated?- 3 #= Y+2. Y = 1.Requirements for manual source files
masterThe SWI-Prolog manual is built from
*.docfiles which are converted to LaTeX via thedoc2texprogram.Writing Predicate Names
- Underscores: The style file
pl.styallows underscores to be used without special precautions outside math mode. Do NOT writeexpand\_file/2; simply writeexpand_file/2. - Special Characters: If a predicate name contains TeX-special characters,
doc2texuses a URL quoting mechanism (sequences named\S<name>).
Summary Requirements
Every described predicate MUST have an entry in the
summary.docfile. Note that the content ofsummary.docis ordered alphabetically.- Underscores: The style file
Understand the difference between Trace Mode and Trace Points
masterSWI-Prolog provides two distinct tracing features:
Trace Mode
- Purpose: The main command line debugger for stepping through resolution states (ports) in the "Byrd Box Model".
- Behavior: Can pause execution to allow inspection.
- Activation: Via
trace/0,spy/1(in debug mode), orset_breakpoint/4(in debug mode). - Control: Use
visible/1to control console output andleash/1to control which ports cause pauses. - Termination:
notrace/0stops tracing;nodebug/0stops all debugging.
Trace Points
- Purpose: A lightweight feature to write specific ports to the console when a predicate is evaluated.
- Behavior: Never pauses execution. It does not require debug mode to be active.
- Activation: Via
trace/1ortrace/2. - Control: Does not respect
visible/1orleash/1; ports are explicitly defined viatrace/2.