310 likes | 330 Views
Syntax and Meaning of Prolog Programs. Notes for Ch.2 of Bratko For CSCE 580 Sp03 Marco Valtorta. Data Objects. The typing system of Prolog is simple Data objects include: simple objects constants atoms numbers variables structures. Atoms.
E N D
Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580 Sp03 Marco Valtorta
Data Objects • The typing system of Prolog is simple • Data objects include: • simple objects • constants • atoms • numbers • variables • structures
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’)
Numbers • Integers • limits are implementation dependent. • ?-current_prolog_flag( X,Y) gives 2147483647 and –2147483648 (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])
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,_).
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
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
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
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)
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
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}
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
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!
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)) ;
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
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
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).
Examples • Exercise 2.6: ch2_2.pl
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
The Procedure execute program success/failure indicator execute goal list instantiation of variables
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
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)
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
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
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)
Monkey and Banana Program • Figure 2.14. 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
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
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
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