4. PROLOG Data Objects And PROLOG Arithmetic

Slides:



Advertisements
Similar presentations
Prolog.
Advertisements

Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
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).
Types and Arithmetic Operators
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
String Escape Sequences
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Course Content • Introduction to Prolog and Logic Programming.
Objectives You should be able to describe: Data Types
DEDUCTIVE DATABASE.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Formal Models of Computation Part II The Logic Model
Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CPS120: Introduction to Computer Science
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CPS120: Introduction to Computer Science Variables and Constants.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
For Friday No reading Prolog Handout 2. Homework.
Chapter 2 Syntax and meaning of prolog programs Part 1.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Section 16.5, 16.6 plus other references
Expressions and Assignment
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Fundamentals of PL/SQL part 1 (Basics)
ITEC113 Algorithms and Programming Techniques
Arithmetic operations & assignment statement
Debugging and Random Numbers
Multiple variables can be created in one declaration
Variables and Primative Types
Building Java Programs Chapter 2
Numbers.
Building Java Programs
Declarative Computation Model Single assignment store (VRH 2
Chapter 2 Syntax and meaning of prolog programs
The Data Element.
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
The Data Element.
Presentation transcript:

4. PROLOG Data Objects And PROLOG Arithmetic Syntax of simple objects. Anonymous variables. PROLOG arithmetic.

PROLOG Data Objects Programs process data. PROLOG data objects : Terms structures simple objects literal constants variables atoms numbers

PROLOG Data Objects II PROLOG data objects are called terms. PROLOG distinguishes between terms according to their syntactic structure. Structures look different from simple objects. PROLOG is a typeless language. All it checks is that arithmetic operations are done with numbers and I/O is done with ASCII characters. Checks are done at run time. PROLOG fans claim that typelessness gives great flexibility. Personally, I think it’s a pain in the neck.

Atoms Atoms : non-numeric literal constants. Strings containing combinations of lower case letters upper case letters digits special characters like +, -, _, *, >, < etc. Three types of atom : alphanumerics, special character strings and quoted character strings. Alphanumerics : Similar to C++ or Java identifiers. Strings of letters, digits and _. Must start with a lower case letter. Relation names must always be alphanumeric atoms. e.g. octavius, constantine1, fred_bloggs.

Atoms II Special character strings : Strings of the allowed special characters. May not contain letters, digits or _. e.g. >==>, ----, <<<>>>. Quoted character strings : Strings of any character enclosed between ‘. e.g. ‘Fred Bloggs’, ‘3 pounds of spuds.’. Very useful for atoms beginning with upper case letters. emperor(‘Octavius’).

Numbers PROLOG allows both integer and floating point types. Integer : Floats : e.g. 0.23, 1.0234, -12.23. Floats using exponential notation : e.g. 12.3e34, -11.2e-45, 1.0e45. PROLOG suffers from the usual problems with floating point rounding errors. PROLOG is terrible at numeric computation. See below.

Variables Strings of letters, digits and _. Must start with an upper case letter or with _. Similar to alphanumerics. e.g. X, Variable, fred_bloggs, _23. PROLOG variables are logical variables, not store variables. Given values by instantiation during matching not by assignment . Sometimes we don’t want to give a variable a name because we don’t care about it’s value. Anonymous variables.

Variables II Very common in queries : |? - parent(_,gaius). true ? Can also use them in facts or rules : killer(X) :- murdered(X,_). |? - killer(tiberius). yes GNU PROLOG doesn’t bother printing the name of tiberius’ killer. Each use of _ in a clause can take a different value.

Arithmetic Computation In PROLOG PROLOG was designed for symbolic computation. Not good at numeric computation. PROLOG arithmetic operations : +, -, *, *, div, mod. Arithmetic doesn’t work as you might expect. |? - X = 3 + 4. X = 3 + 4 yes |? - = means do these terms match? X is a logical variable so it matches with the structure 3 + 4. Very useful.

Arithmetic Computation In PROLOG II To get PROLOG to actually perform the arithmetic we must use the is operator. |? - X is 3 + 4. X = 7 yes |? - X is 1 + 2 * 3 / 4 - 5. X = -2.5 ; |? - Not quite the same as assignment in C++ or Java. Means compute arithmetic expression on right hand side and test whether it matches with expression on left hand side. |? - X = 8, X is 4 + 4. X = 8

Arithmetic Computation In PROLOG III Can use expressions containing other logical variables but must be careful about the sub-goal ordering. |? - X is 3 + 4, Y is X + X. X = 7 Y = 14 yes |? - Y is X + X, X is 3 + 4. uncaught exception:error(...) |? - Remember, PROLOG works left to right. Can’t use is backwards. It’s functional not relational.

Arithmetic Computation In PROLOG IV The usual arithmetic comparison operators are available. =:= Equal. =\= Not equal. < Less than. > Greater than. >= Greater than or equal to. =< Less than or equal to. Like is they won’t work with uninstantiated logical variables. For non numeric values programmers normally use the general matching and non-matching operators, = and \=.

A Numeric Rule fact(1,1). % fact 1 fact(N,R) :- % fact 2 NewN is N - 1, fact(NewN,NewR), R is NewR * N. NB : Must use logical variables NewN and NewR so we can use is to force evaluation of the arithmetic expressions. |? - fact(1,V). V = 1 ? yes |? - fact(2,V). V = 2 ? |? - fact(4,V). V = 24 ? |? -

Summary PROLOG data objects are called Terms. Terms are either structures or simple objects. Simple objects are variables or literal constants. Literal constants are numbers or atoms. PROLOG is typeless. Run time checks that only numbers are used in arithmetic and characters in I/O. PROLOG allows both integer and floating numeric types. Variables are logical variables not store variables. Given values by instantiation not assignment. Given values required to make query succeed.

Summary II Sometimes we don’t want to give a variable a name because we don’t care about its value. Anonymous variables. Denoted by _. Can be used in queries, facts or rules. PROLOG is for symbolic computation. Not very good for numeric computation. Provides usual arithmetical and logical operators (some with strange names). Must use is operator to force numeric computation to occur. Only one uninstantiated variable allowed. Must be on LHS of is. is will not work backwards.