Logic programming is a paradigm where one expresses programs as a set of facts, and a set of rules.
When given a query, Prolog tries to find a chain of connections between the query and the specified facts and rules that lead to an answer.
Every prolog program is comprised of facts about the world, and a bunch of rules that can be used to discover new facts.
% Facts:
outgoing(ren). % **attribute** about some **thing**
silly(ren).
paernt(alice, bob). % **relationship** between two or more **atoms**
age(ren, 8).
parent(bob, carol).
% ^ they must be in lower case
% facts can be nested
own(carey, car(2021, blue, subaru, forrester))
% Rules:
comedian(P):-silly(P), outgoing(P). % comma is an **AND**
grandparent(X,Y):-parent(X,Q), parent(Q,Y).
old_comedian(C):-comedian(C),
age(C,A),A>60.
% variables always need to be captialized but they don't have to be in single word
% recursive rules:
ancestor(X,Z):-parent(X,Z).
ancestor(X,Z):-parent(X,Y),ancestor(Y,Z).
% ^ when multiple rules are written, then there's an OR relation
% negation
serious(X):-not(silly(X))
% queries/resolution
?- parent(alice, Who)
Who = bob
each rule has two parts:
Variables has to have first letter capitalized, and atoms must be in lower case
Prolog operates according to the Closed World Assumption, which states that only those things that can be proven to be true by the program’s facts/rules are true. i.e. if something doesn’t exist, it would be evaluated to false.
parent(nel, ted).
parent(ann, bob).
parent(ann, bri).
parent(bri,cas).
gparent(X,Y) :-
parent(X,Q), parent(Q,Y)
Whenever there’s a query, prolog will resolve it by
This whole process could be thought of as a “backtrack” processing. Each time it tries a mapping, continue to resolve, and move to next possible mapping
Prolog repeatedly compares the current goal with each fact/rule to see if they are a match.