SWI-Prolog (swipl-devel)

repository·master·Indexed 22 days ago

https://github.com/swi-prolog/swipl-devel

An 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.

Tokens
21.7K
Snippets
61
Records
141
Agent score
78%

What's inside SWI-Prolog

  1. Introduction to CLP(B)

    master
    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.
  2. Explore SWI-Prolog GUI and Editor tools

    master

    For 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-win executable, 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.
  3. Release management scripts overview

    master

    The 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.gz file from the GIT repo and its submodules. Typically invoked via make-distribution.
  4. Use CLP(FD) for declarative integer arithmetic

    master

    The CLP(FD) library allows you to use constraints instead of low-level arithmetic predicates like is/2 or =:=/2. This provides greater flexibility, allowing you to reorder goals and improve termination properties. For example, using #=/2 allows 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).
  5. Syntax for Boolean expressions in CLP(B)

    master

    In CLP(B), you can construct Boolean expressions using the following syntax:

    OperatorMeaning
    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 Exprs is a member of the list Is (which can contain integers or ranges like From-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.

  6. What are association lists in library(assoc)?

    master

    An 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.
  7. How CLP(FD) constraints differ from low-level arithmetic

    master

    The primary advantage of CLP(FD) constraints is their relational nature. Low-level predicates like is/2 or =:=/2 require 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.
  8. Requirements for manual source files

    master

    The SWI-Prolog manual is built from *.doc files which are converted to LaTeX via the doc2tex program.

    Writing Predicate Names

    • Underscores: The style file pl.sty allows underscores to be used without special precautions outside math mode. Do NOT write expand\_file/2; simply write expand_file/2.
    • Special Characters: If a predicate name contains TeX-special characters, doc2tex uses a URL quoting mechanism (sequences named \S<name>).

    Summary Requirements

    Every described predicate MUST have an entry in the summary.doc file. Note that the content of summary.doc is ordered alphabetically.

  9. Understand the difference between Trace Mode and Trace Points

    master

    SWI-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), or set_breakpoint/4 (in debug mode).
    • Control: Use visible/1 to control console output and leash/1 to control which ports cause pauses.
    • Termination: notrace/0 stops tracing; nodebug/0 stops 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/1 or trace/2.
    • Control: Does not respect visible/1 or leash/1; ports are explicitly defined via trace/2.