Search code examples
prolog

Homework, want to solve with Prolog


Can I solve the homework for my logic class with Prolog?

5 people are interrogated. The statements from A and B are lost.
C says: If A is truthful then B is truthful.
D says: If C is truthful then A is truthful.
E says: If D is truthful then A is truthful.

1) Is it possible that C lies?
2) Is it possible that D lies? 
3) Is it possible that E lies?

My latest attempt was

f(A, B, C, D, E):-
    (C->(A->B)),
    (D->(C->A)),
    (E->(A->D)).

This compiles, but when I query Prolog with

f(A, B, C, D, E).

I get the error "Arguments are not sufficiently instantiated".


Solution

  • Can model with:

    stat(truth).
    stat(lies).
    
    go(A, B, C, D, E) :-
        maplist(stat, [A, B, C, D, E]),
        stat_if(C, A, B),
        stat_if(D, C, A),
        stat_if(E, D, A).
    
    stat_if(truth, A, B) :-
        implies(A, B).
    stat_if(lies, A, B) :-
        \+ implies(A, B).
    
    implies(truth, truth).
    % If lying, cannot determine
    implies(lies, _).
    

    Then run with e.g.:

    ?- go(A, B, C, D, truth).
    A = B, B = C, C = D, D = truth ;
    A = D, D = truth,
    B = C, C = lies ;
    A = D, D = lies,
    B = C, C = truth ;
    A = B, B = D, D = lies,
    C = truth ;
    false.
    
    ?- go(A, B, C, D, lies).
    false.