Newbie question...simple rule question... - PROLOG

This is a discussion on Newbie question...simple rule question... - PROLOG ; Dear All, I am trying to understand the best way to write a rule which checks on all the elements. ------------- people(a). people(c). ok(b). ok(a). everybodyok_works :- findall(X,ok(X),List),maplist(people,List). everybodyok_bad :- people(X),ok(X). ------------- What am I missing? Is there a better ...

+ Reply to Thread
Results 1 to 7 of 7

Newbie question...simple rule question...

  1. Default Newbie question...simple rule question...

    Dear All,

    I am trying to understand the best way to write a rule which checks on
    all the elements.

    -------------
    people(a).
    people(c).
    ok(b).
    ok(a).

    everybodyok_works :- findall(X,ok(X),List),maplist(people,List).

    everybodyok_bad :- people(X),ok(X).

    -------------

    What am I missing? Is there a better way to write rule which says:
    "Everybody is ok if for all people we know has said they are OK." ?

    Thanks in Advance,
    Mans


  2. Default Re: Newbie question...simple rule question...

    On 2005-08-11, mans <mshapshak@gmail.com> wrote:
    > Dear All,
    >
    > I am trying to understand the best way to write a rule which checks on
    > all the elements.
    >
    > -------------
    > people(a).
    > people(c).
    > ok(b).
    > ok(a).
    >
    > everybodyok_works :- findall(X,ok(X),List),maplist(people,List).
    >
    > everybodyok_bad :- people(X),ok(X).
    >
    > -------------
    >
    > What am I missing? Is there a better way to write rule which says:
    > "Everybody is ok if for all people we know has said they are OK." ?


    Prolog tries to find instances that satisfy some condition. Think
    from the other side. Everybody is ok iff there is nobody who is not
    ok.

    Cheers --- Jan

  3. Default Re: Newbie question...simple rule question...

    Dear Jan,

    Thanks for you quick reply.

    I am trying to make my prolog as similar to the natural language as
    possible and hence deal with the way the actual user is
    thinking...which in this case may be the reverse of the way prolog
    thinks??.

    But in any case how about this if I stick withe same input
    declarations:

    notok(X) :- \+ok(X).
    everybodyok_new :- not((people(X),notok(X))).

    Which seems to work..although I am not sure what the meta call means:

    [trace] 125 ?- everybodyok_new.
    Call: (7) everybodyok_new ? creep
    ^ Call: (8) not((people(_G506), notok(_G506))) ? creep
    Call: (10) people(_G506) ? creep
    Exit: (10) people(a) ? creep
    Call: (10) notok(a) ? creep
    Call: (11) ok(a) ? creep
    Exit: (11) ok(a) ? creep
    Fail: (10) notok(a) ? creep
    Redo: (10) people(_G506) ? creep
    Exit: (10) people(c) ? creep
    Call: (10) notok(c) ? creep
    Call: (11) ok(c) ? creep
    Fail: (11) ok(c) ? creep
    Exit: (10) notok(c) ? creep
    Exit: (9) '<meta-call>'((people(c), notok(c))) ? creep
    ^ Fail: (8) not((people(_G506), notok(_G506))) ? creep
    Fail: (7) everybodyok_new ? creep

    Thanks again!
    Mans


  4. Default Re: Newbie question...simple rule question...

    On 2005-08-11, mans <mshapshak@gmail.com> wrote:
    > Dear Jan,
    >
    > Thanks for you quick reply.
    >
    > I am trying to make my prolog as similar to the natural language as
    > possible and hence deal with the way the actual user is
    > thinking...which in this case may be the reverse of the way prolog
    > thinks??.
    >
    > But in any case how about this if I stick withe same input
    > declarations:
    >
    > notok(X) :- \+ok(X).
    > everybodyok_new :- not((people(X),notok(X))).
    >
    > Which seems to work..although I am not sure what the meta call means:
    >
    > [trace] 125 ?- everybodyok_new.
    > Call: (7) everybodyok_new ? creep
    > ^ Call: (8) not((people(_G506), notok(_G506))) ? creep
    > Call: (10) people(_G506) ? creep
    > Exit: (10) people(a) ? creep
    > Call: (10) notok(a) ? creep
    > Call: (11) ok(a) ? creep
    > Exit: (11) ok(a) ? creep
    > Fail: (10) notok(a) ? creep
    > Redo: (10) people(_G506) ? creep
    > Exit: (10) people(c) ? creep
    > Call: (10) notok(c) ? creep
    > Call: (11) ok(c) ? creep
    > Fail: (11) ok(c) ? creep
    > Exit: (10) notok(c) ? creep
    > Exit: (9) '<meta-call>'((people(c), notok(c))) ? creep


    This is an artifact introduced by SWI-Prolog to deal with complex
    goals (i.e. goals with control structures) given to call/1, once/1,
    etc.

    > ^ Fail: (8) not((people(_G506), notok(_G506))) ? creep
    > Fail: (7) everybodyok_new ? creep


    Now that you understand this, you are ready for the more
    readable form:

    everybodyok :- forall(people(X), ok(X)).

    And for the definition of forall/2 (a built-in):

    forall(Cond, Action) :-
    \+ (Cond, \+ Action).

    Enjoy --- Jan

  5. Default Re: Newbie question...simple rule question...

    Jan,
    Thanks!
    Mans
    P.S. Just out of curiosity. Internally does prolog use go through the
    list similar to findall(X,ok(X),List),maplist(­people,List).?


  6. Default Re: Newbie question...simple rule question...

    On 2005-08-11, mans <mshapshak@gmail.com> wrote:
    > Jan,
    > Thanks!
    > Mans
    > P.S. Just out of curiosity. Internally does prolog use go through the
    > list similar to findall(X,ok(X),List),maplist(-people,List).?


    No :-) \+/1 can be implemented as

    \+(G) :- G, !, fail.
    \+(_).

    In fact the compiler handles \+, but that doesn't change things too much.

    Cheers --- Jan


  7. Default Re: Newbie question...simple rule question...

    Try this, and let me know if it works please:

    everybodyok :- (
    people(X),
    ( ok(X)
    -> fail
    ; !, fail
    )
    ; true
    ).

    Regards,
    M


    "mans" <mshapshak@gmail.com> ha scritto nel messaggio
    news:1123762484.841598.229320@g44g2000cwa.googlegroups.com...
    > Dear All,
    >
    > I am trying to understand the best way to write a rule which checks on
    > all the elements.
    >
    > -------------
    > people(a).
    > people(c).
    > ok(b).
    > ok(a).
    >
    > everybodyok_works :- findall(X,ok(X),List),maplist(people,List).
    >
    > everybodyok_bad :- people(X),ok(X).
    >
    > -------------
    >
    > What am I missing? Is there a better way to write rule which says:
    > "Everybody is ok if for all people we know has said they are OK." ?
    >
    > Thanks in Advance,
    > Mans
    >




+ Reply to Thread

Similar Threads

  1. Newbie Question: Reading older grammar rule code...
    By Application Development in forum PROLOG
    Replies: 2
    Last Post: 09-18-2006, 06:17 AM
  2. Simple flicker question (newbie)
    By Application Development in forum DOTNET
    Replies: 2
    Last Post: 11-16-2005, 06:46 AM
  3. Simple Newbie question
    By Application Development in forum Graphics
    Replies: 5
    Last Post: 10-18-2005, 10:53 AM
  4. Simple question from a newbie
    By Application Development in forum Microsoft Exchange
    Replies: 0
    Last Post: 09-21-2005, 09:56 AM
  5. Simple newbie question
    By Application Development in forum C
    Replies: 3
    Last Post: 02-16-2005, 05:35 AM