fork download
  1.  
  2. % ==========================================
  3. % A. FACTS
  4. % ==========================================
  5.  
  6. parent(robert, john).
  7. parent(roberta, john).
  8. parent(robert, sarah).
  9. parent(roberta, sarah).
  10.  
  11. parent(john, alice).
  12. parent(maria, alice).
  13. parent(john, ben).
  14. parent(maria, ben).
  15.  
  16. parent(sarah, chloe).
  17. parent(david, chloe).
  18.  
  19. % ==========================================
  20. % B. RULES
  21. % ==========================================
  22.  
  23. grandparent(X, Z) :-
  24. parent(X, Y),
  25. parent(Y, Z).
  26.  
  27. sibling(X, Y) :-
  28. parent(P, X),
  29. parent(P, Y),
  30. X \= Y.
  31.  
  32. % ==========================================
  33. % C. INPUT AND CHECK RELATIONSHIP
  34. % ==========================================
  35.  
  36. main :-
  37. read(Relation),
  38. read(Person1),
  39. read(Person2),
  40.  
  41. ( check_relation(Relation, Person1, Person2)
  42. -> format('true: ~w(~w, ~w)~n',
  43. [Relation, Person1, Person2])
  44. ; format('false: ~w(~w, ~w)~n',
  45. [Relation, Person1, Person2])
  46. ),
  47.  
  48. check_relation(parent, X, Y) :-
  49. parent(X, Y).
  50.  
  51. check_relation(grandparent, X, Y) :-
  52. grandparent(X, Y).
  53.  
  54. check_relation(sibling, X, Y) :-
  55. sibling(X, Y).
  56.  
  57. :- initialization(main).
  58.  
Success #stdin #stdout 0.01s 6012KB
stdin
sibling.
john.
roberta.
stdout
false: sibling(john, roberta)