0

I'm new to Prolog, and I'm having trouble understanding the OR operator ";" so this is an example below:-

/*attributes(Person,Eats,Footwear).*/

attributes( personA,
        eats(fried;baked),
        footwear(shoes;slippers)
       ).

attributes( personB,
        eats(roasted;baked),
        slippers
       ).

person(Person, Eats, Footwear) :-

attributes(Person,
        Eats,
        Footwear
        ).

so I need to differentiate between personA and personB, eg if I put in the query

person(Person, roasted, _).

since only personB has the attribute roasted, it should return Person = personB

then, eg if I put in the query

person(Person, baked, _).

since both A and B has the attribute baked, it should return Person = personA Person = personB

Can anyone explain how to properly make the rules for this. Thanks.

false
  • 10,264
  • 13
  • 101
  • 209
Akbar Mna
  • 1
  • 2

1 Answers1

1

To get the result you want, you have to rewrite the knowledge base in this way (you cannot use ; inside a term):

footwear(personA,slippers).
footwear(personA,shoes).
footwear(personB,slippers).

eats(personA,fried).
eats(personA,baked).
eats(personB,roasted).
eats(personB,baked).

Then you query:

?- eats(Person,roasted).
Person = personB.

?- eats(Person,baked).
Person = personA
Person = personB

If you are into probabilistic programming, you can translate this program into a probabilistic program to get maybe some interesting results.

damianodamiano
  • 2,528
  • 2
  • 13
  • 21
  • HI, for this sort of approach how would I write a query to compare two data, like "person(Person, baked, shoes)." – Akbar Mna Feb 12 '18 at 08:11
  • What should be the result of person(Person, baked, shoes)? What do you want to find with this query? – damianodamiano Feb 12 '18 at 08:13
  • it would return Person = personA since he eats baked, and wear shoes. – Akbar Mna Feb 12 '18 at 08:15
  • 1
    Simply `?-eats(Person,baked), footwear(Person,shoes).` – damianodamiano Feb 12 '18 at 08:18
  • wow, that makes sense. I completely missed that. Thanks. anyways since the OR operator is not to be used in a term, where is it used? – Akbar Mna Feb 12 '18 at 08:21
  • This answer could help you: https://stackoverflow.com/questions/4115021/predicate-control-in-prolog – damianodamiano Feb 12 '18 at 08:26
  • 1
    @AkbarMna technically, you *can* use `;` anywhere you like. The question is: where does Prolog actually apply an *interpretation* of it as an operator. That happens in the body of a predicate, or at the command line. So you can say, `?- eats(Person, baked) ; eats(Person, fried).` and this will show you all `Person` values where that person eats baked or that person eats fried. But if you write `eats(baked ; fried)`, Prolog does *not* interpret `;`. It's just a functor, and it sees this as `eats(';'(baked, fried))`. This may seem odd, but it allows you to write predicates to reason about `;`. – lurker Feb 12 '18 at 21:03