2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
Prolog.1 Prolog. Prolog.2 Textbook and Software  Title PROLOG programming for artificial intelligence  Author Ivan Bratko  Get the software – windows.
Chapter 11 :: Logic Languages
1 Part 1 The Prolog Language Chapter 2 Syntax and Meaning of Prolog programs.
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
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).
Simply Logical – Chapter 3© Peter Flach, 2000 Luc De Raedt and Andreas Karwath (original slides by Peter Flach) Fortgeschrittene Programmierung PROLOG.
1 COMP313A Programming Languages Logic Programming (3)
Introduction to Prolog Language Presented by San Myint.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
Introduction to C Programming
Declarative Programming Autumn 2014 Basic syntax and sample programs.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
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.
1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
PROLOG KAIST Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.
Formal Models of Computation Part II The Logic Model
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
Introduction to AgentSpeak and Jason for Programming Multi-agent Systems (1) Dr Fuhua (Oscar) Lin SCIS Athabasca University June 19, 2009.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
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.
1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
Functions Reading: Chapter 6 (94 – 107) from the text book 1.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Artificial Intelligence CS370D
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Logics for Data and Knowledge Representation ClassL (part 1): syntax and semantics.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
Introduction to Scripting
Prolog programming Introduction to Prolog (part2)
Artificial Intelligence CS370D
Prolog syntax + Unification
Chapter Two: Syntax and Meaning of Prolog Programs
Chapter 2 Syntax and meaning of prolog programs
Chapter 2: Prolog (Introduction and Basic Concepts)
Truth tables.
PROLOG.
Presentation transcript:

2. Syntax and Meaning

Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and goals The relation between Prolog and logic

Data Objects Data objects in Prolog: Data objects Simple objects Structures Constants Variables Atoms Numbers

Data Objects Atoms can be constructed in three ways: –Strings of letters, digits and ‘_’, starting with a lower-case letter. anna nil x25 x_25 x_ x_ _y –Strings of special characters....:. –String of characters enclosed in single quote ‘Tom’ ‘South_America’ ‘Sarah Jones’ ‘ 大同 ’ Numbers used in Prolog include integer numbers and real numbers. Data objects Simple objects Structures Constants Variables Atoms Numbers

Data Objects Variables are strings of letters, digits and underscore characters, starting with an upper-case character: –X Result Object2 _x23 _23 Anonymous variable haschild(X):-parent(X,Y). haschild(X):-parent(X, _ ) somebody_has_child:-parent(_,_). somebody_has_child:-parent(X,Y). Data objects Simple objects Structures Constants Variables Atoms Numbers

Data Objects Structured objects (or simply structure) are objects that have several components. The components themselves can, in turn, be structures. –The date can be viewed as a structure with three components: day, month, year. –date(1,may,1983) Data objects Simple objects Structures Constants Variables Atoms Numbers functorarguments date 1 may1983 Any day in May 1983 can be represented as –date(Day, may, 1983)

Data Objects P1=(1,1) P2=(2,3) S (6,4) (4,2) (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(6,4), point(7,1))

Data Objects We can use the same name, point, for points in both 2D and 3D: –point(X,Y) point(X,Y,Z) Prolog will recognize the difference because each functor is defined by two things: –the name, whose syntax is that of atoms; –the arity - i.e., the number of arguments.

Data Objects All structured objects in Prolog are trees, represented in the program by terms. (a+b)*(c-5) *(+(a,b),-(c,5)) * +– c5ab

Data Objects r1r2 seq r1r2 seq(r1,r2) r1 r2 par r1 r2 par(r1,r2) r1 r3 r2 par r1 r2r3 par par(r1,par(r2,r3)) r1 r3 r2r4 par r1 r2 r3 seq par r3 par(r1,seq(par(r2,r3),r4))

Matching The most important operation on terms is matching. Given two terms, we say that they match if: –they are identical or –the variables in both terms can be instantiated to objects in such a way that after the substitution of variables by those objects the terms become identical. –For example, the following instantiation makes the terms date(D,M,1983) and date(D1,may,Y1) identical: D is instantiated to D1 M is instantiated to may Y1 is instantiated to 1983.

Matching Matching is a process that takes as input two terms and checks whether they match. –If the terms do not match we say that this process fails. –If they do match then the process succeeds and it also instantiates the variables in both terms to such value that the terms become identical.

Matching The request for the matching operation can be communicated to the Prolog system by using the operator ‘=’. ?- date(D,M,1983)=date(D1,may,Y1). D=D1 M=may Y1=1983 D=1 D1=1 M=may Y1=1983 D=third D1=third M=may Y1=1983 Less general Matching in Prolog always results in the most general instantiation.

Matching ?- date(D,M,1983)=date(D1,may,Y1), date(D,M,1983)=date(15,M,Y). To satisfy the first goal: D=D1 M=may Y1=1983 After having satisfied the second goal: D=15 D1=15 M=may Y1=1983 Y=1983

Matching The general rules to decide whether two terms, S and T, match: –If S and T are constants the S and T match only if they are the same object. –IF S is a variable and T is anything, then they match, and S is instantiated to T. Conversely, if T is a variable then T is instantiated to S. –If S and T are structures then they match only if S and T have the same principal functor, and all their corresponding components match

Matching triangle pointA triangle Xpoint 4y2Z

Matching vertical(seg(point(X,Y),point(X,Y1)). horizontal(seg(point(X,Y),point(X1,Y)). ?-vertical(seg(point(1,1),point(1,2)). yes ?-vertical(seg(point(1,1),point(2,Y)). no ?-horizontal(seg(point(1,1),point(2,Y)). Y=1 ?-vertical(seg(point(2,3),P)). P=point(2,Y) ?-vertical(S),horizontal(S). S=seg(point(X,Y),point(X,Y))

Declarative Meaning of Prolog Programs Given a program and a goal G, the declarative meaning says: –A goal G is true (i.e., satisfiable, or logically follows from the program) if and only if there is a clause C in the program such that there is a clause instance I of C such that –the head of I is identical to G, and –all the goals in the body of I are true.

Declarative Meaning of Prolog Programs In general, a question to the Prolog system is a list of goals separated by commas. A list of goals is true if all the goals in the list are true for the same instantiation of variables. A comma between goals thus denotes the conjunction of goals: they all have to be true. The disjunction of goals: any one of the goals in a disjunction has to be true.

Procedural Meaning

The procedural meaning specifies how Prolog answers question.

Example: Monkey and Banana The problem: –There is a monkey at the door into a room. In the middle of the room a banana is hanging from the ceiling. The monkey is hungry and and wants to get the banana, but he cannot stretch high enough from the floor. At the window of the room there is a box the monkey may use. The monkey can perform the following actions: walk on the floor, climb the box, push the box around and grasp the banana if standing on the box directly under the banana. Can the monkey get the banana?

Example: Monkey and Banana Finding a representation of the problem: –We can think of the ‘monkey world’ as always being in some state that can change in time. –The current state is determined by the positions of the objects. –For example, the initial state is determined by: Monkey is at door. Monkey is on the floor. Box is at window. Monkey does not have banana.

Example: Monkey and Banana It is convenient to combine all these four pieces of information into one structured object. Let us choose the word ‘state’ as the functor to hold the four components together. –The initial state becomes state(atdoor,onflorr,atwindow,hasnot) Horizontal position of monkey Vertical position of monkey Position of box Monkey has or has not banana

Example: Monkey and Banana Formalize the rules of the game: –The goal is a situation in which the monkey has the banana. state(_, _, _, has) –What are the allowed moves that change the world from one state to another? grasp banana, climb box, push box, walk around Such rules can be formalized in Prolog as a 3-place relation named move: move(State1, Move, State2)

Example: Monkey and Banana move(state(middle,onbox,middle,hasnot), grasp, state(middle,onbox,middle,has)). move(state(P,onfloor,P,H), climb, state(P,onbox,P,H)). move(state(P1,onfloor,P1,H), push(P1,P2), state(P2,onfloor,P2,H)). move(state(P1,onfloor,B,H), walk(P1,P2), state(P2,onfloor,B,H)). canget(state(_, _, _, has)). canget(State1):- move(State1,move State2), canget(State2). State1 canget move State2 has State m

Example: Monkey and Banana state(atdoor,onfloor,arwindow,hasnot) state(P2,onfloor,arwindow,hasnot) walk(atdoor,P2) state(atwindow,onbox,arwindow,hasnot)state(P2’,onfloor,P2’,hasnot) No move possible climb backtrack push(P2,P2’) P2=atwindow state(P2’,onbox,P2’,hasnot) climb state(middle,onbox,middle,has) grasp P2’=middle

Order of Clauses and Goals Danger of indefinite looping Program variation through reordering of clauses and goals