Artificial Intelligence CS370D

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

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Where Syntax Meets Semantics
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.
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.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Prolog COMP 112 #
Regular Expressions, Backus-Naur Form and Reverse Polish Notation.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
Predicates and Quantifiers
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Meta-interpreters Interpreted languages like Prolog can easily treat program code as data Interpreted languages like Prolog can easily treat program code.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Chapter 4: Basic C Operators
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Significant Digits p. 940 – 941 or significant figures or sigfigs or sf.
Atomic Sentences Chapter 1 Language, Proof and Logic.
INFIX, PREFIX, & POSTFIX EXPRESSIONS Saurav Karmakar Spring 2007.
Declarative Programming Arithmetic in PROLOG Autumn 2014.
Sequences and Summations Section 2.4. Section Summary Sequences. – Examples: Geometric Progression, Arithmetic Progression Recurrence Relations – Example:
Logical and Functional Programming
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
Artificial Intelligence
Artificial Intelligence Lecture No. 23 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Cs774 (Prasad)L4ListProcessing1 List processing in Prolog
1 Artificial Intelligence CS370D Prolog programming List operations.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Review Use of Stack Introduction Stack in our life Stack Operations
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
Chapter 5 Recursion as a Problem-Solving Technique
Side Effect Operators Changing the value of variables
Expressions and Assignment
Computing Fundamentals
CS2403 Programming Languages Expressions and Assignment Statements
CSC 131: Introduction to Computer Science
Algorithms and Data Structures
PART II STACK APPLICATIONS
Tests, Backtracking, and Recursion
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Real Numbers In this class, we will consider only real numbers.
Stacks, Queues, and Deques
Artificial Intelligence CS370D
Artificial Intelligence CS370D
Answers (1,2,6,4) (1,3). Answers (1,2,6,4) (1,3)
1st-order Predicate Logic (FOL)
Function notation.
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
(Part 2) Infix, Prefix & Postfix
Chapter 4: Expression and Operator
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Lesson 5.3 What is a Function?
1st-order Predicate Logic (FOL)
Presentation transcript:

Artificial Intelligence CS370D Prolog programming Operators in Prolog.

Outline: Operators in Prolog. Operator Precedence. Pattern types. Operator examples.

Operators in Prolog: Operators provide a more convenient way of writing certain expressions in Prolog that could otherwise be difficult to read for humans. We can define the atom has as infix operators and then write Prolog facts like: peter has information. these fact are exactly equivalent to: has(ahmed,information). we can write 3 * 155 instead of *(3, 155).(inflex) Both notations are considered to be equivalent, i.e. matching works: ?- +(1000, 1) = 1000 + 1. true The objective here is to show you how you can define your own operators in Prolog.

Operator Precedence: Some operators bind stronger than others. In mathematics, for example,* binds stronger than +. The degree to which an operator is binding is called its precedence. In Prolog operator precedence are numbers (in SWI-Prolog between 0 and 1200). The arithmetic operator *, for example, has precedence 400, + has precedence 500. That is, the lower an operator’s precedence value, the stronger it is binding.

Operator Precedence: (cont) operator definitions do not specify any operation or action. Operator definitions in Prolog look like this: :- op(Precedence, pattern, Name of fact or rule).

Pattern types:

Pattern types: (cont) xfx, xfy or yfx meaning that the predicate is binary and is to be converted to an infix operator fx or fy meaning that the predicate is unary and is to be converted to an prefix operator xf or yf meaning that the predicate is unary and is to be converted to a postfix operator.

Operator examples: Prolog Program: is_female(noura). owns(noura,fido). isan_elephant(fido). :-op(150,xfy,friend_of). :-op(150,xf,is_female). :-op(150,xf,isan_elephant). :-op(150,xfy,owns). sara friend_of X:- X is_female, X owns Y, Y isan_elephant

Operator examples: (Cont) Queries: ?- sara friend_of noura. true. ?- sara friend_of X. X = noura. ?- X friend_of noura. X = sara. ?- X friend_of Y. X = sara,Y = noura. ?- is_female(X).

Operator examples: (Cont) NOTE: The rule sara friend_of X:- X is_female, X owns Y, Y isan_elephant IS EQUIVELANT of: friend_of(sara,X):- X is_female, X owns Y, Y isan_elephant but if you write the second rule and then ask the first 4th questions in the previous slide that related of this relation, you must change the questions like: ?- friend_of (sara,noura). ?- friend_of (sara,X). ?- friend_of(X,noura). ?- friend_of(X,Y).

Operator examples: (Cont) NOTE: If you using the second style of rule and then don’t change the style of questions and then ask: ?- sara friend_of noura. The SWI prolog answer: Correct to: “friend_of(sara,noura)"? Please answer 'y' or 'n'? yes true.