0% found this document useful (0 votes)
13 views32 pages

2024 Ai 05

The document provides an overview of Prolog, a logic programming language, including its features, syntax for facts and rules, and how to execute Prolog programs. It covers topics such as Horn clauses, querying facts, and defining relationships through rules, with practical examples related to a family tree. Additionally, it includes exercises for students to practice translating statements into Prolog and defining relationships based on a specific family tree.

Uploaded by

pierreerard4
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)
13 views32 pages

2024 Ai 05

The document provides an overview of Prolog, a logic programming language, including its features, syntax for facts and rules, and how to execute Prolog programs. It covers topics such as Horn clauses, querying facts, and defining relationships through rules, with practical examples related to a family tree. Additionally, it includes exercises for students to practice translating statements into Prolog and defining relationships based on a specific family tree.

Uploaded by

pierreerard4
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/ 32

Artificial Intelligence

(5) Programming in Logic (1)

Department of Integrated Information Technology,


College of Science and Engineering, Aoyama Gakuin University
Takeshi Morita
E-mail: [email protected]

Artificial Intelligence (5) 1


Overview
•Overview of Prolog
•Facts
•Variables
•Rules
•Exercise 5

Artificial Intelligence (5) 2


Prolog (Programming in Logic)

• A logic programming language based on first-order logic.


• Designed by Alain Colmerauer(アラン・カルメラウアー)
and Robert Kowalski(ロバート・コワルスキー) in 1972.
• Features
• Declarative programming language(宣言型プログラミング言語)
• Expressing the logic of a computation without describing its
control flow.
• A computation is initiated by running a query over facts and rules
in Prolog.
• Reasoning mechanisms
• Pure Prolog is restricted to Horn clauses(ホーン節).

Artificial Intelligence (5) 3


Horn clause(ホーン節)
• Clause(節)
• Propositional formula consisting of only the disjunction of
literals.
• e.g., pi1 ∨ pi2 ∨ ... ∨ pin
• Horn clause(ホーン節)
• A clause with at most one positive literal.
• e.g., ¬ A1 ∨ ¬ A2 ∨ B1
≡ ¬ (A1 ∧ A2) ∨ B1
≡ A 1 ∧ A 2 → B1

Implication form Semantics


Definite clause(確定節) B ← A1,A2,...,An Rule
Fact(事実) B← Fact
Goal clause(ゴール節) ← A1,A2,...,An Question

Artificial Intelligence (5) 4


SWI-Prolog

• A comprehensive free Prolog environment.


• https://www.swi-prolog.org/
• https://github.com/SWI-Prolog/swipl-devel
• Download SWI-Prolog
• https://www.swi-prolog.org/download/stable
• SWISH
• A Web based SWI-Prolog environment.
• https://swish.swi-prolog.org/
• https://github.com/SWI-Prolog/swish

Artificial Intelligence (5) 5


Computer programming in Prolog

• A Prolog program consists of a set of


clauses(節), where each clause if either a
fact or a rule.
• Specifying some facts about objects and
their relationships.
• Defining some rules about objects and their
relationships.
• Asking questions about objects and their
relationships.

Artificial Intelligence (5) 6


Overview
•Overview of Prolog
•Facts
•Variables
•Rules
•Exercise 5

Artificial Intelligence (5) 7


Facts
• The fact that Tom is a parent of Bob can be
written in Prolog as:
relationship
objects
(predicate)

parent(tom, bob).
• Notes
• The names of all relationships and objects must begin
with a lower-case letter.
• The relationship is written first, and the objects are
separated by commas, and the objects are enclosed
by a pair of round brackets.
• A full stop "." must come at the end of a fact.

Artificial Intelligence (5) 8


Facts
pam tom Facts about a family tree.
parent(pam, bob).
parent(tom, bob).
bob liz
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
ann pat parent(pat, jim).

jim

A family tree.
Artificial Intelligence (5) 9
Questions about facts
• The special symbol is written as a question mark followed by a
hyphen.
• The question "Is Bob a parent of Pat?" can be written in Prolog as:

?- parent(bob, pat).
true.
?- parent(liz, pat).
false.

• When a question is asked in a Prolog system, it will search through


the database (facts and rules).
• Matching predicates and their arguments.
• If Prolog finds a fact that unifies with the question, Prolog will
respond true (yes).
• If no such fact exists in the database, Prolog will respond false (no).

Artificial Intelligence (5) 10


Running the program
with a Prolog system (1)
•Create a prolog file (*.pl).
•Write facts and rules in the file.
•Open the command prompt or the
terminal application.
•Change to the directory where the
prolog file is saved.
•Run the following command:
• swipl [prolog file name]
• e.g.,
• swipl family.pl

Artificial Intelligence (5) 11


Running the program with a
Prolog system (2)
• Create a prolog file (*.pl).
• Write facts and rules in the file.
• Open the swi-prolog.
• Change to the directory where the prolog file is
saved using cd predicate.
• ?- cd("[directory]").
• ?- pwd.
• Read file as the Prolog source file using consult
predicate.
• ?- consult([filename]). e.g, ?- consult(family).
• or equivalently:
• ?- [filename]. e.g., [family].
• Built-in predicates
• https://www.swi-prolog.org/pldoc/man?section=builtin
Artificial Intelligence (5) 12
Overview
•Overview of Prolog
•Facts
•Variables
•Rules
•Exercise 5

Artificial Intelligence (5) 13


Variables
•Variables are strings of letters, digits
and underscores.
•Variables start with an upper-case
letter or an underscore:
• X, Result, _A, _x23, _23, etc.
•When Prolog is asked a question
containing a variable, Prolog searches
through all its facts to find an object
that the variable could stand for.

Artificial Intelligence (5) 14


Questions including variables

pam tom Questions


?- parent(X, liz).
X = tom.
bob liz
?- parent(bob, X).
X = ann;
X = pat.
ann pat ?- parent(X, Y).
X = pam, Y = bob;
X = tom, Y = bob;
X = tom, Y = liz;
jim X = bob, Y = ann;
X = bob, Y = pat;
X = pat, Y = jim.
A family tree.
Artificial Intelligence (5) 15
Conjunctions(連言)
•Composed queries can be expressed
using a comma.
•The comma is pronounced "and".
•When a sequence of goals (separated
by commas) is given to Prolog, Prolog
attempts to satisfy each goal in turn by
searching for a unifying goal in the
database.
•All goals have to be satisfied in order
for the sequence to be satisfied.

Artificial Intelligence (5) 16


Composed Questions
pam tom
Questions
Who are Tom's grandchildren?
bob liz
?- parent(tom, X), parent(X, Y).
X = bob, Y = ann;
X = bob, Y = pat.
ann pat
Do Ann and Pat have a common parent?

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


X = bob.
jim

A family tree.
Artificial Intelligence (5) 17
Question 5-1
• Assuming the parent relation as defined in the previous slide,
what will be Prolog's answers to the following questions?

?- parent(pam, X), parent(X, pat).


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

• Formulate in Prolog the following questions about the


parent relation:

1. Who is Pat's parent?


2. Does Liz have a child?
3. Who is Pat's grandparent?

Artificial Intelligence (5) 18


unary (one-place) relations

•unary (one-place) relations


•To declare simple yes/no properties
of objects.
•e.g,
• male(tom). <=> gender(tom, male).
• female(pam). <=> gender(pam, famale).

Artificial Intelligence (5) 19


Overview
•Overview of Prolog
•Facts
•Variables
•Rules
•Exercise 5

Artificial Intelligence (5) 20


Rules
• Rules are used when you want to say that a fact depends on a
group of other facts.
• Facts are always, unconditionally, true.
• In English, we use the word "if" to express a rule.
• We could define "mother" in a similar way as the parent
relation.
• mother(pam, bob).
• mother(pat, jim).
• The "mother" relation can be defined much more elegantly by
making use of the fact that it can be logically derived from the
already known relations "parent" and "female".

For all X and Y,


X is the mother of Y if
X is a parent of Y, and X is female.
Artificial Intelligence (5) 21
Rules
goal goal

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

head body
(a conclusion part) (a condition part)

For all X and Y, if X is a parent of Y and X is a female


then X is the mother of Y.

Artificial Intelligence (5) 22


Facts and rules about a family tree

pam tom Facts Rules


parent(pam, bob). mother(X, Y) :-
parent(tom, bob). parent(X, Y),
parent(tom, liz).
bob liz parent(bob, ann).
female(X).
parent(bob, pat).
parent(pat, jim). father(X, Y) :-
parent(X, Y),
ann pat female(pam). male(X).
female(liz).
female(pat).
female(ann).
male(tom).
jim
male(bob).
male(jim).
A family tree.
Artificial Intelligence (5) 23
Questions using facts and rules
Questions
pam tom
Is Pam the mother of Bob?

?- mother(pam, bob).
true .
bob liz
X = pam and Y = bob.
mother(pam, bob) :- parent(pam, bob), female(pam).

Prolog tries to find out whether the condition part is true.


ann pat
mother(pam, bob)

has been replaced with the goals:

parent(pam, bob), female(pam)


jim
These two goals can be found as facts.
A family tree. Then, the conclusion part of the rule is also true.

Artificial Intelligence (5) 24


Examples of rules
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
X
For all X and Z, parent
X is a grandparent of Z if grandparent
Y
(1) X is a parent of Y, and
(2) Y is a parent of Z. parent

Z
sister(X, Y) :- Z
parent(Z, X), parent(Z, Y), female(X).
parent parent
For all X and Y,
X is a sister of Y if female
(1) both X and Y have the same parent, and X Y
(2) X is a female. sister

Artificial Intelligence (5) 25


Questions using facts and rules
Questions
pam tom Who is Pat's sister?

?- sister(X, pat).
X = ann;
bob liz X = pat.

Pat is a sister to herself?!


This is not what we had in mind when
defining the sister relation.
ann pat
To correct our rule about sisters we have to
add that X and Y must be different.

jim

A family tree.
Artificial Intelligence (5) 26
Question 5-2
1. Define the relation "grandchild"
using the "parent" relation. Hint: It
will be similar to the grand parent
relation.
2. Define the relation aunt(X, Y) in
terms of the relations "parent" and
"sister".

Artificial Intelligence (5) 27


Overview
•Overview of Prolog
•Facts
•Variables
•Rules
•Exercise 5

Artificial Intelligence (5) 28


Exercise 5
•Answer Exercises 5-1 and 5-2 shown
on the next two slides.
•Write your answers in a Word file,
convert it to PDF with the following
file name.
• Exercise5-[Student ID]-[Name].pdf
•Submit the PDF file and
"isono_family.pl" file to CoursePower
by 11:00 on October 28, 2024.

Artificial Intelligence (5) 29


Exercise 5-1
•Translate the following statements into
Prolog rules:
1. Everybody who has a child is happy.
2. For all X, if X is an animal and has
feathers, then X is a bird.

Artificial Intelligence (5) 30


Exercise 5-2
• Define the facts using the relations parent(X, Y), male(X),
and female(X) based on the Isono and Fuguta family tree.
• See https://ja.wikipedia.org/wiki/サザエさんの登場人物
• Define the rules father(X, Y), mother(X, Y),
grandparent(X, Y).
• Translate the following statements into questions in Prolog.
• Who is the father of katsuo?
• Who is the mother of tarao?
• Who are the grandparents of tarao?
• Who are the children of namihei?
• Save the facts and rules in the "isono_family.pl".
• Describe the questions in Prolog and results in the report file.

Artificial Intelligence (5) 31


References
1. Ivan Bratko, Prolog Programming for
Artificial Intelligence (4th Edition), Pearson
Education Canada (2011) ISBN: 978-
0321417466.
2. William F. Clocksin, Christopher S. Mellish,
Programming in Prolog: Using The Iso
Standard, Springer (2013) ISBN: 978-
3540006787.
3. SWI-Prolog Reference manual:
https://www.swi-
prolog.org/pldoc /doc_for?object=manual

Artificial Intelligence (5) 32

You might also like