简体   繁体   中英

What's the difference between Prolog rules and queries?

I'm new to logical programming and find it difficult to understand the difference between rules and queries, I feel they are basically the same. Any help to clarify this would be greatly appreciated.

Syntactically, they are largely the same; " p(1). " could be either a rule or a query, depending on where you put it.

Semantically, they are not.
" p(1). " as a rule tells Prolog " p(1) is true".
" p(1). " as a query asks Prolog "is p(1) true?".

A rule is a definition such as

foo(X) :- bar(X), baz(X).

as it appears in a Prolog program.

A query is either the right hand side of a definition like the above, ie (bar(X), baz(X)) or what you type at the Prolog interpreter prompt to get the program running.

A query is a statement you are asking to have proven (which in the process of doing so may instantiate variables, which can server as your "output"); rules make up the "program" used to develop that proof.

Your intuition is correct: they're both variations on a Horn clause. The basic structure of a Horn clause is:

head(...) :- body.

If you have a head without a body, you have a fact. If you have both, you have a predicate. If you have just a body, then you have a query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM