# Lesson 18 - Module 7 Knowledge Representation and...

This preview shows pages 1–6. Sign up to view the full content.

Module 7 Knowledge Representation and Logic – (Rule based Systems) Version 1 CSE IIT, Kharagpur

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lesson 18 Rule based Systems - II Version 1 CSE IIT, Kharagpur
7.2.5 Programs in PROLOG These minimal notes on Prolog show only some of its flavor. Here are facts plays(ann,fido). friend(john,sam). where ann, fido, john, and sam are individuals, and plays and friend are functors . And here is a rule likes(X,Y) :- plays(X,Y),friend(X,Y). It says that if X plays with Y and X is a friend of Y then X likes Y. Variables start with capital letters (If we are not interested in the value of a variable, we can just use _ (underscore)). In a rule the left-hand side and the right-hand side are called respectively the head and the tail of the rule. The prompt in prolog is | ?- You exit prolog with the statement halt. You can add rules and facts to the current session in a number of ways: 1. You can enter clauses from the terminal as follows: 2. | ?- consult(user). 3. | like(b,a). 4. | like(d,c). 5. ^D which adds the two clauses to the working space. 6. Or you can read in a file: 7. | ?- consult('filename'). which is added at the end of the working space. 8. Or you can assert a clause 9. | ?- assert(< clause >). 10. Here are some confusing "equalities" in prolog: Version 1 CSE IIT, Kharagpur

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
variable arithmetic predicate relation substitution computation ----------------------------------------------------------- == identical no no = unifiable yes no =:= same value no yes is is the yes yes value of and some examples of their use: ?- X == Y. no ?- X + 1 == 2 + Y. no ?- X + 1 = 2 + Y. X = 2 Y = 1 ?- X + 1 = 1 + 2. no Example: Factorial1 factorial(0,1). factorial(N,F) :- N>0, N1 is N-1, factorial(N1,F1),F is N*F1. then ?- factorial(5,F). F = 120 ?- factorial(X,120). instantiation error Example: Factorial2: dropping N>0 from factorial1 factorial(0,1). factorial(N,F) :- N1 is N-1, factorial(N1,F1),F is N*F1. then ?- factorial(5,F). F = 120; Here ";" asks for next value of F keeps on going until stack overflow Example: Factorial3: Changing order of terms (1) factorial(0,1). (2) factorial(N,F) :- factorial(N1,F1), N is N1+1, F is N*F1. Version 1 CSE IIT, Kharagpur
then ?- factorial(5,F). F = 120 ?- factorial(X,120). X = 5; integer overflow Here is why factorial(X,120) returns 5. For brevity, instead of "factorial" we will write "f". f(X,120) / | / | [0/N5,1/F5,1/N4,1/F4,2/N3,2/F3,3/N2,6/F2,4/N1,24/F1,5/N1] / +-----------------------+ / | | | f(0,1) f(N1,F1) X is N1+1 6 is X*F1 / | / | [0/N5,1/F5,1/N4,1/F4,2/N3,2/F3,3/N2,6/F2,4/N1,24/F1] / +-----------------------+ / | | | f(0,1) f(N2,F2) N1 is N2+1 F1 is N1*F2 / | / | [0/N5,1/F5,1/N4,1/F4,2/N3,2/F3,3/N2,6/F2] / +-----------------------+

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/20/2010 for the course MCA DEPART 501 taught by Professor Hemant during the Fall '10 term at Institute of Computer Technology College.

### Page1 / 12

Lesson 18 - Module 7 Knowledge Representation and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online