Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   prolog (http://www.programmingforums.org/showthread.php?t=11752)

zander Oct 30th, 2006 5:36 AM

prolog
 
hey does anyone use prolog? i'm doing a module on it in university and can't get to grips with it at all. it's completely different from object oriented languages that i've done, and i can't seem to make the connections between the rules, predicates and operators. predicates just seem to be created without any connection to anything else in the program

here's some this is the program that were working on the enbolded bits are the bits i don't understand.its really just where the predicates come from and what connection they have to the rest of the program.
Quote:


% EXPERT SYSTEM RULES (EXPERT KNOWLEDGE BASE)

rule(if eats(X, Y) and living(Y) then carnivore(X)).
rule(if carnivore(X) and big(X) then dangerous(X)).
rule(if has_feathers(X) then bird(X)).
rule(if bird(X) and small(X) then good_pet(X)).
rule(if cuddly(X) then good_pet(X)).


find_good_pet :-
retractall(kbfact(_)),

check_hypotheses([good_pet(lenny),good_pet(eddie),good_pet(tweety)]).

bird(a_kind_of,animal).
bird(moving_method,fly).
bird(active_at,daylight).
pigeon(a_kind_of,bird).
pigeon(colour,black).
pigeon(size,small).

% ------------------------------------------------------------------
% MAIN EXPERT SYSTEM SHELL CODE

% check_hypothesis(+Hypotheses)
% Succeeds when one of the hypotheses is proved true, or it
% has tried them all.
% Picks a hypothesis, and uses b_chain to find out if it is true.
% If it is true then b_chain succeeds and check_hypothesis writes out
the
% appropriate recommendation. If false it backtracks to 'on' to find
% another hypothesis to try.
% Once it has tried all the hypotheses it will backtrack to second
% check_hypothesis clause and write an appropriate message.

check_hypothesis(Hypoth) :-
bchain(Hypoth), % b_chain to check if true
atext(Hypoth, Text), % get hold of appropriate text
write_list(Text). % write out the recommendation

check_hypotheses(Hypotheses) :-
member(Hypoth, Hypotheses), % get a member of hypotheses
check_hypothesis(Hypoth).
check_hypotheses(_) :-
write('None of the possible hypotheses seems to be true\n').

% bchain(+Goal)
% Succeeds if Goal is true, given rules + facts supplied by user as
% backward chaining proceeds.

bchain(G1 and G2):- % G1 and G2 are true if
bchain(G1), % G1 can be proven in turn
bchain(G2). % and G2 can be too
bchain(Goal) :- % G's true if its a fact!
Goal \= (_G1 and _G2),
kbfact(Goal).
bchain(Goal):- % Goal is true if
rule(if Preconditions then Goal), % there is a rule concluding it
bchain(Preconditions). % and its Preconditions can be
% proven by backward chaining

bchain(Goal):- % Goal is true if
prompt_user(Goal).

% prompt_user(+Goal)
% True if there is some text to use to ask the user about it,
% and when you ask the user they say yes.

prompt_user(Goal) :-
qtext(Goal, Text),
write_list(Text),
format(" >> "),
get_reply(Reply),
process(Reply,Goal).

process(yes,Goal):-
assert( kbfact(Goal)).
process(why,Goal):-
tell_why(Goal),
!,
prompt_user(Goal).

get_reply(Reply):-
read(Answer),
means(Answer,Reply),
!.
get_reply(Reply):-
format("Answer unknown, please try again. >> "),
get_reply(Reply).

write_list([]).
write_list([Word|Words]) :-
format("~w", [Word]),
write_list(Words).
i know i'm being really vague but, any comments would be really appreciated.

jsilver608 Nov 10th, 2006 3:08 AM

Here is a site that may help you out:

http://www.csupomona.edu/~jrfisher/w.../contents.html


All times are GMT -5. The time now is 1:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC