lect w04 chp 04a prolog
TRANSCRIPT
-
7/28/2019 Lect W04 Chp 04a Prolog
1/18
Artificial Intelligence
(Part 2b)AI PROGRAMMING LANGUAGE:
PROLOG
-
7/28/2019 Lect W04 Chp 04a Prolog
2/18
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 SearchExhaustive 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
-
7/28/2019 Lect W04 Chp 04a Prolog
3/18
PROLOG
LISP and PROLOG are most frequently used
languages in AI
Syntax and semantic features encourage
powerful way of thinking about problems andsolutions
Tools for thinking
-
7/28/2019 Lect W04 Chp 04a Prolog
4/18
LEVELS OF KNOWLEDGE-BASED SYSTEM
Defines capabilities
of an intelligent
system formalizing
representation
language
-
7/28/2019 Lect W04 Chp 04a Prolog
5/18
Intro to PROLOG
Best-known example for LOGic
PROgramming Language
Uses first-order predicate calculus to express
specification Elegant syntax and well-defined semantics
Based on theorem proving by J.A.Robinson
1965. He designed proof procedure calledresolution
-
7/28/2019 Lect W04 Chp 04a Prolog
6/18
Syntax for predicate calculus
programming
To represent facts and
rules
English Pred
calculus
Prolog
and ,
Or ;Only if :-
not not
-
7/28/2019 Lect W04 Chp 04a Prolog
7/18
Facts, Rules, and Queries A knowledge base offacts -are terms which are followed by a full
stop.
parent(ayah,saya). %ayah is my parent
parent(mak,saya).
female(mak). %mak is a female
male(ayah).
Queries- are also complex terms which are followed by a full stop.
?- parent(X,saya). %who is my parent
Rules -create new knowledge
mother(X,Y) :-parent(X,Y) , female(X). %X is mother of Y if X is
%parent of Y and X is female
-
7/28/2019 Lect W04 Chp 04a Prolog
8/18
Prolog command..facts
Open Swi-Prolog window
File-New-Type the facts and rules with full stops at the end
Add facts to database
parent(dad, my).
parent(mum, my).
female(mum).
male(dad).
Save- close file -backtoSwiprolog- File-Consult-choose file-open
-
7/28/2019 Lect W04 Chp 04a Prolog
9/18
Type at the command line for query. Use
symbol ; to list next parent
Prolog command..queries..
?- parent(X, my).
?- female(dad).
?- male(X).
-
7/28/2019 Lect W04 Chp 04a Prolog
10/18
To add more facts and rules, eg.
File- Edit Choose File type new rule to indicate relation mother
mother(X,Y) :- parent(Y,X) , female(X).
Save-Close file
Consult, then write a Query to: List who is my mother
See if ayah is my mother?
List how many mothers in the database?
Prolog command..rule
-
7/28/2019 Lect W04 Chp 04a Prolog
11/18
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
rules to capture the
following
relationships:
father_of(Father,Child)
mother_of(Mother,Child)
grandparent_of(Grandparent,Child)
sister_of(Sister,Person)
aunt_of(Aunt,Person)grandchild_of(Grandchild,Child)
father_of(X, Y) :- male(X), parent(X, Y).
Ex:
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
-
7/28/2019 Lect W04 Chp 04a Prolog
12/18
Exercise (Family relationships)
3) Test your
knowledge basewith these queries:
Do you have an aunt?
Who are your
grandparents?
Who are the
grandchildren of your
grandparents?
Do you have a sister?
-
7/28/2019 Lect W04 Chp 04a Prolog
13/18
Recursion in Prolog
Recursion is the primary controlmechanism for prologprogramming
In Prolog, a list is either anempty list or a term connected by
. to another list Someones ancestor can be one
of their parents or an ancestor ofone of their parents
Find an ancestor
ancestor( Old, Young ) :- parent( Old, Young ).ancestor( Old, Young ) :- parent( Old, Middle ),
ancestor( Middle, Young ).
-
7/28/2019 Lect W04 Chp 04a Prolog
14/18
Recursion in Prolog
When we want to write recursive programs, we need to think about two things: 1. How will the program terminate?
2. How will the program break up the data it works on?
Recursion is an example of a divide-and-conquerStrategy
Note that we normally put the base case first, so that Prolog tests it first!
To ensure that a program terminates, we must have at least one base case anon-recursive clause
We must also ensure that something gets (in some sense) reduced each timea recursive step happens, so that we can say when we have got to the end
Example testing if a term is a list:
The base case is when we have an empty listthe smallest list possible
The recursive case breaks down a non-empty list into a head and a tail andthen tests the tail, so the thing being tested gets smaller each time.
ancestor( Old, Young ) :- parent( Old, Young ).
ancestor( Old, Young ) :- parent( Old, Middle ),
ancestor( Middle, Young ).
Base case
Recursive-
clause
-
7/28/2019 Lect W04 Chp 04a Prolog
15/18
Recursion in Prolog
Example run:?- ancestor( paul, harry ).
Call: ancestor(paul, harry ).
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)
ancestor( Old, Young ):- parent( Old,Young ).
ancestor( Old, Young ):- parent( Old,Middle ),
ancestor( Middle, Young ).
-
7/28/2019 Lect W04 Chp 04a Prolog
16/18
recursive predicate definitionshttp://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdf
Task: Define a predicate ancestor of (X,Y) which is true if Xis an ancestor of Y.
RECURSIVE
http://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdfhttp://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdfhttp://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdfhttp://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdf -
7/28/2019 Lect W04 Chp 04a Prolog
17/18
Exercise the recursive predicate ancestor using your
family tree, add the rule above, then make queries:
?- ancestor (my,X).
?- ancestor (X,my).
?- ancestor (X, mum).
USE TRACE FACILITY TO DISPLAY RECURSION
Exercise in Prolog
ancestor( X, Y):- parent( X,Y ).
ancestor( X, Z ):- parent( X,Z ), ancestor( Z,Y ).
-
7/28/2019 Lect W04 Chp 04a Prolog
18/18
move(1,8).
move(2,7).
move(2,9).
move(3,8).
move(3,4).
move(4,3).
move(4,9). move(7,6).
EXERCISE on RECURSION- KNIGHTS LEGAL MOVE
-TERMINATE RECURSIVE IF X IS IN XPOSITION
-AVOID DUPLICATE STATES
move(7,2).move(6,7).
move(6,1).
move(1,6).
move(8,3).
move(8,1).
move(9,4).move(9,2).
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]).