Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

Problem with building an expert system in prolog

I keep getting syntax errors while compiling this code in prolog. I'm new to programming so I don't quite understand. Can someone please help me debug where I went wrong? The expert system was meant to classify animals with vertebrates and invertebrates. This is the code I currently have:

:-dynamic known/3
top_goal(X):-animal(X).

%Animals and their classifications
animal(lion):-
    type(vertebrate),
    color(brown).

animal(bee):-
    type(invertebrate),
    color(yellow).

animal(crickets):-
    type(invertebrate),
    color(green).

%background information about vertebrates and invertebrates.

animal(vertebrate):-
    vertebral_column(present),
    nerve_cord(dorsal_and_hollow),
    skeleton(internal),
    heart(located_on_right_side),
    haemoglobin(present_in_red_blood_cells).

animal(invertebrate):-
    vertebral_column(absent),
    skeleton(external),
    heart(located_on_dorsal_side),
    haemoglobin(dissolved_in_plasma).

%Ask rules
animal(X):-ask(type,X).
animal(X):-ask(color,X).
animal(X):-ask(sound,X).

ask(A,V):-
    write(A:V),%ask user
    write('?:'),
    read(Y),%get the answer
    asserta(known(Y,A,V),%remember it
    Y==yes.%succeed or fail

solve:-
    retractall(known(_,_,)),
    top_goal(X),
    write('The animal is '),write(X),nl.

solve:-
    write('This animal is unknown '),nl.
question from:https://stackoverflow.com/questions/65908684/problem-with-building-an-expert-system-in-prolog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have some typos.

ask(A,V):-
    write(A:V),%ask user
    write('?:'),
    read(Y),%get the answer
    asserta(known(Y,A,V)), %remember it ***missing ')'
    Y==yes.%succeed or fail

and here

solve:-
    retractall(known(_,_,_)), % *** missing _
    top_goal(X),
    write('The animal is '),write(X),nl.

Then some codes are missing: color/1, haemoglobin/1, heart/1, nerve_cord/1, skeleton/1, top_goal/1, type/1, vertebral_column/1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...