Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Sunday, 18 January 2009

Lists

The list is a simple data structure widely used in non-numeric programming.
A list is a sequence of any number of items, such as ann, tennis, tom, skiing. Such a list can be written in Prolog as:
[ann,tennis,tom,skiing]
Lists can be empty or non-empty. Empty list is shown as [] atom. Non-empty list can be viewed as consisting of two things:
The first item called head,
The remaining part called tail.

Unification

How do we ask: ''what does Fred eat ?'‘
eats(fred,oranges).
How do we ask: “what Fred eats” ?
?- eats(fred,what).
But Prolog will say “no” to this question. The reason is that Prolog can't find the relation “eats(fred,what)” in its database.
In this case we have to use a variable which will be unified to match a relation given in the program. This process is known as unification.


Now that we know how to use a variables, we can ask the same question as before using the variable What instead of an atom.
?- eats(fred,What).
What=orange
Yes
In this case Prolog try to unify the variable with an atom. ''What=oranges'' means that the query is successfull when “what” is unified with oranges.


With the same program if we ask :
?- eats(Who,oranges).
Which means who eats oranges. To this query Prolog should answer :
?- eats(Who,oranges).
Who=fred
yes

Monkey and Banana

A monkey is in a room. Suspended from the ceiling is a bunch of bananas, beyond the monkey's reach. In the corner of the room is a box.
How can the monkey get the bananas?
The solution is that the monkey must push the box under the bananas, then stand on the box, and then grab the bananas.
In other variants of the problem, the bananas are in a chest and the monkey first has to open the chest using a key.


move( state(middle, onbox, middle, hasnot),grasp, % Grasp banana
state(middle, onbox, middle, has) ).

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

% canget(State,Moves): monkey in State can get banana by performing Moves

canget( state(_,_,_,has), [] ).
canget(State1,[M|List]) :- move(State1,M,State2), canget(State2,List).
solve(Moves) :- canget(state(atdoor,onfloor,atwindow,hasnot),Moves).


For help, use ?- help(Topic). or ?- apropos(Word).

?- [mb].
% mb compiled 0.00 sec, 2,280 bytes
Yes

?- solve(Moves).
Moves = [walk(atdoor, atwindow), push(atwindow, middle), climb, grasp]
Yes

First Program

Rules:
In order to work, people must be full and delighted.
work(X) :- delighted(X), full(X).
In order to be full, people must eat something.
full(X) :- ate(X).
In order to be delighted, people must sleep and have fun.
delighted(X) :- slept(X), fun(X).
Facts:
Mustafa ate.
ate(mustafa).
Mustafa slept.
slept(mustafa).
Mustafa had fun.
fun(mustafa).
Query:
Can Mustafa work?




Facts:
eats(fred,oranges).
eats(tony,apple).
eats(john,apple).
Queries:
?- eats(fred,oranges). /* yes
?- eats(john,apple). /*yes
?- eats(mike,apple). /*no