Forward - display results in Prolog

for example :
if twol then twolegs.
if twoh then twohands
if tenf then tenfingers.
if twof then twofeets.
if smallh then smallhair.
if fourlegs and nohands and nofingers and fourfeets and doghair then dog.
if twolegs and twohands and tenfingers and twofeets and smallhair then person. 

% forward chaining forward :-  
new_derived_fact(P),!,  
write('solution: '),writeln(P),  
assert(derived_fact(P)),  
forward ;  writeln('No more facts').
 new_derived_fact(P) :- 
 if Cond then P,  \+ fact(P),  \+ derived_fact(P), 
 truth(Cond). truth(P) :-
  fact(P) ;  derived_fact(P). 
truth(P1 and P2) :-  truth(P1),  truth(P2).
 truth(P1 or P2) :-  truth(P1) ;  truth(P2).  

%database 
solution(person, address, personid, personhouse). 
solution(dog, dogaddress, dogid, doghouse).

I have this code in prolog using forward chaining.

How to display the result “person” like “address, id, house” from datababase?? ’

Thank you all!

To answer your question: check out the format/N predicate to do nice printouts.

I’m going to offer some advice based on my initial reading of your code. Feel free to ignore what I write as it is only my personal opinion - not fact!

Now, it’s usually the case that messy code is a result of messy thinking, and they say a problem well stated is half solved. So maybe have a comment at the beginning detailing the problem to be solvedm then maybe try to improve the format of your code and write comments specifying the purpose of different clauses. Then ask yourself how the code relates to the initial problem description comment, i.e., see if it all fits together and solves the problem.

2 Likes