For Programmers: Free Programming Magazines  


Home > Archive > Prolog > April 2005 > newcomer question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author newcomer question
Anders Lindén

2005-04-10, 8:58 pm

Hello!

I have this scenario:
There is a lot of beer sorts.
There are also some pubs where they sell beer.
beerkind is a predicate that tells that a certain pub has a certain beer.
What I wanted to do was to write a predicate that can tell if there is a beer that is only sold in one pub.
Its used by writing onepub(X)., and seems to work.
It uses a predicate that can tell if a beer is sold in _many_ pubs, which is used by manypubs(X)..
However, it has the weakness of listing a certain beer many times!
How is that solved?

source follows:


beer(guinness).
beer(fiddlers_armbow).
beer(pripps_bla).
beer(pripps_yellow).
beer(fulblask).

pub(mcgills).
pub(sheers).
pub(pelles).
pub(nisses).
beerkind(mcgills, guinness). % mcgills has guinness
beerkind(mcgills, fiddlers_armbow).
beerkind(sheers, pripps_bla).
beerkind(sheers, guinness).
beerkind(nisses, pripps_yellow).
beerkind(pelles, pripps_yellow).
beerkind(pelles, guinness).

manypubs(X):- beerkind(A,X),beerkind(B,X),different(A,
B).
nopubs(X):- beer(X),not(beerkind(_,X)).
onepub(X):- beer(X),not(manypubs(X)),not(nopubs(X)).
not(X):- X,!,fail;true.
different(X,Y):- not(X=Y).


Jan Burse

2005-04-11, 8:57 am

> However, it has the weakness of listing a certain beer many times!
> onepub(X):- beer(X),not(manypubs(X)),not(nopubs(X)).


Should be the case, as not is deterministic.
Anders Lindén

2005-04-11, 8:57 am


"Jan Burse" <janburse@fastmail.fm> wrote in message news:425A2B16.3000308@fastmail.fm...
>
> Should be the case, as not is deterministic.



ok, then I wonder how to make it deterministic.

/Anders


Jan Burse

2005-04-11, 8:57 am



Jan Burse wrote:
>
>
>
> Should be the case, as not is deterministic.


Sorry I got a typo in my response, you should read:
Shouldn't be the case that onepub is non deterministic
for each X, as the not predicate implemented as negation
as failure is usually deterministic irrelevant of the goal
after the not.
Torkel Franzen

2005-04-12, 3:58 am

"Anders Lindén" <xxxx@xxx.xx> writes:

> However, it has the weakness of listing a certain beer many times!


By "it" I take it you mean manypubs. Which is it a weakness that
manypubs(X) finds guinness in different ways?
Anders Lindén

2005-04-12, 8:57 am

well, I just dont want guinness to show up more than once, its a demand from me :)

/Anders

"Torkel Franzen" <torkel@sm.luth.se> wrote in message news:vcb3btwsgu7.fsf@beta19.sm.ltu.se...
> "Anders Lindén" <xxxx@xxx.xx> writes:
>
>
> By "it" I take it you mean manypubs. Which is it a weakness that
> manypubs(X) finds guinness in different ways?



Anders Lindén

2005-04-12, 8:57 am


"Jan Burse" <janburse@fastmail.fm> wrote in message news:425A51DB.2070802@fastmail.fm...
>
>
> Jan Burse wrote:
>
> Sorry I got a typo in my response, you should read:
> Shouldn't be the case that onepub is non deterministic
> for each X, as the not predicate implemented as negation
> as failure is usually deterministic irrelevant of the goal
> after the not.



whieh means? ;)

/Anders


Bart Demoen

2005-04-12, 8:57 am

Anders Lindén wrote:

> However, it has the weakness of listing a certain beer many times!
> How is that solved?

[..]
> manypubs(X):- beerkind(A,X),beerkind(B,X),different(A,
B).


Hints:

X is sold in many pubs if
X is a beer
and
for X it is possible to prove once that two different pubs serve X

or

X is sold in many pubs if
X is a beer
and
the set of pubs serving X has at least two elements


Extra hints:
once/1
or
setof/3

Cheers

Bart Demoen
Jan Burse

2005-04-12, 8:57 am

Hi

Generally, if you have, i.e. a
surplus in variables in the body
of a rule:

p(X) :- q(X,Y)

This tends to generate multiple X,
because it acts like a projection,
it is logically equivalent:

p(X) :<=> exists Y(q(X,Y)).

If you use once/1, you destroi
the generating property of the
predikate, i.e.:

mode p(+), p(-)
p(X) :- q(X,Y)

But:

mode p(+)
p(X) :- once(q(X,Y)).

So you need something like the
DISTINCT in SQL. I think the
setof can do that.

Bye

Bart Demoen wrote:

> Anders Lindén wrote:
>
>
> [..]
>
>
>
> Hints:
>
> X is sold in many pubs if
> X is a beer
> and
> for X it is possible to prove once that two different pubs serve X
>
> or
>
> X is sold in many pubs if
> X is a beer
> and
> the set of pubs serving X has at least two elements
>
>
> Extra hints:
> once/1
> or
> setof/3
>
> Cheers
>
> Bart Demoen

tmp123

2005-04-12, 3:58 pm


Bart Demoen wrote:

> Extra hints:
> once/1
> or
> setof/3
>


Nothing else !

student

2005-04-12, 8:59 pm

Anders Lindén wrote:
> Hello!
>
> I have this scenario:


> There is a lot of beer sorts.


> There are also some pubs where they sell beer.


> beerkind is a predicate that tells that a certain pub has a certain beer.


> What I wanted to do was to write a predicate that can tell if there is a beer that is only sold in one pub.


>
> beer(guinness).
> beer(fiddlers_armbow).
> beer(pripps_bla).
> beer(pripps_yellow).
> beer(fulblask).
>
> pub(mcgills).
> pub(sheers).
> pub(pelles).
> pub(nisses).


> beerkind(mcgills, guinness). % mcgills has guinness
> beerkind(mcgills, fiddlers_armbow).
> beerkind(sheers, pripps_bla).
> beerkind(sheers, guinness).
> beerkind(nisses, pripps_yellow).
> beerkind(pelles, pripps_yellow).
> beerkind(pelles, guinness).
>


beer_sold_in_more_than_one_pub(Beer) :-
beerkind(Pub1, Beer),
( beerkind(Pub2, Beer), \+ Pub2 == Pub1 ).

beer_sold_in_only_one_pub(Pub1,Beer) :-
beerkind(Pub1, Beer),
... ?
George Sp

2005-04-12, 8:59 pm

Hi,
My guess is

onepub(X):- not(nopubs(X)), not(manypubs(X)).

Would it work? Not tested.
My thinking is "Beer is served in one pub only if the beer X is served
somewhere and only in one pub(not in many pubs)"
Just a guess :-).
And maybe manypubs/1 used in onepub/1 should be with a '!' (cut)? Something
like:

manypubs1(X):- !, beerkind(A,X),beerkind(B,X),different(A,
B).

Any good?

"Anders Lindén" <xxxx@xxx.xx> wrote in message
news:4259a20c$1@griseus.its.uu.se...
> Hello!
>
> I have this scenario:
> There is a lot of beer sorts.
> There are also some pubs where they sell beer.
> beerkind is a predicate that tells that a certain pub has a certain beer.
> What I wanted to do was to write a predicate that can tell if there is a
> beer that is only sold in one pub.
> Its used by writing onepub(X)., and seems to work.
> It uses a predicate that can tell if a beer is sold in _many_ pubs, which
> is used by manypubs(X)..
> However, it has the weakness of listing a certain beer many times!
> How is that solved?
>
> source follows:
>
>
> beer(guinness).
> beer(fiddlers_armbow).
> beer(pripps_bla).
> beer(pripps_yellow).
> beer(fulblask).
>
> pub(mcgills).
> pub(sheers).
> pub(pelles).
> pub(nisses).
> beerkind(mcgills, guinness). % mcgills has guinness
> beerkind(mcgills, fiddlers_armbow).
> beerkind(sheers, pripps_bla).
> beerkind(sheers, guinness).
> beerkind(nisses, pripps_yellow).
> beerkind(pelles, pripps_yellow).
> beerkind(pelles, guinness).
>
> manypubs(X):- beerkind(A,X),beerkind(B,X),different(A,
B).
> nopubs(X):- beer(X),not(beerkind(_,X)).
> onepub(X):- beer(X),not(manypubs(X)),not(nopubs(X)).
> not(X):- X,!,fail;true.
> different(X,Y):- not(X=Y).
>
>



George Sp

2005-04-13, 3:59 am


Hi,
My guess is

onepub(X):- not(nopubs(X)), not(manypubs(X)).

Would it work? Not tested.
My thinking is "Beer is served in one pub only if the beer X is served
somewhere and not in many pubs. Many is more than one."
Just a guess :-).
And maybe manypubs/1 used in onepub/1 should be with a '!' (cut)? Something
like:

manypubs1(X):- !, beerkind(A,X),beerkind(B,X),different(A,
B).

Any good?

"Anders Lindén" <xxxx@xxx.xx> wrote in message
news:4259a20c$1@griseus.its.uu.se...
> Hello!
>
> I have this scenario:
> There is a lot of beer sorts.
> There are also some pubs where they sell beer.
> beerkind is a predicate that tells that a certain pub has a certain beer.
> What I wanted to do was to write a predicate that can tell if there is a
> beer that is only sold in one pub.
> Its used by writing onepub(X)., and seems to work.
> It uses a predicate that can tell if a beer is sold in _many_ pubs, which
> is used by manypubs(X)..
> However, it has the weakness of listing a certain beer many times!
> How is that solved?
>
> source follows:
>
>
> beer(guinness).
> beer(fiddlers_armbow).
> beer(pripps_bla).
> beer(pripps_yellow).
> beer(fulblask).
>
> pub(mcgills).
> pub(sheers).
> pub(pelles).
> pub(nisses).
> beerkind(mcgills, guinness). % mcgills has guinness
> beerkind(mcgills, fiddlers_armbow).
> beerkind(sheers, pripps_bla).
> beerkind(sheers, guinness).
> beerkind(nisses, pripps_yellow).
> beerkind(pelles, pripps_yellow).
> beerkind(pelles, guinness).
>
> manypubs(X):- beerkind(A,X),beerkind(B,X),different(A,
B).
> nopubs(X):- beer(X),not(beerkind(_,X)).
> onepub(X):- beer(X),not(manypubs(X)),not(nopubs(X)).
> not(X):- X,!,fail;true.
> different(X,Y):- not(X=Y).
>
>



George Sp

2005-04-13, 3:59 pm

Hi,
My guess is

onepub(X):- not(nopubs(X)), not(manypubs(X)).

Would it work? Not tested.
My thinking is "Beer is served in one pub only if the beer X is served
somewhere and not in many pubs. Many is more than one."
Just a guess :-).
And maybe manypubs/1 used in onepub/1 should be with a '!' (cut)? Something
like:

manypubs1(X):- !, beerkind(A,X),beerkind(B,X),different(A,
B).

Any good?
From so much beer I got drunk ;-).

"student" <nospam@a.b.c.dINVALID> wrote in message
news:ewX6e.14348$m31.138709@typhoon.sonic.net...
> Anders Lindén wrote:
>
>
>
>
>
>
>
> beer_sold_in_more_than_one_pub(Beer) :-
> beerkind(Pub1, Beer),
> ( beerkind(Pub2, Beer), \+ Pub2 == Pub1 ).
>
> beer_sold_in_only_one_pub(Pub1,Beer) :-
> beerkind(Pub1, Beer),
> ... ?


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com