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).
Related
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
How to make the program proceed to menu1/0 after false appear
mylist([a,b,c]).
myprog(X) :- mylist(L), member(X, L).
go :- start_message,menu1.
start_message :-
write('This is a program.'),nl,
write('Below are a list:'),nl,
myprog(X),
write(X),nl,
fail.
menu1 :-
nl,write('Select operation:'),nl,
write('1. Input time'),nl,
write('2. Exit program'),nl.
Below is what I am stuck with:
go.
This is a program.
Below are a list:
a
b
c
false
You can do like this
start_message :-
write('This is a program.'),nl,
write('Below are a list:'),nl,
( myprog(X),
write(X),nl,
fail
; true ).
Now when myprog/1 cannot succeed anymore, control goes to true/0 and start_message/0 succeeds.
Remove the fail statement in the last line of start_message.
I'm trying to write a critical section guarded by a mutex in SWI-Prolog and have been looking at using setup_call_cleanup/3 and setup_call_catcher_cleanup/4.
The problem I have is that my Goal is a sequence of operations of which any may fail and that means the system backtracks to the start of setup_call_cleanup and calls Cleanup. Unfortunately, with backtracking I'm not able to report the error appropriately. To illustrate my issue let's consider this simple example:
setup_call_cleanup(
mutex_lock(mtx),
( Step1 = true, Step2 = true, Step3 = true ),
( mutex_unlock(mtx), writeln([Step1, Step2, Step3]) ).
and compare it with the following:
setup_call_cleanup(
mutex_lock(mtx),
( Step1 = true, Step2 = true, fail, Step3 = true ),
( mutex_unlock(mtx), writeln([Step1, Step2, Step3]) ).
In the first case all is ok -- I can see all steps done. But in the second case I'm not able to see that Step1 and Step2 has been carried out. I'd like to see it because they may have external side effects which backtracking cannot undo. Also, I don't want to include error handling within the Goal to make the critical section as lean and fast as possible.
I have two ideas:
Decorate each step with nb_setval to store a value to indicate the completed steps,
Re-code steps, so they throw exceptions that carry details of the problem.
The former will make code rather bloated, whereas the latter seems too heavyweight for my needs. Is there anything like setup_nb_call_cleanup?
The trick, I think, is to run the goals one by one, guarded for errors and failure and return step that failed. A good start is
until_failure((A,B), Result) :-
!,
until_failure(A, Result),
( var(Result)
-> until_failure(B, Result)
; true
).
until_failure(G, Result) :-
( catch(G, Result, true)
*-> true
; Result = false(G)
).
Now you can run e.g.,
?- until_failure((Step1 = true,
Step2 = true,
fail,
Step3 = true), Result),
writeln([Step1, Step2, Step3]).
[true, true, _5742]
Result = false(fail)
See http://swish.swi-prolog.org/p/ReQWsvCg.swinb. SWISH doesn't allow for
handling mutexes, but you can easily wrap this inside with_mutex/2. The details depend notably on how you want to handle non-determinism.
Thank you Jan for the inspiration; very useful. I ended up coding a similar step_by_step rule:
step_by_step(Goal, Steps, Error) :-
step_by_step_(Goal, 0, Steps, Error).
step_by_step_((A, B), InStep, OutStep, Error) :-
!,
step_by_step_(A, InStep, OutStep1, Error),
( var(Error) ->
step_by_step_(B, OutStep1, OutStep, Error)
;
OutStep = InStep
).
step_by_step_(Goal, InStep, OutStep, Error) :-
( catch(Goal, Ex, (Error = exception(Ex), OutStep = InStep)) *->
(OutStep is InStep + 1 ; true), !
;
Error = false(Goal),
OutStep = InStep
).
I'm not happy with (OutStep is InStep + 1 ; true), ! but wasn't able to find a better way.
Anyway, the rule gives me what I want:
-- if all goes ok, it just runs all steps in sequence:
?- step_by_step((Step1 = true, Step2 = true, Step3 = true), Steps, Error).
Step1 = Step2, Step2 = Step3, Step3 = true,
Steps = 3.
-- if one of the step fails or throws an exception, it returns the number of steps completed successfully and the failed goal:
?- step_by_step((Step1 = true, Step2 = true, fail, Step3 = true), Steps, Error).
Step1 = Step2, Step2 = true,
Steps = 2,
Error = false(fail).
or the exception:
?- step_by_step((Step1 = true, Step2 = true, throw(bomb), Step3 = true), Steps, Error).
Step1 = Step2, Step2 = true,
Steps = 2,
Error = exception(bomb).
As the question says, thats kind of what i wanna simulate in prolog. So i'm making a game,here's some code:
move(X):-
get_char(Y)
get_char(_),
get_char(Z),
not(OldLoc='Z'),
not(NewLoc = 'Z'),
validmove(OldLoc,NewLoc).
move(_):-
write('Thanks for playing!'), nl.
move(X):-
write('Invalid move!'), nl,
write('Try Again?'), nl,
move(X).
what i want to do is if the first predicate check fails at not(OldLoc='Z'),not(NewLoc = 'Z'), then go to the next predicate move(_) and it fails at validmove(OldLoc,NewLoc) then go to the next move(X). I'm very new to prolog and i'm almost completely clueless.
If-then-else:
http://www.swi-prolog.org/pldoc/doc_for?object=send_arrow/2
Another if-then-else:
http://www.swi-prolog.org/pldoc/doc_for?object=(*-%3E)/2
Sample #1:
?- test = test -> print('TRUTH') ; print('FALSE').
TRUTH
Sample #2:
?- test = test -> (test2 = test3 -> print('TRUTH'); print('FALSE2')); print('FALSE').
FALSE2
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.