PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.

Slides:



Advertisements
Similar presentations
Prolog.
Advertisements

EXPERT SYSTEMS AND KNOWLEDGE REPRESENTATION Ivan Bratko Faculty of Computer and Info. Sc. University of Ljubljana.
PROLOG Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
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
DEFINITE CLAUSE GRAMMARS Ivan Bratko University of Ljubljana Faculty of Computer and Information Sc.
PROLOG Basics A Prolog program consists of a database of facts and rules, and queries (questions). Fact:.... Rule:... :-.... Query: ?-.... Variables: must.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Measuring With Jugs A Solution in Finite Domain. Intro to Oz * variables -- a set of alphanumeric characters starting with a CAPITAL letter * atoms --
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.
Measuring With Jugs A Solution in Finite Domain. Intro to Mozart - Oz variables -- a set of alphanumeric characters statring with a CAPITOL letter – you.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
DEDUCTIVE DATABASE.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
computer
Computer Science 101 Introduction to Programming.
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
AND/OR Graphs Ivan Bratko Faculty of Information and Computer Sc. University of Ljubljana Useful for solving problems by problem decomposition Searching.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
©Brooks/Cole, 2001 Chapter 9 Regular Expressions.
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
Sys Prog & Scrip - Heriot Watt Univ 1 Systems Programming & Scripting Lecture 12: Introduction to Scripting & Regular Expressions.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
INT213-Week-2 Working with Variables. What is variable
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Logic Programming (LP) expresses code using of a restricted form of symbolic logic. LP programs are declarative, rather than algorithmic. An LP program.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Variables. Some Rules of Variable Naming Convention : Variable names are case-sensitive. A variable’s name can be any legal identifier. It can contain.
CCE Department – Faculty of engineering - Islamic University of Lebanon Chapter 6 Binary Arithmetic.
Models of Computing Regular Expressions 1. Formal models of computation What can be computed? What is a valid program? What is a valid name of a variable.
Get out your Homework and a pencil. Be ready as soon as the bell rings! Write any questions you have on a post it note and put it on my desk by the computer.
DATA TYPES, VARIABLES AND CONSTANTS. LEARNING OBJECTIVES  Be able to identify and explain the difference between data and information  Be able to identify,
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
1 Course Overview Why this course “formal languages and automata theory?” What do computers really do? What are the practical benefits/application of formal.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
Characters and Strings
CSC 8310 Programming Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
Data Types, Identifiers, and Expressions
Letters.
Unit 2 Smarter Programming.
C-Character Set Dept. of Computer Applications Prof. Harpreet Kaur
INTRODUCTION ABOUT ASSEMBLY
Section 3.2c Strings and Method Signatures
Prolog programming Introduction to Prolog (part2)
Data Types, Identifiers, and Expressions
Secondary Computing and ICT Curriculum
“If you can’t write it down in English, you can’t code it.”
CEV208 Computer Programming
Chapter Two: Syntax and Meaning of Prolog Programs
Requirements for coding in Assembly Language
Introduction Reading: Sections 1.5 – 1.7.
Understanding Variables
Learn to identify and evaluate expressions.
Characters and Strings
Introduction to MATLAB
Constants, Variables and Data Types
Presentation transcript:

PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia

DATA OBJECTS data objects simple objects structures constants variables atoms numbers

SYNTAX FOR DATA OBJECS Type of object always recognisable from its syntactic form

THREE SYNTACIC FORMS FOR ATOMS (1) Strings of letters, digits, and “_”, starting with lowercase letter: x x15 x_15 aBC_CBa7 alpha_beta_algorithm taxi_35 peter missJones miss_Jones2

ATOMS, CTD. (2) Strings of special characters ---> << !...::. ::= []

ATOMS, CTD. (3) Strings in single quotes ‘X_35’ ‘Peter’ ‘Britney Spears’

SYNTAX FOR NUMBERS Integers Real numbers (floating point) E e-21

SYNTAX FOR VARIABLES Strings of letters, digits, and underscores, starting with uppercase letter X Results Object2B Participant_list _x35 _335 Lexical scope of variable names is one clause Underscore stands for an anonymous variable Each appearance of underscore: another anon. var.

ANONYMOUS VARIABLES visible_block( B) :- see( B, _, _). Equivalent to: visible_block( B) :- see( B, X, Y).

STRUCTURES Structures are objects that have several components For example: dates are structured objects with three components Date 17 June 2006 can be represented by term: date( 17, june, 2006) functor arguments An argument can be any object, also a structure

FUNCTORS Functor name chosen by user Syntax for functors: atoms Functor defined by name and arity

TREE REPRESENTATION OF STRUCTURES Often, structures are pictured as trees date( 17, june, 2006) date 17 june 2006

Therefore all structured objects in Prolog can be viewed as trees This is the only way of building structured objects in Prolog

SOME GEOMETRIC OBJECTS P2 = (2,3) (6,4) S T (4,2) P1=(1,1) (7,1) P1 = point( 1, 1) P2 = point( 2, 3) S = seg( P1, P2) = seg( point(1,1), point(2,3)) T = triangle( point(4,2), point(5,4), point(7,1))

LINE SEGMENT S = seg( point(1,1), point(2,3)) S = seg point point

ARITHMETIC EXPRESSIONS ARE ALSO STRUCTURES For example: (a + b) * (c - 5) Written as term with functors: *( +( a, b), -( c, 5)) * + - a b c 5

MATCHING Matching is operation on terms (structures) Given two terms, they match if: (1) They are identical, or (2) They can be made identical by properly instantiating the variables in both terms

EXAMPLE OF MATCHING Matching two dates: date( D1, M1, 2006) = date( D2, june, Y2) This causes the variables to be instantianted as: D1 = D2 M1 = june Y2 = 2006 This is the most general instantiation A less general instantiation would be: D1=D2=17,...

MOST GENERAL INSTANTIATION In Prolog, matching always results in most general instantiation This commits the variables to the least possible extent, leaving flexibility for further instantiation if required For example: ?- date( D1, M1, 2006) = date( D2, june, Y2), date( D1, M1, 2006) = date( 17, M3, Y3). D1 = 17, D2 = 17, M1 = june, M3 = june, Y2 = 2006, Y3 = 2006

MATCHING Matching succeeds or fails; if succeeds then it results in the most general instantiation To decide whether terms S and T match: (1) If S and T are constants then they match only if they are identical (2) If S is a variable then matching succeeds, S is instantiated to T; analogously if T is a variable (3) If S and T are structures then they match only if (a) they both have the same principal functor, and (b) all their corresponding arguments match

MATCHING ≈ UNIFICATION Unification known in predicate logic Unification = Matching + Occurs check What happens when we ask Prolog: ?- X = f(X). Matching succeeds, unification fails

COMPUTATION WITH MATCHING % Definition of vertical and horizontal segments vertical( seg( point( X1,Y1), point( X1, Y2))). horizontal( seg( point( X1,Y1), point( X2, Y1))). ?- vertical( seg( point( 1,1), point( 1, 3))). yes ?- vertical( seg( point( 1,1), point( 2, Y))). no ?- vertical( seg( point( 2,3), P)). P = point( 2, _173).

AN INTERESTING SEGMENT Is there a segment that is both vertical and horizontal? ?- vertical( S), horizontal( S). S = seg( point( X,Y), point(X,Y)) Note, Prolog may display this with new variables names as for example: S = seg( point( _13,_14), point( _13, _14))

DECLARATIVE MEANING Given a program P and a goal G, G is true ( i.e. logically follows from P) if and only if: (1) There is a clause C in P such that (2) there is a clause instance I of C such that (a) the head of I is identical to G, and (b) all the goals in the body of I are true An instance of a clause C is obtained by renaming each variable in C and possibly substituting the variable by some term. E.g. an instance of p(X,Y) :- q(Y,Z) is p(U,a) :- q(a,V).

DECLARATIVE vs PROCEDURAL MEANING OF PROLOG PROGRAMS Consider: P :- Q, R. Declarative readings of this: P is true if Q and R are true. From Q and R follows P. Procedural readings: To solve problem P, first solve subproblem Q and then R. To satisfy P, first satisfy Q and then R.

PROCEDURAL MEANING Specifies how Prolog answers questions Procedural meaning is an algorithm to execute a list of goals given a Prolog program: program success/failure indication goal list execute instantiation of variables

procedure execute( Program, GoalList, Success) execute = declarative meaning + procedural elements G is true ( i.e. logically follows from P) if and only if: (1) there is a clause C in P such that (2) there is a clause instance I of C such that (a) the head of I is identical to G, and (b) all the goals in the body of I are true Search program from top to bottom to find such clause Match G and head of C Execute goals in order as they appear in program