Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.

Similar presentations


Presentation on theme: "TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University."— Presentation transcript:

1 TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University

2 TCP1211-Logic Programming Outline Performing Input and Output operations. Performing Input and Output operations. Equality and Comparison operators. Equality and Comparison operators. Defining new operators. Defining new operators.

3 TCP1211-Logic Programming Input and Output Streams (1) Input Streams File 1 File 2 User... Prolog Engine File x File y User... Output Streams The user input stream is the default input to Prolog system. User terminal (keyboard) is a user input stream where the user key in data. The user output stream is the default output to Prolog system. User terminal (screen) is a user output stream where the user can see the results (which are not saved).

4 TCP1211-Logic Programming Input and Output Streams (2) Input Streams File 1 File 2 User... Prolog Engine File x File y User... Output Streams We can always change the current input/output streams Input Streams File 1 File 2 User... Prolog Engine File x File y User... Output Streams

5 TCP1211-Logic Programming Input and Output Streams (3) At the beginning of the execution, the input stream and output stream correspond to the user terminals(keyboard and screen). The goal: see(FileName). causes the input to be switched from the previous input stream to FileName. Therefore, Prolog is ready to read the input from FileName. too_easy(prolog). boring(X):-too_easy(X).... ?- see(‘kb1.pl’). yes. ?- read(X). X = too_easy(prolog) ?- see(user). yes. kb1.pl

6 TCP1211-Logic Programming Input and Output Streams (4) The goal: tell(FileName). causes the output to be switched to FileName instead of the previous output stream. Therefore, Prolog is ready to display the output to FileName. likes(ali, garfield). ?- tell(‘kb2.pl’). yes. ?- write(‘likes(ali, garfield).’). yes. ?- tell(user). yes. kb2.pl

7 TCP1211-Logic Programming Processing Files of Terms – Reading Terms (1) To Read a Term: read(X): a predefined predicate that read a term X from user input stream (keyboard or from a file). Assume the input/output streams are user. ?- read(P). :12. P = 12 input output ?- read(Y). :male(peter). Y = male(peter)

8 TCP1211-Logic Programming Processing Files ofTerms – Reading Terms (2) Assume the input stream is the file Kb1.pl whereas the output stream is user (screen) female(sarah). smart(sarah). 13. person(X):-female(X). ?- see(’C:\Program Files\WIN-PROLOG 4\Kb1.pl’). yes ?- read(P). P = female(sarah) ?- read(P). P = smart(sarah) ?- read(P). P = 13 ?- read(P). P = (person(_46718) :- female(_46718)) ?- read(P). P = end_of_file ?- seen. yes Kb1.pl User Streams

9 TCP1211-Logic Programming Processing Files of Terms – Writing Terms (1) To Write a Term write(X): a predefined predicate that output a term X to the output stream from user input stream (keyboard or from a file). Assume the output stream is user. dispList([]). dispList([H|T]):- write(H), tab(3), dispList(T). ?- dispList([a,b,c,d]). a b c d yes

10 TCP1211-Logic Programming Processing Files ofTerms – Writing Terms (2) female(sarah). smart(sarah). 13. person(X):-female(X). ?- tell(’C:\Program Files\WIN-PROLOG 4\Kb1.pl’). yes ?- write ( male(pet) ). yes ?- write ( person(sam) ). yes ?- told. yes female(sarah). smart(sarah). 13. person(X):-female(X). male(pet)person(sam) Assume the input stream is the file Kb1.pl whereas the output stream is user (screen). Kb1.pl

11 TCP1211-Logic Programming Processing Characters put(X): put(X): X must be an ASCII code, the corresponding character is written on the current output stream. Eg. ?- put(65). A get0(Y): get0(Y): read a single character from the current input stream. Y will be instantiated with the ASCII code of the character. Eg. ?-get0(X). D X = 68 (ASCII CODE of ‘D’)

12 TCP1211-Logic Programming Updating clauses of the Knowledge Base (1) assert assert(C): asserts (add) a clause C at the end of the KB. asserta asserta(C): asserts (add) a clause C at the beginning of the KB. assertz assertz(C): asserts (add) a clause C at the end of the KB. retract retract(C): deletes a clause that matches the clause C.

13 TCP1211-Logic Programming Updating clauses of the Knowledge Base (2) ?- assert(good(peter)). yes ?- good(X). X = peter ?- retract(good(peter)). yes ?- good(X). no

14 TCP1211-Logic Programming Testing the types of terms var var(X): succeeds if X is currently an uninstantiated variable. nonvar nonvar(X): succeeds if X is not a variable or is already an instantiated variable. atom atom(X): succeeds if X is currently an atom. integer integer(X): succeeds if X is currently an integer. float float(X): succeeds if X is currently a real number. number number(X): succeeds if X is currently a number. atomic atomic(X): succeeds if X is currently an atom or a number. compound compound(X): succeeds if X is currently a structure.

15 TCP1211-Logic Programming Testing the types of terms – Example Count how many times an atom is in a given list L : count(Input,[],0). count(Input, [H|Tail], N):-count(Input,Tail,N). count(Input, [Input|T],N):- atom(Input), count(Input,T,N1), N is N1+1.

16 TCP1211-Logic Programming Equality Comparison “=” X = Y is true if X and Y match. “is” X is E: where E is an arithmetic expression. Is true if the evaluated expression matches with X ?- L = [a,b,c]. L = [a,b,c] ?- X = Y. X=Y= _ ?- X = peter. X = peter ?- peter=X. X = peter ?- peter=pet. no ?- X = 3+2. X = 3 + 2 ?- X is 3+2. X = 5 ?- X =3+2, Y is X. X = 3 + 2, Y = 5

17 TCP1211-Logic Programming (In)Equality “=:=” E1 =:=E2 is true if E1 is equal with E2. “=\=”E1 =:=E2 is true if E1 is not equal to E2. ?- 2 is X. Error !!!!!!! ?- Y is X. Error!!!!!!!! ?- 2+3 =:= 3+2. yes ?- X = 4, Y is X+1, Y=:=X. no ?- 2+3 =:= 12-7. yes ?- X = 4, Y is X+1, Y=\=X. X = 4, Y = 5 ?- X+2=:=X+2. Error !!!!!

18 TCP1211-Logic Programming “= =”Literal equality “\= =” Literal Inequality Term1 = = Term2 is true if Term1 is literally identical to Term2 Term1 \= = Term2 is true if Term1 is literally different from Term2 ?- X+2 ==X+2. X = _ ?- 2 ==2. Yes ?- peter ==peter. yes ?- X == X. X = _ ?- faster(ali, ahmad)==faster(ali, ahmad). yes ?- faster(ali, ahmad)==faster(ali, X). No ?- X\= =Y. yes

19 TCP1211-Logic Programming Defining our own operators op( Precedence, Type, Functor). Functor: operator name. Precedence: integer from 1 to 1200. The lower the number the higher is the precedence Type: Prefix/infix/postfix format of the operator.

20 TCP1211-Logic Programming Operator Precedence – Example 1 P A B. The built in operator :- has Precedence 1200 and Type xfx The built in operator, has Precedence 1000and Type xfy is of higher priority. This means we execute A B first and then we execute,,, :-

21 TCP1211-Logic Programming Prefix/Infix/Postfix operators (1) Infix format: 3+2-5/2 : arg1 Op arg2 …. The operator is Inside the expression Prefix format: +(3, -(2, /(5,2)) ) : Op (arg1, Op (arg2, ….)) The operator Precedes the arguments Postfix format: (3,(2, (5,2)/)-)+ : (arg1, (arg2…)Op2)Op1 The arguments Precede the operator

22 TCP1211-Logic Programming Prefix/Infix/Postfix operators (2) Infix format: xfx nonassociative / xfy right-assoc / yfx left-assoc Prefix format: fx nonassociative / fy right-assoc Postfix format: xf nonassociative / yf left-assoc

23 TCP1211-Logic Programming Left and Right Associative 10+5+8 is executed as (10+5) + 8 and not as 10+(5+8) because + “yfx” left-associative. 4^3^2 is executed as 4^(3^2) and not (4^3)^2 because ^ “xfy” right-associative.

24 TCP1211-Logic Programming :- op(1000, xfy, isa). :- op(900, xfx, of). :- op(800, xfy, and). A and B :- A,B. ‘Ali’ isa lecturer of tcp1211 and tcp1241. ‘Ali’ isa sportsman. ?- 'Ali' isa Somebody. Somebody = (lecturer of tcp1211 and tcp1241) ; Somebody = sportsman ?- 'Ali' isa lecturer of SomeSubjects. SomeSubjects = (tcp1211 and tcp1241) Defining operators – Example 1

25 TCP1211-Logic Programming ?- Who isa Somebody. Who = 'Ali', Somebody = (lecturer of tcp1211 and tcp1241) ; Who = 'Ali', Somebody = sportsman ?- 'Ali' isa lecturer of tcp1211 and AnotherSubject. AnotherSubject = tcp1241 Defining operators – Example (2)

26 TCP1211-Logic Programming More about defining operators An operator definition do not specify its meaning. An operator definition do not specify its meaning. An operator definition does not indicate when a query involving the operator will evaluate to true. An operator definition does not indicate when a query involving the operator will evaluate to true. Any expression constructed using newly defined operators will be mapped to Prolog’s internal representation. Any expression constructed using newly defined operators will be mapped to Prolog’s internal representation. Example: Consider the following operator definition – Example: Consider the following operator definition – :-op(500, xf, is_smart).

27 TCP1211-Logic Programming More about defining operators(cont.) This definition allows us to form the following statement: john is_smart. This definition allows us to form the following statement: john is_smart. We can then issue the query ?-john is_smart. We can then issue the query ?-john is_smart. In order to answer this query, Prolog will try to prove is_smart(john). which is Prolog’s internal representation. In order to answer this query, Prolog will try to prove is_smart(john). which is Prolog’s internal representation. Thus, an operator definition tells Prolog how to translate a user friendly notation into its internal representation. Thus, an operator definition tells Prolog how to translate a user friendly notation into its internal representation.

28 TCP1211-Logic Programming Summary How to specify and use input/output streams in Prolog. How to specify and use input/output streams in Prolog. Some built-in predicates for testing the types of terms. Some built-in predicates for testing the types of terms. The difference between various equality comparison operators. The difference between various equality comparison operators. How to define your own operators - prefix, infix and postfix. How to define your own operators - prefix, infix and postfix.

29 TCP1211-Logic Programming Prolog – Tip No. 1

30 TCP1211-Logic Programming Variables – How different are they from C’s? Variables in Prolog are not typed. Variables in Prolog are not typed. Therefore, variable declarations are not required. Therefore, variable declarations are not required. Problem: students accustomed to imperative languages feel reluctant to create new variables ‘on-the-fly’. Problem: students accustomed to imperative languages feel reluctant to create new variables ‘on-the-fly’. This leads to incorrect codes  described next. This leads to incorrect codes  described next.

31 TCP1211-Logic Programming Variables – assignment vs. instantiation Variables in Prolog cannot be assigned! The can only be instantiated. Variables in Prolog cannot be assigned! The can only be instantiated. The value of a variable can only be changed by un- instantiating it first. The value of a variable can only be changed by un- instantiating it first. Problem: The misconception that instantiation and assignment are the same. Problem: The misconception that instantiation and assignment are the same.

32 TCP1211-Logic Programming Is this code correct? factorial(Number,Factorial):- NewNumber is Number-1, factorial(NewNumber,Factorial), Factorial is Number * Factorial.

33 TCP1211-Logic Programming Why does it fail to work? Due to the following clause: Due to the following clause: Factorial is Number * Factorial. Why? Why? How to avoid this?  Use new variables (generously). Avoid reusing variables unless you are very sure that the reuse will give correct behavior. How to avoid this?  Use new variables (generously). Avoid reusing variables unless you are very sure that the reuse will give correct behavior.


Download ppt "TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University."

Similar presentations


Ads by Google