0% found this document useful (0 votes)
19 views22 pages

T3 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)
19 views22 pages

T3 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/ 22

CSC3230 Fundamentals AI tutorial 3

⚫ Prepared by:
– Mr. Lawrence Cha
⚫ Modified by:
– John Tang, rm. 1005.

⚫ All questions are welcome. You can interrupt me and


ask.

1
Goals of this tutorial

⚫ Goals of this tutorial


– Learning what Prolog is.
– Learning how to write Prolog programs.

2
Two types of Logic

⚫ Propositional logic(very simple):


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

⚫ First order logic(more general logic):


⚫ Including quantifiers and variables
⚫ eg. x, y Parent(x,y)  Child(y,x)
⚫ where x and y are variables.

3
Prolog in general
⚫ What is prolog ?
– An AI Programming Language
– Prolog – Logic programming (first order logic).
– Philosophy of programming ››› Goal Oriented.
– Declarative language (Execution is not in the step-by-step
manner).

⚫ Characteristics of prolog?
– Programming prolog is same as writing down anything you know
that is true, in the form of rules and facts.
– A.I. problems are usually `searching' problems, prolog is
inherently a depth-first search engine.

4
Features of Prolog
⚫ Emphasize knowledge

⚫ Does not emphasize data type

⚫ Programmers don’t need to know about how the algorithm


exploiting knowledge.

⚫ It is easy to express relationships between objects.

5
Compared with other language

⚫ Lisp(functional) and Prolog(declarative) are two principal


language of Artificial Intelligence.

6
Use of Prolog

⚫ Applied in some central areas of AI


– formulating problems using state space,
– heuristic search (best first search),
– expert system,
– knowledge representation,
– planning, language
– processing, learning,
– game playing,
– constraint satisfactory problem.
⚫ Implement other language and prolog interpreter.

7
Elementary concepts in prolog (Clause)

⚫ Clause:
– A prolog program consists of clauses, and each clause
terminates with a full stop.

– Prolog clauses are of three types: Facts, Rules,


questions asked by users.

8
Elementary concepts in prolog (Clause)
⚫ Terms:
– All data objects in Prolog. Terms included:
⚫ Constant: Denotes a known entity/object/thing. Must begin with a
lowercase letter, e.g. john, book.
⚫ Variable: Representing a unknown object. Must begin with an
uppercase letter or an underscore, e.g. Name, _type.
– ‘_’ is the anonymous variable. It means ‘don’t care’.
⚫ Compound Terms: Representing a structure, which is treated as single
object in the program. And they can pictured as tree with the root of
the tree is the functor (name of the term) and offspring of the root as
the components.

9
Compound Term(an example)

⚫ Compound Terms: date(12, oct, 1999)


⚫ Matching the terms, two terms are matched if
(1) they are identical, or
(2) the variables in both terms can instantiated to be identical after the
substitution.

Date
For example:
?- date(D,M,1999) = date(12,october,Y).
D = 12 12 October 1999
M = october
Y = 1999? ; Continue to find
no.
10
Writing prolog programs
⚫ Prolog clauses are of three types:
– Facts - declare things that are always, unconditionally true.

– Rules - declare things that are true, depending on a given


condition.

– By means of questions - users can ask the program what are


true.

11
Writing facts

⚫ Fact
– The general syntax is a predicate followed by a number
of arguments.
– Facts do not contain variables.
⚫ For example:

Fact Possible meaning


drink(coke). >>> Coke is a kind of drink
father(peter,john). >>> Peter is the father of John
parents(peter,mary, john). >>> Peter and Mary are parents of John.

Predicate 3 Arguments
12
Writing rules
⚫ Rules: expresses an if-then relationship.
– The general syntax of a rule : a(...) :- b(...), c(...), ..., z(...).
– Meaning : a(...) is true if b(...), c(...), ..., z(...) are all true.
– The left side of the :- sign is called a head (conclusion).
– The right side of the :- sign is the body(condition), which is a
sequence of goals .
– The commas in the body can be read as the logical `and'.
– When there are more than 2 rules with the same head, they
have the meaning of logical ‘or’.

⚫ A question is the goal for the prolog program. Prolog will try to satisfy
the goals using its databases (knowledge bases). Notes that prolog
answers “no” doesn’t mean that answer is definitely fault. It means
that the system cannot answer this questions with its databases.

13
Illustration of a prolog program

⚫ parent (tom, bob).


⚫ parent (pam, bob).
⚫ parent (tom, liz).
⚫ parent (bob, ann).
⚫ parent (bob, pat).
⚫ parent (pat, jim).

parent relation name


tom, bob, etc. Arguments
parent (tom, bob). a clause
a particular instance of the parent relation

14
Querying Prolog System
User Queries Prolog's
Answers

?- parent (bob, pat). Yes


- is bob the parent of pat?
?- parent (liz, pat). No
- is liz the parent of pat?
?- parent (_,tom). no
- is there parent of tom?
?- parent (X, liz). X = tom
- who is the parent of liz?
?- parent (bob, X). X = ann;
- who are brother or sister under bob? X = pat

?- parent (X, Y). X = pam


- give all parent and children Y = bob;
relation?
X = tom
Y = bob;

X = tom
Y = liz;
...
15
Another relation defined : grandparent

⚫ Who is a grandparent of Jim?


– 1.Who is a parent of Jim?
⚫ Assume that there is some Y.
– 2.Who is a parent of Y?
⚫ Assume that there is some X.
– ?- parent (X, Y), parent (Y, jim).

⚫ Answer:
– X = bob
– Y = pat

16
Another relation defined : grandchildren

⚫ Who are Tom's grandchildren?


– 1.Who is a child of Tom?
⚫ Assume that there is some X.
– 2.Who is a child of X?
⚫ Assume that there is some Y.
– ?- parent (tom, X), parent (X, Y).
⚫ Answer:
– X = bob
– Y = ann ?;

– X = bob
– Y = pat?;
17
Another relation defined : common parent

⚫ Do Ann and Pat have a common parent?


– 1. Who is a parent, X, of Ann?
– 2. Is (this same) X a parent of Pat?

⚫ ?- parent (X, ann), parent (X, pat).


⚫ X = bob

Defining new relations

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


?- sister (X, Y) :- parent (Z, X), parent (Z, Y), female (X).

18
Recursive definitions in prolog

⚫ Predecessor relation:
– predecessor (X, Z) :- parent (X, Z).
– predecessor (X, Z) :- parent (X, Y), parent (Y, Z).
– predecessor (X, Z) :- parent (X, Y1), parent (Y1, Y2), parent (Y2, Z). etc.

⚫ Any problem?

19
Recursive definitions in prolog(cont’d)

A recursive definition:
predecessor(X,Z):-parent(X,Z).
predecessor(X,Z):-parent(X,Y),predecessor(Y,Z).
⚫ ?- predecessor (pam, X).
– X = bob;
– X = ann;
– X = pat;
– X = jim

⚫ Prolog can be used to define recursive definitions easily.


⚫ Recursive programming is, in fact, one of the fundamental principles
of Prolog programming.
20
Loading SICStus Prolog
Starting SICStus Prolog system
Type 'sics' at the Unix command prompt on Sparc machines to run the SICStus Prolog system, like this:
% sics
You will then see something similar to this:

SICStus 3.8.6 (sparc-solaris-5.6): Thu Apr 5 21:49:32 MET DST 2001


Licensed to cse.cuhk.edu.hk
| ?-

The prompt | ?- is the top level Prolog prompt, indicating that it is ready to accept queries.

Loading program
Prolog programs are text files with the .pl extension. Assume that you have a program called myprog.pl, you
can load it by using one of the following methods:
| ?- [myprog].
or
| ?- consult(myprog).
If the program is successfully loaded (i.e. your program has no error), you'll get a message like this:

{consulting /tmp/myprog.pl...}
{/tmp/myprog.pl consulted, 0 msec 1424 bytes}

yes

Listing programs
To show all facts and rules that have been loaded so far, type
| ?- listing.
or you can give the name of the predicate as the argument

Quiting SICStus Prolog


To exit, type either `halt.' or ctrl-d.
21
End

22

You might also like