Artificial Intelligence Course Sharif University of Technology

Similar documents
Prolog Programming. Chapter 2. Syntax and Meaning of Prolog Programs 1/16/2010

Syntax and Meaning of Prolog Programs

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

A Sample Prolog Session

Last Week COMP219: Artificial Intelligence Lecture 8 Prolog Lecture 2 Recursive Definitions Recursion The Ancestor Relation

COMP219: Artificial Intelligence. Lecture 6: Recursion in Prolog

Logic Programming and PROLOG (III)

Logik für Informatiker: PROLOG Part 3: SLD Trees

Logic Languages. Hwansoo Han

Part I Logic programming paradigm

Topic A: Introduction to Prolog

Programming Languages Third Edition

GPS: The general problem solver. developed in 1957 by Alan Newel and Herbert Simon. (H. Simon, 1957)

Programming Language Concepts, cs2104 Lecture 04 ( )

Introduction to Prolog

What is Prolog? - 1. A Prolog Tutorial. What is Prolog? - 2. Prolog Programming. » Declaring some facts about objects and their relationships

Introduction to Logic Programming in Prolog 1 / 39

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

Topic B: Backtracking and Lists

Outcome-Oriented Programming (5/12/2004)

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

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.

Recursion, Structures, and Lists

PROLOG PROgramming in LOGic

What is Prolog? - 1. A Prolog Tutorial. Prolog Programming. What is Prolog? - 2. » Declaring some facts about objects and their relationships

Declarative Programming Prolog CS360

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Logic - CM0845 Introduction to Prolog

What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2

Software Paradigms (Lesson 6) Logic Programming

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Part I CHR tutorial. Cambridge University Press Constraint Handling Rules Thom Fruhwirth Excerpt More information

LING/C SC/PSYC 438/538. Lecture 20 Sandiway Fong

An introduction to logic programming with Prolog

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Programming Paradigms

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Prolog Programming. Lecture Module 8

Topic 1: Introduction to Knowledge- Based Systems (KBSs)

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

Modelica Change Proposal MCP-0019 Flattening (In Development) Proposed Changes to the Modelica Language Specification Version 3.

Brief Introduction to Prolog

Prolog. Intro to Logic Programming

Functional Programming Languages (FPL)

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Functional programming with Common Lisp

15 Unification and Embedded Languages in Lisp

SMURF Language Reference Manual Serial MUsic Represented as Functions

Learning Rules. How to use rules? Known methods to learn rules: Comments: 2.1 Learning association rules: General idea

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Java+- Language Reference Manual

Introduction to Prolog Paper Refs. Prolog tutor. Julian Verdurmen. Seminar Softw. tech. for teaching and learning. September 30, 2009

The role of semantic analysis in a compiler

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Functional Programming

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

Fundamentals of Prolog

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Decaf Language Reference Manual

CMSC 331 Final Exam Section 0201 December 18, 2000

Prolog Introduction. Gunnar Gotshalks PI-1

MIDTERM EXAM (Solutions)

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Chapter 16. Logic Programming Languages

Chapter 6 Fundamentals of the Logical Paradigm. Programming Languages and Paradigms J. Fenwick, B. Kurtz, C. Norris (to be published in 2012)

Fundamentals of Programming Session 4

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

YOLOP Language Reference Manual

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

RSL Reference Manual

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Lecture 21: Relational Programming II. November 15th, 2011

Typescript on LLVM Language Reference Manual

Desktop Command window

Basic concepts. Chapter Toplevel loop

CHIL CSS HTML Integrated Language

Predicate Calculus. Problems? Syntax. Atomic Sentences. Complex Sentences. Truth

a little more on macros sort of like functions, but..

Racket Pattern Matching

Programming Paradigms

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

The Prolog to Mercury transition guide

CS 381: Programming Language Fundamentals

SFPL Reference Manual

Logic Programming Paradigm

PROgramming in LOGic. Part II. By Forrest Pepper 12/7/07

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Introduction to Prolog

Language Reference Manual

Fundamentals of Programming Session 8

Pace University. Fundamental Concepts of CS121 1

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CS 415 Midterm Exam Spring SOLUTION

CSE 341: Programming Languages

ACSC300: Logic Programming

2.2 Syntax Definition

arxiv: v1 [cs.pl] 1 May 2017

Transcription:

Artificial Intelligence Course Sharif University of Technology

Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

Data objects Data objects Simple objects structures Constants variables Atoms numbers

Data objects The Prolog system recognizes the type of an object in the program by its syntactic form.

Atoms 1. Strings of letters, digits and the underscore character, _', starting with a lowercase letter: anna nil x25 x_25 x_25ab x_ x y alpha_beta_procedure

Atoms 2. Strings of special characters: <---> ======>.:. ::= 3. Strings of characters enclosed in single quotes: 'Torn' 'SouthAmerica' 'Sarah Jones'

numbers 1 1313 0-97 3.14-0.0035 100.2

Variables Strings of letters, digits and underscore characters, Starting with an upper-case letter or an underscore character: X Result Object2 Participant_list ShoppingList X23 _23

Anonymous variable When a variable appears in a clause once only, we can use the so-called 'anonymous' variable, which is written as a single underscore character: hasachild( X) :- parent( X, Y). hasachild( X) :- parent( X, _).

Anonymous variable Each time a single underscore character occurs in a clause it represents a new anonymous variable: somebody_has_child :- parent( X, Y). somebody_has_child :- parent( _, _). somebody_has_child :- parent( X, X).

Anonymous variable If the anonymous variable appears in a question clause then its value is not output when Prolog answers the question. Example: we are interested in people who have children, but not in the names of the children:?- parent( X, _).

Lexical scope of variable names The lexical scope of variable names is one clause. for example, if the name X occurs in two clauses, then it signifies two different variables. But each occurrence of X within the same clause means the same variable. The situation is different for constants: the same atom always means the same object in any clause - that is, throughout the whole program

Structures Structured objects are objects that have several components. The components themselves can, in turn, be structures. structures are treated as single objects. to combine the components into a single object we have to choose a functor. date( Day,may, 2001)

Functors each functor is defined by two things: 1. the name, whose syntax is that of atoms; 2. the arity - that is, the number of arguments. Example: point( X, Y) point( X, Y, Z)

Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

Matching Given two terms, we say that they match if: 1. they are identical, or 2. the variables in both terms can be instantiated to objects in such a way that after the substitution of variables by these objects the terms become identical. Example: date( D, M, 2001) and date( D1, may, Y1) match!

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

Most general instantiation?- date(d, M, 2001) = date(dl, may,y1 ). D=1 D1 = 1 M = may Y1 = 2001 D = D1 M=may Y1 = 2001 Matching in Prolog always results in the most general instantiation.

Matching rules To decide whether two terms, S and T match: 1. If S and T are constants then S and T match only if they are the same object. 2. If S is a variable and T is anything, then they match, and S is instantiated to T. 3. If S and T are structures then they match only if a) S and T have the same principal functor, and b) all their corresponding components match. The resulting instantiation is determined by the matching of the components.

Renaming the variables vertical( seg( point(x,y), point(x,z) ) ). horizontal( seg( point(x,y), point(z,y)) ).?- vertical( seg( point(2,3), P) ). P = point(2,_136)

Renaming the variables The system has to generate new names in order to rename the user's variables in the program, because of two reasons: 1. The same name in different clauses signifies different variables, and 2. In successive applications of the same clause, its 'copy' with a new set of variables is used each time.

Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

Declarative & procedural meaning of Prolog programs P :- Q, R. declarative readings : P is true if Q and R are true. From Q and R follows P procedural readings: To solve problem P, first solve the subproblem Q and then the subproblem R. To satisfy P, first satisfy Q and then R.

Declarative meanings The declarative meaning of programs determines whether a given goal is true, and if so, for what values of variables it is true. The declarative meaning of programs in 'pure Prolog' does not depend on the order of clauses and the order of goals in clauses.

Procedural meanings procedural meanings do not only define the logical relations between the head of the clause and the goals in the body, but also the ordering which the goals are processed. The procedural meaning does depend on the order of goals and clauses. Thus the order can affect the efficiency of the program.

Instances and variants An instance of a clause C is the clause C with each of its variables substituted by some term. A variant of a clause C is such an instance of the clause C where each variable is substituted by another variable.

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.

Conjunction and disjunction A comma between goals denotes the conjunction of goals: they all have to be true. Disjunction is indicated by a semicolon: anyone of the goals in a disjunction has to be true. P :- Q; R. P:- Q. P :- R.

Conjunction and disjunction The comma binds stronger than the semicolon: P :- Q, R; S, T, U. P :- (Q, R); ( S, T, U). and means the same as the clauses: P :- Q,R P :- S, T, U.

Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

Procedural meaning The procedural meaning of Prolog is a procedure for executing a list of goals with respect to a given program. To 'execute goals' means: try to satisfy them.

Procedural meaning program Success/failure indicator Goal list Execute procedure Instantiation of variables

Execute procedure To execute a list of goals: G1,G2,...,Gm If the goal list is empty then terminate with success. If the goal list is not empty then continue with (the following) operation called SCANNING

Execute procedure (cont.) SCANNING: Scan through the clauses in the program from top to bottom until the first clause, C, is found such that the head of C matches the first goal G1. (If there is no such clause then terminate with failure.)

Execute procedure (cont.) If there is such a clause C of the form H :- B1,..., Bn. then rename the variables in C to obtain a variant C' of C, such that C' and the list G1,.., Gm have no common variables. Let C' be H' :- B1',, Bn'. Match G1 and H'; let the resulting instantiation of variables be S.

Execute procedure (cont.) In the goal list G1, G2,..., Gm, replace G1 with the list B1',..., Bn Bl',..., Bn', G2,..., Gm Substitute the variables in this new goal list with new values as specified in the instantiation S, Bl",..., Bn", G2',..., Gm'

Execute procedure (cont.) Execute (recursively) this new goal list. If the execution of this new goal list terminates with success then terminate the execution of the original goal list also with success.

Execute procedure (cont.) If the execution of the new goal list is not successful then abandon this new goal list and go back to SCANNING through the program. Continue the scanning with the clause that immediately follows the clause C (C is the clause that was last used) and try to find a successful termination using some other clause.

Example Program: big( bear). big( elephant). small( cat). brown( bear). black( cat). gray( elephant). dark( Z) :-black( Z). dark( Z) :brown(z). Question:?- dark( X), big( X).

Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

Example: monkey and banana 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 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 (if it is already at the box) and grasp the banana if standing on the box directly under the banana. Can the monkey get the banana?

Example: monkey and banana 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 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 (if it is already at the box) and grasp the banana if standing on the box directly under the banana. Can the monkey get the banana? Let s write the program ourselves!

Example: states Represented as structured object: 1. horizontal position of monkey, 2. vertical position of monkey, 3. position of box, 4. monkey has or has not banana. State( middle, onbox, middle, hasnot)

Example: moves There are four types of moves: 1. grasp banana, 2. climb box, 3. push box, 4. walk around.

The solution %move( State1, Move, State2): making Move in State1 results in State2; % a state is represented by a term: % state( MonkeyHorizontal, MonkeyVertical, BoxPosition, Haslsanana) move( state( middle, onbox, middle, hasnot), % Before move grasp, state( middle, onbox, middle, has) ). move( state( P, onfloor, P, H), climb, % Climb box state( P, on box, 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). % Grasp banana % After move

The solution %move( State1, Move, State2): making Move in State1 results in State2; % a state is represented by a term: % state( MonkeyHorizontal, MonkeyVertical, BoxPosition, Haslsanana) move( state( middle, onbox, middle, hasnot), % Before move grasp, state( middle, onbox, middle, has) ). move( state( P onfloor, P, H), climb, % Climb box state( P, on box, 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). % Grasp banana % After move?-canget( State).

Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

Order of clauses and goals Given a declaratively correct program, changing the order of clauses and goals can improve the program's efficiency while retaining its declarative correctness. Reordering is one method of preventing indefinite looping. We will discuss about these topics next session!

Danger of indefinite looping p :- p. declaratively perfectly correct, but procedurally is quite useless.?- p. Prolog will enter an infinite loop not noticing that no progress is being made.

Any Question?