Need Help for small Prolog Exercise 
Author Message
 Need Help for small Prolog Exercise

Hi Prolog freaks,

I am studiing computer science in Mannheim(Germany). I have to do an
exercise in Prolog and I need some help to solve it. Maybe you can send me
some  hints or better a (documented) listing of the prolog program.

Here are the exercise:

In a street are 5 houses.
The house are painted in different colours.
Man of different nationalities are living in the houses.
Every man has his favourite drink, animal and cigarette.

The facts are:

- The english man lives in the red house.
- The spanish man owns the dog.
- In the green house it always smells like coffee.
- The man who smokes 'Camel' owns chicken.
- The Ukrainian drinks tea.
- The green house is right beside the white house.
- The man who smokes 'Lord' lives in the yellow house.
- The man in the middle house drinks milk.
- The man from Norway lives in the first house.
- The man who smokes 'Marlboro' lives beside the man who owns the fox.
- The man who smokes 'Lord' lives beside the man who owns the horse.
- The man who smokes 'HB' drinks wine.
- The japanese smokes 'Lucky Strike' .
- The man from norway lives beside the blue house.

The Questions:
1. Who drinks the Water?
2. Who owns the elefant?

So, if there is a person who likes to solve these kind of problems, I would
like to hear from you.

        Dagobert Kellner



Tue, 24 Oct 1995 18:45:29 GMT  
 Need Help for small Prolog Exercise

Quote:
> I am studiing computer science in Mannheim(Germany). I have to do an
> exercise in Prolog and I need some help to solve it. Maybe you can send me
> some  hints or better a (documented) listing of the prolog program.

There are lots of Prolog programs written to solve this (and similar)
puzzles.  It is not a very hard program to write, and I really think
that you shall solve it yourself.

And, sending a documented program listing?  Is that how you usually do
your exercises?  By copying others work?  Why are you then studying?
Why not let someone else do it instead?

--
Roland Karlsson             SICS, PO Box 1263, S-164 28 KISTA, SWEDEN

Telex: 812 6154 7011 SICS   Ttx: 2401-812 6154 7011=SICS



Tue, 24 Oct 1995 21:09:16 GMT  
 Need Help for small Prolog Exercise
Here is a copy of the assignment that I have given to my students here.
The assignment statement has enough hints to solve the problem.
However, I must warn that there are simpler ways to solve the
problem. Also, the assignment statement was prepared on a diffierent
machine with a wordprocessor not available on UNIX. Some character
in the text below may need alteration.

Please do not post any solutions. Thanks.

 SCS 233: Foundations of Artificial Intelligence
        Department of Computer science
        University of Tasmania, Hobart.                 1 May 1993
        Assignment 3
Due date: 28 May 1993.
Score: 30%

In this assignment you will use several methods and skills that have been
learned in this course.  The problem involves a set of simple sentences that
describe the occupants of (all) five houses on a street.  You will write DCG
rules to read these sentences and assert Prolog clauses that capture the
meaning/information contained in these sentences. A search algorithm will
be used to search and find the name of the person satisfying  the given query.

The 5 houses on the street are of different colours and are inhabited by
persons from different countries. These persons drive cars of different
brands, own different pets and have different names. The sentences describing
the houses are as follows:

the person in the red house is from england.
the person from spain drives a suzuki.
the person in the first house is from australia.
diana lives in a yellow house.
john lives  next to a house with a nissan.
the person from australia lives next to a blue house.
susan owns a holden.
peter owns an elephant.
the dog owner is from russia.
saddam is from iraq.
diana lives next to a house with a toyota.
the person in the green house owns a cat.
the blue house is to the right of the yellow house.
the green house is to the right of an ivory house.
the green house is next to the ivory house.
the pet in the third house is a rabbit.
someone owns a bird.
someone owns an kingswood.

Create a file named text by typing in the above text.  Some explanation
of some of the terms/phrases used in the above text is in order:
1. Houses are numbered left to right.
2. Determiners a, an and the have been used interchangeably.
3. Next house means adjacent house to the left or right.
4. To the right of means any one of the houses on the right.
5. Assume that the language has no other grammatical structures beyond
those used in the above sentences.
6. The vocabulary  for the language must contain, beside the words
used in the above text, the following word: who.

Create a file called facts by typing in the following Prolog facts:

        :-  op(100,xfx,:).
        samehouse(colour : red, nation : england).
        samehouse(nation : spain, car : suzuki).
        samehouse(no : 1, nation : australia).
        samehouse(name : diana, colour : yellow).
        nexthouse(name : john, car : nissan).
        nexthouse(nation : australia, colour : blue).
        samehouse(name : susan, car : holden).
        samehouse(name : peter, pet : elephant).
        samehouse(pet : dog, nation : russia).
        samehouse(name : saddam, nation : iraq).
        nexthouse(name : diana, car : toyota).
        samehouse(colour : green, pet : cat).
        righthouse(colour : blue, colour : yellow).
        righthouse(colour : green, colour : ivory).
        nexthouse(colour : green, colour : ivory).
        samehouse(no : 3, pet : rabbit).
        somehouse(pet : bird).
        somehouse(car : kingswood).

STEP 1 (5 Marks):
Write a procedure for predicate getText/2. An execution of goal
getText(FileName, Text) must read the sentences from file FileName
and return Text. Text is the list of words and punctuation marks from
the file FileName.

STEP 2 (5 Marks):
Define a set of  DCG rules that describes the grammar of the sentences
listed above. The rules should have enough syntactic entities to enable
you to isolate all syntactic structures of interest from the sentences.
In some cases it may be helpful to treat phrases as basic units;
for example, to the right of may be treated as a basic atomic unit
(word) of the language.

Define predicate readText/1  such that goal readText(text) reads the
sentences from the file text and succeeds if every sentence in the file
is grammatically correct.

 STEP 3 (5 Marks):
The meaning of these sentences can be encoded as Prolog facts using
the following predicates:
        samehouse/2,
        nexthouse/2,
        somehouse/1, and
        righthouse/2
For example, the meaning of the sentence "a person from spain
drives a suzuki." is captured by the fact: samehouse( nation:spaniard,
car:suzuki). Include semantic actions with the DCG rules so that  it
displays, on the terminal, the  meaning of each parsed sentence.
If you have done everything correctly, so far, then the displayed text
should be similar to the one in file facts.

STEP 4 (5 Marks):
The following is a possible term for representing a house:
house(no:Number,name:Name,nation:Nation,pet:Pet,colour:Colour,car:Car).
We say that the (value of the) variables specify the attributes of
the house.  Structure Street as a list containing  5 terms --
one for each house. In its initial state Street consists of 5 houses
with only their Numbers instantiated. All other variables are in their
uninstantiated state.

 A variable in term Street can either be uninstantiated or have one of
the 5 possible values. The state of various variables in term Street
define the state of term Street.The term Street can appear in one of
the large number of possible states.  (Would you care to estimate of
the number of possible states of term Street? My estimate: ((6!)/2)5) From
a state of Street one can determine its next state. The new state for term
Street is derived by  instantiating a variable that was  uninstantiated
in the previous state of the term.

This suggests that we can model the problem of determining all attributes
of each house in term Street as a search problem. The predicates used
to capture the meanings of the sentences, namely, samehouse/2,
nexthouse/2, somehouse/1, and righthouse/2, can be used to define the
next state predicate, s/1. An interesting scheme to define s/1
is as follows:

The goal s(Street) fails (i.e., Street has no next state) if
(a) An attribute value appears more than once in Street. Or,
(b) Any of the restrictions imposed by the predicates samehouse/2,
nexthouse/2 or righthouse/2 is violated.

Otherwise, the goal s(Street) succeeds and returns a next state of term
Street if there is a house with an uninstantiated variable (undetermined
attribute). The next state is determined as described by the following cases:

(a) This case applies if samehouse(Property1,Property2) is a fact  and
Property1 (resp. Property2) is in term Street but Property2 (resp. Property1)
is not in term Street.  In this case the next state of term Street is
unique. This is obtained by including Property2 (resp. Property1)  into  
term Street as an attribute of the house with Property1 (resp. Property2).

(b) This case applies if (a) does not apply and if nexthouse(Property1,
Property2) is a fact and Property1 (resp. Property2) is in
term Street but Property2 (resp. Property1) is not in term Street.
There are two possible next states. These are obtained by including Property2
(resp. Property1) in  term Street in a house adjacent to the one
with Property1 (resp. Property2).

(c) This case applies if neither (a) nor (b) applies and if
righthouse(Property1,Property2) is a fact and Property1 (respectively
Property2) is in term Street but Property2 (resp. Property1)  is not in
term Street. There may be unto 4 next states for term Street.
These are obtained by including Property2 (resp. Property1) in term
Street as an attribute of a house to the right (resp. left) of the house
with Property1 (resp. Property2).

(d) This case applies if none of the cases (a), (b) or (c) apply and if
Property appears as an argument in a fact describing samehouse/2,
somehouse/1, nexthouse/2, or righthouse/2 and Property is not an attribute
of any house in term Street. There may be up to 5 next states for term  Street.
These are obtained by including Property as an attribute of a house in term
Street.

Define a procedure for s/1. If you have not yet completed Step 3 then
you may decide to consult file facts here.

Advice: It will help if you develop procedures for the following predicates:

has_attribute(House,Domain:Value)  The goal fails if attribute Domain in
House is uninstantiated.  Otherwise, the goal succeeds if  attribute  Domain
in House  is already  instantiated and has value that matches Value.
This predicate is also useful in locating the house with a given property
and  in determining the value for an attributes of a house.
rejects_attribute(House,Domain: Value) Succeeds if Value does not match
the corresponding  attribute of  House.
set_attribute(House,Domain: Value)  Assigns Value to   attribute Domain of  
House. Normally, this goal will be called only if neither has_attribute(House,
Domain: Value) nor rejects_attribute( House,Domain: Value) can succeed.

To determine if term Street obeys some constraint  (say)
samehouse(Prop1, Prop2)  one proceeds as follows:
Get the house with property Prop1,  (say) House1.
Get the number attribute of House1, (say) Number1.
Similarly, determine Number2 using Prop2.
Samehouse constraint is violated if Number1 is not same as Number2.

STEP 5 (5 Marks):
Use a suitable search algorithm (Hint: dfs. Why?) to assign values to
all variables in term Street. This step will require you to write a procedure
to detect the goal (final) state of the term Street.

STEP 6 (5 Marks):
Organise the program so that it lets the user to make a query and get
answer in a natural language sentences. Example queries are:
        Who owns a cat.
        Who drives a holden.
        Who is from spain.
        Who lives in the yellow house.
A question can be answered by replacing the word who by name of the
person satisfying the query. The ...

read more »



Sun, 29 Oct 1995 08:07:30 GMT  
 Need Help for small Prolog Exercise

Quote:
> [Blah ...]
> the person in the red house is from england.
> the person from spain drives a suzuki.
> the person in the first house is from australia.
> diana lives in a yellow house.
> john lives  next to a house with a nissan.
> the person from australia lives next to a blue house.
> susan owns a holden.
> peter owns an elephant.
> the dog owner is from russia.
> saddam is from iraq.
> diana lives next to a house with a toyota.
> the person in the green house owns a cat.
> the blue house is to the right of the yellow house.
> the green house is to the right of an ivory house.
> the green house is next to the ivory house.
> the pet in the third house is a rabbit.
> someone owns a bird.
> someone owns an kingswood.
> [Blah ...]

I thought the final question to this problem was "Who owns a zebra?"

Jacob

--
=============================================================================
Jacob L. Cybulski,  Deputy  Director,  Amdahl  Australian  Intelligent  Tools
Programme,  Dept of  Comp Sci  &  Comp Eng,  La Trobe  University,  Bundoora,
Vic. 3083, Australia. Ph: +613 479 1270, Fax: +613 470 4915, Telex: AA 33143,

=============================================================================



Sun, 29 Oct 1995 14:45:31 GMT  
 Need Help for small Prolog Exercise

Quote:
Malhotra) writes:

#  The following is a possible term for representing a house:
#
# house(no:Number,name:Name,nation:Nation,pet:Pet,colour:Colour,car:Car).

A uniquely poor representation of a house, I think.  What's wrong with

     house(Number,Name,Nation,Pet,Colour,Car)

The ':'/2 terms only confuse things.  If you keep the arguments in the
same order, they don't need `tags' like no, name.  If you allow the
arguments to come in different orders, you'll make it almost impossible
to write the program.

Ken Johnson

--
Son, all the pretty, intelligent, healthy     # Ken Johnson, AIAI, Edinburgh
young women are taken. It's a basic rule of   # tel 031 650 2756, fax 650 6513
the universe, and if you don't like it, go    # Seeking job: C, Prolog, Unix,
somewhere else.        -- my dad  1906-1992   # Dos, training, documentation.



Tue, 31 Oct 1995 18:59:11 GMT  
 Need Help for small Prolog Exercise

Quote:
> > house(no:Number,name:Name,nation:Nation,pet:Pet,colour:Colour,car:Car).
> A uniquely poor representation of a house, I think.  What's wrong with

Sorry, Ken, but there can be no uniquely poor representations of anything
in Prolog, as I'm sure some of our more theoretically-minded readers will
be pleased to prove :-)

Paul S.



Tue, 31 Oct 1995 21:10:26 GMT  
 Need Help for small Prolog Exercise
 Hello,

    I am giving a talk on "Why logic and  functional  programming  has
failed in Industry".

    Could anybody (preferably  working in  industry) who has tried  to
introduce LP or FP into  industry and who has *failed* in this attempt
tell me why they think this was so. Were there any single events which
contributed to the failure? - please provide specific examples.

   Joe Armstrong



Sat, 04 Nov 1995 22:08:29 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Need help with a prolog exercise...

2. Need help on exercise in Miranda

3. Need some help to solve some exercises

4. S-Prolog/Small Prolog from CUG ?

5. Core Python Programming, chapter 5 exercises number 5-3, question on this exercise

6. cw 2.0003, filter box on report properties is too small, need help...mucho

7. Help! Need Eiffel small example programs

8. Need help with small example

9. need help with small utility

10. Help needed writing small program -- possible?

11. Need help on small programming problem.

12. [Need help: Clock function in smaller unit...

 

 
Powered by phpBB® Forum Software