Prolog task with pigs, exception error - prolog

I've got this pig riddle...what do I do wrong here? it fails with the uncaught exception (below!). I need to output what does each pig eat and where it comes from as the result. Could someone rectify what I've done wrong. Thanks in advance.
all_pigs([H],L2) :- pig(H,L2).
all_pigs([H|T],L2) :- pig(H,L2), all_pigs(T, L2).
or([H]) :- H,!.
or([H|_]) :- H,!.
or([_|T]) :- or(T).
solve(Kunekune, Landrasse, Schwarzfussschwein, Haegenauchschwein) :-
Kunekune = [Kunekune_land, Kunekune_food],
Landrasse = [Landrasse_land, Landrasse_food],
Schwarzfussschwein = [Schwarzfussschwein_land, Schwarzfussschwein_food],
Haegenauchschwein = [Haegenauchschwein_land, Haegenauchschwein_food],
All = [Kunekune, Landrasse, Schwarzfussschwein, Haegenauchschwein],
all_pigs([japan, danemark,france, australia], [Kunekune_land, Landrasse_land, Schwarzfussschwein_land, Haegenauchschwein_land]),
all_pigs([grass, vegetables, eggs, chestnuts], [Kunekune_food, Landrasse_food, Schwarzfussschwein_food, Haegenauchschwein_food]),
Landrasse = [danemark,Landrasse_food],
pig([japan, vegeables], All),
pig([australia, !(eggs)], All),
Schwarzfussschwein = [Schwarzfussschwein_land, chestnuts],
Kunekune = [!(france), grass].
uncaught exception: error(existence_error(procedure,pig/2),all_pigs/2)

Related

Using attribute_goal/2 the right way in SICStus Prolog

I'm writing a "solver" using the SICStus Prolog attributed variables interface:
:- module(attach2, [attach2/2]).
:- use_module(library(atts)).
:- attribute att/2.
attach2(X,Y) :- put_atts(X,att(X,Y)),
put_atts(Y,att(Y,X)).
verify_attribute(_,_,[]).
attribute_goal(V,(true,G,true,G,true)) :- get_atts(V,att(X,Y)), G = attach2(X,Y).
Sample query:
| ?- attach2(X,Y), copy_term(X,Xc,Xcc), copy_term(Y,Yc,Ycc), copy_term(X+Y,XYc,XYcc).
Xcc = attach2:(true,attach2(Xc,_A),true,attach2(Xc,_A),true),
Ycc = attach2:(true,attach2(Yc,_B),true,attach2(Yc,_B),true),
XYc = _C+_D,
XYcc = (attach2:attach2(_C,_D),attach2:attach2(_D,_C)),
attach2(X,Y),
attach2(Y,X) ? ;
no
If I change attribute_goal/2 to ...
attach2(X,Y) :- put_atts(X,att(X,Y)),
put_atts(Y,att(X,Y)).
... the answer get better:
| ?- attach2(X,Y), copy_term(X,Xc,Xcc), copy_term(Y,Yc,Ycc), copy_term(X+Y,XYc,XYcc).
Xcc = attach2:(true,attach2(Xc,_A),true,attach2(Xc,_A),true),
Ycc = attach2:(true,attach2(_B,Yc),true,attach2(_B,Yc),true),
XYc = _C+_D,
XYcc = attach2:attach2(_C,_D),
attach2(X,Y) ? ;
no
My conclusion from this:
copy_term/3 collects goals from attribute_goal/2 and if it encounters more than one attributed variable, then it removes trivial true and duplicate goals.
It this essentially right?

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.

gprolog expert system - issues reading user input

I'm working through this prolog expert system example that accepts user input. I am having issues with the way user_input is read. I am using gprolog.
animal(dog) :-
is_true('has fur'),
is_true('says woof').
animal(cat) :-
is_true('has fur'),
is_true('says meow').
animal(duck) :-
is_true('has feathers'),
is_true('says quack').
is_true(Question) :-
write('Does the animal have the following attribute: '),
write(Question), write('? '),
read_string(user_input,"\n","\r",End,Response), Response == "yes". /*this line*/
go:-
animal(Animal),
write('I guess that the animal is: '),
write(Animal).
go:-
write('I cannot recognize your animal').
I am getting:
uncaught exception: error(existence_error(procedure,read_string/5),is_true/1)
It's the same error if I try:
read_string(user_input,"\n","\r",_,Response), Response == "yes".

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

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