I have attempted to write an extremely compact summary of the most basic concepts in Prolog. I would be interested to hear if anybody has suggestions for improvement or if there are errors.
Cheers/JCR
The main types of terms are:
Atoms: A string of letters that starts with a lower case letter
Examples: p, q, causes
Numbers: The real numbers
Examples: -1, 1, 3.14
Variables: An arbitrary string of letters that starts with an upper case letter
Examples: X, Y, Extraversion
Compounds: An atom followed by a comma delimited list of terms in parentheses
Examples: p(A, B), person(name(N), age(A)) and line(point(X1, Y1), point(X2, Y2))
Constraints: A numerical expression enclosed in braces
A program consists of one or more clauses ; each clause ends with a period. Clauses come in two forms: Facts and rules.
Facts are statements which are unconditionally true, and usually written as compounds with atoms or numbers as arguments
Examples: mother(marge, bart), age(bart, 10)
Rules are statements which are conditionally true, and have the form Head :- Body, meaning Head is true if Body is true. Head is usually a compound with variables as arguments; body is usually a set of compounds or constraints separated by commas, also with variables as arguments
The comma means conjunction; i.e. all the terms in Body have to be true in order for Head to be true
Examples: bachelor§:- not(married§), male§.
The semicolon means disjunction; i.e. at least one term in Body has to be true. Another way to state a disjunction is to write two or more clauses with the same Head
Examples: parent(P, C):- mother(P, C); father(P, C)., the same as parent(P, C):-mother(P, C). parent(P, C):-father(P, C).
The scope of any variable is limited to the clause. Within a clause variables with the same name have to be instantiated to the same object. Variables with different names can, but do not have to, hold different objects.
Proof search involves showing that a set of goals are true; true means that two terms unify .
Two atoms unify if they are the same
Examples: bart = bart and lisa = lisa.
Two numbers unify if they are the same
Examples: 1 = 1, 3.14 = 3.14
A variable unifies with any type of term and is instantiated to that term
Examples: X = bart, Y = 3.14, Z = parent(abe, homer)
Try to unify the query goal with a fact; if this succeeds respond true and, if the query contains any variables, return the objects that unify with each variable.
Try to unify the query goal with the head of a rule; if this succeeds, set out to prove everything in the body of the rule, with any variables in the head of the rule substituted for any terms in the original query. This might involve attempts to prove other rules.
This is only true if some constraint library is loaded that interprets{}/1 terms as constraints. Other constraint libraries use instead operators (usually starting with the # character) for expressing constraints. I would not include this in a “brief summary of Prolog” as it’s not a standard or universal feature.
Agree. I use the summary in my research paper which makes heavy use of clpr, but it should be stated that braces are interpreted as constraints only with this library included.
First off this looks really good. I know I might go nuts trying to technically explain Prolog in one page.
The below are just comments for feedback. You probably know these by now in trying to write the summary; do with them as you need including ignoring them. No need to respond.
Compounds can also have operators as the functor, e.g. +(1,2), or ./2 as with list.
Is § suppose to be there. I am guessing you used LaTeX and this is just a copy/paste problem.
at least one term in Body has to be true. That I disagree with unless you clarify it to note that all of the statements in the body are separated by ;. In other words when I read the statement I can think that this is valid
There is no actual distinction between a fact and a rule. A fact is simply a clause of a predicate with no body. Thus the steps of “proof” are incorrect. It’s simply lookup clauses, unify the call with the clause head, and if it succeeds then execute the steps of the clause.
For what it’s worth, as a longtime Prolog programmer I still don’t think about Proofs or search. I find that language confusing, especially the word proof, even though that may be common terminology. (In English, a proof is a sequence of inferences. If you listed out the steps taken to arrive at a unification, that would be a proof, but we normally don’t list them.)
Instead of Proof, I think about calling a predicate. It executes and finds a solution. If the predicate is non-deterministic, I can backtrack to find more solutions.
One notion you skipped is “fail”. It’s very important to Prolog that you a predicate may succeed repeatedly but then it eventually fails, and failure triggers the previous predicate to attempt to find a next solution.
Also, you wrote “true means that two terms unify”. This is false. If two terms unify, then A=B is true, but there is a lot more to Prolog than just unifying things. “5 is 3 + 2” is true, but it is not unifying two terms. length([a], A) succeeds with A=1 (and is true), but it is not about unifying two terms.
Actually, it is. The standard specification of is/2 is that the right operand is evaluated as an arithmetic expression and the resulting value is unified with the left operand.
As far as I understand it (I may be wrong), a program consists of predicates, which in turn may have clauses.
A clause is equivalent to a statement, whereas a predicate is akin to a function or procedure (ie a block of comma or semicolon separated clauses), in my understanding of the Prolog jargon.