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

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Introduction to C Programming
PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O.
FATIH UNIVERSITY Department of Computer Engineering Input and Output Notes for Ch.6 of Bratko For CENG 421 Fall03.
True or false A variable of type char can hold the value 301. ( F )
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Carlos Varela Rennselaer Polytechnic Institute September 10, 2007
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Introduction to C Programming
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Review of C++ Programming Part II Sheng-Fang Huang.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
High-Level Programming Languages: C++
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Declarative Programming Arithmetic in PROLOG Autumn 2014.
1 Operations Making Things Happen (Chap. 3) Expressions.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Artificial Intelligence
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Cs774 (Prasad)L4ListProcessing1 List processing in Prolog
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
1 CS161 Introduction to Computer Science Topic #6.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Artificial Intelligence Programming in Prolog
Expressions and Assignment
Stacks Chapter 4.
Artificial Intelligence CS370D
Input and Output and Built_in Predicates
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Presentation transcript:

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

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.

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).

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

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

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

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)

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

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

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

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’)

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.

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

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.

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.

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

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 =:= yes ?- X = 4, Y is X+1, Y=\=X. X = 4, Y = 5 ?- X+2=:=X+2. Error !!!!!

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

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

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,,, :-

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

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

TCP1211-Logic Programming Left and Right Associative 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 :- 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

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)

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).

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.

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.

TCP1211-Logic Programming Prolog – Tip No. 1

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.

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.

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

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.