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.
Related
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.
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.
Can someone explain to me why is:
notmarried(P) :- \+(married(P)), male(P).
different then:
notmarried(P) :- male(P), \+(married(P)).
Good question!
The answer has to do with logical purity: in Prolog negation is implemented as negation-as-failure. In general a goal \+ G states that G cannot be proven at this point of time---not that G is logically false.
As a consequence the conjunctions you wrote may not be commutative.
The subject is that a variable in prolog can be bound to some value (X=foo) or unbound (not yet known value).
Now, assume the following facts:
married(tom).
married(john).
What must be after "not married" in
\+married(P), male(P)
?
P can be any value except "tom" or "john". But prolog has no way to store this fact in "P" (not with the basic statements). So, the result of "not married" is "yes, it is possible there some people not married" and P unbound. With P unbound, male(P) takes the first male, and we have the first answer.
Now, the second query:
male(P), \+married(P).
After male, prolog will bound P to one of the males. Now, it will check if this male is married or not, answering yes/not. In case of not, it will backtract to another male, and so on.
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.
i just can't get my head around this problem i'm having with prolog. Only just started, but i can't seem to find a way to find out if an object is unique. Heres my code:
/* (Student Name, Student Number)*/
Student(stuart, 11234).
Student(ross, 11235).
Student(rose, 11236).
Student(stuart, 11237).
how can i find out if a student is unique. Take for example Stuart, there's two students named Stuart, so Stuart is not unique. How could i write a procedure to tell if its another Student called Stuart.
I've tried spending so many hours on this, but i can't seem to get my head around dealing with the original Stuart rather than the other Stuart because i can't exclude the one i'm trying to find out if its unique.
Thanks for the help.
With your database example this could do
unique(S) :-
student(S, N), \+ (student(S, M), M \= N).
as it yields
?- unique(S).
S = ross ;
S = rose ;
false.
Generally, Prolog is targeted toward existence of solutions. Then predication about cardinality need some support from the 'impure' part of the language: nb_setarg it's currently our best friend when we need to efficiently tracking cardinality.
Using a metapredicate like this:
%% count_solutions(+Goal, ?C)
%
% adapted from call_nth/2 for http://stackoverflow.com/a/14280226/874024
%
count_solutions(Goal, C) :-
State = count(0, _), % note the extra argument which remains a variable
( Goal,
arg(1, State, C1),
C2 is C1 + 1,
nb_setarg(1, State, C2),
fail
; arg(1, State, C)
).
:- meta_predicate count_solutions(0, ?).
you could solve the problem without considering the second argument
unique(S) :-
student(S, _), count_solutions(student(S, _), 1).
The same predicate could use aggregate_all(count, student(S,_), 1) from library(aggregate), but such library currently builds a list internally, then you could consider the answer from Peter as easier to implement.
There are probably quite a few ways to solve this problem but I would do it this way:
% a student has a name and a number
student(stuart, 11234).
student(ross, 11235).
student(rose, 11236).
student(stuart, 11237).
This code says "find a list the same length as the number of students with Name" and then "make Count the same as the length of the list":
% for every student name there is an associated count of how many times
% that name appears
number_students(Name, Count) :-
findall(_, student(Name, _), Students),
length(Students, Count).
This predicate will only be true if the number_students is 1:
% a student name is unique (appears once and only once) is the
% number_students count is 1
unique_student(Name) :-
number_students(Name, 1).
Testing:
12 ?- unique_student(ross).
true.
13 ?- unique_student(rose).
true.
14 ?- unique_student(bob).
false.
15 ?- unique_student(stuart).
false.
This is an easy way to solve the problem, but it isn't a great Prolog solution because you cannot say things like "give me a unique student name" and get a list of all the unique names.
Some comments on the code you have. This is not a fact:
Student(Ross).
These are two different facts (in SWI-Prolog, at least):
student(ross).
student('Ross').
In other words, predicate names must start with small letters, and identifiers starting with a capital letters denote variables, not atoms. You can put any character string in single quotes to make it a valid atom.
Now this out of the way, it is not clear what you are aiming at. What are you going to do with your unique student? How do you know the first one is the one you are looking for, and not the second? And why not use the student number for that (at least in your example the two Stuarts seem to have different numbers)?