1 / 33

Control and Side Effects Programming

Control and Side Effects Programming. Faculty of Information Technology Multimedia University. Outline. Performing Input and Output operations. Equality and Comparison operators. Defining new operators. Input and Output Streams (1). Prolog Engine. File x File y User. File 1 File 2

mairi
Download Presentation

Control and Side Effects Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Input and Output Streams (1) Prolog Engine Filex Filey User ... File1 File2 User ... Output Streams Input 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). TCP1211-Logic Programming

  4. Input and Output Streams (2) Prolog Engine Filex Filey User ... File1 File2 User ... Output Streams Input Streams We can always change the current input/output streams Prolog Engine Filex Filey User ... File1 File2 User ... Output Streams Input Streams TCP1211-Logic Programming

  5. 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. ?- see(‘kb1.pl’). yes. ?- read(X). X = too_easy(prolog) ?- see(user). yes. kb1.pl too_easy(prolog). boring(X):-too_easy(X). ... TCP1211-Logic Programming

  6. 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. kb2.pl ?- tell(‘kb2.pl’). yes. ?- write(‘likes(ali, garfield).’). yes. ?- tell(user). yes. likes(ali, garfield). TCP1211-Logic Programming

  7. 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(Y). :male(peter). Y = male(peter) ?- read(P). :12. P = 12 input output TCP1211-Logic Programming

  8. Processing Files ofTerms – Reading Terms (2) Assume the input stream is the file Kb1.pl whereas the output stream is user (screen) User Streams Kb1.pl ?- 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 female(sarah). smart(sarah). 13. person(X):-female(X). TCP1211-Logic Programming

  9. 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 TCP1211-Logic Programming

  10. Processing Files ofTerms – Writing Terms (2) Kb1.pl 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). ?- tell(’C:\Program Files\WIN-PROLOG 4\Kb1.pl’). yes ?- write (male(pet)). yes ?- write (person(sam)). yes ?- told. yes Kb1.pl female(sarah). smart(sarah). 13. person(X):-female(X). male(pet)person(sam) TCP1211-Logic Programming

  11. Processing Characters put(X): X must be an ASCII code, the corresponding character is written on the current output stream. Eg. ?- put(65). A 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’) TCP1211-Logic Programming

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

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

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

  15. 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. TCP1211-Logic Programming

  16. 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, Y is X. X = 3 + 2 , Y = 5 ?- X = 3+2. X = 3 + 2 ?- X is 3+2. X = 5 TCP1211-Logic Programming

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

  18. “= =” 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 TCP1211-Logic Programming

  19. 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. TCP1211-Logic Programming

  20. 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 TCP1211-Logic Programming

  21. 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 TCP1211-Logic Programming

  22. 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 TCP1211-Logic Programming

  23. 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. TCP1211-Logic Programming

  24. Defining operators – Example 1 :- 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) TCP1211-Logic Programming

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

  26. More about defining operators • An operator definition do not specify its meaning. • 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. • Example: Consider the following operator definition – :-op(500, xf, is_smart). TCP1211-Logic Programming

  27. More about defining operators(cont.) • This definition allows us to form the following statement: 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. • Thus, an operator definition tells Prolog how to translate a user friendly notation into its internal representation. TCP1211-Logic Programming

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

  29. Prolog – Tip No. 1 TCP1211-Logic Programming

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

  31. Variables – assignment vs. instantiation • 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. • Problem: The misconception that instantiation and assignment are the same. TCP1211-Logic Programming

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

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

More Related