liveBook · Manning

Prolog

As explained in Fabulous Adventures in Data Structures and Algorithms

Prolog is a declarative logic programming language in which programs consist of facts and rules and execution answers queries through logical resolution.

Prolog is a declarative logic programming language: programs define facts and rules, while queries ask for values that make those rules true. Unification fills holes in terms through substitution, and backtracking searches alternative solutions. Together, they turn logical relationships into executable queries. section 10.3

Shared interests become a query

Facts such as likes(Alice, scuba), likes(Bob, scuba), and likes(Carol, birdwatching) can support the rule “if likes(X, Y) and likes(Z, Y), then compatible(X, Z).” Querying compatible(Bob, Q) first unifies Bob’s interests with scuba, then backtracks to birdwatching. The resulting values for Q are Alice, Bob, and Carol, but not David. section 10.3

if likes(Bob, Y) and likes(Q, Y) then compatible(Bob, Q)

Use the rule form to inspect how a general compatibility relation becomes a concrete subproblem involving Bob and an unknown person. section 10.3

Rules may combine several relationships, such as requiring both a shared friend and a shared interest. The same unification machinery also solves first-order logic problems and can represent type-inference problems, linking logic programming with generic types and omitted type information. section 10.3

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains unification and backtracking through a worked compatibility example, while relating them to first-order logic.
Examples
Uses likes facts, a compatibility rule, and queries that return alternative substitutions for an unknown person.
What you can do after
Evaluate logic queries by tracing substitutions, solving subproblems, and backtracking across alternative matches.
How it connects
Connects Prolog with unification, backtracking search, first-order logic, and type inference.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.