Error in assignment using IF-Else Prolog - prolog

This is part of my code where I am using if else-
foo(4,Lines,A) :-
....
.....
1. ( (X1\=X2) ->write ('Bye')
2. ; (X1 = X2,X3=X4,N\=4) ->write('Hello'),nl,D is A + 4,write(D),
foo(4,lines,D)
3. ).
So this is printing Hello in line2 but then it gives an error.It is not printing value of D.THis is the error I get-
ERROR: at_end_of_stream/1: stream `<stream>(000000000894F470)' does not exist
So is there something wrong with D is A + 4 in line 2 because that is what is giving the error???

I don't really think there was anything wrong with your code. The only error was at write ('Bye') where you had a space and I removed it. Maybe there is something wrong with the value of D. See this,it works for me:
foo(4,Lines,A):- /*read(X1),read(X2),read(X3),read(X4),read(A),read(N),*/
((X1\=X2) -> write('Bye');
(X1 = X2,X3=X4,N\=4) -> write('Hello'),nl,D is A + 4,write(D),
foo(4,lines,D)).
I added the read/1 so I can test the code. It works for me. This is what I get:
3 ?- foo(4,Lines,A).
|: 1. /*read(X1)*/
|: 1. /*read(X2)*/
|: 2. /*read(X3)*/
|: 2. /*read(X4)*/
|: 3. /*read(A)*/
|: 3. /*read(N)*/
Hello
7
|: 1.
|: 1.
|: 2.
|: 2.
|: 3.
false.
After the Hello 7, the rule foo/3 runs again. I expect that you'll do something else then, but I cannot see what, so that was my result.
For more info about the error you got (at_end_of_stream) check the link.
Succeeds after the last character of the named stream is read, or Stream is not a valid input stream. The end-of-stream test is only available on buffered input streams (unbuffered input streams are rarely used; see open/4).

Related

Prolog output message understanding

This is a really quick one, I don't know the correct way to google it!
Say we have an output such it gives:
X = [4|_1218]
what generally has to be changed to get the output just to say 4?
member1(Head, [Head|_]):-!.
member1(Item, [_|Tail]):-
member1(Item, Tail).
findnotin([Head|_], RuledOut, [Head|_]):-
not(member1(Head, RuledOut)).
Edit Completedish program:
find_not_in([], _, _).
find_not_in([Head|Candidates], RuleOut, [Head|Legal]):-
not(member1(Head, RuleOut)),
find_not_in(Candidates, RuleOut, Legal).
find_not_in([_|Candidates], RuleOut, Legal):-
find_not_in(Candidates, RuleOut, Legal).

Prolog find minimum in list error

Im trying to make my code work, but somehow I got stuck on a problem, Im very newbie to prolog. This is my code.
dist(valmiera, riga, 107).
%dist(riga, valmiera, 107).
dist(cesis, riga, 70).
dist(valmiera, rujiena, 50).
dist(rujiena, valka, 30).
dist(valmiera, strenci, 200).
dist(strenci, valka, 30).
dist(valmiera, cesis, 40).
dist(liepaja, saldus, 100).
dist(saldus, riga, 200).
dist(liepaja, jelgava, 270).
dist(jelgava, riga, 50).
path(A,B,C,[A,B]):- dist(A,B,C).
path(A,B,D,[A|As]):- dist(A,W,C), path(W,B,E,As), D is C+E.
%, findMin(YList, E), path(A,B,X,E),!.
shortestPath(A,B,X,E):-findall(Y,path(A,B,S,Y),YList), findMin(YList, E), path(A,B,X,E),!.
findMin([],fail).
findMin([H],E):-E=H,!.
findMin([H,V],E):-H<V, E=H,!; V<H, E=V, !; H=:=V, E=H, !.
findMin([H|T],E):-findMin(T,U), H<U, E=H,!;findMin(T,U), U<H, E=U,!;findMin(T,U), U=:=H, E=U,!.
but when I call findMin() I get this error
uncaught exception: error(type_error(evaluable,'.'/2),(<)/2)
I'm realy stuck and don't know what to do. Any help will be appreciated.
Applications purpose is get shortest path by calling shortestPath(), paths are in dist (a,b,distance)
The exception is because the terms you are trying to compare are lists.
[liepaja,saldus,riga]<[liepaja,jelgava,riga] ?
An expression:
Term1 < Term2
succeeds if
eval(Term1) < eval(Term2)
So, Term1 and Term2 have to be evaluable terms (e.g., (1+1)<(2+2)).
Try to change the shortestPath/4 body:
shortestPath(A,B,X,E):-
findall(couple(W,P),path(A,B,W,P),WP_List),
findMin(WP_List, couple(Weight,ShortestPath)),...
In this way you have a list of couple(Weight,Path) and in findMin you can get the Weight per Path.

swi prolog , matching data

this is my source code , can someone tell me what is error , and wat is the best way to do this , i want to show out who matching who ...can somebody help me ?
% Author:
% Date: 08-Sep-11
person(may,female,25,blue).
person(john,male,30,blue).
match:-person(Fn,'female',Fage,Fatt),
person(Mn,'male',Mage,Matt),
Fage<=Mage,
Fatt=Matt,
write(Fn ,'-- match with----',Mn).
error message :55 ?- match.
ERROR: Undefined procedure: match/0
ERROR: However, there are definitions for:
ERROR: catch/3
false.
match :-
person(Fn,female,Fage,Fatt),
person(Mn,male,Mage,Matt),
Fage =< Mage,
Fatt = Matt,
format('~w~s~w~n',[Fn ,'-- match with----',Mn]).

Debugging in SWI-prolog - unbound variables

Consider the following Prolog code. It edits lines of a particular type in its input and prints out the remaining lines w/o any change. It uses a DCG called rule which isn't included below, since it's not important to the question.
go:-
prompt(_, ''),
processInput.
processInput:-
read_line_to_codes(current_input, Codes),
processInput(Codes).
processInput(Codes):-
(Codes \= end_of_file
->
(phrase(rule(Part1, Part2), Codes)
->
format('~s - ~s\n', [ Part1, Part2 ])
;
format('~s\n', [ Codes ])),
processInput
;
true).
:- go, halt.
This works fine. However, suppose I change processInput/1 to the following, it just says that Warning: /home/asfernan/tmp/tmp.pl:28: Goal (directive) failed: user: (go,halt).
processInput(Codes):-
(Codes \= end_of_file
->
(\+phrase(rule(Part1, Part2), Codes)
->
format('~s\n', [ Codes ]))
;
format('~s - ~s\n', [ Part1, Part2 ]),
processInput
;
true).
The if & else parts of the phrase(rule(Part1, Part2), Codes) DCG match have been exchanged. This is obviously a newbie mistake, but the fact that go, halt failed isn't very helpful. What can I do to make the error message indicate that the failure was because Part1 & Part2 were not bound in the format('~s - ~s\n', [ Part1, Part2 ]) line? I was able to track down this error because the code is small, but I may not have been able to do so had the code been big.
In Prolog the following is not the same:
..., ( Cond -> Then ; Else ), ...
and
..., ( \+ Cond -> Else ; Then ), ...
In general, a goal \+ Cond will never instantiate its variables. So you
have to stick to the original formulation.
In case you are interested to process entire
files with DCGs, consider SWI's library(pio).

error message, not working properly - prolog

How to repair a program to be ready to be used with SWI Prolog?
Link to source: http://ai-programming.com/prolog_bot_tutorial.htm
chatterbot2:
I modified read_string and write_string to read and write:
knowledge_base([
['WHAT IS YOUR NAME',
['MY NAME IS CHATTERBOT2.',
'YOU CAN CALL ME CHATTERBOT2.',
'WHY DO YOU WANT TO KNOW MY NAME?']
],
['HI',
['HI THERE!',
'HOW ARE YOU?',
'HI!']
],
['HOW ARE YOU',
['I''M DOING FINE!',
'I''M DOING WELL AND YOU?',
'WHY DO YOU WANT TO KNOW HOW AM I DOING?']
],
['WHO ARE YOU',
['I''M AN A.I PROGRAM.',
'I THINK THAT YOU KNOW WHO I''M.',
'WHY ARE YOU ASKING?']
],
['ARE YOU INTELLIGENT',
['YES,OFCORSE.',
'WHAT DO YOU THINK?',
'ACTUALY,I''M VERY INTELLIGENT!']
],
['ARE YOU REAL',
['DOES THAT QUESTION REALLY MATERS TO YOU?',
'WHAT DO YOU MEAN BY THAT?',
'I''M AS REAL AS I CAN BE.']
] ]).
select(0, [H|_], H).
select(N, [_|T], L) :- N > 0, N1 is N - 1, select(N1, T, L).
list_length([], 0).
list_length([_|T], Length):- list_length(T, Length2), Length is Length2 + 1.
quit_session(X):- X = bye,
nl, write('IT WAS NICE TALKING TO YOU USER, SEE YOU NEXT TIME!').
no_response_found(ListOfResponse):-
list_length(ListOfResponse, NumOfResponse),
NumOfResponse == 0,
write('I''M NOT SURE IF I UNDERSTAND WHAT YOU ARE TALKING ABOUT.'), !.
no_response_found(_).
get_keyword(KeyList, [KeyList,_]).
get_response(RespList, [_, RespList]).
select_response(RespList, Response):-
list_length(RespList, NumOfResponse),
IndexOfResponse is integer(random(NumOfResponse)),
select(IndexOfResponse, RespList, Response), !.
select_response(_, _).
find_match(Input, [FirstRecord|RestDatabase], ListOfResponse):-
get_keyword(Keyword, FirstRecord),
Keyword == Input, get_response(ListOfResponse, FirstRecord), !;
find_match(Input, RestDatabase, ListOfResponse).
find_match(_, [_], _).
chatterbot2:-
repeat,
nl, write('>'),
read(Input),
knowledge_base(ListOfRecord),
find_match(Input, ListOfRecord, ListOfResponse),
no_response_found(ListOfResponse),
select_response(ListOfResponse, Response),
write(Response), nl,
quit_session(Input).
When I try to use it I get:
鐀1 ?- chatterbot2.
>hi.
I'M NOT SURE IF I UNDERSTAND WHAT YOU ARE TALKING ABOUT.
ERROR: random/1: Domain error: `not_less_than_one' expected, found `0'
Exception: (7) select_response([], _G492) ? creep
2 ?- chatterbot2.
>'What do you do ?'.
I'M NOT SURE IF I UNDERSTAND WHAT YOU ARE TALKING ABOUT.
ERROR: random/1: Domain error: `not_less_than_one' expected, found `0'
Exception: (7) select_response([], _G485) ? creep
3 ?- chatterbot2.
>'Dog is black'.
I'M NOT SURE IF I UNDERSTAND WHAT YOU ARE TALKING ABOUT.
ERROR: random/1: Domain error: `not_less_than_one' expected, found `0'
Exception: (7) select_response([], _G485) ? creep
4 ?-
EDIT:
With random value =/= 0 :
1 ?- chatterbot2.
>'NOT IN BASE'.
I'M NOT SURE WHAT ARE YOU TALKING ABOUT._G907
is it possible to delete that value of blank arguement _G907 ?> and become only sentence?
Your errors in this particular example are caused by your input. Your input is matched to the first entries in your knowledge_base. Since none of your inputs match, the list that is returned has length 0, causing a problem with the call to random, which seems to need a value of at least 1.
Try 'WHAT IS YOUR NAME' as input for example and see if that works.

Resources