Appendices

Syntax cheat sheet, essential built-in predicates by category, operator quick reference, glossary, learning resources and suggested study plan (12 weeks).

Appendices

48.1 Syntax cheat sheet 48.2 Essential built-in predicates by category

true, fail, false, !, ,, ;, ->, *->, \+, call/N, once/1, ignore/1, forall/2, repeat, halt/0,1 Unification & comparison

=, \=, ==, \==, @< ..., compare/3, unify_with_occurs_check/2, dif/2, ?=/2, =@=

var, nonvar, atom, number, integer, float, atomic, compound, callable, is_list, ground, string

functor/3, arg/3, =../2, copy_term/2, term_variables/2, setarg/3, numbervars/3, term_to_atom/2

is/2, =:=, =\=, <, >, =<, >=, succ/2, plus/3, between/3 Atoms & strings

atom_codes, atom_chars, char_code, atom_length, atom_concat, sub_atom, atom_number, number_codes, atomic_list_concat, split_string, string_concat, sub_string, upcase_atom

findall/3,4, bagof/3, setof/3, aggregate_all/3, forall/2

assert/1, asserta/1, assertz/1, retract/1, retractall/1, abolish/1, clause/2

write, writeq, print, write_canonical, write_term, nl, format/1,2,3, read/1, read_term/2, open/3,4, close/1, get_char/1, put_char/1, current_output/1

catch/3, throw/1, setup_call_cleanup/3, must_be/2 sort/2, sort/4, msort/2, keysort/2, predsort/3

length/2, member/2, memberchk/2, append/3, reverse/2, nth0/3, nth1/3, last/2, sum_list/2, max_list/2, exclude/3, include/3, maplist/2..7, foldl/4..6

consult/1, make/0, listing/1, trace/0, spy/1, time/1, statistics/2, op/3, current_op/3, set_prolog_flag/2, shell/1 48.3 Operator quick reference "if" (rule neck) / directive unify / not unifiable identical / not identical arithmetic equal / not equal univ (term ↔ list) standard order comparison CLP(FD) constraints existential quantifier in bagof/setof; power in arithmetic integer division; DCG non-terminal indicator (name//arity) number of arguments undoing choices to explore alternatives Choice point saved state for alternative clauses !, prunes choice points Definite Clause Grammar Ground term contains no variables Horn clause logical clause with at most one positive literal binding a variable to a term last call optimization intended instantiation of arguments (+, -, ?) relation defined by clauses (name/arity) inference rule used by Prolog (SLD) memoization of goals (SLG resolution) making two terms identical Warren Abstract Machine 48.5 Learning resources

*The Art of Prolog* (2nd ed.) — Leon Sterling & Ehud Shapiro (free, MIT Press open access)

*Programming in Prolog* — Clocksin & Mellish *Prolog Programming for Artificial Intelligence* — Ivan Bratko

*Learn Prolog Now!* — Blackburn, Bos, Striegnitz (free online) *The Craft of Prolog* — Richard O'Keefe *Clause and Effect* — William Clocksin

*Simply Logical* — Peter Flach (free online) *The Power of Prolog* — Markus Triska (free online book + YouTube videos)

*Logic Programming with Prolog* — Max Bramer *Warren's Abstract Machine: A Tutorial Reconstruction* — Hassan Aït-Kaci (free)

Online: swi-prolog.org manual & tutorials, SWISH, Scryer Prolog docs, "99 Prolog Problems" (P-99), Exercism Prolog track, Advent of Code in Prolog

Community: SWI-Prolog Discourse (swi-prolog.discourse.group), r/prolog, Stack Overflow [prolog] tag, Association for Logic Programming (ALP), ICLP conference 48.6 Suggested study plan (12 weeks)

Install, top level, facts/rules/queries, terms Unification, execution model, arithmetic, comparison

Recursion & lists — Part I exercises, Project 1 Control, cut, operators, term inspection, all-solutions Dynamic database, text, DCGs, I/O

Exceptions, higher-order, difference lists, libraries, modules, testing — Part II exercises, Project 3 Purity, CLP(FD) — Project 2 CLP(B/Q/R), CHR, coroutining, tabling

Meta-interpreters, term expansion, search — Project 4 or 7 AI applications, interpreters & compilers — Project 5

Performance, WAM, concurrency, related paradigms Web, databases, FFI, packs, portability, style — Project 6 or 8 🎓 Final Words

Prolog changes how you think about programming: describe the problem, and let unification and search find the answers. You've gone from facts and rules to DCGs, CLP(FD), tabling, meta-interpreters, and the WAM. Keep your code pure where you can, test with the most general queries, read *The Art of Prolog* and *The Power of Prolog*, and build the capstone projects. Happy reasoning! 🦉

% Facts, rules, queries
likes(mary, wine).
happy(X) :- likes(X, wine), \+ sad(X).
?- happy(Who).

% Terms
atom   'Quoted Atom'   42   3.14   "string"   X   _   f(a, B)   [H|T]   [1, 2, 3]

% Control
( Cond -> Then ; Else )     \+ Goal     !     call(G)     once(G)     forall(C, A)
catch(G, E, Handler)        throw(E)    setup_call_cleanup(S, G, C)

% Arithmetic & comparison
X is 2 + 3 * 4      X =:= Y   X =\= Y   X < Y   X =< Y   X >= Y
X = Y   X \= Y   X == Y   X \== Y   X @< Y   dif(X, Y)

% Lists
member(X, L)  append(A, B, C)  length(L, N)  nth0(I, L, E)  reverse(L, R)  msort(L, S)
maplist(G, L1, L2)  foldl(G, L, A0, A)  include(P, L, R)  findall(X, G, L)  setof(X, G, S)

% DCG
greeting --> [hello], name.      phrase(greeting, Words).

% CLP(FD)
:- use_module(library(clpfd)).
X #= Y + 1,  Vs ins 1..9,  all_distinct(Vs),  label(Vs).

% Database & modules
:- dynamic counter/1.  assertz(F).  retract(F).
:- module(m, [p/1]).   :- use_module(library(lists)).

% Tabling & coroutining
:- table path/2.   freeze(X, G).   when(ground(X), G).