I'm trying to solve:
Given facts such as
Bob is taller than Mike.
Mike is taller than Jim
Jim is taller than George
Write a recursive program that will determine that Bob's height is greater than George's.
My solution so far is:
taller_than(bob, mike).
taller_than(mike, jim).
taller_than(jim, george).
taller_than(X,Y):-
taller_than(X, Z),
taller_than(Z, Y).
It returns True as expected, but then I reach the stack limit. I'm guessing I need a base case, but I'm not sure what it would be? Is my solution otherwise correct?
Oh so close.
Your main problem is that you have facts taller_than/2 and predicates taller_than/2 with the same signature. This even caught me off guard when I gave it an initial test run. You need to change the name of either the fact or the predicate. For this the name of the predicate is changed.
As you noted you need a base case and had you done the name change I think you would have figured this out also.
taller_than_rule(X,Y) :-
taller_than(X,Y).
taller_than_rule(X,Y) :-
taller_than(X, Z),
taller_than_rule(Z, Y).
Example run:
?- taller_than_rule(bob,Who).
Who = mike ;
Who = jim ;
Who = george ;
false.
Related
Lets suppose my db.pl file consists of only
male(Oliver)
male(james)
male(sam)
female(X) :- \+ male(X)
Now if I query,
?- male(X).
Then this will successfully return all the males.
But if I query,
?- female(X)
Then this will not. However, if I put a name in the female predicate, it correctly gives the output as yes/no.
What am I missing here? How do I get the list of all females? Or basically list of "not male".
I thought I'd give a proper answer as well. As mentioned in the comments, the fact male(Oliver). contains the variable Oliver such that e.g. also male(catherine_the_great) succeeds. But fixing this mistake will not solve the problem. Suppose we fix the facts for male and define female as above:
male(oliver)
male(james)
male(sam)
female(X) :-
\+ male(X).
Then we can not derive male(catherine_the_great) anymore but female(catherine_the_great) succeeds:
?- male(catherine_the_great).
false.
?- female(catherine_the_great).
true.
Unfortunately, by this definition, a hamburger is also female:
?- female(hamburger).
true.
What's even worse is, when we ask if there exists someone female (at all!), we get no as an answer:
?- female(X).
false.
The reason for this is that \+ is not classical negation(*) but what is called weak negation / negation as failure. \+ male(X) is true if there is no X such that male(X) is derivable. But as soon as male/1 succeeds with any solution (e.g. male(oliver)), the negation fails. In such cases the substitution male(X) is not proper substiution for female(X) but how are we supposed to get a list of all women? A possible solution is a list of persons of which we define male/female as subsets:
person(oliver).
person(james).
person(sam).
person(jackie).
person(selma).
male(oliver).
male(james).
male(sam).
female(X) :-
person(X),
\+ male(X).
When we query a list of all women, we now get:
?- female(X).
X = jackie ;
X = selma.
I'd rather use a separate predicate listing women though because it is less error prone when adding new persons.
In general, negation as failure is rarely safe. Two rules of thumb i have for myself:
whenever \+ p(X) appears as a goal, it is fully instantiated (a ground term). In our case, this is what person assures.
p(X) terminates for every ground term X. This assures termination of the negation as well.
(*) in classical logic, the rule of generalization female(jackie) → ∃X female(X) holds but here we can not derive ∃X female(X) despite being able to derive female(jackie).
likes(alice, sports).
likes(alice, music).
likes(carol, music).
likes(david,animals).
likes(david,X) :- likes(X,sports).
likes(alice,X) :- likes(david,X).
?- likes(alice,X).
I've been trying to learn prolog an few days now, and when I attempted this question, I realised that I don't completely understand when the variables are instantiated and used. The initial goal is : likes(alice , X). After that, the next goal to prove is likes(david , X)? Then is it likes(X, sports). Then does X become alice?
Another route:
The initial goal is : likes(alice , X). After that, the next goal to prove is likes(david , X)? Then X becomes sports. Then the goal becomes likes(david , sports). Then I don't know.
Could someone please indicate where my thinking is flawed.
Given your code Prolog would try to unify the goal against he first fact, likes(alice, sports)., and it would succeed. X would be unified with sports.
If you asked Prolog to continue then it would next unify X with music (the second fact).
And if you continued again it would skip the next three facts/rules, and try to prove likes(alice,X) :- likes(david,X).. This would lead to trying to prove likes(david,X) which succeeds with the fact likes(david,animals) so X, in the original goal, would unify with animals.
And if you asked it to continue again you would find that it tries to prove likes(david,X) :- likes(X,sports). which leads to X unifying with alice, so the original goal would suggest that alice likes alice.
When I ran your code with this goal:
?- likes(alice,X), write(X), nl, fail.
...I got this output:
sports
music
animals
alice
No.
With the final No. being caused because I had the fail predicate in my goal. It was a goal that was always going to fail, but it produced a side-effect by outputting the intermediate result of X.
Let's break down the code. First, you have some facts:
likes(alice, sports). % Alice likes sports
likes(alice, music). % Alice likes music
likes(carol, music). % Carol likes music
likes(david, animals). % David likes animals
Just given these facts, you can make basic queries:
?- likes(alice, sports). % Does Alice like sports?
true ;
false. % no more solutions
So, yes, Alice likes sports (the result was true). Does Alice like animals?
?- likes(alice, animals).
false.
Evidently not. At least, according to the data we have, we cannot prove that Alice likes animals. (Remember, we only have the facts so far, but none of the rules, shown below.)
Well then, what does Alice like, according to the facts?
?- likes(alice, X).
X = sports ;
X = music.
Alice likes sports and music.
Now let's add in your rules:
likes(david, X) :- likes(X, sports).
This says that, David likes someone (X) if that someone (X) likes sports.
Let's see who/what David likes:
?- likes(david, X).
X = animals ;
X = alice ;
false % no more solutions
So David likes animals (because a fact says so), and David likes Alice (because we have a rule that says David likes X if X likes sports, and Alice likes sports).
Your other rule:
likes(alice, X) :- likes(david, X).
Says, Alice likes someone (X) if David likes that same someone (X).
With the new rules added, let's see who/what Alice likes:
?- likes(alice, X).
X = sports ;
X = music ;
X = animals ;
X = alice ;
false
Alice likes sports and music (because the facts say so). Alice likes animals because David likes animals and the rule says that if David likes X, then Alice likes X. Alice also evidently likes herself because, according to the first rule, we showed that David likes Alice. According to the second rule, Alice likes anyone that David likes. Therefore, Alice likes Alice.
You can get the step-by-step execution by running trace. then execute your query.
Note that this is fairly well behaved with these simple rules and facts. In more complex cases you have to be careful about naming your rules and your facts the same way because it can lead to infinite logical loops.
Problem:
Jack is looking at Anne, Anne is looking at George
Jack is married, George is not.
Is a married person looking at an unmarried person?
I am looking at this solution found in this link which entails:
unmarried(X) :- not(married(X)).
unmarried("George").
unmarried("Anne").
married("Jack").
married("Anne").
looking_at("Jack", "Anne").
looking_at("Anne", "George").
check(X, Y):-
looking_at(X,Y),
married(X),
unmarried(Y).
There are several questions immediately apparent once this much is done. For the first part, I was confused as to why Anne is defined as Married("Anne") and Unmarried("Anne"), but I quickly put that aside assuming that it possibly means to define Anne as either married or unmarried
A quick look at SO doesn't help either, as I found only some remotely
related questions; This particular question being the closest one.
Now back to the problem that I have here...
unmarried(X) :- not(married(X)). handles the operation such that if Anne is passed as the first argument of check(_, _)
The programmer for that solution has handled it with a single check(_, _) according to which:
check(Anne,George) will infer:
Anne looking at George
Anne being married
George being unmarried
That compiler produces this result as:
Anne->Jack->Anne->Anne
George->Anne->George->George
At this point, I don't know why the compiler is producing those results. And as far as I know, this is not giving the solution either. In the old desktop version of prolog that I used, check(Anne,George). should have produced a YES, seeing as all the conditions were TRUE(Well I have never tried the online swi-prolog tbh; is it different?)
For check(Jack,Anne) it is:
Jack looking at Anne
Jack being married
Anne being unmarried
I don't see how this necessarily solves the problem. Could someone post a better solution or explain in detail how this is working?
Requirement:
I need a solution for the Problem that I posted at the begining of this question. If you can resolve it from the existing condition that I have posted, thats cool. However, I am also open to alternate ideas and solutions.
First lets look at the puzzle itself. The question "Is a married person looking at an unmarried person?" is clearly a yes/no question. Considering the marital status we have incomplete knowledge(Anne). The people who's marital status we know are not gazing at each other, so we have to consider Anne to find an answer. If we assume that:
Anne is married then the answer is yes because she is looking at the unmarried George.
Anne is not married then the answer is yes because the married Jack is looking at her.
So either way there is a married person looking at an unmarried person, thus the answer to the puzzle is yes.
Regarding the given solution: I think the author tries to model "Anne is either married or not married" by the facts married("Anne") and unmarried("Anne"). However the facts seem to express that Anne is married and unmarried at the same time. Also the rule unmarried(X) :- not(married(X)). in combination with the facts married/1 and unmarried/1 yields the solution "Anne" twice. Thus check/2 also yields the Anne-looks-at-George solution twice:
?- check(X,Y).
X = "Jack",
Y = "Anne" ? ;
X = "Anne",
Y = "George" ? ;
X = "Anne",
Y = "George"
I can see where the author is trying to go with his solution but it isn't really expressing that there is an assumption involved and how the two unique solutions are connected.
My attempt is the following: I would keep four facts from the original version and add another one for Anne:
married(jack).
unmarried(george).
looking_at(jack,anne).
looking_at(anne,george).
unknown(anne).
Then I can make assumptions about people who's marital status I don't know:
person_assumption(A,married) :- unknown(A).
person_assumption(A,unmarried) :- unknown(A).
Now the relevant cases for the yes-answer are: (1) a known unmarried person is being looked at by a known married person and (2) a person P1 is
looking at known unmarried person under the assumtion that P1 is married
AND
being looked at by a known married person under the assumption that P1 is unmarried
The predicate problematicgaze/1 is modeling these observations:
problematicgaze((P1-P2)) :- % case (1)
married(P1),
unmarried(P2),
looking_at(P1,P2).
problematicgaze((if_married(P1)-P2,P3-if_unmarried(P1))) :- % case (2)
assumedproblematic(if_married(P1),P2),
assumedproblematic(P3,if_unmarried(P1)).
assumedproblematic(if_married(P1),P2) :-
person_assumption(P1,married),
unmarried(P2),
looking_at(P1,P2).
assumedproblematic(P1,if_unmarried(P2)) :-
person_assumption(P2,unmarried),
married(P1),
looking_at(P1,P2).
This breaks down to: either I get a solution and the answer is yes or the predicate fails and the answer is no. So I ask if there is a problematic gaze in the given situation:
?- problematicgaze(G).
G = (if_married(anne)-george,jack-if_unmarried(anne)) ? ;
no
As expected there is no answer from the first rule of problematicgaze/1 but from the second. No matter which assumption is taken for Anne a married person is looking at an unmarried one. Other than that no solution is found.
The solution to which you have linked is not correct. A pointed out in the comments, I am not sure how this gives answers:
?- check(X, Y).
X = "Jack",
Y = "Anne" ;
X = "Anne",
Y = "George" ;
X = "Anne",
Y = "George".
Please if someone knows the program was meant to be used, explain. I am afraid I am just missing something.
Here is one attempt at a real solution. First, we leave the ground facts exactly as given:
looking_at(jack, anne).
looking_at(anne, george).
married(jack).
unmarried(george).
I will now define a predicate solution/2 which has the solution and any possible bindings that explain it. There are three possible solutions, if I understand: "yes", "no", and "undetermined". In the case of a "yes" answer:
solve(yes, married_unmarried(A, B)) :-
married(A),
looking_at(A, B),
unmarried(B).
The "no" case is a bit different. It is supposed to say:
For all A looking at a B, either A is unmarried or B is married.
With SWI-Prolog and forall/2, you can write:
solve(no, false) :-
forall(looking_at(A, B),
( unmarried(A) ; married(B) )).
This is equivalent to:
solve(no, false) :-
\+ ( looking_at(A, B),
\+ ( unmarried(A) ; married(B) ).
The undetermined case is more interesting, but we could try and cheat:
solve(undetermined, B) :-
\+ solve(yes, B),
\+ solve(no, B).
We can neither prove that someone is looking nor that no one is looking.
I am using SWI Prolog, the following code is used in my homework(the code is not the homework but needed to write the methods the course requires):
nat(0).
nat(s(X)) :-
nat(X).
plus(0,N,N) :-
nat(N).
plus(s(M),N,s(Z)) :-
plus(M,N,Z).
times(0,N,0) :-
nat(N).
times(s(M),N,Z) :-
times(M,N,W),
plus(W,N,Z).
exp(s(M),0,0) :-
nat(M).
exp(0,s(M),s(0)) :-
nat(M).
exp(s(N),X,Z) :-
exp(N,X,Y),
times(X,Y,Z).
exp(a,b,c) means c=b^a.
It is copied directly from the book: "The Art of Prolog: Advanced Programming Techniques".
When I run the following query:
exp(s(s(0)),L,s(s(s(s(0))))).
I get an answer:
L = s(s(0))
But when I ask for another answer by entering ;
L = s(s(0)) ;
I get an infinite loop(ending in an out of stack error), I was expecting to get false.
What is the problem in the code? is there a code that does the same(with the same representation of natural numbers) but behaves the way I described? and if so, a few pointers or suggestions would be of great help.
Thanks in advance.
It's normal that it runs in to an infinite loop for the given program: if you call exp/3, With the second element being uninstantiated, it starts branching over all possible values for L. In other words, if you query:
exp(s(s(0)),L,s(s(s(s(0))))).
It acts as:
I instantiate L to s(0), is s(0) (thus 1) correct?
No! I instantiate L to s(s(0)) is 2 correct?
Yes return 2. Hold on, the user asks for more answers....
Is 3 correct? No
Is 4 correct? No
Is ... correct? (you get the idea)
Each time you make an attempt, the stack is risen one level deeper (because it requires one more level to construct s(X) over X...
You could try to use another way: there is a logical upperbound: the result (third argument), so you could first instantiate the second argument as the third and test and then decrement the second argument until you find the correct result.
Say I have the following theory:
a(X) :- \+ b(X).
b(X) :- \+ c(X).
c(a).
It simply says true, which is of course correct, a(X) is true because there is no b(X) (with negation as finite failure). Since there is only a b(X) if there is no c(X) and we have c(a), one can state this is true. I was wondering however why Prolog does not provide the answer X = a? Say for instance I introduce some semantics:
noOrphan(X) :- \+ orphan(X).
orphan(X) :- \+ parent(_,X).
parent(david,michael).
Of course if I query noOrphan(michael), this will result in true and noOrphan(david) in false (since I didn't define a parent for david)., but I was wondering why there is no proactive way of detecting which persons (michael, david,...) belong to the noOrphan/1 relation?
This probably is a result of the backtracking mechanism of Prolog, but Prolog could maintain a state which validates if one is searching in the positive way (0,2,4,...) negations deep, or the negative way (1,3,5,...) negations deep.
Let's start with something simpler. Say \+ X = Y. Here, the negated goal is a predefined built-in predicate. So things are even clearer: X and Y should be different. However, \+ X = Y fails, because X = Y succeeds. So no trace is left under which precise condition the goal failed.
Thus, \+ \+ X = Y does produce an empty answer, and not the expected X = Y. See this answer for more.
Given that such simple queries already show problems, you cannot expect too much of user defined goals such as yours.
In the general case, you would have to first reconsider what you actually mean by negation. The answer is much more complex than it seems at first glance. Think of the program p :- \+ p. should p succeed or fail? Should p be true or not? There are actually two models here which no longer fits into Prolog's view of going with the minimal model. Considerations as these opened new branches to Logic Programming like Answer Set Programming (ASP).
But let's stick to Prolog. Negation can only be used in very restricted contexts, such as when the goal is sufficiently instantiated and the definition is stratified. Unfortunately, there are no generally accepted criteria for the safe execution of a negated goal. We could wait until the goal is variable free (ground), but this means quite often that we have to wait way too long - in jargon: the negated goal flounders.
So effectively, general negation does not go very well together with pure Prolog programs. The heart of Prolog really is the pure, monotonic subset of the language. Within the constraint part of Prolog (or its respective extensions) negation might work quite well, though.
I might be misunderstanding the question, and I don't understand the last paragraph.
Anyway, there is a perfectly valid way of detecting which people are not orphans. In your example, you have forgotten to tell the computer something that you know, namely:
person(michael).
person(david).
% and a few more
person(anna).
person(emilia).
not_orphan(X) :- \+ orphan(X).
orphan(X) :- person(X), \+ parent(_, X).
parent(david, michael).
parent(anna, david).
?- orphan(X).
X = anna ;
X = emilia.
?- not_orphan(X).
X = michael ;
X = david ;
false.
I don't know how exactly you want to define an "orphan", as this definition is definitely a bit weird, but that's not the point.
In conclusion: you can't expect Prolog to know that michael and david and all others are people unless you state it explicitly. You also need to state explicitly that orphan or not_orphan are relationships that only apply to people. The world you are modeling could also have:
furniture(red_sofa).
furniture(kitchen_table).
abstract_concept(love).
emotion(disbelief).
and you need a way of leaving those out of your family affairs.
I hope that helps.