A closer look to prolog San Nabaty 23-04-2018.

Slides:



Advertisements
Similar presentations
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Advertisements

1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
4. PROLOG Data Objects And PROLOG Arithmetic
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
JavaScript, Third Edition
Introduction to C Programming
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
Algebra Problems… Solutions
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Formal Models of Computation Part II The Logic Model
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to Programming with RAPTOR
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
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.7 – Day 1 Inequalities. 2 Objectives ► Solving Linear Inequalities ► Solving Nonlinear Inequalities ► Absolute Value Inequalities ► Modeling with Inequalities.
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.
Section 16.5, 16.6 plus other references
Chapter 2 Sets and Functions.
Chapter 6 JavaScript: Introduction to Scripting
ARITHMETIC IN C Operators.
numerical coefficient
COMP 170 – Introduction to Object Oriented Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
Debugging and Random Numbers
Variables and Primative Types
Chapter 7 Objectives Define basic terms in algebra: integer, number statement, expression, and coefficient Learn the relationships between positive and.
Chapter 2 - Introduction to C Programming
Tests, Backtracking, and Recursion
Copyright 2012, 2008, 2004, 2000 Pearson Education, Inc.
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Lesson 4 Using Basic Formulas
Learning Resource Services
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Naming sequences Name these sequences: 2, 4, 6, 8, 10, . . .
Artificial Intelligence CS370D
Place Value, Names for Numbers, and Reading Tables
Chapter 2 - Introduction to C Programming

Key Words and Introduction to Expressions
Chapter 2 - Introduction to C Programming
Graphs, Linear Equations, and Functions
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Functions.
Starter WWK Product notation – a way of simplifying repeated sums, e.g., a + a  2 ‘lots’ of a  2 x a  2a Variable – a symbol that represents an unknown.
Solving Linear Equations
Copyright © Cengage Learning. All rights reserved.
Programming Techniques
Chapter 3: Selection Structures: Making Decisions
Chapter 2 Syntax and meaning of prolog programs
Programming Languages 2nd edition Tucker and Noonan
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

A closer look to prolog San Nabaty 23-04-2018

Contents Syntax Characters Operators Equality and Unification Arithmetic Summary of Satisfying Goals

syntax The syntax of a language describes how we are allowed to fit words together. In English, the syntax of the sentence "I see a zebra" is correct, but the syntax of "zebra see I a" is not correct. Prolog programs are built from terms. A term is either a constant, a variable, or a structure. We saw each of these terms in the previous chapter, but we did not know them by these names. Each term is written as a sequence of characters. Characters are divided into four categories as follows:   A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + - * / \ ~ A < > : . ? @ # $ 8. The first row consists of upper-case letters. The second row consists of lower-case letters. The third row consists of digits. The fourth row consists of sign characters.

Constants Constants names specific objects or specific relationships. There are two kinds of constants: atoms, and numbers. Examples of atoms : likes , mary , john , book , owns. The following is example of atom: a void = 'george-smith' --> george_smith ieh2304. The following is not example of atom: 2304ieh george-smith Void _alpha. The special symbols that Prolog uses to denote questions "?-" and rules ":-" are also atoms. The "e" notation is used to denote a power of 10. So, for example, the number -2.67e2 is -2.67 x 102 or just -267; 6.02e-23 is 6.02x 10"2 3 .  

Variables The second kind of term used in Prolog is the variable. Variables look like atoms, except they have names beginning with a capital letter or an underline sign "_". A variable should be thought of as standing for some object that we are unable or unwilling to name at the time we write the program. In the example Prolog clauses we have seen so far, we have used variables with names such as X, Y, and Z. However, the names can be as long as you like, for example:   Answer Input Gross_Pay _3_blind_mice A_very_long_variable_name Sometimes one needs to use a variable, but its name will never be used. For example, if we want to find out if anyone likes John, but we do not need to know just who it is, we can use the anonymous variable. The anonymous variable is written as a single underline character. Our example is written in Prolog as: ?- likes(_, john).  

Structures Structures are useful when there is a common kind of object, of which many may exist. Books, for example. In Chapter 1 we discussed the fact owns(john, book) .  to denote that John owns some particular book. If we later said  owns(mary, book). this means that Mary owns the same object that John owns,because it has the same name. There is no other way of telling objects apart, except by their name. We could say: owns(john,wuthering_heights) owns(mary, moby_dick) Structures help to organize the data in a program, because they permit a group of related information to be treated as a single object (a library card) instead of as separate entities. The way that you decompose data into components depends on what problem you want to solve. Inside the owns fact we have a structure by the name of book, which has two compo-nents, a title and an author. Since the book structure appears inside the fact as one of the fact's arguments, it is acting as an object, taking part in a relationship. If we like, we can also have another structure for the author's name, because there were three Bronte writers we wish to distinguish: owns(john, book(wuthering_heights, author(emily, bronte))).  

Structures Structures may participate in the process of question-answering using variables. For example, we may ask if John owns any book by any of the Bronte sisters: ?- owns(john, book(X, author(Y, bronte))) If this is true, X will then be instantiated to the title that was found, and Y will be instantiated to the first name of the author. Or, we may not need to use the variables, so we can use anonymous ones: ?- owns(john, book(_, author(_, bronte)))  We could improve the book structure by adding another argument indicating which copy the book is. For example, a third argument, where we would insert an integer, would provide a way of uniquely identifying a book: owns(john, book(ulysses, author(james, joyce), 3129)).   which we could use to represent John owns the 3,129th copy of Ulysses, by James Joyce.  

Characters Prolog recognizes two kinds of characters: printing characters and non-printing characters. Printing characters cause a symbol to appear on your computer terminal's display. Non-printing characters do not cause a symbol to appear, but cause an action to be carried out. Such actions include printing a blank space, beginning new lines of text, or perhaps making a beeping sound. The following are all the printing characters that can be used : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z   0 1 2 3 4 5 6 7 8 9   ! " # $ % & ' ( ) = - ~ A | \ { } [ ] _ ' @ + ; * : < > , . ? / Some of the characters have special meanings. For example, the round brackets are used to enclose the components of a structure. However, we shall see in later chapters that all the characters may be treated as information by Prolog programs. Characters may be printed, read from the keyboard, compared, and take part in arithmetic operations.  

Operators When we write the arithmetic expression "x+y*z", we call the "plus" sign and the "multiply" sign operators. If we had to write the arithmetic expression "x+y*z" in the normal way for structures, it would look like this: +(x,*(y,z)), and this would be a legal Prolog term. The operators are sometimes easier to use, however, because we have grown accustomed to using them in arithmetic expressions ever since our schooldays. Also, the structure form requires that round brackets be placed around the factor's components, which may be awkward at times. The precedence of an operator is used to indicate which operation is carried out first. Each operator that is used in Prolog has a precedence class associated with it. The precedence class is an integer that is associated with an operator. This means that expressions like "8/4/4" are read as "(8/4)/4". Also, "5+8/2/2" is read as "5+((8/2)/2) “.  

Equality and Unification One noteworthy predicate is equality, which is an infix operator written as "=". When an attempt is made to satisfy the goal ?- X = Y (pronounced "X equals Y"), Prolog attempts to unify X and Y, and the goal succeeds if they unify. We can think of this act as trying to make X and Y equal. The equality predicate is built-in, which means that it is already defined in the Prolog system.   Within a use of some clause, X always equals X, and we exploit this property when defining the equality predicate in the way shown. Given a goal of the form X=Y, where X and Y are any two terms which are permitted to contain instantiated variables, the rules for deciding whether X and Y are equal are as follows: If X is an instantiated variable, and if Y is instantiated to any term, then X and Y are equal. Also, X will become instantiated to whatever Y is. For example, the following question succeeds, causing X to be instantiated to the structure rides(student, bicycle) ?- rides(student, bicycle) = X

Equality and Unification Two structures are equal if they have the same factor and number of components, and all the corresponding components are equal. For example, the following goal succeeds, and causes X to be instantiated to bicycle: rides(student, bicycle) = rides(student, X)  Look closely: this is not a question about rides; it is a question about =. Structures can be "nested" one inside another to any depth. If such nested structures are tested for equality, the test may take more time to carry out, because there is more structure to test.  Exercise : Say whether the following goals would succeed, and which variables, if any, would be instantiated to what values: pilots(A, London) = pilots(London, pans) point(X, Y, Z) = point(Xl, Yl, Zl) letter(C) = word(letter) noun(alpha) = alpha  ‘student’= student  f(X, X) = f(a, b) f(X, a(b, c)) = f(Z, a(Z, c))

Arithmetic In Prolog we are allowed to write arithmetic as infix operators: X =:=Y X and Y stand for the same number X =\=Y X and Y stand for different numbers  X < Y X is less than Y  X > Y X is greater than Y  X =< Y X is less than or equal to Y  X >= Y X is greater than or equal to Y   Note that the "less than or equal to" symbol is not written as "<=" as in many pro-gramming languages. As these comparison operators are predicates, one might think it possible to write a Prolog fact as follows : 2 > 3 in order to assert that 2 is actually greater than 3. A fact like this one is perfectly well-formed in Prolog, However, Prolog will not allow further facts to be added to predicates that are "built in" to Prolog. This prevents you from changing the meaning of built-in predicates in unexpected ways.

Arithmetic Consider the following database about the population and area of various countries We will use the predicate pop to represent the relationship between a country and its population.   pop(usa, 203). pop(india, 548). pop(china, 800). pop(brazil, 108). area(usa, 3). area(india, 1). area(china, 4). area(brazil, 3).

Arithmetic Now to find the population density of a country, we must use the rule that the density is the population divided by the area. This can be represented as the predicate density, where the goal density(X,Y) succeeds for country X having Y as the population density of that country. A Prolog rule for this is: density(X, Y) :- pop(X, P), area(X, A), Y is P / A. The rule is read as follows: The population density of country X is Y, if: The population of X is P, and The area of X is A, and Y is calculated by dividing P by A.

Summary of Satisfying Goals Note// We need to use the "is" predicate any time we require to evaluate an arithmetic expression. Getting back to the population density example, it is not hard now to see that typical questions and their answers are: ?- density(china, X).  X=200  yes -----------------------------------------  ?- density(turkey, X).  no  ----------------------------------------- In the first question, the X=200 is Prolog's answer, meaning 200 people per square mile. The second question failed, because the population of Turkey could not be found in our example database.

Summary of Satisfying Goals Prolog attempts to satisfy the goals in a conjunction, whether they appear in a rule body or in a question, in the order they are written (left to right). This means that Prolog will not attempt to satisfy a goal until its neighbor on the left has been satisfied. And, when it has been satisfied, Prolog will attempt to satisfy its neighbor on the right. Consider the following simple program about family relations:   female(mary). parent(C, M, F) :- mother(C, M), father(C, F). mother(john, ann). mother(mary, ann).  father(mary, fred). father(john, fred). Let us look at the sequence of events that leads to answering the question:   ?- female(mary), parent(mary, M, F), parent(john, M, F). This question is to find whether mary is a sister of john.

Thank you for your attention