4. Recursion
depends2/2 in chapter 3 looks two levels down, and depends3/2 looks three levels down. But a real dependency tree can be as deep as it likes. We need one rule for "any number of levels". In Prolog, that rule is recursive. It uses itself.
A rule that uses itself
my_app needs a package P if my_app depends on P directly, or if my_app depends on another package that needs P.
needs(A, B) :- depends(A, B).
needs(A, C) :- depends(A, B), needs(B, C).- The first clause is the base case. It stops the recursion.
- The second clause takes one step down the tree, then asks
needs/2again for the rest.
Press Next answer until you see false. Prolog finds all six packages, including fsevents, which is three levels down.
Ask in both directions
As with facts, the same rule answers the opposite question. Which packages need rollup?
In JavaScript, "what does my_app need" and "who needs rollup" would be two different functions, each with its own loop and its own way to walk the tree. In Prolog, it is one rule of two lines.
How the recursion runs
Take ?- needs(my_app, fsevents).
- The first clause tries
depends(my_app, fsevents). There is no such fact. - The second clause tries
depends(my_app, B). The first fact givesB = react. Then it asksneeds(react, fsevents), which fails after trying both clauses. - Prolog goes back and tries
B = vite. It asksneeds(vite, fsevents). - Inside, the second clause finds
depends(vite, rollup), and asksneeds(rollup, fsevents). - The first clause proves
depends(rollup, fsevents). The answer istrue.
Each step goes one level down the tree. The base case ends the search when a direct dependency is found.
The order matters
The logical meaning of a rule doesn't depend on the order of the goals in its body. But how it runs does. Look at this version of the second clause.
needs(A, C) :- needs(A, B), depends(B, C).It looks just as true. But to prove needs(my_app, X), Prolog first proves needs(my_app, B), which uses the same clause, which first proves needs(my_app, B2), and so on. It never reaches depends/2, and the query never ends. This is called left recursion.
This box is not runnable, because the query never ends. To see it for yourself, copy the two needs/2 clauses into the first box of this page in place of the ones there, and run it. Press Next answer again and again. After the last real answer, the query runs until you press Stop.
depends(my_app, react).
depends(react, scheduler).
needs(A, B) :- depends(A, B).
needs(A, C) :- needs(A, B), depends(B, C).
?- needs(my_app, P).The rule of thumb is to put a goal that makes progress, such as depends(A, B), before the recursive call.
A cycle in the data has the same effect. If a depends on b and b depends on a, then needs(a, X) goes around the cycle forever. Real package managers have to deal with this. We don't need to for this tutorial.
Another example, a family tree
Recursion is the same in any domain. This is the classic example of Prolog.
Try it
- In the box below, add
depends(esbuild, esbuild_wasm).and check thatneeds(my_app, P)finds it. - Write
needed_by(B, A), which is true whenAneedsB. You can write it with one clause that usesneeds/2.