n00b
n00b

Reputation: 911

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.

Upvotes: 3

Views: 1120

Answers (4)

Nick Barnes
Nick Barnes

Reputation: 21346

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?".

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363627

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, i.e. (bar(X), baz(X)) or what you type at the Prolog interpreter prompt to get the program running.

Upvotes: 2

Daniel Lyons
Daniel Lyons

Reputation: 22803

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.

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49813

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.

Upvotes: 0

Related Questions