100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Your First Prolog Program

A hands-on walkthrough of writing a family.pl file, loading it into SWI-Prolog, and querying it interactively with backtracking.

FoundationsBeginner8 min readJul 10, 2026
Analogies

Your First Prolog Program

Writing your first real Prolog program means saving facts and rules into a .pl file, loading that file into the SWI-Prolog top-level, and then querying it interactively. Unlike scripting languages where you simply run a file to see printed output, a Prolog program is a passive knowledge base — nothing happens until you issue a query at the ?- prompt that asks the engine to search it for an answer.

🏏

Cricket analogy: A .pl file is like a team's compiled statistics dossier before a match — it doesn't 'do' anything on its own, but once the analyst poses a question at the press conference (a query), the dossier is searched for the answer, just as loading family.pl only becomes useful once you type ?- sibling(ann, pat).

Writing and Loading a File

Create a text file named family.pl containing your facts and rules, then load it from the SWI-Prolog top-level with ?- [family]. (square brackets are shorthand for consult/1) or ?- consult('family.pl').; SWI-Prolog will report each predicate it loaded, and any syntax errors — like a missing period — will be reported with the offending line number so you can fix them before querying. After editing the file, simply reload it the same way to pick up the changes.

🏏

Cricket analogy: Loading family.pl with ?- [family]. is like a team submitting its final playing XI to the match referee before the toss — once submitted (loaded), the umpires (the engine) can reference it for every subsequent decision (query) during the match.

Querying and Reading Results

At the ?- prompt, typing a query like ?- parent(tom, Who). returns the first matching solution, Who = liz, and pressing ; (semicolon) asks Prolog to backtrack and find the next solution, Who = bob, continuing until Prolog reports false. to mean no more solutions exist; typing a period (or simply pressing Enter after the first result) instead accepts that single answer and returns you to the prompt without searching further.

🏏

Cricket analogy: Querying ?- parent(tom, Who). and pressing ; to see more results is like asking a stats database 'who did Rohit Sharma get out?' and tapping 'next' repeatedly to cycle through every dismissal, until the database reports no more results remain.

prolog
% file: family.pl
parent(tom, liz).
parent(tom, bob).
parent(bob, ann).
parent(bob, pat).

sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.

% --- interactive session ---
?- [family].
true.

?- parent(tom, Who).
Who = liz ;
Who = bob.

?- sibling(ann, Who).
Who = pat.

?- halt.

SWI-Prolog's built-in editor integration lets you run ?- edit(family). to open family.pl directly in a text editor from within the REPL, and after saving, ?- make. reloads any files that changed on disk — a fast edit-reload-query loop that avoids retyping the consult command every time.

Forgetting the trailing period at the end of a clause or query is the single most common beginner error — Prolog treats a missing period as 'the statement isn't finished yet' and will simply wait for more input (shown as a blank continuation line) rather than giving an obvious error, which can be confusing the first time it happens.

  • A Prolog program is a passive knowledge base; nothing executes until a query is issued.
  • Load a file with ?- [filename]. or ?- consult('filename.pl').
  • SWI-Prolog reports syntax errors with the offending line number during loading.
  • Typing ; after a result asks Prolog to backtrack and find the next solution.
  • A query ends when Prolog reports false. meaning no further solutions exist.
  • ?- edit(file). opens a file for editing, and ?- make. reloads changed files.
  • Every clause and query must end with a period, or Prolog will wait for more input.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PrologStudyNotes#YourFirstPrologProgram#Prolog#Program#Writing#Loading#StudyNotes#SkillVeris#ExamPrep