0% found this document useful (0 votes)
18 views25 pages

T4 Pro 1

Uploaded by

simon wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

T4 Pro 1

Uploaded by

simon wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CSC3230 Fundamentals AI tutorial 4

⚫ Overview of this tutorial


– Overview of Prolog’s Inference engine.
– Logical operations include Comparison and arithmetic
operators.
– Lists and some of its operations.
– Input and Output of prolog’s program.
– Controlling Backtracking by CUT !!
– Database manipulation by family of assert.

1
How does Prolog answer questions?

⚫ Propositional Logic
– A question to Prolog is always a sequence of one or
more goals.
– To answer a question, Prolog tries to satisfy all the
goals.
– To satisfy a goal means to demonstrate that the goal
logically follows the facts and rules in the program.

⚫ eg. (P  Q)  R
⚫ If sentences P and Q are true, R is true.

2
How does Prolog answer questions?(cont’d)
⚫ First Order Logic
– If the question contains variables, and the variables are
instantiated, Prolog will try to match the objects
assigned into the variables (in place of variables) in
other to satisfy goals.

– If Prolog cannot demonstrate for some instantiation of


variables that the goals logically follow from the
program, then Prolog's answer to the question will be
`no'.

– Prolog starts with the goals, and using rules, substitutes


the current goals with new goals, until new goals
happen to be simple facts.
3
Recall the example in previous tutorial
Current state of database
⚫ parent(pam, bob). ⚫ female(pam).
⚫ parent(tom, bob). ⚫ male(tom).
⚫ parent(tom, liz). ⚫ male(bob).
⚫ parent(bob, ann). ⚫ female(liz).
⚫ parent(bob, pat). ⚫ female(ann).
⚫ parent(pat, jim). ⚫ female(pat).
⚫ male(jim).

4
Recall the example in previous tutorial

⚫ offspring(Y, X) :- parent(X, Y).


⚫ mother(X,Y) :- parent(X, Y), female(X).
⚫ grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
⚫ sister(X, Y) :- parent(Z, X), parent(Z, Y), female(X).
predecessor(X,Z):-parent(X,Z).
predecessor(X,Z):-parent(X,Y),predecessor(Y,Z).

5
Prolog’s Inference through example
Query by users (Goal for the inference)
?- predecessor (tom, pat)
This is the first goal but cannot find in the facts.

Prolog first tries that clause which appears first in the program:
predecessor(X, Z) :- parent(X, Z).

Since the goal is now predecessor(tom, pat), the variables in the


rule must be instantiated as: X=tom, Z=pat.
The original goal predecessor(tom, pat) is now replaced by a new
goal:
parent(tom, pat)

6
Prolog’s Inference through example(cond’t)
parent(tom, pat) no clause satisfying, therefore this goal
fails.

Now Prolog backtracks to the original goal in order to try an


alternative way to derive the top goal predecessor(tom, pat).

The following rule is thus tried:


predecessor(X,Z) :- parent(X, Y), predecessor(Y, Z).
This time, the variables X and Z become instantiated as:
X=tom, Z=pat but Y is not instantiated.

Two new goals:


i. parent(tom, Y) 1 2
ii. predecessor(Y, pat)

7
Prolog’s Inference through example(cond’t)
i. parent(tom, Y) it matches one of the facts in the program:
parent(tom, bob).
Y = bob.

ii. predecessor(Y, pat) ii. predecessor(bob, pat)

To satisfy this goal, the rule predecessor(X,Z) :- parent(X, Z). is


used again.
Therefore, X=bob, Z=pat
The current goal is replaced by: parent(bob, pat).

This goal is immediately satisfied because it


appears in the program as a fact. Execution
parent(bob, pat). complete.

8
Prolog’s Inference through example(cond’t)

1 2

9
Programming Prolog
⚫ Programmers should concentrate mainly on the
declarative meaning and, whenever possible,
avoid being distracted by the execution details.

⚫ Unfortunately, however, procedural aspects cannot


be completely ignored by the programmer for
practical reasons of execution efficiency.

10
Programming Prolog (cond’t)
⚫ Prolog's basic mechanism of resolution
– The way Prolog finds answers is called SLD resolution by
building the SLD Tree.
⚫ Different Logic programming systems build
different SLD-trees, because of
– Computational rule: choose which goal given a query, and
– Search strategy: how to traverse the SLD-tree
⚫ Pure Prolog is a particular logic programming
system with
– Computational rule: select the leftmost goal first.
– Search strategy: depth-first search and left to right.
11
Logical operations
⚫ Comparison
– X > Y X >= Y X< Y X =< Y X == Y X \== Y X =:= Y X
=\= Y

– ?- 1+2 == 2+1.
– no.

– ?- 1+2 =:= 2+1.


– yes.

– (1+2) is interpreted as compound term with functor(+) and two arguments.

12
Arithmetic operators

⚫ + - * / mod div(integer division) is


– ?- X = 1+2.
– X = 1+2 →Not X =3. Because X is interpreted as
compound term
– ?- X is 1+2.
– X =3.

13
Negation

⚫ Prolog's negation is called negation-by-failure.

⚫ not(G) succeeds if G fails. not(G) fails if G succeeds.


⚫ \+ is used instead of not in some Prolog implementation.

⚫ not(G) CANNOT produce values for variables in G!


⚫ not(G) can only be used to check values of variables in G!

14
Negation an example
⚫ student(pat).
⚫ passed(joe).
⚫ failed_student(X) :- not(passed(X)), student(X).

– ?- failed_student(pat).
– yes.
– ?- failed_student(X).
– no. ----> cannot find nobody.

15
Lists

⚫ A list is a sequence of any number of items.


⚫ For example,
⚫ [ann, tennis, tom, basketball]
⚫ The first item is called the head of the list : ann
⚫ The remaining part of the list is called the tail, which has to
be a list. [tennis, tom, basketball]

⚫ An empty list is represented as [].

16
Lists

⚫ The elements of a list can be objects of any kind, in


particular, they can also be lists.
– ?- Hobbies1 = [tennis, music].
– Hobbies2 = [basketball, food].
– L = [ann, Hobbies1, tom, Hobbies2].

– Hobbies1 = [tennis, music].


– Hobbies2 = [basketball, food].
– L = [ann, [tennis, music], tom, [basketball, food]].

⚫ The vertical bar can be used to separate the head and the tail.
⚫ [a, b, c] = [a | [b,c]] = [a, b | [c]] = [a, b, c | []].

17
Operations on Lists

⚫ Membership
– member(X,[X|Tail]).
– member(X,[Head|Tail]) :- member(X,Tail).

⚫ Deleting an Item
– del(X,[X,Tail],Tail).
– del(X,[Y,Tail],[Y|Tail1]) :- del(X,Tail,Tail1).

⚫ S is a sublist of L
– sublist(S,L) :- conc(L1,L2,L), conc(S,L3,L2).

18
Input and Output of prolog

⚫ write/1 to output text strings and values of variables


– ?- write(X), write('Testing ...'), nl.
– nl/0 to output a newline

⚫ read/1 use to ask the user for input.


– ?- run :- read(X),X==y,write(true).
– The input value will be stored in X.

19
Advanced topic of prolog (Controlling
Backtracking)
⚫ Prolog will automatically backtrack if necessary when
satisfying a goal.

⚫ Automatic backtracking is useful because it relieves


programmers of the burden of programming backtracking
explicitly.

⚫ However, uncontrolled backtracking may cause


inefficiency.

⚫ We can do this in Prolog by using the `cut' facility.

20
Advanced topic of prolog (Controlling
Backtracking 2)
⚫ For example
– ?- max(X,Y,X) :- X >= Y.
– ?- max(X,Y,Y) :- X < Y.

⚫ These two rules are mutually exclusive. Thus,

– ?- max(X,Y,X) :- X >= Y, !.
– ?- max(X,Y,Y) :- X < Y.

⚫ Some potential usage, working on large database or AI


game, eg - pruning

21
Advanced topic of prolog(Data Manipulation)

⚫ There is built-in predicates make it possible to update


the database during the execution of the program.

⚫ Adding new clauses to the program during execution.


– using assert/1, asserta/1 and assertz/1.
⚫ Deleting existing clauses.
– using retract/1

22
Advanced topic of prolog(Data Manipulation 2 )
⚫ ?- crisis.
⚫ no.

⚫ ?- assert(crisis).
⚫ yes.

⚫ ?- crisis.
⚫ yes.

⚫ retract(crisis).
⚫ yes.

⚫ ?- crisis.
⚫ no.
23
Advanced topic of prolog(Data Manipulation 3 )
⚫ assert(C) : a clause C is asserted in a arbitrary position in the
database.
⚫ asserta(C) : a clause C is asserted at the beginning of the
database.
⚫ assertz(C) :a clause C is asserted at the end of the database.

⚫ Different position to be asserted affected the shape of SLD


tree. This is what a programmer may need to concern.
– Caching can be done by asserting reusable clause in front.

⚫ retract(C) : erase the first dynamic clause whose head C.


– retractall(H) erase all clauses whose heads H.

24
End

25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy