3. Rules

A fact is always true. A rule is true when some other things are true. Rules let Prolog find answers that no fact says directly.

Your first rule

In chapter 2, you used a comma to look two levels down. This query finds the packages two levels below my_app.

?- depends(my_app, X), depends(X, Y).

A rule gives this question a name.

depends2(A, C) :- depends(A, B), depends(B, C).

Read :- as "if". The rule says "A depends on C at two levels, if A depends on B and B depends on C."

  • The part before :- is the head.
  • The part after :- is the body. A comma in the body means "and", as in a query. Each part between the commas, such as depends(A, B), is called a goal. A query is made of goals too.

Facts and rules are both clauses. A fact is a rule with no body.

The rule works in both directions too. The second query asks who depends on rollup at two levels, and the answer is my_app.

Variables belong to one clause

The A, B, and C in the rule have nothing to do with the X and Who in the queries. A variable means the same value only inside one clause or one query. When Prolog uses the rule, it gives the variables of the rule new names, so that they don't mix with other variables.

Several clauses mean "or"

Several clauses of the same predicate mean "or". Let's say a package is a build tool if it is vite, or if it is esbuild, or if it is rollup. That is three facts of build_tool/1.

uses_build_tool/1 gives my_app and vite, and then vite once more, because vite depends on two build tools. Prolog gives one answer for each way to prove the query. Chapter 10 shows how to remove duplicates.

You can also write "or" inside a body with a semicolon, ;. These two clauses

frontend(react).
frontend(vue).

mean the same as this one clause.

frontend(X) :- ( X = react ; X = vue ).

Here X = react means "X is react". It is not an assignment as in JavaScript. Chapter 5 explains = in detail. Several clauses are easier to read, so use ; only for short cases.

Rules about rules

The body of a rule can use other rules. Here dev_only/1 uses build_tool/1, and direct_build_tool/2 uses both depends/2 and build_tool/1.

How Prolog proves a query

When you ask ?- direct_build_tool(my_app, Tool)., Prolog does this.

  1. It finds a clause whose head matches the query. The head direct_build_tool(App, Tool) matches, with App = my_app.
  2. The query is replaced by the body, depends(my_app, Tool), dev_only(Tool).
  3. It proves depends(my_app, Tool) with the first fact, Tool = react.
  4. It tries to prove dev_only(react), which needs build_tool(react). No fact matches, so this fails.
  5. It goes back to step 3 and tries the next fact, Tool = vite. Now dev_only(vite) is true, so the answer is Tool = vite.

Prolog always works from left to right in a body, and from top to bottom in the database. When something fails, it goes back to the last place where it had another choice.

Try it

  1. Add a rule depends3/2 for three levels, using depends2/2.
  2. Add a rule shares_dependency(A, B) that is true when A and B depend on the same package. Ask ?- shares_dependency(X, Y). and see that X and Y can be the same package. Chapter 9 shows how to say "different".
An unhandled error has occurred. Reload 🗙