Nexus Learn

Lecture 9: Logic Programming with Prolog

Lecture Slides CS-511 Artificial Intelligence 6 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Recap of Lecture 8 and Homework Check

LAST LECTURE · 5 MIN

Today: eligible(X) :- student(X), att75(X). - the rule from L08, now as a running program.

Today's Plan (50 minutes)

ROADMAP

0-5

Recap of Lecture 8 and homework check

S02

5-8

Plan and learning outcomes

S03-S04

8-15

Module 1: What is Prolog? Syntax

S05-S07

15-28

Module 2: Facts, rules, queries; how Prolog answers

S08-S11

28-35

Module 3: Recursion and goal order

S12-S14

35-43

Class activity: a crop knowledge base

S15

43-48

Common mistakes and recap quiz

S16-S17

48-50

Homework and next lecture

S18

Learning Outcomes (SLOs)

BY THE END OF THIS LECTURE YOU WILL BE ABLE TO

SLO 9.1

Explain the parts of a Prolog program and how Prolog answers a query (unification, backtracking)

→ CLO2 · C2 Understand

SLO 9.2

Write facts and rules for a small knowledge base and query it with variables

→ CLO2 · C3 Apply

SLO 9.3

Write a recursive rule and predict the answers and the order Prolog finds them

→ CLO2 · C3 Apply

  • Course Learning Outcome(s) served
  • CLO2: Demonstrate first-order predicate calculus, logical reasoning and problem-solving using Prolog language.

Slide 5

MODULE 1

What Is Prolog?

PROgramming in LOGic - describe the problem, let the computer search for the answer

Prolog: Declarative Programming

MODULE 1 · IDEA

History

Used for

Tool

Colmerauer and Kowalski, about 1972. Japan's Fifth Generation project (1980s) chose Prolog.

Expert systems, language parsing (IBM Watson 2011), scheduling, rule checking.

SWI-Prolog - free for Windows, Linux and Mac (swi-prolog.org).

Prolog Syntax in One Slide

MODULE 1 · SYNTAX

Careful: ali (small a) is a constant; Ali (capital A) is a VARIABLE. Forgetting the full stop is the most common error.

Slide 8

MODULE 2

Facts, Rules and Queries

The L07 university world as a Prolog program

A Knowledge Base and Its Queries

MODULE 2 · PROGRAM

L09/UNIVERSITY.PL

REAL OUTPUT: SWI-PROLOG 9

  • student(ali). student(sana). student(bilal). student(hira).
  • teacher(waqas). teacher(ayesha).
  • attendance(ali, 82). attendance(sana, 91).
  • attendance(bilal, 68). attendance(hira, 77).
  • advises(waqas, ali). advises(waqas, sana).
  • advises(ayesha, bilal). advises(ayesha, hira).
  • % Rule: a student is eligible for the final if attendance is 75% or more
  • eligible(X) :- student(X), attendance(X, A), A >= 75.
  • % Rule: T is the advisor of student S (just a renamed relation)
  • advisor_of(S, T) :- advises(T, S).
  • % Rule: two different students share an advisor
  • classmates(X, Y) :- advises(T, X), advises(T, Y), X \= Y.
  • % Negation as failure: short of attendance = a student who is NOT eligible
  • short_attendance(X) :- student(X), \+ eligible(X).
  • ?- student(ali).
  • true.
  • ?- student(waqas).
  • false.
  • ?- eligible(X).
  • X=ali
  • X=sana
  • X=hira
  • ?- advisor_of(hira, T).
  • T=ayesha
  • ?- classmates(ali, Y).
  • Y=sana
  • ?- short_attendance(X).
  • X=bilal
  • ?- attendance(X, A), A > 80.
  • [X=ali,A=82]
  • [X=sana,A=91]

How Prolog Answers: Search and Backtracking

MODULE 2 · EXECUTION

REAL OUTPUT: L09/TRACE.PL

  • ?- eligible(A),advises(ayesha,A).
  • eligible(A) use rule
  • student(ali) matches a fact
  • attendance(ali,82) matches a fact
  • test 82>=75 yes
  • advises(ayesha,ali) no other clause -> backtrack
  • attendance(ali,A) no other clause -> backtrack
  • student(sana) matches a fact
  • attendance(sana,91) matches a fact
  • test 91>=75 yes
  • advises(ayesha,sana) no other clause -> backtrack
  • attendance(sana,A) no other clause -> backtrack
  • student(bilal) matches a fact
  • attendance(bilal,68) matches a fact
  • test 68>=75 NO -> backtrack
  • attendance(bilal,A) no other clause -> backtrack
  • student(hira) matches a fact
  • attendance(hira,77) matches a fact
  • test 77>=75 yes
  • advises(ayesha,hira) matches a fact
  • Answer: X = hira

Order

Goals left to right. Clauses top to bottom. Go deep first.

Unification

student(A) with student(ali): A = ali.

Backtracking

A goal fails: undo the last choice, try the next clause.

Arithmetic, Comparison and Negation as Failure

MODULE 2 · BUILT-INS

Closed world: Prolog treats anything it cannot prove as false - student(waqas) is false because the KB never says it.

Slide 12

MODULE 3

Recursion

A rule that uses itself - Prolog's way of looping

Recursion: Which Cities Can We Reach?

MODULE 3 · RECURSION

L09/ROUTES.PL

REAL OUTPUT

  • road(burewala, vehari). road(burewala, arifwala).
  • road(vehari, mailsi). road(vehari, multan).
  • road(arifwala, sahiwal). road(sahiwal, lahore).
  • road(multan, lahore).
  • reachable(X, Y) :- road(X, Y).
  • reachable(X, Y) :- road(X, Z), reachable(Z, Y).
  • route(X, Y, [X, Y]) :- road(X, Y).
  • route(X, Y, [X | Rest]) :- road(X, Z), route(Z, Y, Rest).
  • ?- reachable(burewala, lahore).
  • true.
  • ?- reachable(lahore, burewala).
  • false.
  • ?- route(burewala, lahore, R).
  • R = [burewala,vehari,multan,lahore]
  • R = [burewala,arifwala,sahiwal,lahore]
  • ?- setof(C, reachable(burewala, C), L).
  • L = [arifwala,lahore,mailsi,multan,sahiwal,vehari]

Base case

Recursive case

[H | T]

Stops the recursion: a direct road.

One road to Z, then the same problem from Z - a smaller problem.

A list: first element H, rest T. [burewala | Rest].

Goal Order Matters: Infinite Recursion

MODULE 3 · CAUTION

BAD: RECURSIVE CALL FIRST

Two rules

  • reach_bad(X, Y) :- reach_bad(Z, Y), road(X, Z).
  • reach_bad(X, Y) :- road(X, Y).
  • 1. Base case first.
  • 2. Make progress (take a road) BEFORE the recursive call.
  • Cycles in a map need an explored set - Lecture 10.

REAL OUTPUT

  • ?- reach_bad(burewala, lahore).
  • ERROR: stack limit exceeded (stack) - infinite recursion

GOOD: BASE CASE FIRST, A ROAD STEP BEFORE RECURSION

  • reachable(X, Y) :- road(X, Y).
  • reachable(X, Y) :- road(X, Z), reachable(Z, Y).

Activity: A Crop Knowledge Base

CLASS ACTIVITY · PAIRS · 8 MIN

Pairs, 6 min + 2 min check. KB: crop(wheat). crop(cotton). crop(rice). season(wheat, rabi). season(cotton, kharif). season(rice, kharif). needs_water(wheat, medium). needs_water(cotton, high). needs_water(rice, very_high).

Add the fact: sugarcane is a crop.

Write a rule thirsty(C): water need is high or very_high.

Write the query: which crops are Kharif? What are the answers?

What does ?- season(wheat, kharif). return?

Write a rule kharif_crop(C).

Query: Rabi crops that are NOT thirsty. Answer?

Common Mistakes

WATCH OUT

WRONG IDEA

CORRECT IDEA

✗ student(Ali). is a fact about Ali.

✓ Ali is a VARIABLE - it says everything is a student. Write student(ali).

✗ X = 2 + 3 gives X = 5.

✓ = only matches; X is 2 + 3 calculates 5.

✗ Forgetting the full stop at the end.

✓ Every fact and rule ends with '.'

✗ false means 'proved false'.

✓ false means 'cannot be proved' from the KB (closed world).

✗ Recursive call first in the rule is fine.

✓ It can loop forever - base case first, progress before recursion.

Recap Quiz

CHECK YOUR UNDERSTANDING · 3 MIN

  • Q1. In Prolog, which is a variable?
  • (a) ali (b) Ali (c) 'ali' (d) student
  • Q2. In a rule, ':-' is read as:
  • (a) and (b) or (c) if (d) not
  • Q3. ?- X is 3 * 4. gives:
  • (a) X = 3*4 (b) X = 12 (c) false (d) error
  • Q4. When a goal fails, Prolog:
  • (a) Stops (b) Backtracks to the last choice (c) Restarts the program (d) Asks the user

Homework and Next Lecture

BEFORE THE NEXT CLASS

  • Task 1: Write parent/2 facts for 3 generations of your family (6+ facts). Write rules grandparent(G, C) and sibling(X, Y). Run 2 queries in SWI-Prolog; paste the output.
  • Task 2: Add road(multan, khanewal) and road(khanewal, lahore) to routes.pl. How many routes from Burewala to Lahore now? Run route/3.
  • Task 3: Write can_sit_final(X): student, attendance 75% or more AND fee_paid(X). Test it with 3 students.
  • Read: Luger 14.1-14.3; SWI-Prolog 'Getting started'.

NEXT LECTURE

  • Lecture 10:
  • Problem Solving as Search

← All CS-511 material