Defining Binary & Unary Operators

Similar documents
Defining Binary & Unary Operators DO-1

Defining! Binary & Unary! Operators!

What are Operators? - 1. Operators. What are Operators? - 2. Properties. » Position» Precedence class» associativity. » x + y * z. » +(x, *(y, z)).

Operators (2A) Young Won Lim 10/2/13

Operators (2A) Young Won Lim 10/5/13

Lecture 9: A closer look at terms

More Planning and Prolog Operators

Input & Output. York University Department of Computer Science and Engineering. York University- CSE V. Movahedi 08_IO

Lecture 9. Exercises. Theory. Solutions to exercises LPN 8.1 & 8.2. Patrick Blackburn, Johan Bos & Kristina Striegnitz

Wan Hussain Wan Ishak

Lecture Chapter 6 Recursion as a Problem Solving Technique

This book is licensed under a Creative Commons Attribution 3.0 License

THE DESIGN AND IMPLEMENTATION OF A PROLOG PARSER USING JAVACC Pankaj Gupta, B.S. Thesis Prepared for the Degree of MASTER OF SCIENCE

CSI33 Data Structures

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Prefix/Infix/Postfix Notation

Linear Data Structure

Logic Programming (PLP 11.3) Prolog: Arithmetic, Equalities, Operators, I/O, Natural Language Parsing

More Assigned Reading and Exercises on Syntax (for Exam 2)

CSI33 Data Structures

Backtracking. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. Chapter 5

Operators. Java operators are classified into three categories:

TREES. Trees - Introduction

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

A set with only one member is called a SINGLETON. A set with no members is called the EMPTY SET or 2 N

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Logic Programming (PLP 11) Prolog: Arithmetic, Equalities, Operators, I/O, Natural Language Parsing

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Formal Languages and Automata Theory, SS Project (due Week 14)

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Backtracking. Module 6. The Eight Queens Problem. CS 147 Sam Houston State University Dr. McGuire. Backtracking. Recursion Revisited

Chapter 2 Working with Data Types and Operators

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CSE 413 Final Exam. December 13, 2012

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

LECTURE 17. Expressions and Assignment

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Wan Hussain Wan Ishak

Stacks Calculator Application. Alexandra Stefan

The Arithmetic Operators

Principles of Programming Languages COMP251: Syntax and Grammars

Expression Evaluation and Control Flow. Outline

CS 206 Introduction to Computer Science II

An Introduction to Trees

Infix to Postfix Conversion

Logic Programming. April 3, 2017

Prolog Introduction. Gunnar Gotshalks PI-1

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

PROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012.

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Problem Solving and Search

Department of Computer Science

Operators & Expressions

CS W3134: Data Structures in Java

Operators And Expressions

12 Abstract Data Types

CSE au Final Exam Sample Solution

Programming in C++ 5. Integral data types

Information Science 1

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

Stacks, Queues and Hierarchical Collections

Logic Programming Prolog: Arithmetic, Equalities, Operators, I/O. (PLP 11) Databases: assert, retract. (CTM 9.6)

SMPL - A Simplified Modeling Language for Mathematical Programming

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Operators in C. Staff Incharge: S.Sasirekha

Programming with Java

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

ITEC2620 Introduction to Data Structures

Full file at

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Unit 1: Understanding Production Systems

Grade 6 Math Circles Winter February 3/4 Sets. Some of These Things Are Not Like the Others

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Lecture 12 ADTs and Stacks

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Stack and Its Implementation

A programming language requires two major definitions A simple one pass compiler

Warmup Problem. Translate the following sentence from English into Propositional Logic. I want to eat ice cream even though I am on a diet.

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

[a,b,c,d] 15/9/2013. [a,b,c,d] List. List. List. List is one of the most important Prolog data structures.

CSE 214 Computer Science II Stack

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

PA3 Design Specification

Lambda Calculus LC-1

Constants and Variables

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Postfix (and prefix) notation

COMP 250 Fall binary trees Oct. 27, 2017

ARM Evaluation System

COMP 9416, session 1, 2006 p. 1. Datalog. COMP 9416, session 1, 2006

Data Structures and Algorithms

Transcription:

Defining Binary & Unary Operators DO-1

English-French Dictionary Can use compound terms to represent a dictionary > list is a structure that contains an entry followed by the rest of the list > For example list ( entry ( book, livre ), list ( entry ( man, homme ), list ( entry ( apple, pomme ), empty ) ) ) Illustrates how compound terms could be used DO-2

English-French Dictionary 2 Define a custom member function for the list structure member ( X, list ( X, _ ) ). member ( X, list ( _, L ) ) :- member ( X, L ). DO-3

English-French Dictionary 3 Here is a predicate that defines the correspondence between English and French words. translate ( English, French ) :- member ( entry ( English, French ), list ( entry ( book, livre ), list ( entry ( man, homme ), list ( entry ( apple, pomme ), empty ) ) ) ) DO-4

English-French Dictionary 4 We could change the rule to use a dictionary that holds the list structure > It is easier to understand the rule trans1 ( English, French ) :- dictionary ( Dictionary ), member ( entry ( English, French), Dictionary ) > where we have a fact defining the dictionary. It is easier to change the dictionary and to use it in other contexts dictionary ( list ( entry ( book, livre ), list ( entry ( man, homme ), list ( entry ( apple, pomme ), empty ) ) ) ). DO-5

English-French Using Standard Lists We could use the standard list structure. > The standard member predicate member ( X, [ X _ ] ). member ( X, [ _ R ] ) :- member ( X, R ). > The translation predicate trans2 ( English, French ) :- member ( entry ( English, French ), [ entry (book, livre ), entry ( man, homme ), entry ( apple, pomme ) ] ). DO-6

Use an infix member function The previous definition is not a natural way of representing the member function A more "natural" use of member is as an infix operator, as in the following > Use the letter e to represent the mathematical symbol belongs to ( ) trans3 ( English, French ) :- entry(english,french) e [ entry (book, livre), entry ( man, homme ), entry ( apple, pomme) ]. DO-7

Use an infix member function The infix operator e can be defined as follows :- op ( 500, xfy, [ e ] ). > Later slides describe the meaning of the op predicate e is a new operator (predicate) so we must create rules that define what it means > Since e is defined to be infix its rules use infix syntax > Note the similarity with the definition of the member predicate X e [ X _ ]. X e [ _ L ] :- X e L. DO-8

Use an infix member function 3 We can chose of the name of the operator :- op( 500, xfy, [ belongs_to ] ). X belongs_to [ X _ ]. X belongs_to [ _ L ] :- X belongs_to L. trans4 ( English, French ) :- entry ( English, French ) belongs_to [ entry ( book, livre ), entry ( man, homme ), entry ( apple, pomme ) ]. DO-9

Bird Mammal example Define some properties of animals > Use syntax that is more similar to natural language :- op( 100, xfx, [ has, isa, flies ] ). Animal has hair :- Animal isa mammal. Animal has feathers :- Animal isa bird. owl isa bird. cat isa mammal. dog isa mammal. DO-10

Example with mulitple precedence Plays and "and" are at different precedence levels. Define :- op ( 300, xfx, plays ). :- op ( 200, xfy, and ). Example use Term1 = jimmy plays football and squash. Term2 = susan plays tennis and basketball and volleyball. DO-11

Example with mulitple precedence 2 What is the internal stucture when using operators as in the following? Term1 = jimmy plays football and squash. Term2 = susan plays tennis and basketball and volleyball. Recall that everything within Prolog is represented with compound terms, so we have... Term1 = plays ( jimmy, and ( football, squash) ) Term2 = plays ( susan, and ( tennis, and ( basketball, volleyball ) ) ) DO-12

Example with mulitple precedence 3 DeMorgan's law make predicate syntax look more similar to standard mathematics :- op( 800, xfx, <==> ). :- op( 700, xfy, v ). :- op( 600, xfy, & ). :- op( 500, fy, ~ ). Consider representing the following ~ ( A & B ) <==> ~A v ~B. Uses the above In standard Prolog, this could be represented as equivalence ( not ( and ( A, B ) ), or ( not ( A ), not ( B ) ) ). > or, directly use the internal form '<==>' ( '~' ( '&' ( A, B ) ), 'v ' ( '~' ( A ), '~' ( B ) ) ). DO-13

Why have operators? Introduce operators to improve the readbility of programs» Can be infix, prefix or postfix Operator definitions do not define any action, they only introduce new notation» Operators are functors that hold together the components of compound terms or structures A programmer can define their own operators» with their own precedence and associativity» programmer defined operators can be merged in precedence and associativity with the Prolog builtin operators DO-14

op Predicate Define one or more operators with a given precedence, associativity op ( precedence, associativity, symbol or symbol list ) Pages 107..108 give a listing of the predicates defining the "standard" operators in Prolog DO-15

op Precedence component Precedence» between 0 and 1200 the precedence class» lower class numbers have higher priority» higher priority implies do first» Example 3 + 4 * 5 = 3 + ( 4 * 5 )» * (precedence class 400) has lower number than + (precedence class 500) so times is done first» Can always use () to force the order of using operators > Useful when you do not know relative precedence or to make it clear to the reader DO-16

Expression Precedence Class Precedence class of base operand is 0. Precedence class of expression with operator, oper, is the precedence class of oper DO-17

op Associativity component Associativity» Defines which operands belong to which operator when several operators are used in sequence» For example in the following A oper B > is oper a unary operator with operand A is oper a unary operator with operand B is oper a binary operator with operands A and B Can define oper as unary operator with... op ( 100, fy, oper ). -- unary prefix op ( 100, fx, oper ). -- unary prefix op ( 100, xf, oper ). -- unary postfix op ( 100, yf, oper ). -- unary postfix DO-18

Unary prefix associativity f y f x oper oper a. -- legal syntax > oper a has equal precedence class with oper > y says operand of oper can have lower or equal precedence class oper oper a. -- illegal syntax > oper a has equal precedence class with oper > x says operand of oper must have lower precedence class > must use () as follows oper ( oper a ). DO-19

Unary postfix associativity y f x f a oper oper. -- legal syntax > a oper has equal precedence class with oper > y says operand of oper can have lower or equal class a oper oper. -- illegal syntax > a oper has equal precedence class than oper > x says operand of oper must have lower precedence class > must use () ( a oper ) oper. DO-20

op Associativity component 2 Given A oper B Can define oper as a binary operator with... op ( 100, xfy, oper ). -- right associative op ( 100, yfx, oper ). -- left associative op ( 100, xfx, oper ). -- evaluate both operands first op ( 100, yfy, oper ). -- not defined, ambiguous DO-21

Define Test Result Right associative operator :- op ( 100, xfy, op1 ). > C becomes the full structure, L shows the substructure C = 1 op1 2 op1 3 op1 4, C =.. L. C = 1 op1 2 op1 3 op1 4 L = [ op1, 1, 2 op1 3 op1 4 ] > Left most op1 is evaluated last > Apply recursively DO-22

Define Test Result Left associative operator :- op ( 200, yfx, op2 ). > C becomes the full structure, L shows the substructure C = 1 op2 2 op2 3 op2 4, C =.. L. C = 1 op2 2 op2 3 op2 4 L = [ op2, 1 op2 2 op2 3, 4 ] > Right most op2 is evaluated last > Apply recursively DO-23

Define Test Result Evaluate both operands first :- op ( 300, xfx, op3 ). C = 1 op3 2 op3 3 op3 4, C =.. L. C = 1 op3 2 «Syntax Error - check operator precedences» op3 3 op3 4, C =.. L. > Error because the middle op3 expects its operands to its left and right to have lower precedence class but they have equal precedence class DO-24

Define Evaluate both operands first 2 :- op ( 300, xfx, op3 ). Test with different operators to left and right of op3 Result C = 1 op1 2 op3 3 op2 4, C =.. L. C = 1 op1 2 op3 3 op2 4 L = [ op3, 1 op1 2, 3 op2 4 ] > op1 and op2 are done first (higher priority, lower precedence class) > op3 is done last DO-25