0% found this document useful (0 votes)
3 views

AI_Part3_Prolog

The document outlines a course on Artificial Intelligence focusing on the programming language Prolog, covering key topics such as knowledge representation, search algorithms, and problem-solving techniques. It provides an introduction to Prolog, including syntax, facts, rules, and queries, as well as exercises related to family relationships and recursion. Additionally, it discusses the importance of recursion in Prolog programming and provides examples and exercises to illustrate these concepts.

Uploaded by

gboxanas54
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

AI_Part3_Prolog

The document outlines a course on Artificial Intelligence focusing on the programming language Prolog, covering key topics such as knowledge representation, search algorithms, and problem-solving techniques. It provides an introduction to Prolog, including syntax, facts, rules, and queries, as well as exercises related to family relationships and recursion. Additionally, it discusses the importance of recursion in Prolog programming and provides examples and exercises to illustrate these concepts.

Uploaded by

gboxanas54
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Artificial Intelligence

(Part 3)
AI PROGRAMMING LANGUAGE:
PROLOG
Course Contents
Again..Selected topics for our course. Covering all of AI is impossible!

Key topics include:


Introduction to Artificial Intelligence (AI)
Knowledge Representation and Search
Introduction to AI Programming
Problem Solving Using Search
Exhaustive Search Algorithm
Heuristic Search
Techniques and Mechanisms of Search Algorithm
Knowledge Representation Issues and Concepts
Strong Method Problem Solving
Reasoning in Uncertain Situations
Soft Computing and Machine Learning
PROLOG
n LISP and PROLOG are most frequently used
languages in AI
n Syntax and semantic features encourage
powerful way of thinking about problems and
solutions
n Tools for thinking
LEVELS OF KNOWLEDGE-BASED SYSTEM
Defines capabilities
of an intelligent
system formalizing
representation
language
Intro to PROLOG
n Best-known example for LOGic
PROgramming Language
n Uses first-order predicate calculus to express
specification
n Elegant syntax and well-defined semantics
n Based on theorem proving by J.A.Robinson
1965. He designed proof procedure called
resolution
Syntax for predicate calculus
programming
n To represent facts and
rules

English Pred Prolog


calculus

and ,
Or ;
Only if :-
not not
Facts, Rules, and Queries

n A knowledge base of facts -are terms which are followed by a full


stop.
n parent(ayah,saya). %ayah is my parent
n parent(mak,saya).
n female(mak). %mak is a female
n male(ayah).

n Rules -create new knowledge


n mother(X,Y) :-
parent(X,Y) , female(X). %X is mother of Y if X is
%parent of Y and X is female

n Queries- are also complex terms which are followed by a full stop.
n ?- parent(X,saya). %who is my parent
Prolog command..facts
n Open Swi-Prolog window

n File-New-Type the facts and rules with full stops at the end

n Add facts to database

parent(ayah,saya).
parent(mak,saya).
female(mak).
male(ayah).

n Save- close file -backtoSwiprolog-File-Consult-choose file-open


<enter>
Prolog command..rule
n To add more facts and rules, eg.

n File- Edit –Choose File – type new rule to indicate relation mother

mother(X,Y) :- parent(Y,X) , female(X).

n Save-Close file

n Consult, then write a Query to:


n List who is my mother
n See if ayah is my mother?
n List how many mothers in the database?
Prolog command..queries..
n Type at the command line for query. Use
symbol ; to list next parent

?- parent(X,saya).

?- female(ayah).

?- male(X).
Exercise (Family relationships)
1) Use the predicates male/1,
female/1, and parent_of/2 to
represent your family tree as a
Prolog knowledge base

2) Now, formulate
father(Father,Child)
rules to capture the mother(Mother,Child)
following grandparent(Grandparent,Child)
relationships: sister(Sister,Person)
grandchild(Grandchild,Child)

Ex:
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
father_of(X, Y) :- male(X), parent(X, Y).
Exercise (Family relationships)

Do you have an aunt?


3) Test your Who are your
knowledge base grandparents?
Who are the
with these queries: grandchildren of your
grandparents?
Do you have a sister?
Recursion in Prolog
n Recursion is the primary control
mechanism for prolog
programming
n In Prolog, a list is either an
empty list or a term connected by
‘.’ to another list
n Someone’s ancestor can be one
of their parents or an ancestor of
one of their parents
n Find an ancestor
ancestor( Old, Young ) :- parent( Old, Young ).
ancestor( Old, Young ) :- parent( Old, Middle ),
ancestor( Middle, Young ).
Recursion in Prolog
n When we want to write recursive programs, we need to think about two things:
n 1. How will the program terminate?
n 2. How will the program break up the data it works on?

n Recursion is an example of a divide-and-conquer Strategy


n Note that we normally put the base case first, so that Prolog tests it first!
n To ensure that a program terminates, we must have at least one base case – a
non-recursive clause
n We must also ensure that something gets (in some sense) ”reduced” each time
a recursive step happens, so that we can say when we have got to the end

n Example – testing if a term is a list:


n – The base case is when we have an empty list –the smallest list possible
n – The recursive case breaks down a non-empty list into a head and a tail and
then tests the tail, so the thing being tested gets smaller each time.

Base case
ancestor( Old, Young ) :- parent( Old, Young ).
ancestor( Old, Young ) :- parent( Old, Middle ),
ancestor( Middle, Young ). Recursive-
clause
Recursion in Prolog
n Example run: ancestor( Old, Young ):- parent( Old,Young ).
?- ancestor( paul, harry ). ancestor( Old, Young ):- parent( Old,Middle ),
Call: ancestor(paul, harry ). ancestor( Middle, Young ).
Call: parent(paul, harry ).
Fail.
Retry: ancestor(paul, harry ).
Call: parent(paul, Middle ).
Unify: Middle = lili.
Succeed: parent(paul, lili ).
Call: ancestor( lili, harry ).
Call: parent( lili, harry).
Succeed: parent( lili, harry ).
Succeed: ancestor( lili, harry ).
Succeed: ancestor(paul, harry)
recursive predicate definitions
http://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdf

Task: Define a predicate ancestor of (X,Y) which is true if X


is an ancestor of Y.

RECURSIVE
Exercise in Prolog
ancestor( X, Y):- parent( X,Y ).
ancestor( X, Z ):- parent( X,Z ), ancestor( Z,Y ).

n Exercise the recursive predicate ancestor using your


family tree, add the rule above, then make queries:

?- ancestor (saya,X).
?- ancestor (X,saya).
?- ancestor (X, mak).

n USE TRACE FACILITY TO DISPLAY RECURSIVE


EXERCISE on RECURSION- KNIGHT’S LEGAL MOVE

n move(1,8). move(7,2).
n move(2,7). move(6,7).
n move(2,9). move(6,1).
n move(3,8). move(1,6).
n move(3,4). move(8,3).
n move(4,3). move(8,1).
n move(4,9). move(9,4). -TERMINATE RECURSIVE IF X IS IN X
n move(7,6). POSITION
move(9,2).
-AVOID DUPLICATE STATES

member(X,[X|T]).
member(X,[Y|T]) :- member(X,T).

path(Z,Z,L).
path(X,Y,L) :- move(X,Z),not(member(Z,L)),path(Z,Y,[Z|L]).

You might also like