Prolog is a logic programming language: instead of writing step-by-step instructions, you state facts and rules, then ask queries, and the engine searches for values that make them true. Its two core mechanisms, unification and backtracking, take some getting used to, but they make Prolog remarkably compact for puzzles, parsing, expert systems and type checkers. Understanding how the search actually runs is what separates confusion from fluency. Learnr builds a Prolog course of short lessons and query-tracing exercises around the problems you want to solve.
How to learn Prolog, step by step
- Load a family tree into SWI-Prolog. Write parent/2 facts for a small family, then define grandparent/2 and sibling/2 rules. Query them with variables and press semicolon to see every answer the engine finds.
- Learn unification by hand. Before relying on the engine, work out on paper whether terms like point(X, 3) and point(2, Y) unify and what bindings result. Remember that uppercase names are variables and lowercase names are atoms.
- Trace the search. Run trace before a query and step through call, exit, redo and fail ports. Seeing exactly when Prolog backtracks explains infinite loops and duplicate answers better than any description.
- Write list predicates from scratch. Implement length, append, member and reverse using the [H|T] pattern with a base case first. Then try running append backwards to split a list, which shows how Prolog relations work in multiple directions.
- Handle arithmetic and control. Learn that X = 1 + 2 builds a term while X is 1 + 2 evaluates it. Use the cut only to commit to a clause once you understand the search, and prefer CLP(FD) for integer constraints.
- Solve puzzles to build fluency. Tackle the zebra puzzle, a sudoku solver with CLP(FD) and a small grammar with DCGs. Learnr lessons keep core ideas like unification and backtracking in practice between these bigger problems.
What you'll learn
- Write facts and rules and query them in SWI-Prolog
- Predict how unification binds variables in a query
- Trace backtracking through a search tree by hand
- Write recursive predicates over lists using the head and tail pattern
- Use the cut, negation as failure and CLP(FD) constraints appropriately
How long does it take to learn Prolog?
Basic queries, rules and list recursion usually take 3 to 6 weeks at a few hours a week. Thinking declaratively, so you can write correct and efficient programs without fighting the search, takes a few months, whatever other languages you know.
What you need before you start
Nothing beyond basic logic is required. Programmers from imperative languages may need to unlearn loops and assignment, while some exposure to recursion or formal logic helps.
Common mistakes to avoid
- Expecting = to do arithmetic. It only unifies terms, so use is for evaluation and =:= for numeric comparison.
- Putting a recursive clause before its base case or writing left recursion, which sends queries into infinite loops. Order clauses deliberately.
- Sprinkling cuts everywhere to remove unwanted answers, which breaks the logical reading. Fix the rules first, and use the cut only where its effect is clear.
How Learnr builds your course
Type your topic
Tell Learnr you want to learn Prolog, or get as specific as you like.
Get a personalized course
Learnr instantly builds a full Prolog course tailored to your goal and skill level.
Learn and level up
Move through bite-sized lessons and interactive practice, earning XP and keeping your streak.
Prolog questions, answered
What is Prolog used for today?
It appears in natural language processing research, rule engines, scheduling and configuration, program analysis and teaching logic and AI. Parts of IBM Watson's question answering pipeline used Prolog for pattern matching over parsed text.
Which Prolog should I install?
SWI-Prolog is free, actively maintained and widely used in courses, with good libraries and documentation. The free online book Learn Prolog Now! works well with it.
Explore more courses
- Learn Common Lisp
- Learn Artificial Intelligence
- Learn Haskell
- Learn Functional Programming
- See all courses
More programming and tech courses
- Learn SwiftUI
- Learn Jetpack Compose
- Learn NestJS
- Learn Nuxt
- Learn jQuery
- Learn Sass
- Learn Electron.js
- Learn Firebase
See all programming and tech courses
Ready to learn Prolog?
Start your free, personalized Prolog course in seconds. It will be typed in and ready when you get there.
Start learning PrologFree to start. No credit card.