Chapter 2 Syntax and meaning of prolog programs Part 1.

Slides:



Advertisements
Similar presentations
Mod arithmetic.
Advertisements

1 Part 1 The Prolog Language Chapter 2 Syntax and Meaning of Prolog programs.
Prolog.
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
Fractions and Rational Expressions
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
Solving Linear Equations
Chapter 3 Planning Your Solution
Chapter 3 Math Vocabulary
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
8/8/ Inequalities. 8/8/ Bumper Cars You must be at least 130cm tall to ride the bumper cars. This can be represented by the inequality.
Rev.S08 MAC 1105 Module 3 System of Equations and Inequalities.
Spreadsheets Objective 6.02
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.
Introduction to Computational Linguistics Programming I.
2440: 211 Interactive Web Programming Expressions & Operators.
HAWKES LEARNING SYSTEMS Students Matter. Success Counts. Copyright © 2013 by Hawkes Learning Systems/Quant Systems, Inc. All rights reserved. Section 2.3.
Section 6.1 Rational Expressions. OBJECTIVES A Find the numbers that make a rational expression undefined.
MM218 - Unit 7 Seminar Topics
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
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 Term- number or product of a number and a variable  Constant- term with no variable  Coefficient- number that multiplies to a variable  Like Terms-
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.
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.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
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
Chapter 8 Rational Expressions.
BASIC ELEMENTS OF A COMPUTER PROGRAM
INSPIRING CREATIVE AND INNOVATIVE MINDS
Solving Linear Expressions
Solving One-Step Equations
Arithmetic operations & assignment statement
Solving 1-Step Integer Equations
Operators and Expressions
Artificial Intelligence CS370D
Inequalities 12/3/2018.
6.5 Inequalities 12/3/2018.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter Two: Syntax and Meaning of Prolog Programs
8.1.1 Solving Simple Equations
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
Variables and Expressions
Chapter 2 Syntax and meaning of prolog programs
Spreadsheets Objective 6.02
Do Now Evaluate 9h + h if h = 2.1 Evaluate 2 (4 + g) 2 If g = 6.
Spreadsheets Objective 6.02
“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 = 5 3 x 4 = 12 5 – 3 = 2 3 – 5 = / 2 = 2 1 is the remainder when 7 is divided by 2 ? - 5 is 2+3. ? - 12 is 3  4. ? - 2 is ? is ? - 2 is 4/2. ? - 1 is mod(7,2). ArithmeticProlog

Arithmetic Operations Example: ? - X = ? - X is ? - X is 5/2, Y is 2 - 6, Z is 5 mod 2. ? - X is ( )/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 > ? = 2+1 ? =:= 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 ? = ? - 2*4 = 4*2. ? - 5 = ? - X+1 = 5+Y

Data objects data objects variables constants structuressimple objects numbersatoms

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 mayDay2015

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

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

Matching - examples ▫ date(D, M, 2001) and date(D1, may, Y1) ▫ date(D, M, 2001) and date(D1, M1, 1444) ▫ 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= After specifying the second goal, the instantiation become more specific as follow: D= D1, D1 = 15, M = may, Y1= Y, Y = 2001.

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

Matching - examples point(1,2). point(2,5). point(3,1). 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)). (2,5) (3,1) (1,2)

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

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

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?

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))). No ▫ Is the segment (1,1), (2,Y) horizontal ? - horizontal (seg(point(1,1), point(2,Y))). Y=1

Meaning of Prolog programs 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.