Is this first order logic representation correct? - logic

I need am going through one online video for First Order Logic,
the statement to convert in FLO is
Everyone loves its mother.
The instructor writes it like this
(For All x)(There exists y) Mother(x,y) and Loves(x,y)
If x is person1,person2,person3, it will work fine.
However if x is table, it won't work. So using For all X does not seem right to me,here. Could someone explain to me what I am doing wrong?

For simplicity, the instructor assumed that the domain is only humans. Ofcourse, what you are saying is correct. Other statements should be added e.g. Person(x) and Person(y) to make this more specific.

you are right, the mistake is in (there exist y) because if x is a table does not exist a mother. I think the correct answer may be
(For All x)(for all y)(if Mother(x,y) than Loves(x,y))
or, just tobe sure to be understood
(For All x)(for all y)(Mother(x,y) --> Loves(x,y))
It means that for every x and y (even for tables) if y is the mother of x than x loves his mother, that is the assertion you had in the beginning.
Where is the artificial intelligence part?
(Sorry for my english)

Related

Recursive reference in prolog

I meet some problem when I try to implement
friends(mia, ellen).
friends(mia, lucy).
friends(X,Y) :-
friends(X,Z),
friends(Y,Z).
and when i ask ?- friends(mia, X)., it run out of local stack.
Then I add
friends(ellen, mia) friends(lucy, mia)
I ask ?- friends(mia, X). ,it keeps replying X = mia.
I can't understand, why it is recursive?
First, two assumptions:
the actual code you wanted to write is the following one, with appropriate dots:
friends(mia,ellen).
friends(mia,lucy).
friends(X,Y) :-
friends(X,Z),
friends(Z,Y).
transivity holds: friends of friends are my friends too (I would rather model friendship as a distance: "A is near B" and "B is near C" does not necessarly imply "A is near C"). repeat's answer is right about figuring out first what you want to model.
Now, let's see why we go into infinite recursion.
Step-by-step
So, what happens when we ask: friends(mia,X) ?
First clause gives Y=ellen (you ask for more solutions)
Second clause gives Y=lucy (you ask again for more solutions)
Stack overflow !
Let's detail the third clause:
I want to know if friends(mia,Y) holds for some variable Y.
Is there a variable Z such that friends(mia,Z) holds ?
Notice that apart from a renaming from Y to Z, we are asking the same question as step 1 above? This smells like infinite recursion, but let's see...
We try the first two clauses of friends, but then we fail because there is no friends(ellen,Y) nor friends(lucy,Y), so...
We call the third clause in order to find if there is a transitive friendship, and we are back to step 1 without having progressed any further => infinite recursion.
This problem is analogous to infinite Left recursion in context-free grammars.
A fix
Have two predicates:
known_friends/2, which gives direct relationships.
friends/2, which also encodes transitivity
known_friends(mia,ellen).
known_friends(mia,lucy).
friends(X,Y) :- known_friends(X,Y).
friends(X,Y) :- known_friends(X,Z), friends(Z,Y).
Now, when we ask friends(mia,X), friends/2 gives the same answer as the two clauses of known_friends/2, but does not find any answer for the transitive clause: the difference here is that known_friends will make a little progress, namely find a known friend of mia (without recursion), and try to find (recursively) if that friend is a friend of some other people.
Friends' friends
If we add known_friends(ellen, bishop) :-) then friends will also find Y=bishop, because:
known_friends(mia,ellen) holds, and
friends(ellen,bishop) is found recursively.
Circularity
If you add cyclic dependencies in the friendship graph (in known_friends), then you will have an infinite traversal of this graph with friends. Before you can fix that, you have to consider the following questions:
Does friends(X,Y) <=> friends(Y,X) hold for all (X,Y) ?
What about friends(X,X), for all X ?
Then, you should keep a set of all seen people when evaluating friends in order to detect when you are looping through known_friends, while taking into account the above properties. This should not be too difficult too implement, if you want to try.
This clause of friends/2 is flawed:
friends(X,Y) :- friends(X,Z),friends(Y,Z).
Translate that into English: "If X and Y have a mutual friend Z, then X and Y are friends."
Or, let's specialize, let X be "me", let Y be my neighbour "FooBert", and let Z be "you": So if I am your friend and FooBert is your friend... does that make me and FooBert friends? I don't think so, I hate that guy---he always slams the door when he gets home. :)
I suggest you consider the algebraic properties that the relation friends/2 should have, the ones it may have, and ones it should not have. What about reflexivity, symmetry, anti-symmetry, transitivity?

Prolog rules structure and its difference

I'm confused about the structure of how my Prolog rules should be written.
Lets say, I want to state that only birds can fly.
Should my rule be written as
fly(X):- bird(X).
or
bird(X):-fly(X).
What is the difference in meaning between them?
Furthermore, do I need to explicitly state that if the entity is not a bird, it cannot fly?
Also, if I wish to say...
I can either go to the zoo or the library. Either zoo or library, but not both. How should it be?
I am assuming it to be..
go_there(X,Place):-
go_there(X,zoo),
go_there(X,library).
Please go gentle on me, as this is my first try with Prolog programming! Thank you!
To say that only birds can fly can be expressed as, if a creature can fly, then it must be a bird. Depending upon the semantics of this terse phrase, it might leave open the possibility that there could be some birds that cannot fly.
Therefore, the predicate:
fly(X) :- bird(X).
Would not be correct. This says that, if X is a bird, then X can fly, which is not what the only birds can fly says, logically.
The more direct translation to Prolog would then be:
bird(X) :- fly(X).
It says that, X is a bird if X can fly (or, if X can fly, then X must be a bird).
The exclusivity of only would occur due to the absence of any other predicate that says some other creature can fly, such as:
bee(X) :- fly(X).
Regarding the second question:
go_there(X,Place):-
go_there(X,zoo),
go_there(X,library).
This says that, X goes to Place if X goes to the zoo, and X goes to the library. This doesn't sound at all like, X can either go to the zoo, or to the library, but not both. I could say something like:
go_there(fred, zoo).
go_there(fred, library).
These say that fred can go to the zoo, or fred can go to the library.
And then if I query:
go_there(fred, Place).
It will yield two different results for Place:
Place = zoo
Place = library
To say that X can go to one or the other, but not both, requires qualification. Does this mean within a specific period of time that X cannot go to both? Or does it mean that once X goes to one, then it can never ever go to the other? Whether the above predicate satisfies the first case is unclear. It depends upon the context. In either case, you'd need to add some Prolog logic to handle either the time aspect, or, if the "forever" case, you'd need a random selection of one or the other, and then have that selection remain persistent from that point forward.
In response to additional comments, if you to say:
go_there(human(fred), zoo).
go_there(human(fred), library).
You are choosing to represent a person, such as fred, as a human via a functor, human/1. In order to designate fred as human, you have to carry the functor around with fred and always refer to fred as human(fred) whenever used in a predicate that is looking for freds membership in the human race. If you wanted to ask if fred goes to the zoo, go_there(fred, zoo). would fail, unfortunately, because the fact that fred is human needs to be a part of the atom representing fred. You'd have to query, go_there(human(fred), zoo). To determine if fred is human, you would query, go_there(human(fred), _). which is also a bit awkward.
Separating the relationships is clearer:
go_there(fred, zoo).
go_there(fred, libary).
human(X) :- go_there(X, zoo).
human(X) :- go_there(X, library).
Now if you query, human(fred)., you get "true". And if you query, go_there(fred, zoo) you also get "true".

Convert statements into prolog code

Hey
I 'm trying to convert the following statements into prolog code, but I'm not sure if I did it correctly.
1-everybody who respects himself is respected by others:
respects(x,respects(x)) :- respects(y,x)).
2-john respects herself:
respectsherself(john).
respects(john,respectsherself(john)).
Thanks
In prolog variables must start with a capital letter so look out for that.
Everybody who respects himself is respected by others. I think you need some basic facts such as who respects who. Then you can declare a rule that says X is respected by others is implied by X respecting Himself.
respects(john, mary). %john respects mary
respects(john, john). %john respects himself
respects(X, Y) :- respectedbyothers(Y). %X respects Y if Y is respected by others
respectedbyothers(X):-respects(X, X).
An optimization:
%respects(A,B) means A is respected by B
respects(john,john).
respects(X,_):-respects(X,X).
?
Don't you just love prolog :)

Prolog programming - simple negative query

My database is:
eat(magi,limo).
eat(nona,banana).
How do I ask: "Who's not eating limo?" This:
eat(X,not(limo)).
Doesn't work. :(
First of all limo is a symbol and you can't negate symbols. What you want to do is negate the predicate, i.e. not(eat(X, limo)).
However this still does not give you nona as a result. Why not? Well there are infinitely many values X for which eat(X, limo) will be false. The system needs more information than "X does not eat limo" to know which one you want. Instead we need to ask for an X such that "X eats something, but X does not eat limo". This leads us to the following query:
eat(X,Y), not(eat(X, limo)).
Which gives us nona as the solution for X.
eat(X, Y), Y \= limo, writeln(X), false.

HElp with converting to first order logic

Using only these predicates.....
child(X) X is a child
unwell(X,Y) X is unwell on day Y
location(X,Y,Z) Location of X on day Y is Z (school, park, home)
sunny(X) X is a sunny day
Generally, children do not go to school whenever they are unwell
∄x [Child(x) ∧ location(X,y,home) → Child(x) ∧ unwell(X,y)]
Not sure if what i've written is right or wrong
Also not sure, how to convert this line...
On sunny days Julie goes to the park, otherwise she stays at home.
Would appreciate the help... Thanks...
Maybe something like this:
((child(Julie) ^ location(Julie,Y,park)) -> sunny(Y)) ^ ((child(Julie) ^ location(Julie,Y,home)) -> ~sunny(Y))
I will edit this further if I can think of something. Been a while since I touched first-order logic :)
your first answer isn't correct, but it isn't really translatable to FOL because it describes doesn't describe a strict rule, but only something that is generally the case. To describe that kind of things you would need some kind of non monotonic logic.
But even if we leave that out, and just asume it is a strict rule and ignore all exceptional behaviour you here say that there isn't an entity x which if it is a child and at home is sick and a child. i think you meant to be the second precedent of the implication to be $location(X,Y,school)$
the rendering of the second sentence by Sagar V looks nice but perhaps you want to reverse the implication (depends on what causal relation you want to have) and his sentence forces Julie to be a child which isn't in your problem statement

Resources