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.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and 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: ?-
SLD-resolution Introduction Most general unifiers SLD-resolution
1 Part 1 The Prolog Language Chapter 2 Syntax and Meaning of Prolog programs.
Prolog.
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).
2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
4. PROLOG Data Objects And PROLOG Arithmetic
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
Declarative Programming Autumn 2014 Basic syntax and sample programs.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
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 More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Hole in Goal Trees Notes for: D.W. Loveland and M. Stickel. “A Hole in Goal.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Language Translation Principles Part 1: Language Specification.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
Formal Models of Computation Part II The Logic Model
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CMPS 3223 Theory of Computation Automata, Computability, & Complexity by Elaine Rich ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Slides provided.
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: ?-
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
P.1 Real Numbers. 2 What You Should Learn Represent and classify real numbers. Order real numbers and use inequalities. Find the absolute values of real.
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.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
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.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Knowledge Based Information System
Artificial Intelligence CS370D
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
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.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Chapter 2 Syntax and meaning of prolog programs Part 1.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
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),
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Artificial Intelligence CS370D
Prolog syntax + Unification
Chapter Two: Syntax and Meaning of Prolog Programs
Chapter 2 Syntax and meaning of prolog programs
Presentation transcript:

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 Sp03 Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Data Objects The typing system of Prolog is simple Data objects include: –simple objects constants –atoms –numbers variables –structures

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Atoms strings of letters, digits, and underscore, starting with a lower-case letter string of special characters (e.g., ====>) –(Some are predefined!) strings of characters enclosed in single quotes (e.g., ‘Tim’, ‘c:\Marco\foo.pl’)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Numbers Integers limits are implementation dependent. –?-current_prolog_flag( X,Y) gives and – (32 bits) Real numbers: Rarely used –float: Computers cripled (sic) representation of a real number. Represented as ‘IEEE double’ (SWI-Prolog manual, p.234 [glossary of terms])

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Variables Strings of letters, digits, and underscore characters, starting with an upper-case (capital) letter or an underscore character The underscore by itself is the anonymous variable, e.g.: has_a_child( X) :- parent ( X,Y). is equivalent to has_a_child(X) :- parent( X,_).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Scope of Variables Each underscore represents a new anonymous variable: someone_has_child :- parent( _,_). is equivalent to someone_has_child :- parent( X,Y). and not equivalent to someone_has_child :- parent( X,X). The lexical scope of all other (named) variables is one clause

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Structures Structures are data objects that have several components The components are combined by functors –date( 1, may, 2001) –date( Day, may, 2001) –segment( point( 1,1), point( 2,3)) –move( state( P1, onfloor, P1, H), push( P1,P2), state( P2,onfloor,P2,H) The first three denote objects, the last one denotes a predicate

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Terms Syntactically, all Prolog data objects are terms A term is either –a simple object, or –a structure, i.e., a functor followed by “(“, one or more terms separated by commas, and “)” Structures are conveniently represented by trees –actually, any term may be represented by a tree

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Examples of Structures point( 1,1) T = triangle( point( 4,2), point(6,4), point( 7,1)) There are two different functors in point( X,Y) and point( X,Y,Z): point/2 (a functor of arity 2) and point/3 (a functor of arity 3) *( +( a,b), -(c,5)) may represent (a+b)*(c-5) –infix notation is possible and will be described in Ch.3 par( r1, seq( par( r2,r3), r4))) –resistive circuit example in Figure 2.6(d)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Matching Two terms match if –(1) they are identical, or –(2) the variables in both terms may be instantiated in such a way that after the substitution of variables by these objects the terms become identical E.g., date( D,M,2001) and date( D1,may,Y1) match with the unifier {D/D1,M/may,Y1/2001} –Replace D with D1 everywhere in the two terms, then replace M with may, then replace Y1 with 2001

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Most General Unifier ?-date( D,M,2001) = date( D1,may,Y1), date( D,M,2001) = date( 15,M,Y). The first goal succeeds with most general unifier (mgu) g1 = {D/D1, M/may, Y1/2001}; then, we try to match date( D1,may,2001) and date( 15,may,Y) This succeeds with mgu g2 = {D1/15, Y/2001} Composition of g1 and g2 (g1 o g2) gives {D/15, M/may, Y1/2001, D1/15, Y/2001}

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Algorithm to Find MGU for Terms If S and T are constants, they match only if they are identical If S is a variable and T is anything, check whether T contains S; if so, fail; if not, substitute T for S; and conversely If S and T are structures, they match only if –they have the same principal functor –all corresponding components match Example: Figure 2.7

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Computing by Matching ch2_1.pl: vertical( seg( point( X,Y), point( X,Y1))). horizontal( seg( point( X,Y), point( X1,Y))). 1 ?- vertical( seg( point(1,1), point(1,2))). Yes 2 ?- !!. vertical( seg( point(1,1), point(1,2))). Yes 3 ?- ^point(1,2)^point(2,Y). vertical( seg( point(1,1), point(2,Y))). No Imagine how more difficult this would be in Java!

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Matching Examples ?-vertical( seg( point( 2,3), P). P = point( 2,Y) 2 ?- vertical( seg( point(2,3), P)). P = point(2, _G409) fresh variables are used in each copy of a clause ?-vertical(S), horizontal(S). S = seg( point(X,Y),point(X,Y)) a point is the only (degenerate) segment that is both horizontal and vertical 1 ?- vertical(S), horizontal(S). S = seg(point(_G391, _G392), point(_G391, _G392)) ;

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Declarative 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 subproblem Q, then solve subproblem R –To satisfy P, first satisfy Q, and then R

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Declarative Meaning An instance of a clause is the clause with each of its variable substituted by a term A goal G is true 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 G are true A query is true if all of its goals are true for the same instantiation of the variables

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Semicolon (Or) P :- Q; R. stands for P :- Q. P :- R. P :- Q,R;S,T,U. stands for P :- (Q,R); (S,T,U). which is the same as P :- Q,R. P :- S,T,U. In Prolog, disjunctions may only appear in the body (premise) of a rule, not in its head (conclusion).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Examples Exercise 2.6: ch2_2.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Procedural Meaning Sample trace of the procedure execute: –program of Figure 2.10 (in ch2_3.pl) –query: ?-dark( X), big( X). Use the Prolog tracer –non-graphical (nonguitracer/0) –graphical (PCE-based: guitracer/0) I find the non-graphical tracer clearer on this example

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering The Procedure execute execute goal list program success/failure indicator instantiation of variables

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering execute English description: Box on p.42 Pseudocode: Figure 2.11 The instantiation returned is the one that leads to successful termination If a recursive call to execute fails, backtracking occurs, and variable instantiations done after the failure point are undone Hashing (at least on functors) is used to reduce scanning

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Monkey and Banana A classic example of a puzzle-style problem (task environment is observable, deterministic, single-agent, static, etc.) The program solves the problem by search state has four components: monkey’s horizontal position (atdoor, middle, atwindow) monkey’s vertical position (onfloor, onbox) horizontal position of the box (atdoor, middle, atwindow) whether the monkey has the banana (has, hasnot)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Moves There are four types of actions (moves), which change the state of the environment: grasp banana climb box push box walk around These are formalized by the relation move( State1, Move, State2), where State1 and State2 are the states before and after the Move

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Two Moves A simple move: move( state( middle, onbox, middle, hasnot),%pre grasp, %move state( middle, onbox, middle, has) ). %post A move schema: move( state( Pos1, onfloor, Box, Has), %state before walk( Pos1, Pos2), %move state( Pos2, onfloor, Box, Has) ). %state after

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Can the Monkey Get the Banana? If the monkey has the banana, it can get it: canget( state( _,_,_,has) ). If the monkey does not have the banana, it can get it if it can move to a state in which it can get the banana: canget( State1) :- move( State1, Move, State2), canget( State2). See Figure 2.13 (graph)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Monkey and Banana Program Figure Try: ?-canget( state( atdoor, onfloor, atwindow,hasnot) ). Goal tree for this goal is shown in Figure 2.15: –Prolog backtracks only once Why does Prolog try the moves in the order indicated? –The order of clauses is crucial

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Danger of Infinite Looping p :- p. ?-p. If we place the walk rule before the climb and push rules (program fig2_14ord.pl), the monkey will walk aimlessly and never get the banana: ?- canget(state(atdoor,onfloor,atwindow,hasnot)). ERROR: Out of local stack Tracing indicates a repeated goal There are more principled ways to prevent infinite looping in search, as we will see in Ch.11

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Variations on Predecessor The variations (in program ch2_16.pl, in which I included the parent relation) are obtained by reordering goals or clauses of the original pred1 and pred2 always work pred4 fails on, e.g., ?-pred4(tom,pat) pred3 fails on, e.g., ?-pred3(liz,jim) pred1 is “simplest”: it tries the simplest options first

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Combining Declarative and Procedural Views The order of clauses and goals matters Some declaratively correct Prolog programs do not work in practice Should we forget about the declarative meaning of Prolog programs? No! Declarative aspects are easier to formulate and understand First concentrate on the declarative aspects of a problem, then address the procedural aspects as needed