Prolog checking the same fact twice - prolog

This is the knowledge database
student(jack,100,21,m).
takes(100,cs01).
takes(100,cs02).
takes(100,cs03).
teaches(doe,cs01).
course(cs01,ai).
course(cs02,cpp).
course(cs03,java).
isTaughtBy(Sname,Lname) :-
teaches(Lname,Mcode),
student(Sname,Scode,_,_),
takes(Scode,Mcode).
When running isTaughtBy(jack,doe) it returns true (like its supposed to) and false (for some reason, probably because jack takes multiple modules).
This is the trace:
[trace] ?- isTaughtBy(jack,doe).
Call: (8) isTaughtBy(jack, doe) ? creep
Call: (9) teaches(doe, _18526) ? creep
Exit: (9) teaches(doe, cs01) ? creep
Call: (9) student(jack, _18526, _18528, _18530) ? creep
Exit: (9) student(jack, 100, 21, m) ? creep
Call: (9) takes(100, cs01) ? creep
Exit: (9) takes(100, cs01) ? creep
Exit: (8) isTaughtBy(jack, doe) ? creep
true ;
Redo: (9) takes(100, cs01) ? creep
Fail: (9) takes(100, cs01) ? creep
Fail: (8) isTaughtBy(jack, doe) ? creep
false.
Why is it redoing takes(100,cs01) if it was already checked above? Why is it returning false even though its clearly defined in the database? What am i not understanding here? I just want it to return true or false if a student is taught by a lecturer on any of the modules they are taking.

Related

Prolog goal with multiple results

spec(comp1, pc, 32). /* Fact 1 */
spec(comp2, mac, 128). /* Fact 2 */
spec(comp3, pc, 64). /* Fact 3 */
runs(pc, movie_edit, 96). /* Fact 4 */
runs(pc, vb, 16). /* Fact 5 */
runs(pc, cpp, 28). /* Fact 6 */
runs(mac, vb, 24). /* Fact 7 */
runs(mac, prolog, 128). /* Fact 8 */
access(judy, comp1). /* Fact 9 */
access(peter, comp3). /* Fact 10 */
access(david, comp1). /* Fact 11 */
access(david, comp2). /* Fact 12 */
can_use(P, SW) :- access(P, Comp), can_run(Comp, SW). /* Rule 1 */
can_run(Comp, SW) :- spec(Comp, CompType, MemAvail),
runs(CompType, SW, MemNeeded),
MemAvail >= MemNeeded. /* Rule 2 */
?- can_use(judy, vb).
?- can_use(david, prolog).
The first goal returns: true, false.
Whereas the second one returns only true.
My question is why in the first goal we have that extra information.
I'm using SWI-Prolog 7.6.4 version
The reason this happens is because in the former case, there is still "opportunity" for backtracking whereas in the latter, there is no such opportunity.
If we call the goal with the trace, we see:
[trace] ?- can_use(judy, vb).
Call: (8) can_use(judy, vb) ? creep
Call: (9) access(judy, _2968) ? creep
Exit: (9) access(judy, comp1) ? creep
Call: (9) can_run(comp1, vb) ? creep
Call: (10) spec(comp1, _2968, _2970) ? creep
Exit: (10) spec(comp1, pc, 32) ? creep
Call: (10) runs(pc, vb, _2970) ? creep
Exit: (10) runs(pc, vb, 16) ? creep
Call: (10) 32>=16 ? creep
Exit: (10) 32>=16 ? creep
Exit: (9) can_run(comp1, vb) ? creep
Exit: (8) can_use(judy, vb) ? creep
true ;
Redo: (10) runs(pc, vb, _2970) ?
We here thus make a call to runs/3 with runs(pc, vb, MemNeeded) and Prolog finds a first answer with 16. But it sets a backtracking point to look for other runs/3 facts with runs(pc, vb, MemNeeded). Imagine that there is another fact later in the source code, for example runs(pc, vb, 14) at the end, then this can produce another answer.
If we however call the second goal, we see:
[trace] ?- can_use(david, prolog).
Call: (8) can_use(david, prolog) ? creep
Call: (9) access(david, _3726) ? creep
Exit: (9) access(david, comp1) ? creep
Call: (9) can_run(comp1, prolog) ? creep
Call: (10) spec(comp1, _3726, _3728) ? creep
Exit: (10) spec(comp1, pc, 32) ? creep
Call: (10) runs(pc, prolog, _3728) ? creep
Fail: (10) runs(pc, prolog, _3728) ? creep
Fail: (9) can_run(comp1, prolog) ? creep
Redo: (9) access(david, _3726) ? creep
Exit: (9) access(david, comp2) ? creep
Call: (9) can_run(comp2, prolog) ? creep
Call: (10) spec(comp2, _3726, _3728) ? creep
Exit: (10) spec(comp2, mac, 128) ? creep
Call: (10) runs(mac, prolog, _3728) ? creep
Exit: (10) runs(mac, prolog, 128) ? creep
Call: (10) 128>=128 ? creep
Exit: (10) 128>=128 ? creep
Exit: (9) can_run(comp2, prolog) ? creep
Exit: (8) can_use(david, prolog) ? creep
true.
Here we call runs(mac, prolog, MemNeeded)., and this is the last fact of runs/3, hence there is no other possibility to satisfy runs/3 otherwise: since Prolog runs top to bottom, if we have satisfied the last fact/clause, we know that there is no other option.
Since all other calls also take the last predicate as well, or with a different constant as first parameter (SWI-Prolog looks at the first argument when it compiles the source code as an optimization), there are no other backtracking points, and thus there is no way to Redo a certain call.

Brother in law predicate for Prolog not working

Here's the relevant code:
married(X,Y) :- wife(X,Y);husband(X,Y).
parent(X,Y) :- father(X,Y) ;mother(X,Y).
brother(X,Y) :-
man(X),
parent(Z,X),
parent(Z,Y),
X \= Y.
brother_in_law(X,Y) :-
brother(X,Z),married(Z,Y).
I've googled and it seems other have been using the exact code for the brother in law predicate so it should be fine? I checked the other predicates and they seem good too.. not sure what is happening.
Also, by not working I mean that it's not acknowledging the relevant relation when I check.
Look at the trace and you will see the problem:
?- trace, brother_in_law(prins-daniel, Y).
Call: (9) brother_in_law(prins-daniel, _11346) ? creep
Call: (10) brother(prins-daniel, _11680) ? creep
Call: (11) man(prins-daniel) ? creep
Exit: (11) man(prins-daniel) ? creep
Call: (11) parent(_11678, prins-daniel) ? creep
Call: (12) father(_11678, prins-daniel) ? creep
Fail: (12) father(_11678, prins-daniel) ? creep
Redo: (11) parent(_11678, prins-daniel) ? creep
Call: (12) mother(_11678, prins-daniel) ? creep
Fail: (12) mother(_11678, prins-daniel) ? creep
Fail: (11) parent(_11678, prins-daniel) ? creep
Redo: (11) man(prins-daniel) ? creep
Fail: (11) man(prins-daniel) ? creep
Fail: (10) brother(prins-daniel, _11680) ? creep
Fail: (9) brother_in_law(prins-daniel, _11346) ? creep
false.
Who is prins-daniel's father? You don't have a fact for that. Who is prins-daniel's mother? You don't have a fact for that either. As a result, you can't find any brothers, so the query fails.
Does this mean you are missing facts or missing code? The code says X and Y are brothers-in-law if X has a brother Z who is married to Y. Is this the only way to have a brother-in-law?
Side note: prins-daniel is not an atom in Prolog as it would be in Lisp. This is a term:
?- write_canonical(prins-daniel).
-(prins,daniel)
The situation is compounded by longer terms:
?- write_canonical(johann-georg-av-hohenzollern).
-(-(-(johann,georg),av),hohenzollern)
Just something to be aware of.

Prolog list recursion

The goal of my predicate is:
?- line_terminal_stations(east_london, StartsAt, EndsAt).
StartsAt = shoreditch
EndsAt = new_cross
Below is what I have so far, the recursion works as expected and progressively creates a list of stations on the line.
line_terminal_stations(LineName, StationX, StationY):-
next_station(LineName, StationX, StationY, []).
next_station(LineName, StationX, StationY, V) :-
link(StationX, StationY, LineName),
next_station(LineName, StationY, _, [StationX | V]).
However once the final station has been found the predicate fails and begins to 'undo' the list. Whereas when link/3 fails, i want to end the recursion so i can extract the first and last station of the list.
Examples of link/3:
link(shoreditch, whitechapel, east_london).
link(whitechapel, shadwell, east_london).
Example of run-through:
line_terminal_stations(east_london, StartsAt, EndsAt).
Redo: (9) link(_G3031, _G3032, east_london) ? creep
Exit: (9) link(whitechapel, shadwell, east_london) ? creep
Call: (9) next_station(east_london, shadwell, _G3128, [whitechapel]) ? creep
Call: (10) link(shadwell, _G3127, east_london) ? creep
Exit: (10) link(shadwell, wapping, east_london) ? creep
Call: (10) next_station(east_london, wapping, _G3131, [shadwell, whitechapel]) ? creep
Call: (11) link(wapping, _G3130, east_london) ? creep
Exit: (11) link(wapping, rotherhithe, east_london) ? creep
Call: (11) next_station(east_london, rotherhithe, _G3134, [wapping, shadwell, whitechapel]) ? creep
Call: (12) link(rotherhithe, _G3133, east_london) ? creep
Exit: (12) link(rotherhithe, surrey_docks, east_london) ? creep
Call: (12) next_station(east_london, surrey_docks, _G3137, [rotherhithe, wapping, shadwell, whitechapel]) ? creep
Call: (13) link(surrey_docks, _G3136, east_london) ? creep
Exit: (13) link(surrey_docks, new_cross_gate, east_london) ? creep
Call: (13) next_station(east_london, new_cross_gate, _G3140, [surrey_docks, rotherhithe, wapping, shadwell, whitechapel]) ? creep
Call: (14) link(new_cross_gate, _G3139, east_london) ? creep
Fail: (14) link(new_cross_gate, _G3139, east_london) ? creep
Fail: (13) next_station(east_london, new_cross_gate, _G3140, [surrey_docks, rotherhithe, wapping, shadwell, whitechapel]) ? creep
Redo: (13) link(surrey_docks, _G3136, east_london) ? creep
Exit: (13) link(surrey_docks, new_cross, east_london) ? creep
Call: (13) next_station(east_london, new_cross, _G3140, [surrey_docks, rotherhithe, wapping, shadwell, whitechapel]) ? creep
Call: (14) link(new_cross, _G3139, east_london) ? creep
Fail: (14) link(new_cross, _G3139, east_london) ? creep
Fail: (13) next_station(east_london, new_cross, _G3140, [surrey_docks, rotherhithe, wapping, shadwell, whitechapel]) ? creep
Fail: (12) next_station(east_london, surrey_docks, _G3137, [rotherhithe, wapping, shadwell, whitechapel]) ? creep
Fail: (11) next_station(east_london, rotherhithe, _G3134, [wapping, shadwell, whitechapel]) ? creep
Fail: (10) next_station(east_london, wapping, _G3131, [shadwell, whitechapel]) ? creep
Fail: (9) next_station(east_london, shadwell, _G3128, [whitechapel]) ? creep
One approach with minimum code changes would be:
link(shoreditch, whitechapel, east_london).
link(whitechapel, shadwell, east_london).
line_terminal_stations(LineName, First, Last):-
next_station(LineName, _, _, [], Stations),
Stations = [Last|_],
last(Stations, First).
next_station(LineName, StationX, StationY, V, [StationX|V]) :-
\+ link(StationX, StationY, LineName).
next_station(LineName, StationX, StationY, V, Stations) :-
link(StationX, StationY, LineName),
next_station(LineName, StationY, _, [StationX | V], Stations).
Test run:
[debug] ?- line_terminal_stations(east_london, StartsAt, EndsAt).
StartsAt = shoreditch,
EndsAt = shadwell
But as it stands the link/3 requires to be in the right order to first find the true startstation. I.e you can backtrack and find different start-station:
[debug] ?- line_terminal_stations(east_london, StartsAt, EndsAt).
StartsAt = shoreditch,
EndsAt = shadwell ;
StartsAt = whitechapel,
EndsAt = shadwell
But you need recursion?
If the line isn't a ring, you can simply impose that StartAt is a starting point, but not an ending point, and that EndAt is a ending point, but not a starting point.
I mean
line_terminal_stations(Line, StartsAt, EndsAt) :-
link(StartsAt, _, Line),
\+ link(_, StartsAt, Line),
link(_, EndsAt, Line),
\+ link(EndsAt, _, Line).

How to redirect trace output to a file

I am tracing a prolog program
1 ?- trace.
true.
[trace] 1 ?- solve.
Call: (7) solve ?
I also tried
tell('trace_op.txt').
the file is created but empty
Now the trace is really many lines . I want to redirect the output to a file
Can we redirect it to a file ?
On Windows using SWI-Prolog you can use protocol/1
protocol/1 will copy the output that goes to the screen to a file. So if you run a trace/0 and the output goes to the screen, a copy will be sent to a file. To simplify having to write the entire path for protocol/1 I find it easier to set the current working directory using working_directory/2 and then only set the specific file with protocol/1.
Example
For this example create a file, e.g.
trace_example.pl
and add some facts and predicates to demonstrate tracing.
parent(ann,helen).
parent(helen,henry).
parent(henry,mary).
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
Open SWI-Prolog top level and use consult/1 to load the file
consult("C:/ ... /trace_example.pl").
N.B. The directory separators are / not \. Change them if necessary.
Since the SWI-Prolog terminal on Windows will default to using color with tracing and that will add unneeded escape sequences to the output file set_prolog_flag/2 needs to be run to turn the color off.
?- set_prolog_flag(color_term,false).
true.
Verify that the terminal is not using color.
?- current_prolog_flag(color_term,X).
X = false.
A quick run to verify the predicate and facts work
?- ancestor(ann,henry).
true ;
false.
Now set the current working directory to where the output file will be created.
?- working_directory(_,"C:/Users/Eric/Documents/Prolog").
and verify that the change occurred
?- working_directory(CWD,CWD).
CWD = 'c:/users/eric/documents/prolog/'.
Since I don't want to press the space bar for each trace output I disable user interaction for all the debug ports using leash/1
?- leash(-all).
and since I want to see all of the output from all of the debug ports I enable them all with visible/1
?- visible(+all).
Enable the copying of the screen to a file
?- protocol("./trace_output.txt").
start the tracer
?- trace.
and run a query to be traced
?- ancestor(ann,henry).
Call: (8) ancestor(ann, henry) ? creep
Call: (9) parent(ann, henry) ? creep
Fail: (9) parent(ann, henry) ? creep
Redo: (8) ancestor(ann, henry) ? creep
Call: (9) parent(ann, _1124) ? creep
Exit: (9) parent(ann, helen) ? creep
Call: (9) ancestor(helen, henry) ? creep
Call: (10) parent(helen, henry) ? creep
Exit: (10) parent(helen, henry) ? creep
Exit: (9) ancestor(helen, henry) ? creep
Exit: (8) ancestor(ann, henry) ? creep
true ;
Redo: (9) ancestor(helen, henry) ? creep
Call: (10) parent(helen, _1124) ? creep
Exit: (10) parent(helen, henry) ? creep
Call: (10) ancestor(henry, henry) ? creep
Call: (11) parent(henry, henry) ? creep
Fail: (11) parent(henry, henry) ? creep
Redo: (10) ancestor(henry, henry) ? creep
Call: (11) parent(henry, _1124) ? creep
Exit: (11) parent(henry, mary) ? creep
Call: (11) ancestor(mary, henry) ? creep
Call: (12) parent(mary, henry) ? creep
Fail: (12) parent(mary, henry) ? creep
Redo: (11) ancestor(mary, henry) ? creep
Call: (12) parent(mary, _1124) ? creep
Fail: (12) parent(mary, _1124) ? creep
Fail: (11) ancestor(mary, henry) ? creep
Fail: (10) ancestor(henry, henry) ? creep
Fail: (9) ancestor(helen, henry) ? creep
Fail: (8) ancestor(ann, henry) ? creep
false.
end the tracing
?- nodebug.
and end the copying of the screen to the file
?- noprotocol.
Now open the file C:\Users\Eric\Documents\Prolog\trace_output.txt
true.
10 ?- trace.
true.
[trace] 10 ?- ancestor(ann,henry).
Call: (8) ancestor(ann, henry)
Unify: (8) ancestor(ann, henry)
Call: (9) parent(ann, henry)
Fail: (9) parent(ann, henry)
Redo: (8) ancestor(ann, henry)
Unify: (8) ancestor(ann, henry)
Call: (9) parent(ann, _6466)
Unify: (9) parent(ann, helen)
Exit: (9) parent(ann, helen)
Call: (9) ancestor(helen, henry)
Unify: (9) ancestor(helen, henry)
Call: (10) parent(helen, henry)
Unify: (10) parent(helen, henry)
Exit: (10) parent(helen, henry)
Exit: (9) ancestor(helen, henry)
Exit: (8) ancestor(ann, henry)
true ;
Redo: (9) ancestor(helen, henry)
Unify: (9) ancestor(helen, henry)
Call: (10) parent(helen, _6466)
Unify: (10) parent(helen, henry)
Exit: (10) parent(helen, henry)
Call: (10) ancestor(henry, henry)
Unify: (10) ancestor(henry, henry)
Call: (11) parent(henry, henry)
Fail: (11) parent(henry, henry)
Redo: (10) ancestor(henry, henry)
Unify: (10) ancestor(henry, henry)
Call: (11) parent(henry, _6466)
Unify: (11) parent(henry, mary)
Exit: (11) parent(henry, mary)
Call: (11) ancestor(mary, henry)
Unify: (11) ancestor(mary, henry)
Call: (12) parent(mary, henry)
Fail: (12) parent(mary, henry)
Redo: (11) ancestor(mary, henry)
Unify: (11) ancestor(mary, henry)
Call: (12) parent(mary, _6466)
Fail: (12) parent(mary, _6466)
Fail: (11) ancestor(mary, henry)
Fail: (10) ancestor(henry, henry)
Fail: (9) ancestor(helen, henry)
Fail: (8) ancestor(ann, henry)
false.
[trace] 11 ?- nodebug.
true.
12 ?- noprotocol.
Linux
If you're using Linux, you can use the tee command:
$ swipl 2>&1 | tee swipl.log
...
1 ?- trace.
true.
[trace] 1 ?- solve.
Call: (7) solve ?
...
The tee command sends all of its standard input to the standard output and to to the specified file in parallel. The 2>&1 ensures that you also capture any standard error in the standard output as well so it would appear in the swipl.log file.
Windows
In Windows, if you use PowerShell, you can use the Tee-Object command which works similarly:
swipl-win | Tee-Object -file swipl.log
I'm assuming here that swipl-win is what the command line program for SWI Prolog in Windows, and that it exists in your program path for PowerShell.
When you exit swipl, then you can see all of what happened in swipl.log.

Some doubts about declarative meaning of a program that prints a list of lists in Prolog

Write a rule that take a list whose elements are themselves lists and
print on each line the elements of internals list:
Example:
?- printList[[1,a],[2,b]]).
1 a
2 b
The solution it the following one:
/* BASE CASE: The list is empty, so there is nothing to print */
printList([]).
printList([P|R]):- printList(P),
!,
nl,
printList(R).
printList([X|R]):- write(X),
!,
printList(R).
I have the printList rule that reaches the base case when the list is empty and in this case there is nothing to print.
If I am not in the base case (the list is not empty) it calls the second rule printList([P|R]) and now I have my first doubt:
The element P is a list because Prolog automatically handles an internal list as an element?
So, If I have something like:
[[1,a],[2,b],[3,c],[4,d]] I have that Prolog automatically match in this way:
P = [1,a] (the first list as the head element)
R = [[2,b],[3,c],[4,d]] (the other 3 list are the element of a tail list)
And then the program calls the printList predicate on the head element (the first list) and this version of the rule writes all the elements in the current list.
Is this right this interpretation?
Now I have some doubts about about the trace of this program, for example if I execute this statement I obtain the following trace:
[trace] ?- printList([[1,a], [2,b]]).
Call: (6) printList([[1, a], [2, b]]) ? creep
Call: (7) printList([1, a]) ? creep
Call: (8) printList(1) ? creep
Fail: (8) printList(1) ? creep
Redo: (7) printList([1, a]) ? creep
Call: (8) write(1) ? creep
1
Exit: (8) write(1) ? creep
Call: (8) printList([a]) ? creep
Call: (9) printList(a) ? creep
Fail: (9) printList(a) ? creep
Redo: (8) printList([a]) ? creep
Call: (9) write(a) ? creep
a
Exit: (9) write(a) ? creep
Call: (9) printList([]) ? creep
Exit: (9) printList([]) ? creep
Exit: (8) printList([a]) ? creep
Exit: (7) printList([1, a]) ? creep
Call: (7) nl ? creep
Exit: (7) nl ? creep
Call: (7) printList([[2, b]]) ? creep
Call: (8) printList([2, b]) ? creep
Call: (9) printList(2) ? creep
Fail: (9) printList(2) ? creep
Redo: (8) printList([2, b]) ? creep
Call: (9) write(2) ? creep
2
Exit: (9) write(2) ? creep
Call: (9) printList([b]) ? creep
Call: (10) printList(b) ? creep
Fail: (10) printList(b) ? creep
Redo: (9) printList([b]) ? creep
Call: (10) write(b) ? creep
b
Exit: (10) write(b) ? creep
Call: (10) printList([]) ? creep
Exit: (10) printList([]) ? creep
Exit: (9) printList([b]) ? creep
Exit: (8) printList([2, b]) ? creep
Call: (8) nl ? creep
Exit: (8) nl ? creep
Call: (8) printList([]) ? creep
Exit: (8) printList([]) ? creep
Exit: (7) printList([[2, b]]) ? creep
Exit: (6) printList([[1, a], [2, b]]) ? creep
true.
This is quite clear for me (I think that my previous reasoning is right) but I am not understanding why whenever it reaches an element in an internal list it calls the printList relation on it (that is a simple element and not a list), for example here:
Call: (8) printList(1) ? creep
Fail: (8) printList(1) ? creep
the program had considered the first list of the original list and then, into it to have to print its first element, why call the printList relation on this simple element
Is it because this simple element may in turn be internal lists?
Something like:
[[[1.1,a1,a2],[1.2,b1,b2]], [2,b]] (in which I have a list that contains 2 lists and the first element it is a list that contains 2 lists. So the program checks if an element it is an element or an internal list?
I think you're over-thinking it. Look at the code:
printList([P|R]):- printList(P),
Right there you can see that printList/1 is being unified with the head of the list. The fact that all the rules of printList/1 match lists and only lists is a fact that you, the human, can immediately see. But Prolog does not "notice" this fact, so if you are to call, say,
printList([1])
the first matching rule is the one above, so it will immediately try to unify printList(1). This will fail, naturally, because 1 is not a list and so doesn't match any of the rules for printList/1. Prolog then backtracks and tries the next rule, which is the one that starts like this:
printList([X|R]):- write(X),
This is clearly going to unify [1] with X = 1, R = [], so it's clearly going to write the first element, the one, and then proceed as usual. There is no magic here involving "internal lists," which as far as I'm aware is not a concept in Prolog at all (if such a thing is treated in the compiler it is well hidden from the user of Prolog).
Prolog isn't psychic; it has to try the rules to see if they fail, even if that attempt is essentially a failing pattern-match in the head of the Horn clause.
I am unable to distinguish your first from your second question so I hope this answers both. :)

Resources