首页 > 解决方案 > Prolog uncaught exception: error(existence_error(procedure,a/0),top_level/0)

问题描述

I'm working on a program in Prolog that is like a training video for a fast food place. My code compiles, but no matter what I answer once I get to the second state I get an error that says

"uncaught exception: error(existence_error(procedure,a/0),top_level/0)"

%Taylor Loslo
%Program 15
%Krusty Krab Training PRogram

start_state(uniformshirt).

next_state(uniformshirt,a,uniformshoes).
next_state(uniformshirt,b,uniformshoes).
next_state(uniformshoes,a,makekrabbypattystart).
next_state(uniformshoes,b,makekrabbypattystart).
next_state(uniformshoes,c,makekrabbypattystart).
next_state(uniformshoes,d,makekrabbypattystart).
next_state(makekrabbypattystart,a,cookedpattysv).
next_state(makekrabbypattystart,b,veggiesadded).
next_state(makekrabbypattystart,c,sauceadded).
next_state(makekrabbypattystart,d,makekrabbypattystart).
next_state(cookedpattysv,a,pattyveggies).
next_state(cookedpattysv,b,pattysauce).
next_state(veggiesadded,a,pattyveggies).
next_state(veggiesadded,b,veggiesauce).
next_state(sauceadded,a,pattysauce).
next_state(sauceadded,b,veggiesauce).
next_state(pattysauce,s,pattyfinished).
next_state(pattysauce,b,pattyfinished).
next_state(veggiesauce,s,pattyfinished).
next_state(veggiesauce,b,pattyfinished).
next_state(pattyveggies,s,pattyfinished).
next_state(pattyveggies,b,pattyfinished).
next_state(pattyfinished,s,pattyserved).

display_intro :-
    write('WELCOME TO THE KRUSTY KRAB TRAINING PROGRAM'),nl,
    write('Congratulations on your new job as FRY COOK '),nl,nl,
    write('Lets get started!'),nl.


initialize :-
    asserta(stored_answer(money,0)),
    asserta(stored_answer(toes,10)),
    asserta(stored_answer(handswashed,no)),
    asserta(stored_answer(patty,no)),
    asserta(stored_answer(veggies,no)),
    asserta(stored_answer(secretsauce,no)).

goodbye :-
    write('Training Results:'),nl,
    write('Money: $'),stored_answer(money,_),nl,
    write('Toes: '),stored_answer(toes,_),nl,nl.

show_state(uniformshirt) :-
    write('You are deciding what to wear for your first day of work.'),nl,
    write('Do you...'),nl,
    write('(a) Wear your clean Krusty Krab polo shirt'),nl,
    write('(b) Wear your favorite t-shirt that youve been wearing for 3 days straight'),nl,
    write('(q) Quit'),nl,nl.

show_state(uniformshoes) :-
    write('Before heading to work you have to put on shoes'),nl,
    write('Do you pick...'),nl,
    write('(a) Comfortable skater shoes'),nl,
    write('(b) Non-slip sneakers'),nl,
    write('(c) Flip flops'),nl,
    write('(q) Quit'),nl,nl.

show_state(makekrabbypattystart) :-
    write('Now that you are properly dressed lets make a Krabby Patty'),nl,nl,
    write('Do you want to...'),nl,
    write('(a) Start cooking the patty'),nl,
    write('(b) Chop the veggies'),nl,
    write('(c) Add secret sauce'),nl,
    write('(d) Wash your hands'),nl,
    write('(q) Quit'),nl,nl.

show_state(cookedpattysv) :-
    write('Next do you want to...'),nl,
    write('(a) Chop the veggies'),nl,
    write('(b) Add secret sauce'),nl,
    write('(q) Quit'),nl,nl.


show_state(pattysauce) :-
    write('Next do you want to...'),nl,
    write('(s) Serve it to the customer'),nl,
    write('(b) Chop the Veggies'),nl,
    write('(q) Quit'),nl,nl.

show_state(pattyveggies) :-
    write('Next do you want to...'),nl,
    write('(s) Serve it to the customer'),nl,
    write('(b) Add the secret sauce'),nl,
    write('(q) Quit'),nl,nl.

show_state(veggiesadded) :-
    write('Next do you want to...'),nl,
    write('(a) Cook the patty'),nl,
    write('(b) Add secret sauce'),nl,
    write('(q) Quit'),nl,nl.

show_state(veggiesauce) :-
    write('(s) Serve it to the customer'),nl,
    write('(b) Cook the patty'),nl,
    write('(q) Quit'),nl,nl.

show_state(sauceadded) :-
    write('Next do you want to...'),nl,
    write('(a) Cook the patty'),nl,
    write('(b) Chop the veggies'),nl,
    write('(q) Quit'),nl,nl.

show_state(pattyfinished) :-
    write('Next do you want to...'),nl,
    write('(s) Serve it to the customer'),nl,
    write('(q) Quit'),nl,nl.

show_state(pattyserved) :-
    stored_answer(patty,yes),
    stored_answer(veggies,yes),
    stored_answer(secretsauce,no),
    write('You forgot to add the secretsauce!'),nl,
    write('The customer complains its dry and you have to make another'),nl,nl,
    show_state(washedhands),
    goodbye.

show_state(pattyserved) :-
    stored_answer(patty,yes),
    stored_answer(veggies,yes),
    stored_answer(secretsauce,yes),
    write('You served a perfect Krabby Patty!'),nl,nl,
    show_state(washedhands),
    goodbye.

show_state(pattyserved) :-
    stored_answer(patty,no),
    stored_answer(veggies,yes),
    stored_answer(secretsauce,yes),
    write('You forgot to add the patty!'),nl,
    write('The customer complains and you have to make another'),nl,nl,
    show_state(washedhands),
    goodbye.

show_state(pattyserved) :-
    stored_answer(patty,yes),
    stored_answer(veggies,no),
    stored_answer(secretsauce,yes),
    write('You forgot to add the veggies!'),nl,
    write('The customer does no complain because he always picks those off anyways'),nl,nl,
    show_state(washedhands),
    goodbye.

show_state(washedhands) :-
    stored_answer(handswashed,yes),
    write('Thank you for remembering to wash your hands before you started'),nl,nl.

show_state(washedhands) :-
    stored_answer(handswashed,no),
    write('You did not wash your hands before cooking the patty'),nl,
    write('The customer gets sick and sues you.'),nl,nl,
    stored_answer(money,Amount),
    retract(stored_answer(money,_)),
    Newamount is Amount - 10000,
    asserta(stored_answer(money,Newamount)).

show_transition(uniformshirt,b) :-
    write('Mr Krabs notices how filthy your shirt is and sends you home to put on a clean one'),nl,
    write('You lost $5 in wages from missing work'),nl,nl,
    stored_answer(money,Amount),
    retract(stored_answer(money,_)),
    Newamount is Amount - 5,
    asserta(stored_answer(money,Newamount)).

show_transition(uniformshirt,a) :-
    write('Looking good!'),nl,nl.

show_transition(uniformshoes,a) :-
    write('You come into work and immediately slip on grease.'),nl,
    write('You crack your skull open and have to miss work for 6 weeks'),nl,
    write('Mr Krabs refuses to pay you workmans compensation'),nl,
    write('Next time you come into work you wear non-slip shoes...'),nl,nl.

show_transition(uniformshoes,c) :-
    write('OUCH!'),nl,
    write('You lose your big toe when you drop a knife while you are cutting onions'),nl,
    write('You come into work the next day with crutches and non-slip shoes...'),nl,nl,
    stored_answer(toes,_),
    retract(stored_answer(toes,_)),
    asserta(stored_answer(toes,9)).

show_transition(uniformshoes,b) :-
    write('Perfect! Those are the most practical shoes for the job'),nl,nl.

show_transition(sauceadded,a) :-
    retract(stored_answer(patty,no)),
    asserta(stored_answer(patty,yes)),
    write('You cook the patty to perfection.'),nl,nl.

show_transition(sauceadded,b) :-
    retract(stored_answer(veggies,no)),
    asserta(stored_answer(veggies,yes)),
    write('You chopped the onions, lettuce, and tomato. '),nl,
    write('You added the veggies to the burger. '),nl,nl.

show_transition(makekrabbypattystart,a) :-
    retract(stored_answer(patty,no)),
    asserta(stored_answer(patty,yes)),
    write('You cook the patty to perfection.'),nl,nl.

show_transition(makekrabbypattystart,d) :-
    retract(stored_answer(handswashed,no)),
    asserta(stored_answer(handswashed,yes)),
    write('You washed your hands.'),nl,nl.

show_transition(pattysauce,b) :-
    retract(stored_answer(veggies,no)),
    asserta(stored_answer(veggies,yes)),
    write('You chopped the onions, lettuce, and tomato. '),nl,
    write('You added the veggies to the burger. '),nl,nl.

show_transition(veggiesauce,b) :-
    retract(stored_answer(patty,no)),
    asserta(stored_answer(patty,yes)),
    write('You cook the patty to perfection.'),nl,nl.

show_transition(makekrabbypattystart,c) :-
    retract(stored_answer(secretsauce,no)),
    asserta(stored_answer(secretsauce,yes)),
    write('You added the secret sauce to the burger. '),nl,nl.

show_transition(veggiesadded,b) :-
    retract(stored_answer(secretsauce,no)),
    asserta(stored_answer(secretsauce,yes)),
    write('You added the secret sauce to the burger. '),nl,nl.

show_transition(veggiesadded,a) :-
    retract(stored_answer(patty,no)),
    asserta(stored_answer(patty,yes)),
    write('You cook the patty to perfection.'),nl,nl.

show_transition(pattyveggies,b) :-
    retract(stored_answer(secretsauce,no)),
    asserta(stored_answer(secretsauce,yes)),
    write('You added the secret sauce to the burger. '),nl,nl.

show_transition(makekrabbypattystart,b) :-
    retract(stored_answer(veggies,no)),
    asserta(stored_answer(veggies,yes)),
    write('You chopped the onions, lettuce, and tomato. '),nl,
    write('You added the veggies to the burger. '),nl,nl.

show_transition(cookpattysv,b) :-
    retract(stored_answer(secretsauce,no)),
    asserta(stored_answer(secretsauce,yes)),
    write('You added the secret sauce to the burger. '),nl,nl.

show_transition(cookpattysv,a) :-
    retract(stored_answer(veggies,no)),
    asserta(stored_answer(veggies,yes)),
    write('You chopped the onions, lettuce, and tomato. '),nl,
    write('You added the veggies to the burger. '),nl,nl.

show_transition(_,s) :-
    show_state(pattyserved).


go :-
    intro,
    start_state(X),
    show_state(X),
    get_choice(X,Y),
    go_to_next_state(X,Y).

intro :-
    display_intro,
    clear_stored_answers,
    initialize.

go_to_next_state(_,q) :-
    goodbye,!.

go_to_next_state(S1,Cin) :-
    next_state(S1,Cin,S2),
    show_transition(S1,Cin),
    show_state(S2),
    stored_answer(moves,K),
    OneMoreMove is K + 1,
    retract(stored_answer(moves,_)),
    asserta(stored_answer(moves,OneMoreMove)),
    get_choice(S2,Cnew),
    go_to_next_state(S2,Cnew).

go_to_next_state(S1,Cin) :-
    \+ next_state(S1,Cin,_),
    show_transition(S1,fail),
    get_choice(S1,Cnew),
    go_to_next_state(S1,Cnew).

get_choice(_,C) :-
    write('Next entry (letter followed by a period)? '),
    read(C).

% case knowledge base - user responses

:- dynamic(stored_answer/2).

% procedure to get rid of previous responses
% without abolishing the dynamic declaration

clear_stored_answers :- retract(stored_answer(_,_)),fail.
clear_stored_answers.

I'm completely stuck, how do I lose this error and proceed through the rest of the program?

标签: prolog

解决方案


我添加了很多trace/1调用来找出你的问题是什么,但你可以通过做找到它trace(stored_answer/2),因为你会看到这样的东西:

Before heading to work you have to put on shoes
Do you pick...
(a) Comfortable skater shoes
(b) Non-slip sneakers
(c) Flip flops
(q) Quit

 T Exit: (10) show_state(uniformshoes)
 T Call: (10) stored_answer(moves, _5580)
 T Fail: (10) stored_answer(moves, _5580)
 T Redo: (10) show_transition(uniformshirt, a)
 T Fail: (10) show_transition(uniformshirt, a)
 T Redo: (9) go_to_next_state(uniformshirt, a)
 T Call: (10) next_state(uniformshirt, a, _5582)
 T Exit: (10) next_state(uniformshirt, a, uniformshoes)
 T Fail: (9) go_to_next_state(uniformshirt, a)
false.

这表明stored_answer(moves, K)在循环的第一次迭代中失败,因为它还没有被初始化。所以解决方案只是添加一行初始化该变量到您的initialize谓词:

initialize :-
    asserta(stored_answer(money,0)),
    asserta(stored_answer(toes,10)),
    asserta(stored_answer(handswashed,no)),
    asserta(stored_answer(patty,no)),
    asserta(stored_answer(veggies,no)),
    asserta(stored_answer(secretsauce,no)),
    asserta(stored_answer(moves,0)).  %% <-- new

更改后,该程序似乎按您的预期工作。


推荐阅读