Chapter 2 Syntax and meaning of prolog programs

Slides:



Advertisements
Similar presentations
Prolog.
Advertisements

Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.
4. PROLOG Data Objects And PROLOG Arithmetic
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580.
Chapter 3 Math Vocabulary
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Rev.S08 MAC 1105 Module 3 System of Equations and Inequalities.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Section 6.1 Rational Expressions. OBJECTIVES A Find the numbers that make a rational expression undefined.
Chapter 5 Review Advanced Algebra 1. System of Equations and Inequalities - System of Linear Equations in Two Variables - Solutions of Linear Inequalities.
Section 2.1 Solving Equations Using Properties of Equality.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Doing math In java.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
Arithmetic Operations (L05) * Arithmetic Operations * Variables * Declaration Statement * Software Development Procedure Problem Solving Using C Dr. Ming.
Notes 2.1 Order of Operations PEMDAS If an expression has only numbers and symbols, it is a numerical expression. If it has a variable it is a variable.
Spreadsheet Calculations Formulas & Functions Computer Applications I.
Section 6.2 Solving Linear Equations Math in Our World.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Matching Matching and unification. Rules for matching terms. Examples of matching. –The most general instantiation. Computation using matching.
Rational Expressions – Equivalent Forms
Finding Proportions using Cross Multiplication
Chapter 6 JavaScript: Introduction to Scripting
ARITHMETIC IN C Operators.
BASIC ELEMENTS OF A COMPUTER PROGRAM
INSPIRING CREATIVE AND INNOVATIVE MINDS
Solving Equations with the Variable on Each Side
Solving Linear Expressions
Solving One-Step Equations
Data Types, Identifiers, and Expressions
Arithmetic operations & assignment statement
Introduction to Scripting
Exercise 24 ÷ 2 12.
Operators and Expressions
Unit 2 Programming.
Artificial Intelligence CS370D
Coding Concepts (Sub- Programs)
Building Java Programs
Solving Equations: The Addition and Multiplication Properties
Equations and Inequalities
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter Two: Syntax and Meaning of Prolog Programs
8.1.1 Solving Simple Equations
BASIC MATH.
Variables and Expressions
Spreadsheets Objective 6.02
Building Java Programs
Section 6.1 Order of Operations
Section 6.1 Order of Operations
Building Java Programs
Finding Proportions using Cross Multiplication
“Equations and Inequalities”
Section 9.3 Modular Arithmetic.
Presentation transcript:

Chapter 2 Syntax and meaning of prolog programs Part 1

Arithmetic Operations Prolog is mainly a language for symbolic computation. Some of the predefined operators can be used for basic arithmetic operations : Addition ( + ) Subtraction ( - ) Multiplication ( * ) Division ( / ) Division - integer result - ( // ) Power ( ** ) Modulation ( mod )

“ is “ Operator we can get the answers of the arithmetic questions by using variables. The left argument of the is operator is a simple object (variable and number) , the right argument is an arithmetic expression. Var_name is Expression

Arithmetic Operations Prolog 2 + 3 = 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).

Arithmetic Operations Example: ?- X = 1 + 2 . ?- X is 1 + 2 . ?- X is 5/2 , Y is 2-6 , Z is 5 mod 2 . ?- X is (14 + 16)/3 , X + 3 = Y.

Arithmetic Operations Arithmetic operations also uses comparing numerical values. The comparison operators are as follows: =:= Equal. =\= Not equal. < Less than. > Greater than. >= Greater than or equal to. =< Less than or equal to.

Arithmetic Operations Example: ?- 277*37 > 10000. ?- 1+2 = 2+1 ?- 1+2 =:= 2+1 ?- X+4 =\= 5+7

Matching The most important operation on terms is Matching. Two terms are matching when they are equivalent to each other . To request Prolog matching operation, we use ‘=‘ : Matching is a process that takes as inputs two terms and check whether they match. If the terms do not match, we say this process fails. If they match, the process succeeds. Expression1 = Expression2

Matching The general rules to decide whether two terms ( S and T) match are as follows: If S and T are constants , they match only if they are the same object. If S is a variable and T is anything , they match only if S is equivalent to an object in T.

Matching - examples ?- 14+15 = 14+15. ?- 2*4 = 4*2. ?- 5 = 2 + 3. ?- X+1 = 5+Y

Data objects data objects simple objects structures constants variables atoms numbers

Structured Data objects A structure object is a complex data types composed of a functor and a fixed number of arguments. Syntax : Functor (arg1,arg2,...) Example : create a structured object of this date : 1 / may / 2015 date (1, may, 2001)

Structured Data objects To represent any day in may (assuming Day is variable) date (Day, may, 2015) All structured objects can be presented as tree: date may Day 2015

Example (1) How to structures the following expression? (a + b) * (c - 5)

Example (1) * How to structures the following expression? (a + b) * (c - 5) Using symbols : * , + , and – as functors: *(,) *( +(a,b) ,). *(+(a,b), -(c,5) ) * - + c 5 a b

Matching - examples date(D, M, 2001) and date(D1, may, Y1) date(X, Y, Z) and point(X, Y, Z)

Matching - examples date(D, M, 2001) and date(D1, may, Y1) M= may. Y1=2001 date(D, M, 2001) and date(D1, M1, 1444) Not matched date(X, Y, Z) and point(X, Y, Z)

Matching - examples ?- date(D, M, 2001)=date(D1, may, Y1), date(D, M, 2001)=date(15, M, Y). To satisfy the first goal : date(D, M, 2001)=date(D1, may, Y1). , prolog instantiation will be: D=D1, M = may, Y1= 2001. After specifying the second goal, the instantiation become more specific as follow: D= D1, D1 = 15, M = may, Y1= Y , Y = 2001.

Matching - examples Define the shape in the drawing below: (2,5) Triangle shape has: 3 segments Each segment is declared by two points. (1,2) (3,1)

Matching - examples (2,5) (1,2) (3,1) point(1,2). point(2,5). triangle(point(1,2) , point(2,5) , point(3,1)). seg(point(1,2),point(2,5)). seg(point(2,5),point(3,1)). seg(point(1,2),point(3,1)). triangle(seg(point(1,2),point(2,5)),seg(point(2,5),point(3,1)),seg(point(1,2),point(3,1)). (3,1)

Matching - examples triangle triangle point point X point A point 1 2 3 1 2 Z B Y The result instantiation is: A = point(2,Y), X = point(1,2), Z =3, B=1.

Matching - examples Declare vertical and horizontal relations vertical (seg(point(X,_), point(X,_))). horizontal (seg(point(_,Y), point(_,Y))).

Matching – examples (cont.) Is the segment (1,1), (1,4) is vertical? Are there any vertical segment that start at point(2,3)? Is there a segment that is both vertical and horizontal?

Example (3) (cont.) ?- vertical (seg(point(1,1), point(1,4))). Yes Is the segment (1,1), (1,4) is vertical? ?- vertical (seg(point(1,1), point(1,4))). Yes Are there any vertical segment that start at point(2,3)? -? Vertical(seg(point(2,3),P). P= point(2,Y) Is there a segment that is both vertical and horizontal? -? vertical (S), horizontal (S) S= segment(point(X,Y),point(X,Y).

Class exercise (1) Is the segment (1,1), (2,Y) vertical? Is the segment (1,1), (2,Y) horizontal

Class exercise (1) (cont.) Is the segment (1,1), (2,Y) vertical? ?- vertical (seg(point(1,1), point(2,Y))). false Is the segment (1,1), (2,Y) horizontal ? - horizontal (seg(point(1,1), point(2,Y))). Y=1

Meaning of Prolog programs Declarative (Goal true?) Procedural (How)

Meaning of Prolog programs Consider the clause: P :- Q, R. Declarative readings: P is true if Q and R is true From Q and R follows P Procedural readings: To solve problem P, first solve the subproblem Q, and then the subproblem R. To satisfy P, first satisfy Q and then R.

Meaning of Prolog programs Consider the clause: P :- Q ; R. Same as the following two clauses together: P :- Q . P :- R . The comma binds stronger than the semicolon. P :- Q , R ; S , T , U. is understood as: P :- ( Q , R ) ; ( S , T , U ) . and means the same as the clauses: P :- Q , R . P :- S , T , U .

Class Exercise Define the relation: Max( X , Y , Max ). So that Max is the greater of two numbers X and Y.

Class Exercise max(X,Y,X):- X >= Y. max(X,Y,Y):- Y > X. Define the relation: Max( X , Y , Max ). So that Max is the greater of two numbers X and Y. max(X,Y,X):- X >= Y. max(X,Y,Y):- Y > X.