Atoms. Built-in predicate: atom(...) Numbers

Kuliah 2 Slide 1 Data Objects in Prolog Kuliah 2 Atoms Strings of letters, digits and the underscore ("_"), starting with a lower-case letter. da...
3 downloads 3 Views 169KB Size
Kuliah 2

Slide 1

Data Objects in Prolog

Kuliah 2

Atoms Strings of letters, digits and the underscore ("_"), starting with a lower-case letter.

data objects simple objects constants

atoms

Contoh: fasilkom, nona_Anna, x_123, b_

structures

Strings of special characters. Contoh: , ====>, ::=

variables

(Be careful, some strings of special characters have a predefined meaning; for example, ":-".) Strings of characters enclosed in single quotes.

numbers

Kuliah 2

Contoh: 'Tom', 'Frodo Baggins', 'keren abis'

Slide 3

Built-in predicate: atom(...)

Slide 2

Kuliah 2

Slide 4

Numbers

atom(X)

integer numbers

is true if X currently stands for an atom

Contoh: 1, 456, 0, -75 real numbers

Contoh:

Contoh: 2.5, 3.14, -0.6, 0.4e2

?- atom(fasilkom). yes ?- atom(3.5). no ?- atom(=@=). yes

1

Kuliah 2

Slide 5

Built-in predicates:

Kuliah 2

Slide 6

?- integer(23). yes

integer(X)

?- float(23).

is true if X currently stands for an integer

no

float(X)

?- float(3.14).

is true if X currently stands for a real number

yes ?- number(3.14).

number(X)

yes

is true if X currently stands for a number

?- number(hello).

atomic(X) is true if X currently stands for a number or an atom

no ?- atomic(hello). yes ?- atomic(23). yes

Kuliah 2

Slide 7

Variables Strings of letters, digits and the underscore. Start with an upper-case letter or an underscore. Contoh: X, Hasil, _123, Object, Daftar_Gaji

Kuliah 2

?- var(X), X = 5. X=5 yes ?- X = 5, var(X). no

A single underscore represents an anonymous variable.

?- X = 5, nonvar(X).

Built-in predicates:

yes

var(X) succeeds if X is currently an uninstantiated variable;

yes

nonvar(X) succeeds if X is not a variable, or X is an already instantiated variable.

Slide 8

X=5 ?- nonvar(depok). ?- nonvar(Depok). no ?- var(123). no

2

Kuliah 2

Slide 9

Kuliah 2

The meaning of variables

Slide 10

Each time a single underscore occurs in a clause it represents a new anonymous variable.

The rule:

For example, we can say that there is somebody who has a child if there are two objects such that one is a parent of the other:

hasachild(X) :- parent(X, Y). says: for all X, X has a child if X is a parent of some Y.

somebody_has_a_child :- parent(_ , _).

Using an anonymous variable, we can write the above clause as:

This is equivalent to: somebody_has_a_child :- parent(X, Y).

hasachild(X) :- parent(X, _).

But it is quite different from: somebody_has_a_child :- parent(X, X). This last clause is wrong.

Kuliah 2

Slide 11

The lexical scope of variable names is one clause. This means that, 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. But the same constant or atom always means the same object in any clause, that is, throughout the whole program.

Kuliah 2

Slide 12

Structures Structured objects (or structures) are objects that have several components. The components themselves can, in turn, be structures. Although composed of several components, structures are treated as single objects. The components are combined into a single object by a functor. For example, date(14, feb, 2003)

functor

arguments

3

Kuliah 2

Slide 13

Terms

Kuliah 2

Slide 14

All structures are trees.

All data objects in Prolog are terms.

The root of the tree is the functor, and the offsprings of the root are the components. If a component is also a structure then it is a subtree of the tree that corresponds to the whole structure.

For example, feb and date(14,feb,2003) are terms. Variables are also terms.

The structure date(14, feb, 2003) is represented as the tree: date 14

Kuliah 2

Slide 15

Each functor is defined by two things: 1) the name (which is an atom); 2) the arity (the number of arguments).

feb

2003

Kuliah 2

Slide 16

Matching (Unification) of terms The most important operation on terms is matching. Given two terms, we say that they match if:

These two functors are different. point(X, Y) point(X, Y, Z)

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. Matching is a process that takes two terms as input and checks whether they match.

4

Kuliah 2

Slide 17

Kuliah 2

Slide 18

The operator '=' can be used to ask Prolog explicitly to do matching.

?- date(D,M,2003) = date(D1,may,Y1),

For example,

D = 20

date(D,M,2003) = date(20,M,Y).

?- date(Day, feb, 2003) = date(14, Month, Year).

M = may

Day = 14

D1 = 20

Month = feb

Y1 = 2003

Year = 2003

Y = 2003

yes

yes

?- date(14, feb) = date(Day, Month, Year). ?- date(D,feb,2003) = X.

no

D = _G157 X = date(_G157, feb, 2003) yes

Kuliah 2

Slide 19

Matching in Prolog always results in the most general instantiation. The general rules to decide whether two terms, S and T, match are as follows:

Kuliah 2

Slide 20

?- triangle(point(1,2),A,point(2,3)) = triangle(X,point(5,Y),point(2,Z)). A = point(5, _G167)

1) If S and T are constants then S and T match only if they are the same object.

X = point(1, 2)

2) 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.

Z = 3

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.

Y = _G167 yes ?- triangle = triangle, point(1,2) = X, A = point(5,Y), point(2,3) = point(2,Z). /* Give the same instantiation. */

5

Kuliah 2

Slide 21

% A program for the monkey and banana problem. % a state is represented by a term: % state( MonkeyHorizontal, MonkeyVertical, BoxPosition, HasBanana) % Before move

grasp,

% Grasp banana

state( middle, onbox, middle, has) ).

% After move

Slide 22

% canget( State): monkey can get banana in State

% move( State1, Move, State2): making Move in State1 results in State2;

move( state( middle, onbox, middle, hasnot),

Kuliah 2

canget( state( _, _, _, has) ).

% can1: Monkey already has it

canget( State1) :-

% can2: Do some work to get it

move( State1, Move, State2),

% Do something

canget( State2).

% Get it now

move( state( P, onfloor, P, H), climb,

% Climb box

state( P, onbox, P, H) ). move( state( P1, onfloor, P1, H), push( P1, P2),

% Push box from P1 to P2

state( P2, onfloor, P2, H) ). move( state( P1, onfloor, B, H), walk( P1, P2),

% Walk from P1 to P2

state( P2, onfloor, B, H) ).

Kuliah 2

Meaning (Semantics) of Prolog Programs 1. Declarative Meaning

Slide 23

2. Procedural Meaning

The declarative meaning defines whether a goal is true with respect to a given program, and if it is true, for what instantiation of variables it is true. The procedural meaning defines a procedure for satisfying a list of goals in the context of a given program. The procedure outputs the truth or falsity of the goal list and the corresponding instantiations of variables. The procedure backtracks to examine alternatives.

Kuliah 2

Slide 24

For example, the clause: P :- Q, R. where P, Q, and R are terms, has the declarative meanings: P is true if Q and R are true. From Q and R, follows P. The corresponding procedural meaning is: To solve problem P, first solve the subproblem Q and then the subproblem R. To satisfy P, first satisfy Q and then R.

6

Kuliah 2

Slide 25

The declarative meaning of programs in "pure Prolog" does not depend on the order of clauses and the order of goals in clauses.

Kuliah 2

Slide 26

Declarative Meaning Concepts:

The procedural meaning does depend on the order of goals and clauses. The order can affect the efficiency of the program. An unsuitable order may even lead to infinite recursive calls.

An instance of a clause C is the clause C with each of its variables substituted by some term.

Recall the monkey and banana problem.

For example, consider the clause:

A variant of a clause C is such an instance of the clause C where each variable is substituted by another variable. hasachild(X) :- parent(X,Y). Variant: hasachild(A) :- parent(A,B). Instance: hasachild(john) :- parent(john, S).

Kuliah 2

Slide 27

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

Kuliah 2

Slide 28

Declarative Meaning A list of goals is true if all the goals are true for the same instantiation of variables. The values of the variables result from the most general instantiation. A comma between goals denotes the conjunction of goals. A semicolon between goals denotes the disjunction of goals. P :- Q; R. (is read: P is true if Q is true or R is true.) is the same as P :- Q. P :- R.

7

Kuliah 2

Slide 29

Example

Kuliah 2

Slide 30

Example

Rewrite the following program without using the semicolon notation.

translate(1, satu). translate(2, dua). translate(3, tiga).

translate( Number, Word) :-

translate(4, empat).

Number = 1, Word = satu; Number = 2, Word = dua; Number = 3, Word = tiga; Number = 4, Word = empat.

Kuliah 2

Slide 31

Procedural Meaning

Kuliah 2

Slide 32

Procedural Meaning To execute a list of goals: G1, G2, ..., Gm the procedure execute does the following:

program

goal list

execute

If the goal list is empty then terminate with success.

success/failure indicator instantiation of variables

If the goal list is not empty then continue with the following operation called "SCANNING". 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. 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.

8

Kuliah 2

Slide 33 In the goal list G1, G2, ..., Gm, replace G1 with the list B1', ..., Bn', obtaining a new goal list B1', ..., Bn', G2, ..., Gm (Note that if C is a fact then n = 0 and the new goal list is shorter than the original one; such shrinking of the goal list may eventually lead to the empty list and thereby a successful termination.) Substitute the variables in this new goal list with new values as specified in the instantiation S, obtaining another goal list B1", ..., Bn", G2', ..., Gm' Execute (recursively with this same procedure) 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. 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.

9

Suggest Documents