Introduction
A Prolog source file is plain text that holds a logic program: facts and rules about a subject, and directives that tell the system how to load them. Nothing in the file says what to compute. The program is a database of clauses, and a query asks the system to find values that make a goal true; the system searches the clauses, and backtracks when a choice fails. The language is standardised as ISO/IEC 13211-1 and is used by many systems that differ in details: SWI-Prolog, GNU Prolog, SICStus Prolog, YAP, XSB, ECLiPSe, Scryer Prolog, Trealla Prolog, Tau Prolog and Ciao.
Every clause ends with a full stop that is followed by white space or a comment. A fact states something that is true; a rule (:-, read “if”) states that its head is true when its body is; a rule with --> is a grammar rule; a directive (:- at the start) is run while the file loads.
% A family database and a rule over it.
:- module(family, [grandparent/2]).
:- dynamic parent/2.
parent(tom, bob).
parent(bob, ann).
grandparent(X, Z) :-
parent(X, Y),
parent(Y, Z).
greeting --> [hello], name.
name --> [world].
The kinds of file
| Extension | What it is | Page |
|---|---|---|
.pl | Prolog source in SWI-Prolog, GNU Prolog, YAP, XSB and others; the extension is also Perl's, so this site tells them apart from the text | File types |
.pro | Prolog source in some systems (Quintus, SICStus, Arity); also Qt project files, IDL programs and Android ProGuard rules | File types |
.P | Prolog source in XSB and Quintus; the same extension in lower case is used by other languages | File types |
.prolog | Prolog source, named by its language | File types |
.ecl | ECLiPSe source | Dialects and versions |
.yap | YAP source and YAP's start-up files | Dialects and versions |
.plt, .pldoc, .swipl | SWI-Prolog unit tests, documentation and start-up files: Prolog text under other names | File types |
.qlf, .po | compiled Prolog: binary files of one system | File types |
The language names prolog, swipl, swi-prolog, gprolog, yap, ecl and eclipse colour a Markdown code block.
How the viewer reads each file
A Prolog file is shown as coloured text, with an Outline and a Problems tab. The scanner follows the tokenizer of ISO/IEC 13211-1 with the extensions that every current system shares. It marks comments (% and /* */), quoted atoms, strings and back-quoted text, numbers (including 0'c, radix, float and digit-group forms), variables, the head of each clause, built-in predicates and control words, and the directive marks :-, ?- and -->. Then it reads the head of every clause and every directive as a term, using the operator table with the operators the file declares itself.
- Outline – modules and their exports, loaded files, operators declared, declarations (
dynamic,discontiguous,multifile,table), every predicate with its arity and clause count, DCG rules (asname//arity), directives, initialization goals and queries, each with its line - Problems – errors: a quoted item or a comment that is not closed, a missing end full stop at the end of the file, brackets that do not match, an escape that does not exist, a character that is not valid in Prolog text, a clause head that is a variable or a number; and notes: singleton variables, clauses of a predicate that are not together, a quoted item that runs over a line break, a head or directive that does not read as a term (usually an operator that another system declares)
The flag :- set_prolog_flag(double_quotes, atom) is honoured: a double-quoted text after it is coloured as an atom instead of a string. The Overview shows the flag value the file sets.
| File | Shown as |
|---|---|
.pl that passes the Prolog test | Prolog source file, coloured, with the outline and the problems |
.pl with a mark of Perl, or with no clear mark of Prolog | Perl source file, as before |
.pro, .p that pass the same test | Prolog source file |
other .pro, .p | a neutral text file, with a link to the file types page |
.prolog, .ecl, .yap | Prolog source, ECLiPSe source, YAP source |
The test for the shared extensions needs one sure sign. For Prolog that is a directive such as :- module( or :- use_module(, a grammar rule with -->, a rule whose head the reader understands, or a file in which every clause reads as a term; for Perl it is use strict;, my $x, sub name {, a #! line, POD, @ARGV and the like. One mark of Perl keeps a file Perl. The lists are on File types.
The pages
- Lexical structure – layout, comments, atoms, variables, numbers, strings, quoted forms, escapes and the end token
- Terms and operators – the operator table,
op/3, canonical and operator syntax, lists, curly terms, strings and dicts - Clauses and directives – facts, rules, grammar rules, directives, queries,
initialization, declarations and conditional compilation - Built-in predicates – control, type checking, term inspection, arithmetic, atoms and strings, lists, the database, solutions, exceptions and input and output
- Modules and libraries –
module/2,use_module, import and export, and the libraries of the main systems - Dialects and versions – the ISO standard and its corrigenda, and how ten systems differ
- File types – every extension, Prolog against Perl, compiled files, tests, documentation and start-up files
- Tooling – the Prolog systems, editors, test and documentation tools, and checkers
Not described
Known, not yet covered
- Viewer: the bodies of clauses are tokens, not terms: the scanner reads the head of each clause and each directive, so it does not check the syntax of a body, does not follow loaded files, and does not know the operators that a loaded library or another module declares (a head that uses one is reported as a note). Only the operators of a short list of SWI-Prolog libraries (clpfd, chr, clpb, bounds, record, tables, xpath, rdf11 and a few more) are added when the file loads them with
use_module/1. - Viewer: the dialect is not decided: SWI-Prolog strings, dicts and quasi quotations are always read, and the
double_quotesflag is followed only when the file sets it by a directive. The flag's default for the system that runs the file is not known. - Viewer: conditional compilation (
:- if(...)) is not evaluated, so every branch is read as code. - Viewer: files with the extension
.qlfand.po(compiled Prolog) are not read;.plt,.pldocand.swiplhave no registered extension here. - Reference: the pages describe ISO/IEC 13211-1 and the commonest behaviour of the main systems; the corrigenda are covered only by name and by what their drafts say, and the rarer built-in predicates of each system (and the Ciao, Tau and ECLiPSe libraries) are not listed.
- Reference: SWI-Prolog 11.0.0 is installed on the machine that wrote these pages and was used as a reader oracle (
read_term/3only, never to run a program): operator priorities, thedouble_quotesdefault, comments, numbers, characters and clause boundaries were checked against it. No other system (GNU Prolog, SICStus, Scryer, YAP, Trealla) was run, so statements about them come from their manuals. Ulrich Neumerkel's ISO conformity table was read but its cases are not copied (the page states no licence).
Unknown
- Viewer: the binary layout of
.qlfand.pofiles is not documented here; they are shown as bytes.
Also used for
- ECLiPSe source – Source of the ECLiPSe constraint programming system, a Prolog dialect.
- Perl source code – Perl scripts and modules as text; .pl is also used for Prolog.
- YAP Prolog source – Source and start-up files of the YAP Prolog system.
Links
Software
- SWI-Prolog – the most used free Prolog system, with a large library (Windows, macOS, Linux)
- GNU Prolog – a compiler to native code with constraint solving over finite domains (Windows, macOS, Linux)
- Scryer Prolog – a system written in Rust that follows the ISO standard closely (Windows, macOS, Linux)
- Trealla Prolog – a small ISO-compliant interpreter written in C (Windows, macOS, Linux, WebAssembly)
- SICStus Prolog – a commercial system with a large set of libraries (Windows, macOS, Linux)
- YAP – a fast Prolog system (Windows, macOS, Linux)
- XSB – a Prolog system with tabling and deductive-database features (Windows, macOS, Linux)
- ECLiPSe – a Prolog system for constraint programming (Windows, macOS, Linux)
- Tau Prolog – a Prolog interpreter in JavaScript that runs in a browser
- Ciao – a multi-paradigm language that includes a Prolog mode with modules and assertions
Developers
- swipl-devel – the source of SWI-Prolog, with its library and its boot files written in Prolog (C and Prolog)
Reference
- SWI-Prolog manual – the reference manual, including the syntax notes and the flags
- ISO Prolog conformity testing and drafts – Ulrich Neumerkel's pages on the standard, its corrigenda and how systems conform