Syntax and Meaning of Prolog Programs Notes for

  • Slides: 29
Download presentation
Syntax and Meaning of Prolog Programs Notes for Ch. 2 of Bratko For CSCE

Syntax and Meaning of Prolog Programs Notes for Ch. 2 of Bratko For CSCE 580 Sp 03 Marco Valtorta UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Data Objects • The typing system of Prolog is simple • Data objects include:

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 •

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: Marcofoo. pl’) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Numbers • Integers • limits are implementation dependent. – ? -current_prolog_flag( X, Y) gives

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]) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Variables • Strings of letters, digits, and underscore characters, starting with an upper-case (capital)

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 :

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

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( P 1, onfloor, P 1, H), push( P 1, P 2), state( P 2, onfloor, P 2, 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

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),

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( r 1, seq( par( r 2, r 3), r 4))) – 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)

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( D 1, may, Y 1) match with the unifier {D/D 1, M/may, Y 1/2001} – Replace D with D 1 everywhere in the two terms, then replace M with may, then replace Y 1 with 2001 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Most General Unifier ? -date( D, M, 2001) = date( D 1, may, Y

Most General Unifier ? -date( D, M, 2001) = date( D 1, may, Y 1), date( D, M, 2001) = date( 15, M, Y). • The first goal succeeds with most general unifier (mgu) g 1 = {D/D 1, M/may, Y 1/2001}; then, we try to match date( D 1, may, 2001) and date( 15, may, Y) • This succeeds with mgu g 2 = {D 1/15, Y/2001} • Composition of g 1 and g 2 (g 1 o g 2) gives {D/15, M/may, Y 1/2001, D 1/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

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 • ch 2_1. pl: vertical( seg( point( X, Y), point( X,

Computing by Matching • ch 2_1. pl: vertical( seg( point( X, Y), point( X, Y 1))). horizontal( seg( point( X, Y), point( X 1, 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,

More Matching Examples ? -vertical( seg( point( 2, 3), P). P = point( 2, Y) • 2 ? - vertical( seg( point(2, 3), P)). P = point(2, _G 409) • 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(_G 391, _G 392), point(_G 391, _G 392)) ; UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Declarative Meaning of Prolog Programs • P : - Q, R. • Declarative readings:

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

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.

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: ch 2_2. pl UNIVERSITY OF SOUTH CAROLINA Department of

Examples • Exercise 2. 6: ch 2_2. pl UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Procedural Meaning • Sample trace of the procedure execute: – program of Figure 2.

Procedural Meaning • Sample trace of the procedure execute: – program of Figure 2. 10 (in ch 2_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 program success/failure indicator goal list execute UNIVERSITY OF SOUTH CAROLINA instantiation

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

execute • English description: Box on p. 42 • Pseudocode: Figure 2. 11 •

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

Monkey and Banana • A classic example of a puzzle-style problem (task environment is observable, deterministic, singleagent, 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

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( State 1, Move, State 2), where State 1 and State 2 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,

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( Pos 1, onfloor, Box, Has), %state before walk( Pos 1, Pos 2), %move state( Pos 2, 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 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( State 1) : move( State 1, Move, State 2), canget( State 2). • See Figure 2. 13 (graph) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Monkey and Banana Program • Figure 2. 14. Try: ? -canget( state( atdoor, onfloor,

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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Danger of Infinite Looping • p : - p. ? -p. • If we

Danger of Infinite Looping • p : - p. ? -p. • If we place the walk rule before the climb and push rules (program fig 2_14 ord. 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 ch 2_16. pl, in which I

Variations on Predecessor • The variations (in program ch 2_16. pl, in which I included the parent relation) are obtained by reordering goals or clauses of the original • pred 1 and pred 2 always work • pred 4 fails on, e. g. , ? -pred 4(tom, pat) • pred 3 fails on, e. g. , ? -pred 3(liz, jim) • pred 1 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 •

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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering