Convert FOPL expression to prolog

Viewed 100

Given premises and corresponding FOPL. Goal: Prove that Marcus hated Caesar.

  1. Marcus was a Pompeian

    pompeian(Marcus)

  2. All Pompeians were Romans.

    Vx: pompeian(x) --> roman(x)

  3. All Romans were either loyal to Caesar or hated him.

    Vx: roman(x) --> loyalto(x, Caesar) v hate(x, Caesar)

  4. Everyone is loyal to someone.

    Vx Ey: loyalto(x, y)

  5. People only try to assassinate rulers they aren't loyal to.

    Vx Vy: ~loyalto(x, y) --> trytoassassinate(x, y)

  6. Marcus tried to assassinate Caesar.

    trytoassassinate(Marcus, Caesar)

How can I implement each of the premises one by one using SWI Prolog? I tried to write prolog and code and attached my implementation below, I am having difficulty implementing rule number 4.

roman(X):-
    pompeians(X).

hates(X, caesar):-
    roman(X), not_loyal(X, caesar).

not_loyal(X,Y):-
    not(loyal(X,Y)).

% How to implement "Everyone is loyal to someone."?

not_loyal(X,Y):-
    assassinate(X,Y).

assassinate(marcus, caesar).

pompeians(marcus).
1 Answers

How about:

% 1
pompeian(marcus).

% 2
roman(P) :-
    pompeian(P).

% 3
roman_loyalty_ceasar_feeling(caesar, loyal).
roman_loyalty_ceasar_feeling(notcaesar, hatred).

roman_loyalty_feeling(P, LoyalTo, CaesarFeel) :-
    roman(P),
    roman_loyalty_ceasar_feeling(LoyalTo, CaesarFeel). % 3

person(P, LoyalTo, CaesarFeel) :-
    roman_loyalty_feeling(P, LoyalTo, CaesarFeel), % 4
    \+ try_assassinate(P, LoyalTo). % 5

% 6
try_assassinate(marcus, caesar).

Result in swi-prolog:

?- person(Person, LoyalTo, CaesarFeel).
Person = marcus,
LoyalTo = notcaesar,
CaesarFeel = hatred.
Related