0
我寫了下面的代碼:動態謂詞和大小寫
:-dynamic [subjectHaveDomain/2].
:-dynamic [objectHaveDomain/2].
element(X,[X|_]). % Is X part of the list
element(X,[_|R]) :-
element(X,R).
subset([],_). % Is [X|R] a subset of K
subset([X|R],K) :-
element(X,K),
subset(R,K).
subjectHaveDomain(S,[D1|DN]).
objectHaveDomain(O,[D1|DN]). % Subject and objects both have domains
canRead(S,O):- % Subject can read object if the domain of the object is a subset of the domain of the subject.
subjectHaveDomain(S,L1),
objectHaveDomain(O,L2),
subset(L2,L1).
我試圖通過聲明客體和主體來執行此,查找所有Subject
S和Object
s表示使canRead(Subject,Object)
真實的,但我沒有得到預期的結果。
1 ?- assert(subjectHavedomain(a,[s,p])).
true.
2 ?- assert(subjectHavedomain(b,[s,p,ts])).
true.
3 ?- assert(subjectHavedomain(c,[s])).
true.
4 ?- assert(objectHavedomain(o1,[s])).
true.
5 ?- assert(objectHavedomain(o2,[p,ts])).
true.
6 ?- assert(objectHavedomain(o3,[p])).
true.
7 ?- canRead(S,O).
true ;
true ;
true ;...
我期待:提前
S = a,
O = o1;
S = a,
O = o3;
S = b,
O = o1;
S = b,
O = o2;
S = b,
O = o3;
S = c,
O = o1;
感謝。
與 http://stackoverflow.com/questions/42354324/dynamic-predicate-in-prolog 問題是 subjectHaveDomain(S,[D1 | DN])。 objectHaveDomain(O,[D1 | DN])。 不要用變量聲明動態謂詞,你會沒事的。 –
[Prolog中的動態謂詞]的可能重複(http://stackoverflow.com/questions/42354324/dynamic-predicate-in-prolog) –
另外你的元素/ 2本質上就是http://www.swi-prolog。 org/pldoc/man?predicate = member/2和subset/2是http://www.swi-prolog.org/pldoc/man?predicate=subset/2,如果您想使用內置謂詞而不是必須創建自己的 –