Learning Prolog online course. Comp 307: Artificial Intelligence. Lecture 2:3.
Introduction to Prolog. • A logic programming language human(john). human(
mary).
Comp 307: Artificial Intelligence
Lecture 2:2
Prolog (I) • Introduction to Prolog
COMP307
–
Artificial Intelligence
–
Facts, rules, queries Matching and backtracking
• The procedural meaning of Prolog – – –
“if” in Prolog “while” in Prolog Recursion in Prolog
Reading: • Prolog documentation on course home page
Xiaoying Gao Victoria University of Wellington
–
The SWI-Prolog user’s manual • How to start Prolog on Unix, see 2.1 – edit a file: myfile.pl – command line “prolog” or “swi-prolog” – load a file and compile ?-[myfile].
• How to use it with emacs, see 2.5 Lecture 2:1
Comp 307: Artificial Intelligence
↓
Lecture 2:3
–
Learning Prolog online course
Comp 307: Artificial Intelligence
Introduction to Prolog • A logic programming language human(john). human(mary).
Lecture 2:4
Prolog Basics • Program: –
Facts predicate(arguments).
–
Rules Head:- Body.
alive(john). alive(mary). age(mary, 20).
• Arguments: – Constants begin with lower case letter, or in ‘ ’ – Variables begin with upper case letter or _ – Free Variable: _
breathe(X):- human(X), alive(X).
• Queries: the questions that a program answers
?- breathe(mary).
yes
Comp 307: Artificial Intelligence
Lecture 2:5
Comp 307: Artificial Intelligence
Prolog
Java vs Prolog
• Prolog Program – is not a sequence of commands. – like a knowledge base--a collection of assertions. • The Prolog system performs mechanical reasoning based on these assertions. – Matching & Backtracking ?-breathe(john). ?-human(john). yes
yes ?-human(jim). no. ?-human(X).
?-breathe(jim). no ?-breathe(X). X=john; X=mary; no
X=john ; X=mary ; no.
Lecture 2:6
Comp 307: Artificial Intelligence
Lecture 2:7
Procedural vs Declarative
• • • • • • • • • • •
OOP Event-driven Array While If Recursion Data Fields & local variables Data type Files Input/Output Comments
Comp 307: Artificial Intelligence
The Procedural meaning of Prolog Input/Output, “call a method” in Prolog
• • •
Procedural Numeric Programming = Data Structure + Algorithm
• • •
Declarative Non-numeric, Symbolic Programming = facts + rules
• • • • • •
Java C, C++ Pascal Fortran Basic …
•
Prolog
Lecture 2:8
/* rectangle example*/ rectangle_example:write('Enter the width: '), read(W), write('Enter the height: '), read(H), calculate_area(W, H, Area), format("The area is ~w~n", [Area]). calculate_area(W, H, X):X is W*H. |?-rectangle_example.
Comp 307: Artificial Intelligence
Lecture 2:9
Comp 307: Artificial Intelligence
“if” in Prolog
Lecture 2:10
“if– else” in Prolog Multiple clauses and “cut” instead of “if-else”
Multiple clauses instead of “if” rectangle_example:write('Enter the width: '), read(W), write('Enter the height: '), read(H), rectangle_or_square(W, H).
rectangle_example:write('Enter the width: '), read(W), write('Enter the height: '), read(H), rectangle_or_square(W, H).
rectangle_or_square(X, X):write('this is a square'). rectangle_or_square(X, Y):X=\=Y, write('this is a rectangle').
rectangle_or_square(X, X):-!, write('this is a square'). rectangle_or_square(_, _):write('this is a rectangle').
|?-rectangle_example.
|?-rectangle_example.
Comp 307: Artificial Intelligence
Lecture 2:11
“While” and Recursion Loop: Break problem into a sequence of sub-problems, all subproblems are the same. Recursion: Break problem into sub-problems, some of which are smaller versions of the same problem
Every loop is a recursion
Comp 307: Artificial Intelligence
Lecture 2:12
“while” in prolog “Tail Recursion” instead of “while” Base case: stop Normal case: do one step change the arguments call the same clause using different arguments
print_stars(0):-!. print_stars(N):write(‘*’), N1 is N-1, print_stars(N1).
|?-print_stars(5).
Comp 307: Artificial Intelligence
Lecture 2:13
Recursion in Java • n! = 1 * 2 * 3 *...* n n! = n * (n-1)! 0!=1
• Recursive method in Java: a method that calls itself public int factorial(int n) { if (n == 0) return 1; else { int f = factorial (n-1); return n * f; } }
Comp 307: Artificial Intelligence
Recursion in Prolog factorial(0, 1):-!. factorial(N, X):N1 is N-1, factorial(N1, X1), X is N * X1. |?-factorial(4, X). X=24. Base case: do something Normal case: change the arguments call the same clause using different arguments do one step
Lecture 2:14