Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 

Similar presentations


Presentation on theme: "COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 "— Presentation transcript:

1 COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 

2 Comp 307: Artificial IntelligenceLecture 2:2 Prolog (I) Introduction to Prolog – 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 – 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 – Learning Prolog online course

3 Comp 307: Artificial IntelligenceLecture 2:3 Introduction to Prolog A logic programming language human(john). human(mary). alive(john). alive(mary). age(mary, 20). breathe(X):- human(X), alive(X). ?- breathe(mary). yes

4 Comp 307: Artificial IntelligenceLecture 2:4 Prolog Basics Program: – Facts predicate(arguments). – Rules Head:- Body. Arguments: – Constants begin with lower case letter, or in ‘ ’ – Variables begin with upper case letter or _ – Free Variable: _ Queries: the questions that a program answers

5 Comp 307: Artificial IntelligenceLecture 2:5 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 ?-human(john). yes ?-human(jim). no. ?-human(X). X=john ; X=mary ; no. ?-breathe(john). yes ?-breathe(jim). no ?-breathe(X). X=john; X=mary; no

6 Comp 307: Artificial IntelligenceLecture 2:6 Java vs Prolog OOP Event-driven Array While If Recursion Data Fields & local variables Data type Files Input/Output Comments

7 Comp 307: Artificial IntelligenceLecture 2:7 Procedural vs Declarative Procedural Numeric Programming = Data Structure + Algorithm Java C, C++ Pascal Fortran Basic … Declarative Non-numeric, Symbolic Programming = facts + rules Prolog

8 Comp 307: Artificial IntelligenceLecture 2:8 The Procedural meaning of Prolog Input/Output, “call a method” in Prolog /* 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.

9 Comp 307: Artificial IntelligenceLecture 2:9 “if” in Prolog 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_or_square(X, X):- write('this is a square'). rectangle_or_square(X, Y):- X=\=Y, write('this is a rectangle'). |?-rectangle_example.

10 Comp 307: Artificial IntelligenceLecture 2:10 “if– else” in Prolog Multiple clauses and “cut” instead of “if-else” 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(_, _):- write('this is a rectangle'). |?-rectangle_example.

11 Comp 307: Artificial IntelligenceLecture 2:11 “While” and Recursion Loop: Break problem into a sequence of sub-problems, all sub- problems are the same. Recursion: Break problem into sub-problems, some of which are smaller versions of the same problem Every loop is a recursion

12 Comp 307: Artificial IntelligenceLecture 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).

13 Comp 307: Artificial IntelligenceLecture 2:13 Recursion in Java 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; } n! = 1 * 2 * 3 *...* n n! = n * (n-1)! 0!=1

14 Comp 307: Artificial IntelligenceLecture 2:14 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


Download ppt "COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 "

Similar presentations


Ads by Google