Prolog适合这种项目吗?

时间:2017-02-27 00:49:51

标签: prolog logic

我正在开发一个项目,我必须构建一个能够生成大量前提的引擎,并且根据真实情况,告诉我还能推断出什么。给出的例子:

If John plays football then Mary makes cookies.
If Eric listens to rock then john doesn't play Football.
If Eric doesn't listen to rock then john plays Football.
Either Eric listens to rock or Luiza listens to rock.

当给定的真实事实是“Luiza听摇滚”时,解决方案应该是:

Eric listens to rock = false
John plays football = true
Mary makes cookies = true

当真实的事实是“Eric听摇滚”时,解决方案应该是:

Luiza listens to rock = false
Jonh plays football = false
Mary makes cookies = (can't solve)

当真实的事实是“约翰踢足球”时,解决方案应该是:

Mary makes cookies = true
Luiza listens to rock = (can't solve)
Jonh plays football = (can't solve)

问题:Prolog是解决此类问题的正确工具吗?代码示例表示赞赏。

1 个答案:

答案 0 :(得分:1)

是的,Prolog对此非常棒。

以下是类似问题的示例:

/*
1. Mr. K made his sighting at some point earlier in the week than the one who saw the balloon, but at some point later in the week, than the one who spotted the Kite ( who isn't Ms. G ).
2. Friday's sighting was made by either Ms. Barn or the one who saw a plane ( or both ).
3. Mr. Nik did not make his sighting on Tuesday.
4. Mr. K isn't the one whose object turned out to be a telephone pole.
*/

?-
% Set up a list of lists to be the final solution
        Days = [[tuesday,_,_],[wednesday,_,_],[thursday,_,_],[friday,_,_]],
/* 1 */ before([_,mr_k,_],[_,_,balloon],Days),
/* 1 */ before([_,_,kite],[_,mr_k,_],Days),
/* 2 */ (member([friday,ms_barn,_],Days);
            member([friday,_,plane],Days);
            member([friday,ms_barn,plane],Days)),
% Fill in the rest of the people
        members([[_,mr_k,_],[_,ms_barn,_],[_,ms_g,_],[_,mr_nik,_]],Days),
% Fill in the rest of the objects
        members([[_,_,balloon],[_,_,kite],[_,_,plane],[_,_,tele_pole]],Days),
% Negations should be done after the solution is populated
/* 1 */ member([_,NOT_ms_g,kite],Days), NOT_ms_g \= ms_g,
/* 3 */ member([tuesday,NOT_mr_nik,_],Days), NOT_mr_nik \= mr_nik,
/* 4 */ member([_,NOT_mr_k,tele_pole],Days), NOT_mr_k \= mr_k,
    write(Days),
    nl,
    fail.

% Checks that `X` comes before `Y`
% in the list `Ds`
before(X,Y,Ds) :-
    remainder(X,Ds,Rs),
    member(Y,Rs).

% Finds a member of a list and
% unifies the third parameter such
% that it is the remaining elements in
% the list after the found member
remainder(X,[X|Ds],Ds).
remainder(X,[_|Ds],Rs) :- remainder(X,Ds,Rs).

% An extended version of `member` that
% checks if the members of the first list
% are all members of the second
members([],_).
members([X|Xs],Ds) :-
    member(X,Ds),
    members(Xs,Ds).

它产生以下结果:

[[tuesday, ms_g, tele_pole], [wednesday, mr_nik, kite], [thursday, mr_k, plane], [friday, ms_barn, balloon]]
相关问题