I can't understand the result of my code in Prolog Programming - prolog
I am doing Prolog Programming for my research and I got some problems..
First, all of my codes are below.
%% Lines are without period(.)
diagnosis :-
readln(Line1),
readln(Line2),
readln(Line3),
readln(Line4),
readln(Line5),
readln(Line6),
readln(Line7),
readln(Line8),
readln(Line9),
readln(Line10),
write(Line1),nl,
write(Line2),nl,
write(Line3),nl,
write(Line4),nl,
write(Line5),nl,
write(Line6),nl,
write(Line7),nl,
write(Line8),nl,
write(Line9),nl,
write(Line10),nl.
%% (get_symptom(Line1,[man]) -> write('man!!!!!!!!!')),
%% (get_symptom(Line2,[woman]) -> write('woman!!!!!!!!!')).
%% if A then B else C, (A->B; C)
%% grammar
s --> np, vp.
np --> det, n.
vp --> v, np.
det --> [a].
n --> [man].
v --> [has].
n --> [woman].
n --> [fever].
n --> [runny_nose].
get_symptom(Line,N) :- s(Line,[]), member(N,Line).
member(X, [X|T]).
member(X,[H|T]) :-
member(X,T).
%% FindSymptom(Line, [Symptom]) : - s(Line,[]), np(_, _, object,[a,
%% Symptom]), n(singular, [Symptom], []).
start :-
write('What is the patient''s name? '),
readln(Patient), %% Here, this can be used for input of all symtoms
diagnosis,
hypothesis(Patient,cold,S1),
append([cold/S1/red],[],N1), write(S1),
write('until...'),
hypothesis(Patient,severe_cold,S2), write(S2),
append([severe_cold/S2/red],N1,BarList),
write('until...'),
%% write(Patient,"probably has ",Disease,"."),nl.
hypothesis(Patient,Disease,100),
write(Patient),
write(' probably has '),
write(Disease),
write('.'),
test_barchart(BarList).
start :-
write('Sorry, I don''t seem to be able to'),nl,
write('diagnose the disease.'),nl.
symptom(Patient,fever) :-
get_symptom(Line1, [fever]);
get_symptom(Line2, [fever]);
get_symptom(Line3, [fever]);
get_symptom(Line4, [fever]);
get_symptom(Line5, [fever]);
get_symptom(Line6, [fever]);
get_symptom(Line7, [fever]);
get_symptom(Line8, [fever]);
get_symptom(Line9, [fever]);
get_symptom(Line10, [fever]).
symptom(Patient,runny_nose) :-
get_symptom(Line1, [runny_nose]);
get_symptom(Line2, [runny_nose]);
get_symptom(Line3, [runny_nose]);
get_symptom(Line4, [runny_nose]);
get_symptom(Line5, [runny_nose]);
get_symptom(Line6, [runny_nose]);
get_symptom(Line7, [runny_nose]);
get_symptom(Line8, [runny_nose]);
get_symptom(Line9, [runny_nose]);
get_symptom(Line10, [runny_nose]).
hypothesis(Patient,cold,Score_Cold) :-
(symptom(Patient,fever), Score_Cold is 100),write('!!!');
Score_Cold is 0.
hypothesis(Patient,severe_cold,Score_Severe) :-
((symptom(Patient,fever), Score1 is 50);
Score1 is 0),
((symptom(Patient, runny_nose), Score2 is 50);
Score2 is 0),
Score_Severe is Score1 + Score2.
%% hypothesis(Disease) :-
%%(hypothesis1(Patient,cold,Score1),
%%Score1 =:= 100 -> Disease = cold);
%%(hypothesis2(Patient,severe_cold,Score2),
%%Score2 =:= 100 -> Disease = severe_cold).
%% make graph for the result
:- use_module(library(pce)).
:- use_module(library(plot/barchart)).
:- use_module(library(autowin)).
test_barchart(BarList):-
new(W, picture),
send(W, display, new(BC, bar_chart(vertical,0,100))),
forall(member(Name/Height/Color,
BarList),
( new(B, bar(Name, Height)),
send(B, colour(Color)),
send(BC, append, B)
)),
send(W, open).
%% [X/100/red, y/150/green, z/80/blue, v/50/yellow]
%% append List
append([], L, L).
append([H|T], L2, [H|L3]):-
append(T, L2, L3).
As you can see, I want to make a bar_graph from 10 input lines by extracting symptoms..
But when I executed this code, I got the result as below...
1 ?- start.
What is the patient's name? GJ
|: a man has a runny_nose
|: a
|: a
|: a
|: a
|: a
|: a
|: a
|: a
|: a
[a,man,has,a,runny_nose]
[a]
[a]
[a]
[a]
[a]
[a]
[a]
[a]
[a]
!!!100until...100until...!!![GJ] probably has cold.
true
I only typed one symptom (runny_nose). I want to get Score for "cold" is 0, Score for "severe_cold" is 50 and BarGraph result... But What Happened? I can't find..
*****Edited******
I found that the problem is related to local variables (Line1, .. ,Line10) But how can I deal with? If I can make all the variables; Line1, ... , Line10 as global variables then, I think the problem can be solved...
****Addition*****
I changed my 'start' predicate as follows...I didn't use 'diagnosis' and 'hypothesis' predicates/ But the problems is maybe.. 'get_symptoms' predicate. Is there any choice I can choose except that I don't use 'get_symptoms' and 'symptoms' predicates...? Then the code will become very coarse...
start :-
write('What is the patient''s name? '),
readln(Patient), %% Here, this can be used for input of all symtom
readln(Line1),
readln(Line2),
readln(Line3),
readln(Line4),
readln(Line5),
readln(Line6),
readln(Line7),
readln(Line8),
readln(Line9),
readln(Line10),
(symptom(Patient,fever,Line1,Line2,Line3,Line4,Line5,Line6,Line7,Line8,Line9,Line10) -> (Cold is 80, Severe_Cold is 50)),
(symptom(Patient,runny_nose,Line1,Line2,Line3,Line4,Line5,Line6,Line7,Line8,Line9,Line10) -> Severe_Cold is Severe_Cold + 50),
write(Severe_Cold), write(Cold),
append([cold/Cold/red],[],N1),
append([severe_cold/Severe_Cold/red],N1,BarList),
%% write(Patient,"probably has ",Disease,"."),nl.
write(Severe_Cold),
((Cold =:= 100 -> Disease = cold) ; (Severe_Cold =:= 100 -> Disease = severe_cold)),
write(Patient),
write(' probably has '),
write(Disease),
write('.'),
test_barchart(BarList).
When programming in Prolog, you need to do some research into the language to understand how it works. Many Prolog beginners make the mistake of learning a couple of snippets of Prolog logic and then apply what they know of other languages to attempt to create a valid Prolog programming. However, Prolog doesn't work like other common languages at all.
Regarding variables, there are no globals. Variables are always "local" to a predicate clause. A predicate clause is one of one or more clauses that describe a predicate. For example:
foo(X, Y) :- (some logic including X and Y).
foo(X, Y) :- (some other logic including X and Y).
foo(X, X) :- (some other logic including X).
These are three clauses describing the predicate foo/2. The value of X or Y instantiated in one clause isn't visible to the other clauses.
If you want to instantiate a variable in one predicate and use it in another, you have to pass it as a parameter:
foo([1,2,3,4], L),
bar(L, X).
Here, foo might instantiate L using some logic and based upon the instantiated value of [1,2,3,4] for the first argument. Then L (now instantiated) is passed as the first argument to the predicate bar.
If you need a value to be persistent as data, you could assert it as a fact as follows:
foo :-
(some logic that determines X),
assertz(my_fact(X)),
(more logic).
bar :-
my_fact(X), % Will instantiate X with what was asserted
(some logic using X).
This would work, but is not a desirable approach in Prolog to "fake" global variables. Asserting items into persistent data is designed for the purpose of maintaining a Prolog database of information.
So you can see that the logic you have involving Line1, Line2, ... will not work. One clue would be that you must have received many warnings about these variables being "singleton". You need to study Prolog a bit more, then recast your logic using that knowledge.
Related
negation \+ and vanilla meta-interpreter
The following is the classic "textbook" vanilla meta-interpreter for prolog. % simplest meta-interpreter solve(true) :- !. solve((A,B)):- !, solve(A), solve(B). solve(A) :- clause(A,B), solve(B). The following is simple program which establishes facts two relations which are "positive" and one relation which makes use of negation by failure \+. % fruit fruit(apple). fruit(orange). fruit(banana). % colour yellow(banana). % Mary likes all fruit likes(mary, X) :- fruit(X). % James likes all fruit, as long as it is yellow likes(james, X) :- fruit(X), yellow(X). % Sally likes all fruit, except yellow fruit likes(sally, X) :- fruit(X), \+ (yellow(X)). The meta-interpeter can handle goals related to the first two relations ?-solve(likes(mary,X)) and ?- solve(likes(james,X)_. However it fails with a goal related to the third relation ?- solve(likes(sally,X). The swi-prolog reports a stack limit being reached before the program crashes. Question 1: What is causing the meta-interpreter to fail? Can it be easily adjusted to cope with the \+ negation? Is this related to the sometimes discussed issue of built-ins not being executed by the vanilla meta-interpreter? Question 2: Where can I read about the need for those cuts in the vanilla meta-interpreter? Tracing suggests the goal is being grown endlessly: clause(\+call(call(call(call(yellow(apple))))),_5488) Exit:clause(\+call(call(call(call(yellow(apple))))),\+call(call(call(call(call(yellow(apple))))))) Call:solve(\+call(call(call(call(call(yellow(apple))))))) Call:clause(\+call(call(call(call(call(yellow(apple)))))),_5508) Exit:clause(\+call(call(call(call(call(yellow(apple)))))),\+call(call(call(call(call(call(yellow(apple)))))))) Call:solve(\+call(call(call(call(call(call(yellow(apple))))))))
Change solve(A) into: solve(Goal) :- writeln(Goal), sleep(1), clause(Goal, Body), solve(Body). ... and we see: ?- solve_mi(likes(sally,X)). likes(sally,_8636) fruit(_8636) \+yellow(apple) \+call(yellow(apple)) \+call(call(yellow(apple))) \+call(call(call(yellow(apple)))) ... clause/2 determines the body of \+yellow(apple) to be \+call(yellow(apple)), which is not a simplification. Can use instead: solve_mi(true) :- !. solve_mi((Goal1, Goal2)):- !, solve_mi(Goal1), solve_mi(Goal2). solve_mi(\+ Goal) :- !, \+ solve_mi(Goal). solve_mi(Goal) :- clause(Goal, Body), solve_mi(Body). Result in swi-prolog: ?- solve_mi(likes(sally,X)). X = apple ; X = orange ; false. I'm using solve_mi because solve conflicts with e.g. clpBNR, and I'm not using variable names A and B because they convey no meaning. For understanding the cuts, I'd recommend gtrace, to see the unwanted unification with other goals that would otherwise take place.
How to link constant with variable by assert?
I want to add in the DB a constant and a linked variable: ?- assertz(my(x, A)) So that in the future I can define A and get the only one entry. Sth like that: ?- assertz(my(x, A)), ..., A = 2. ?- my(A, B). A = x, B = 2. Can this be done?
As I noted in the comments your idea of a link like a pointer is not the way to approach solving your problem. A common solution is to walk the tree and construct a new tree as you walk the tree by replacing the leaf of the tree with a new leaf that contains the value from the input tree along with the associated value, what you are thinking should be linked. Since you are somewhat new to Prolog I will do this with two examples. The first will just walk a tree and only return true when successfully walked. It can be used to understand how to walk a tree and run with gtrace to single step the code to understand it. The second example will expand on the tree walk and add the type (link as you think) to the leaf item. The the old leaf for something simple like an atom a, will become a new leaf in the tree like (a,atom). Also this was quickly written as a demonstration only. I am sure it will have problems if pressed into doing anything more than the single example. :- module(example, [ example/1 ]). example(walk) :- Term = term_size(a(1,"Hello",'Atom',1+2,[a,$,T])), walk(Term). example(infer_type) :- Term = term_size(a(1,"Hello",'Atom',1+2,[a,$,T])), infer_type(Term,Is), write(Is). walk([]) :- !. walk([T]) :- var(T), !. walk(L) :- is_list(L), !, L = [H|T], walk(H), walk(T). walk(T) :- compound(T), !, T =.. [_|Args], !, walk(Args). walk(T) :- integer(T), !. walk(T) :- var(T), !. walk(T) :- atomic(T), !. walk(T) :- T =.. [Arg|Args], !, walk(Arg), walk(Args). infer_type([],[]) :- !. infer_type([T],[(T,var)]) :- var(T), !. infer_type(L,S) :- is_list(L), !, L = [H|T], infer_type(H,I), infer_type(T,Is), S = [I|Is]. infer_type(T,S) :- compound(T), !, T =.. [F|Args], !, infer_type(Args,Is), S =.. [F|Is]. infer_type(T,(T,integer)) :- integer(T), !. infer_type(T,(T,var)) :- var(T), !. infer_type(T,(T,atom)) :- atomic(T), !. infer_type(T,S) :- T =.. [Arg|Args], !, infer_type(Arg,I), infer_type(Args,Is), S =.. [I|Is]. Example run Note: I know there are warnings; it is a demo not production code. Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.3) ?- working_directory(_,'C:/Users/Groot'). true. ?- [example]. Warning: c:/users/Groot/example.pl:20: Warning: Singleton variables: [T] Warning: c:/users/Groot/example.pl:24: Warning: Singleton variables: [T] true. ?- example(walk). true. ?- example(infer_type). term_size(a((1,integer),(Hello,atom),(Atom,atom),(1,integer)+(2,integer),[(a,atom),(($),atom),(_25642,var)])) true. As an exercise I did not identify the string as a string, the change should be easy.
Rule in prolog to define whether an atom (which in this case a name) is the oldest son or not?
Here's the code in prolog: son(blake, john). son(blake, katey). son(toney, john). son(toney, katey). son(flory, john). son(flory, katey). son(charlie, stark). son(charlie, shrek). son(valenti, stark). son(valenti, shrek). age(blake, 13). age(toney, 15). age(flory, 19). age(charlie, 48). age(valenti, 49). The definition of predicate and rules are: son(X, Y) means X is the son of Y age(M, N) means age of M is N siblings(P, Q) means P and Q are siblings My question is how to make a rule, let it be named oldestSon(X) that true if X is the oldestSon of a family? When the query is oldestSon(flory) and oldestSon(valenti) it returns true or yes, and when the query is oldest(toney) it returns false or no. I give it a try by writing these lines of code: oldestSon(X) :- son(X, _), son(Y, _), siblings(X, Y), age(X, ageX), age(Y, ageY), ageX >= ageY. But, when I try to input a query, in any of the case, when the query is oldestSon(blake). It returns like this: false ? ; false ? ; false ? ; ... How to make it only make only one output without using any external library or making another rule?
Your code is not working because: it doesn't correctly describe the conditions for someone to be the oldest son in a family, and it uses constants (ageX and ageY) where it should be using variables (AgeX and AgeY). Assuming that "the oldest son in a family is the one who has no older sibling", you can code: oldestSon(X) :- age(X, AgeX), not( ( siblings(X, Y), age(Y, AgeY), AgeY > AgeX ) ). I am also assuming that the predicate siblings/2 is already defined as: siblings(X, Y) :- son(X, Z), son(Y, Z), X \= Y.
Is there a way to do size comparison?
I have recently discovered the language Prolog and have been doing exercises on its basics. I am currently creating a database on animal classes like mammals, birds and reptiles, I want to expand the database by having a size comparison within the animals but not sure how. Here is my database. warm_blooded(bat). warm_blooded(penguin). cold_blooded(crocodile). has_fur(bat). has_feathers(penguin). has_scales(crocodile). gives_birth_live(bat). lays_eggs(penguin). lays_eggs(crocodile). produces_milk(bat). has_lungs(crocodile). has_lungs(bat). has_lungs(penguin). %% if the being belongs to the mammalai class ,mammalia being the scientific word for mammal mammalia(X) :- warm_blooded(X), produces_milk(X), ( has_fur(X) ; gives_birth_live(X) ), format('~w ~s mammal ~n', [X, "is a"]). %% if the being belongs to the aves class aves being the scientific word for bird aves(X) :- warm_blooded(X), has_feathers(X), lays_eggs(X), has_lungs(X), format('~w ~s bird ~n', [X, "is a"]). %% if the being belongs to the reptillia class(reptillia being the scientific word for reptile reptillia(X) :- cold_blooded(X), lays_eggs(X), has_scales(X), has_lungs(X), format('~w ~s reptile ~n', [X, "is a"]). I've tried adding sizes within the parameters but I keep getting compilation errors. I want to have an output wherein the user is able to determine which animal is bigger when compared with each other.
A simple an effective way is to just associate a size fact with each animal. size(bat,1). size(penguin,2). size(crocodile,3). Then add one predicate with two clauses to chose the larger of the two animals. larger(A,B,A) :- size(A,S1), size(B,S2), S1 > S2. larger(A,B,B) :- size(A,S1), size(B,S2), S2 >= S1. Examples: ?- larger(penguin,crocodile,X). X = crocodile. ?- larger(penguin,bat,X). X = penguin ; false. ?- larger(bat,bat,X). X = bat. Note that for examples where the the second animal is smaller, it tries the first clause and succeeds, but then has a choice point and so tries the second clause and fails. This is the pure solution. If you want to use a cut to avoid the choice point, which is impure, you can do the following larger_2(A,B,A) :- size(A,S1), size(B,S2), S1 > S2, !. larger_2(A,B,B) :- size(A,S1), size(B,S2), S2 >= S1, !. Examples: ?- larger_2(penguin,crocodile,X). X = crocodile. ?- larger_2(penguin,bat,X). X = penguin. ?- larger_2(bat,bat,X). X = bat. Another way as noted by Daniel Lyons is to use ->/2 larger_3(A,B,Larger) :- size(A,SA), size(B,SB), ( SA > SB -> Larger = A ; Larger = B ). This variation is not one operator of just ->/2 but a combination of both ->/2 and ;2. This also does not leave a choice point and is impure because it too uses a cut (!). Using listing/1 we can see the implementation in Prolog. ?- listing('->'/2). :- meta_predicate 0->0. system:A->B :- call(( A -> B )). true. ?- listing(;/2). :- meta_predicate 0;0. system:A:B;A:C :- !, call(A:(B;C)). system:A:B;C:D :- call(A:(B;C:D)). true. Notice the cut !. How the two operators work together is noted in the SWI-Prolog documentation. The combination ;/2 and ->/2 acts as if defined as: If -> Then; _Else :- If, !, Then. If -> _Then; Else :- !, Else. If -> Then :- If, !, Then. One other point to note about the use of ->/2 with ;/2 is that the syntactic layout among many Prolog programmers is to use () with the combination and offset the operators ->/2 and ;2 so that the ; stands out. ( % condition -> % true ; % false ) When a ; is used as an OR operator and not offset the ; is often overlooked in doing a quick scan of the source code as it is seen as a comma , instead of a ;. Also note the absence of . or , after SA > SB and Larger = A and Larger = B but at the end an operator is needed, ).
Arithmetic operations in the head of a clause
I am a writing Prolog predicate that would compare some point (myPosition(2,2)) in Cartesian coordinate system with some other point in the neighbourhood. As a result, it should show the direction (north, east, south, west) that we should choose to get from myPosition(2,2) to that point. Here is the code of my .pl file: myPosition(2,2). turn_to_east(X+1,Y) :- myPosition(X,Y), write('East.'). turn_to_west(X-1,Y) :- myPosition(X,Y), write('West.'). turn_to_north(X,Y+1) :- myPosition(X,Y), write('North.'). turn_to_south(X,Y-1) :- myPosition(X,Y), write('South.'). turn_to_the_point(X,Y) :- turn_to_east(X,Y). turn_to_the_point(X,Y) :- turn_to_west(X,Y). turn_to_the_point(X,Y) :- turn_to_north(X,Y). turn_to_the_point(X,Y) :- turn_to_south(X,Y). Then, when I upload the file to SWI-Prolog and write: turn_to_the_point(1,2). I get just: false. Why can't I get the answer 'West.' or any other?
The reason is that by default Prolog does not interpret +, - and other arithmetic operators. 1+1 is simply 1+1 (this is syntactical sugar for +(1,1)), not 2. This could be useful if you for instance would like to define your own expression evaluator, and you see + as the boolean sum, or something completely different. There is however one way to collapse such expression such that it derives 2 out of 1+1 using the (is)/2 predicate. For instance: turn_to_east(NX,Y) :- myPosition(X,Y), NX is X+1, write('East.'). Given you query turn_to_east/2 with turn_to_east(1,2), NX = 1. Now you fetch the myPosition/2 data: X = 2 and Y = 2. Prolog does an equivalence check in the meantime and sees that the Y-coordinate of turn_to_east/2 is the same as the one of myPosition. Next it collapses 2+1 to 3 and sees that this is not equivalent to NX = 1 so this predicate fails. But if you had queried turn_to_east(3,1) it would have succeeded and thus write East.. If you modify your entire theory with the above discussed concept, like: myPosition(2,2). turn_to_east(NX,Y) :- myPosition(X,Y), NX is X+1, write('East.'). turn_to_west(NX,Y) :- myPosition(X,Y), NX is X-1, write('West.'). turn_to_north(X,NY) :- myPosition(X,Y), NY is Y+1, write('North.'). turn_to_south(X,NY) :- myPosition(X,Y), NY is Y-1, write('South.'). turn_to_the_point(X,Y) :- turn_to_east(X,Y). turn_to_the_point(X,Y) :- turn_to_west(X,Y). turn_to_the_point(X,Y) :- turn_to_north(X,Y). turn_to_the_point(X,Y) :- turn_to_south(X,Y). It answers the query correctly: ?- turn_to_the_point(1,2). West. true ; false. A note in general is that predicates better not have side-effects like write/1 stuff: this is not only for Prolog, almost all programming languages advice to split a program into calculation and interaction. Perhaps a better way to solve this, is to see the direction as an parameter: myPosition(2,2). turn_to_point(NX,Y,east) :- myPosition(X,Y), NX is X+1. turn_to_point(NX,Y,west) :- myPosition(X,Y), NX is X-1. turn_to_point(X,NY,north) :- myPosition(X,Y), NY is Y+1. turn_to_point(X,NY,south) :- myPosition(X,Y), NY is Y-1. turn_to_the_point(X,Y) :- turn_to_point(X,Y,D), write(D). In that case the turn_to_the_point/2 predicate is clearly an interaction predicate whereas its turn_to_the_point/3 variant does computations.