swi prolog , matching data - prolog

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]).

Related

how do I solve an error in asking question in prolog?

I am fairly familiar in Prolog language. was writing a DSS in prolog and I encountered the error
pl:96:12: syntax error: . or operator expected after expression
1 error(s)
personally I don't see where the error is
here is a part of the code where the compiler is indicating the error
ask(Question) :-
write('Do you have ?:'),
write(Question),
write('?,y/n '),
read(Response), nl,
( (Response == yes ; Response == no)
->
assert(yes(Question)) ;
assert(no(Question)), fail).
:- dynamic yes/1,no/1.

Creating a Knowledge Base and Querying in Prolog

I want to create the Knowledge base equivalent to the statements as below in Prolog.
John likes all kind of food.
Apple and vegetable are food
Anything anyone eats and not killed is food.
Anil eats peanuts and still alive
Harry eats everything that Anil eats.
So this one of the version borrowed from here
Once this file is created I want to find the answer for the query "John likes peanuts." saying likes(john, peanut).
Attempt to write rules in Prolog from my side is as below
alive(anil).
eats(anil,peanut).
food(apple).
food(vegitables).
food(X):-likes(john,X).
eats(X,Y),not(killed(X)):-food(Y).
eats(anil,X):-eats(harry,X).
killed(X):-not(alive(X)).
alive(X):-not(killed(X)).
But I am getting the errors and warnings as below;
ERROR: No permission to modify static procedure `(',')/2'
Warning: Clauses of eats/2 are not together in the source-file
Warning: Current predicate: food/1
Warning: Use ':- discontiguous eats/2.' to suppress this message
Warning: Clauses of alive/1 are not together in the source-file
Warning: Current predicate: killed/1
Warning: Use ':- discontiguous alive/1.' to suppress this message
"John likes all kind of food."
likes(john,X) :- food(X).
"Apple and vegetable are food."
food(apple).
food(X) :- vegetable(X).
"Anything anyone eats and not killed is food."
food(X) :- eats(P,X), alive(P).
"Anil eats peanuts and still alive."
eats(anil,peanuts).
alive(anil).
"Harry eats everything that Anil eats."
eats(harry,X) :- eats(anil,X).
Asking
?- likes(john,X).
produces (in SWI Prolog)
70 ?- likes(john,X).
X = apple ;
ERROR: food/1: Undefined procedure: vegetable/1
Exception: (9) vegetable(_G19387424) ? fail
Exception: (8) food(_G19387424) ? fail
71 ?-
Fixing it with
:- dynamic(vegetable/1).
we get
75 ?- likes(john,X).
X = apple ;
X = peanuts ;
false.
and so, of course,
101 ?- likes(john,peanuts).
true .

Prolog Procedure si(A) Does not Exist

This is the code
verificar(S) :-
(si(S)
->
true ;
(no(S)
->
fail ;
preguntar(S))).
preguntar(Pregunta) :-
write('Tiene los siguientes sintomas: '),
write(Pregunta),
write('?'),
read(Respuesta),
nl,
( (Respuesta == si)
->
assert(si(Pregunta));
assert(no(Pregunta)), fail).
and the problem is
procedure `si(A)' does not exist
Reachable from:
verificar(A)
resfriado
hipotesis(A)
evaluar
The problem is for the first run, the program does not know what you mean with si(A) since there is no predicate or rule defined. Quickfix: Add dummy data like
si(nothing).
no(nothing).
which can be removed after the first "valid" entry in your knowledge base.
You have not written the predicate for si(). This is why you are getting the error:-
procedure `si(A)' does not exist

Error in assignment using IF-Else 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).

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