How to check if a rule exists in a proog file database

I am working on a college assignment where I have to check if a specific offer exists in the current offer database (as a fact or, as a rule).

The idea is to use a rule whose chapter checks (+ name, + arguments). This rule must be true if there is another rule in the database whose head is the name (arguments)

Any help would be greatly appreciated ...

+4
source share
2 answers

Using call/1 not a good idea, because call/1 actually names the goal, but you just want to find out if a fact / rule exists, and you don’t want to wait after a long calculation that the call may cause an error, and you don’t want to, so that something is printed on the screen, if the called rule, in turn, calls, for example, writeln/1 . In addition, you would like verify/2 succeed, even if the call failed (but this is actually a fact / rule).

As a solution, SWI-Prolog offers callable/1

 callable(+Term) True if Term is bound to an atom or a compound term, so it can be handed without type-error to call/1, functor/3 and =../2. 

Here are two versions of verify/2 , one of which is call/1 , and the other is callable/1 .

 verify1(Name, Arguments) :- Term =.. [Name | Arguments], call(Term). verify2(Name, Arguments) :- Term =.. [Name | Arguments], callable(Term). father(abraham, isaac) :- writeln('hello'). father(abraham, adam) :- fail. 
+5
source

Are you familiar with the concept of unification? What you need to do: just call a predicate that looks like the one you are trying to find.

So let's say in your database:

 father(abraham,isaac). 

Now you want to call something like:

 verify(father,[abraham,isaac]). 

Then your predicate will contain the calling mechanism of father(abraham,isaac). which should then return true. The call to father(abraham,adam) should fail.

For this you need two predicates: =../2 and call/2 . If you use SWI-Prolog, call help(=..). and help(call) from the command line of the interpreter to access the documentation.

I hope I did not spoil your assignment. You still need to figure out what to do with partially created predicates (so say something like verify(father,[abraham,X]). Yourself, but it shouldn't be hard here.

Good luck.

+1
source

All Articles