找到你要的答案

Q:How to specify an unique fact in prolog?

Q:如何实现指定一个独特的事实吗?

I want to specify a comment of a person about a thing if is positive or negative but can't be both.

I want to put into my file a general rule and these facts:

:-comment(Person, Thing, B), comment(Person, Thing, OtherB), B \=OtherB.
comment(person, book, positive).
comment(person, book, negative).

And when I try to make a Query I will receive an error, or something telling me something is contradictory.

Of course are valid the follows facts:

comment(person, book, positive).
comment(person, icecream, negativa).

我想指定一个人对一件事的评论,如果是正面或负面的,但不能两者。

我想把我的文件概括为一条普遍的规则和这些事实:

:-comment(Person, Thing, B), comment(Person, Thing, OtherB), B \=OtherB.
comment(person, book, positive).
comment(person, book, negative).

当我试图做一个查询,我会收到一个错误,或告诉我一些东西是矛盾的。

当然是有效的如下事实:

comment(person, book, positive).
comment(person, icecream, negativa).
answer1: 回答1:

You should add to your file a checkContradictory (at least in gnuprolog) in the following way:

yourfile.lp

comment(person, book, positive).
comment(person, book, negative).

checkContradictory:- checkCommentContradiction, ... others checking predicates

checkCommentContradiction:-comment(Person, Thing, positive),
            comment(Person, Thing, negative),throw(contradiction_with_comments).

So if you want to check your file before the query, just execute your checkContradictory or if you have an main predicate, just add checkContradictory like requirement.

Important if you need to have a yes free of error and an Exception when there are a contradiction you need to add an findall:

yourfile.lp

comment(person, book, positive).
comment(person, book, negative).

checkFreeOfContradictory:- checkAllCommentsContradictions.

checkAllCommentsContradictions:-findall(X,checkCommentContradiction,R).

checkCommentContradiction:-comment(Person, Thing, B1),
            comment(Person, Thing, B2),
            (B1==B2->true;throw(contradiction_with_comments)).

Mainly because the same fact will unify with comment(Person, thing, B1) and comment(Person, Thing, B2).

你应该添加到您的文件checkcontradictory(至少在gnuprolog)以下面的方式:

yourfile.lp

comment(person, book, positive).
comment(person, book, negative).

checkContradictory:- checkCommentContradiction, ... others checking predicates

checkCommentContradiction:-comment(Person, Thing, positive),
            comment(Person, Thing, negative),throw(contradiction_with_comments).

所以,如果你想查询之前,检查你的文件,就执行你的checkcontradictory或如果你有一个主要的谓语,只加checkcontradictory需求。

重要的是如果你需要有一个是免费的错误和异常时有矛盾,你需要添加一个所有:

yourfile.lp

comment(person, book, positive).
comment(person, book, negative).

checkFreeOfContradictory:- checkAllCommentsContradictions.

checkAllCommentsContradictions:-findall(X,checkCommentContradiction,R).

checkCommentContradiction:-comment(Person, Thing, B1),
            comment(Person, Thing, B2),
            (B1==B2->true;throw(contradiction_with_comments)).

主要是因为同一事实将统一意见(人、事、B1)和评论(人、事、B2)。

answer2: 回答2:

will it be easier if you restructure your predicates? replacing one predicate by two in such a way:

positive_comment(Person,Book).
negative_comment(Person,Book).

then use some like

positive_comment(Person,Book):-
negative_comment(Person,Book),
throw_some_error,
false.
negative_comment(Person,Book):-
positive_comment(Person,Book),
throw_some_error,
false.

or better use separate checker:

check_comments_consistency(Person,Book):-
  positive_comment(Person,Book),
  negative_comment(Person,Book),
  throw_some_error.

... or something like that.

you get the idea?

will it be easier if you restructure your predicates? replacing one predicate by two in such a way:

positive_comment(Person,Book).
negative_comment(Person,Book).

然后用一些像

positive_comment(Person,Book):-
negative_comment(Person,Book),
throw_some_error,
false.
negative_comment(Person,Book):-
positive_comment(Person,Book),
throw_some_error,
false.

或更好地使用单独的检查:

check_comments_consistency(Person,Book):-
  positive_comment(Person,Book),
  negative_comment(Person,Book),
  throw_some_error.

…或类似的东西。

你明白了吗?

prolog  unique