CSCI 330: Prolog Facts, queries, and basic syntax

Prolog approaches the problem of computation from an entirely different perspective than traditional programming languages.

In prolog, we attempt to create a set of facts and rules that describe how the "universe of interest" behaves, and then leave it up to the prolog executable to use those facts and rules to answer any questions we may have.

For instance, we may come up with a fact like:
1 is a positive integer
and then a rule like:
if X is an integer and Y equals X+1 then Y is an integer
We could then issue a query like
is 17 an integer? and the prolog engine would try to use the facts and rules to work out a yes/no answer.

Solving a problem then becomes a matter of describing our universe (or at least the relevant portion) succinctly, and phrasing our question appropriately.

  1. Facts and rules are stored in your prolog files, anything entered while running prolog is treated as a query.
  2. Facts, rules, and queries always end with a . and the prolog engine will wait for you to complete your query before it attempts to find an answer.
  3. Queries are answered true or false, and depending on the query the engine may also supply the set of variable values it used to come up with a true answer. If prolog cannot find a way to combine the facts and rules to establish a true response then it will respond with false.

    Sometimes there are multiple ways to satisfy a query. In these cases prolog will say yes and display the first solution found. If you press ENTER it will complete the query, if you press the semi-colon key ( ; ) it will give the next solution. Continue pressing ; until you have seen all the solutions you wish to see.