Prolog - are the rules correct, but not outputting as they expected?

Clue

Four guests (Colonel Mustard, Professor Slum, Miss Scarlett, Ms. Green) attend a dinner party at Mr. Boddy's house. Suddenly the lights went out! When they return, Mr. Boddy lies dead in the middle of the table. All are suspects. Further consideration reveals the following facts:

  • Mr. Boddy dealt with Ms. Green.
  • Professor Slum married Mrs. Green.
  • Mr. Boddy was very rich.
  • Colonel Mustard plaster is very greedy.
  • Miss Scarlett was also associated with Mr. Boddy.

There are two possible motives for killing:

  • Hatred: someone hates someone else if this person deals with his spouse.
  • Greed: someone is ready to commit murder if they are greedy and not rich, and the victim is rich.

A: Prolog. : colMustard, profPlum, missScarlet, msGreen, mrBoddy. , ( ) , - ! married(X,Y) :- married(Y,X) % INFINITE LOOP

?-suspect(Killer,mrBoddy)
Killer = suspect_name_1
Killer = suspect_name_2
etc.

B: , /2, , , .

?-suspect(Killer,mrBoddy)
Killer = unique_suspect.

C: , . , / .

?-suspect(Killer,mrBoddy)
Killer = unique_suspect.

,

suspect(Killer,mrBoddy).

suspect(Killer,mrBoddy).
Killer = profPlum

Killer = colMustard.

.

%8) Clue

%facts

affair(mrBoddy,msGreen).
affair(missScarlett, mrBoddy).
affair(X,Y) :- affair(X,Y), affair(Y,X).

married(profPlum, msGreen).
married(X,Y) :- married(X,Y), married(Y,X).

rich(mrBoddy).
greedy(colMustard).

%rules

hate(X,Y) :- married(X,Spouse), affair(Y,Spouse).
greed(X,Y) :- greedy(X), not(rich(X)), rich(Y).


%suspect

suspect(X,Y):- hate(X,Y).
suspect(X,Y):- greed(X,Y).
+3
2

- affair/2 and married/2. (.. , ). , , X Y, Y X. suspect/2 .

, , Prolog. :

?- trace, suspect(Killer, mrBoddy).

.

+1

. : , Prolog; ; Prolog . - , . :

?- suspect(Killer,mrBoddy).
Killer = profPlum ;
ERROR: Out of local stack

. . , . , .

- , false. false, , . , (, , ). 1. , , :

?- suspect(Killer,mrBoddy), false.

married(profPlum, msGreen) :- false.
married(X,Y) :- married(X,Y), false, married(Y,X).

hate(X,Y) :- married(X,Spouse), false, affair(Y,Spouse).

suspect(X,Y):- hate(X,Y), false.
suspect(X,Y):- false, greed(X,Y).

, . ,

married(X,Y) :- married(X,Y), married(Y,X).

.

, . ? , :- . , , , , , . :

X Y Y X
, X Y.

, . , . , — . , married(profPlum, msGreen) , married(msGreen, profPlum) . , , .

, , husband_wife/2

married(M,F) :- husband_wife(M,F).
married(F,M) :- husband_wife(M,F).

, . Prolog , . . 2


:
1 . , not/1 (\+)/1.
2 @larsmans.

>

+3

Source: https://habr.com/ru/post/1543996/


All Articles