Existential Quantification in Prolog Facts

I am taking my first steps in Prolog (swi-prolog) and cannot solve the following problem: how can I include in my actual quantitative rules; in particular, how can I include the sentence "All each other" \forall x \exists y friend(x,y)

as fact? Every question I've found so far is only about queries, not facts. Thank!

+3


source to share


1 answer


In the example you provided, you are actually quantifying variables, not rules. With this in mind, consider the following example:

friend_of(a,b).

friend(X) :-
   friend_of(X,Y).

      

The variables in a rule are universal, so you can write the rule as a boolean formula like this:

X

Y

(friend(X)

friend_of(X,Y))

Since the variable Y

does not appear in the rule chapter, its universal quantifier can be moved to the rule body as an existential quantifier:

X

(friend(X)

← ∃Y

friend_of(X,Y))

Now this formula reads as: Forall X

friend(X)

is true if there Y

is something that is friend_of(X,Y)

true. This looks like what you wanted.

If you consider facts on the other hand, they are used to state that something is happening. The friend_of / 2 fact in the above example is just a short way of writing

friend_of(a,b) :- true.

      

However, there are no variables here, so nothing is needed to quantify.

EDIT: Regarding the case in your comments, I would like to point out that predicates are relationships. The relationship is not necessarily symmetric, so I named the relationship friend_of / 2. That is, it friend_of(a,b)

doesn't necessarily mean friend_of(b,a)

. Relationships are also not necessarily reflective. It is debatable whether a friend's attitude is reflective or not. However, this is certainly possible. With this in mind, and the examples in your comments, we assume that you have some facts, describing a

, b

and c

as humans, for example:

person(a).
person(b).
person(c).

      

Then you can describe the reflexive relationship of friends / 2 as follows:



friends(a,b) :- false.   % example from your comment
friends(a,c) :- false.   % example from your comment
friends(X,X) :-          % the relation is reflexive
   person(X).            % among people

      

The rule expressing reflexivity basically states that all friends are at least with him. From this rule, your requirement "Everyone" is someone close to you. If you query this relationship, you get the desired output:

   ?- friends(a,X).
X = a

      

The most general query also gives results for each person, although the actual friendship between two different people is not listed:

   ?- friends(X,Y).
X = Y = a ? ;
X = Y = b ? ;
X = Y = c

      

Note that the facts / 1 character is needed to limit the answers to actual people. If you are asking friends / 2 with someone not human:

   ?- friends(cos(0),X).
no

      

If you try to define reflexivity without such a goal:

friend(X,X).

      

your definition would be too general:

   ?- friends(a,X).           % desired result
X = a
   ?- friends(cos(0),X).      % undesired result
X = cos(0)

      

And the most general query won't call real people:

   ?- friends(X,Y).
X = Y

      

+5


source







All Articles