ZAK's Declarative Lab (Prolog)
Type a knowledge base of facts and rules on the left, ask a query on the right — parent(X, ali). — and get every answer exactly as SWI-Prolog prints it, down to the ;, the full stop and the false. The knowledge base is checked as you type, the way an examiner reads it: syntax slips, singleton variables, a misspelt predicate or atom, a capital letter where a name was meant, = where is was meant, a variable used in arithmetic before it has a value, left recursion, negation before its variable is bound. Open Resolution to watch the search and backtracking. Theory in Further Programming.
Example library
33 knowledge bases with sample queries — facts, rules, recursion, lists, arithmetic, negation, cut and exam-style scenarios. Press Examples to search them.
Facts & queries 3
A knowledge base of facts; queries with constants and variables; how Prolog answers
Rules 4
head :- body, conjunction, several clauses for one predicate, backtracking through alternatives
Recursive rules 3
ancestor, path-finding, counting — base case + recursive case
Lists 7
[H|T] patterns, member, append, length, reverse, findall
Arithmetic 4
is, comparison operators, factorial, sums — and the difference between = and is
Negation & cut 8
\+ (not), the cut !, if-then-else — and the mistakes they cause
Exam-style scenarios 4
Knowledge bases in the style of 9618 Paper 3 §20.1 questions, numbered like the paper
Prolog as the exam writes it
- Facts
parent(sonia, ali).— predicate name and atoms in lower case, a full stop at the end. Rulesmother(M, C) :- parent(M, C), female(M).—:-reads “if”, the comma reads “and”. - Variables start with a capital letter (or _). A query with variables returns every binding that makes it true; a query without variables returns
trueorfalse. - Exam line numbers (
01 stock(pen, 25).) are accepted and ignored, so you can paste a knowledge base straight from a past paper. - Arithmetic uses
is:X is Y + 1.=only unifies. Comparisons are< > =< >= =:= =\=. - Lists
[H|T], with member, append, length, reverse, msort, findall, bagof/setof, aggregate_all, sum_list, maplist … built in. Negation\+ goal, cut!, if-then-else( C -> T ; E ), catch/throw, assert/retract (kept between queries), format/2, strings,op/3, DCG rules — the SWI-Prolog dialect, so what you learn here works at home in SWI-Prolog. - Answers as SWI-Prolog prints them:
X = ali ;thenX = ayesha.— a full stop when no choice point is left,false.when the last one fails,X = Y, Y = 1when two variables share a value. The Lab reproduces SWI’s clause indexing, so even that detail matches. - Checked as you type (spelling and logic): missing full stops or commas,
<=and!=, capitalised predicate names, unbalanced brackets, singleton variables with “did you mean”, a predicate or atom one typo away from one used elsewhere,"strings"where atoms were meant,=with arithmetic, an expression passed as an argument, a variable used in arithmetic before any goal binds it,N is N + 1, left recursion, a call identical to its head, no base case,\+before its variable has a value, duplicate or scattered clauses, calls to predicates with no clauses. - At run time: SWI-Prolog’s own error messages — unknown procedure (with “However, there are definitions for …” and a spelling suggestion), “arguments are not sufficiently instantiated”, type errors, division by zero — each with a hint, plus infinite recursion and runaway loops stopped with an explanation.