augu
augu

Reputation: 3

How to specify an unique fact in prolog?

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).

Upvotes: 0

Views: 408

Answers (2)

4KCheshireCat
4KCheshireCat

Reputation: 51

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?

Upvotes: 0

titusfx
titusfx

Reputation: 2026

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).

Upvotes: 1

Related Questions