Akiyuki Kato
Akiyuki Kato

Reputation: 1

How to code this on Prolog?

Please can you explain what will i do to code this thing up on Prolog?

Mason, Alex, Steve, and Simon arc standing in a police lineup. One of them is blond, handsome, and unscarred. Two of them who are not blond are standing on either side of Mason. Alex is the only one standing next to exactly one handsome man. Steve is the only one not standing next to exactly one scarred man. Who is blond, handsome, and not scared?

i have here,

p --> standing(x,y)

twoOfThem(not blond, standing either side of Mason)

standing(mason,[x,y]):-
         blond([x,y]) == false.

Alex only one standing next to exactly one handsome

standing(alex,x):-
         handsome(x).

Steve is only not standing next to unscarred.

standing(steve,x):- 
         unscared(x).

Upvotes: 0

Views: 188

Answers (1)

CapelliC
CapelliC

Reputation: 60034

without using CLP(FD), you should used combinatorial ability of Prolog expressing the problem and the constraints in appropriate way. For instance

puzzle(Name) :-

    L = [[mason, Pos1, Blond1, Handsome1, UnScared1],
         [alex,  Pos2, Blond2, Handsome2, UnScared2],
         [steve, Pos3, Blond3, Handsome3, UnScared3],
         [simon, Pos4, Blond4, Handsome4, UnScared4]
        ],

   permutation([1,2,3,4], [Pos1,Pos2,Pos3,Pos4]),
   maplist(yn,
       [Blond1, Handsome1, UnScared1,
        Blond2, Handsome2, UnScared2,
        Blond3, Handsome3, UnScared3,
        Blond4, Handsome4, UnScared4
       ]),
...

Each variable (those symbols starting Uppercase!) is an attribute of a person, and can assume a value from the domain. yn/1 it's a service fact, allows those binary values to assume either yes or no:

yn(y).
yn(n).

The constraints can then be expressed in this way (just the first here)

...
   % Two of them who are not blond are standing on either side of Mason.
   member([mason, I1, _,_,_], L),
   member([_,     I2, n,_,_], L),
   member([_,     I3, n,_,_], L),

   (I2>I1, I3>I1 ; I2<I1, I3<I1),
...

and the solution will be

   % One of them is blond, handsome, and unscarred.
   member([Name,  _, y, y, y], L).

I'm not sure I understand every constraint (in English), indeed my program doesn't find a solution.

The program is rather slow, and calls for CLP(FD). Edit your question (add appropriate tag, for instance), if you are interested in a CLP(FD) solution.

Upvotes: 1

Related Questions