Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

Mod arithmetic.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
Part 1 The Prolog Language Chapter 3 Lists, Operators, Arithmetic
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Logic Programming Lecture 9: Constraint logic programming.
Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
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.
4. PROLOG Data Objects And PROLOG Arithmetic
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.

(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
LING 388: Language and Computers Sandiway Fong Lecture 4.
CC112 Structured Programming Lecture 4 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Lecture 9: Constraint logic programming
Section 4.3 – Riemann Sums and Definite Integrals
Precise definition of limits The phrases “x is close to a” and “f(x) gets closer and closer to L” are vague. since f(x) can be arbitrarily close to 5 as.
Exponent Rules and Dividing Polynomials Divide exponential forms with the same base. 2.Divide numbers in scientific notation. 3. Divide monomials.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
Declarative Programming Arithmetic in PROLOG Autumn 2014.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
© 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 Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Artificial Intelligence
Doing math In java.
Cs774 (Prasad)L4ListProcessing1 List processing in Prolog
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
Scientific Notation What is scientific Notation? Scientific notation is a way of expressing really big numbers or really small numbers. It.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Sullivan PreCalculus Section 3.5 Solving Polynomial and Rational Inequalities Objectives Solve Polynomial Inequalities Solve Rational Inequalities.
CSE 220 – C Programming Expressions.
Expressions and Assignment
Arithmetic operations & assignment statement
Assignment statement and Arithmetic operation 2
Arithmetic Operator Operation Example + addition x + y
Artificial Intelligence CS370D
Lecture 3 Expressions Richard Gesick.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
(Part 2) Infix, Prefix & Postfix
Chapter 2 Syntax and meaning of prolog programs
Presentation transcript:

Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Arithmetic in Prolog Prolog provides a number of basic arithmetic tools Integer and real numbers = 5 3 x 4 = 12 5 – 3 = 2 3 – 5 = -2 4  2 = 2 1 is the remainder when 7 is divided by 2 ?- 5 is 2+3. ?- 12 is 3  4. ?- 2 is 5-3. ?- -2 is 3-5. ?- 2 is 4/2. ?- 1 is mod(7,2). ArithmeticProlog

Example queries ?- 10 is 5+5. yes ?- 4 is 2+3. no ?- X is 3  4. X=12 yes ?- R is mod(7,2). R=1 yes

Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is (X+3)  2.

Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is (X+3)  2. ?- addThreeAndDouble(1,X). X=8 yes ?- addThreeAndDouble(2,X). X=10 yes

A closer look It is important to know that +, -, / and  do not carry out any arithmetic Expressions such as 3+2, 4-7, 5/5 are ordinary Prolog terms – Functor: +, -, /,  – Arity: 2 – Arguments: integers

A closer look ?- X =

A closer look ?- X = X = 3+2 yes ?-

A closer look ?- X = X = 3+2 yes ? = X.

A closer look ?- X = X = 3+2 yes ? = X. X = 3+2 yes ?-

The is/2 predicate To force Prolog to actually evaluate arithmetic expressions, we have to use is just as we did in the other examples This is an instruction for Prolog to carry out calculations Because this is not an ordinary Prolog predicate, there are some restrictions

The is/2 predicate ?- X is

The is/2 predicate ?- X is X = 5 yes ?-

The is/2 predicate ?- X is X = 5 yes ? is X.

The is/2 predicate ?- X is X = 5 yes ? is X. ERROR: is/2: Arguments are not sufficiently instantiated ?-

The is/2 predicate ?- X is X = 5 yes ? is X. ERROR: is/2: Arguments are not sufficiently instantiated ?- Result is

The is/2 predicate ?- X is X = 5 yes ? is X. ERROR: is/2: Arguments are not sufficiently instantiated ?- Result is Result = 10 yes ?-

Restrictions on use of is/2 We are free to use variables on the right hand side of the is predicate But when Prolog actually carries out the evaluation, the variables must be instantiated with a variable-free Prolog term This Prolog term must be an arithmetic expression

Notation Two final remarks on arithmetic expressions – 3+2, 4/2, 4-5 are just ordinary Prolog terms in a user-friendly notation: 3+2 is really +(3,2) and so on. – Also the is predicate is a two-place Prolog predicate

Notation Two final remarks on arithmetic expressions – 3+2, 4/2, 4-5 are just ordinary Prolog terms in a user-friendly notation: 3+2 is really +(3,2) and so on. – Also the is predicate is a two-place Prolog predicate ?- is(X,+(3,2)). X = 5 yes

Comparing terms: ==/2 Prolog contains an important predicate for comparing terms This is the identity predicate ==/2 The identity predicate ==/2 does not instantiate variables, that is, it behaves differently from =/2

Comparing terms: ==/2 Prolog contains an important predicate for comparing terms This is the identity predicate ==/2 The identity predicate ==/2 does not instantiate variables, that is, it behaves differently from =/2 ?- a==a. yes ?- a==b. no ?- a=='a'. yes ?- a==X. X = _443 no

Comparing variables Two different uninstantiated variables are not identical terms Variables instantiated with a term T are identical to T

Comparing variables Two different uninstantiated variables are not identical terms Variables instantiated with a term T are identical to T ?- X==X. X = _443 yes ?- Y==X. Y = _442 X = _443 no ?- a=U, a==U. U = _443 yes

Comparing terms: \==/2 The predicate \==/2 is defined so that it succeeds in precisely those cases where ==/2 fails In other words, it succeeds whenever two terms are not identical, and fails otherwise

Comparing terms: \==/2 The predicate \==/2 is defined so that it succeeds in precisely those cases where ==/2 fails In other words, it succeeds whenever two terms are not identical, and fails otherwise ?- a \== a. no ?- a \== b. yes ?- a \== 'a'. no ?- a \== X. X = _443 yes

Arithmetic terms +, -,, etc are functors and expressions such as 2+3 are actually ordinary complex terms The term 2+3 is identical to the term +(2,3)

Arithmetic terms +, -,, etc are functors and expressions such as 2+3 are actually ordinary complex terms The term 2+3 is identical to the term +(2,3) ?- 2+3 == +(2,3). yes ?- -(2,3) == 2-3. yes ?- (4<2) == <(4,2). yes

Summary of comparison predicates Negation of arithmetic equality predicate=\= Arithmetic equality predicate=:= Negation of identity predicate\== Identity predicate== Negation of unification predicate\= Unification predicate=

Precedence Every operator has a certain precedence to work out ambiguous expressions For instance, does 2+3*3 mean 2+(3*3), or (2+3)*3? Because the precedence of + is greater than that of *, Prolog chooses + to be the main functor of 2+3*3

Associativity Prolog uses associativity to disambiguate operators with the same precedence value Example: Does this mean (2+3)+4 or 2+(3+4)? – Left associative – Right associative

Types of operators in Prolog yfx left-associative, infix xfy right-associative, infix xfx non-associative, infix fx non-associative, prefix fy right-associative, prefix xf non-associative, postfix yf left-associative, postfix

Operators in SWI Prolog

Summary of this lecture In this lecture we have – Arithmetic operators – Comparing operators – Precedence and Associativity