作业,想用Prolog解决

问题描述 投票:0回答:1

我可以用 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 D lies?

我最近的尝试是

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

这可以编译,但是当我用

查询Prolog时
f(A, B, C, D, E).

我收到错误“参数未充分实例化”。

prolog swi-prolog
1个回答
0
投票

可以建模:

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) :-
    consistent(A, B).
stat_if(lies, A, B) :-
    \+ consistent(A, B).

consistent(truth, truth).
consistent(lies, lies).

然后运行例如:

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

认为这就是你想要的逻辑......

© www.soinside.com 2019 - 2024. All rights reserved.