2 ms·
To add to triska's excellent explanation, the thing to understand about the syntax of logic programming languages is that it's not something arbitrary that some
by YeGoblynQueenne 8y ago
To add to triska's excellent explanation, the thing to understand about the
syntax of logic programming languages is that it's not something arbitrary
that someone thought up, like the syntax of most other programming languages.
Their syntax is in fact that of First Order Logic.
In Prolog and similar languages, everything you see in a source file is a Horn
clause, a clause (a disjunction of literals) with at most one positive
literal. The following are Horn clauses:
¬p ∨ ¬q ∨ ¬t ∨ u
¬p ∨ ¬q ∨ ¬t
u
Horn clauses are typically written as implications, with the positive literal
at the start of the clause, followed by the left-facing implication arrow:
u ← p ∧ q ∧ t
← p ∧ q ∧ t
u ←
In Prolog, Mercury etc languages the left-arrow is rendered as ":-" and the
comma, ",", is used as the conjunction operator. The full-stop is used to
terminate a clause:
u :- p, q, t. (1)
:- p, q, t. (2)
u. (3)
Horn clauses with exactly one positive literal, like (1) above, are called
definite clauses, or "rules"; clauses with no positive literals (2) are called
goal clauses, or directives; and clauses with no negative literals (3) are
called unit clauses, or facts. The literal before the :- is called the head
literal and the rest are the body literals.
The structure of a clause denotes its semantics. To simplify (and avoid a
lengthy exposition of Prolog's refutation theorem proving) a rule can be
proved true or false, a fact is always true and a goal is executed when it's
encountered. The queries entered at the REPL to execute a program are also
goals (that match a rule's head, and are proved by refutation).
This is all part of the appeal of Prolog, Mercury, etc: that everything is the
same kind of object (a Horn clause). It's like having the most general LEGO®
brick possible, general enough to build anything that can be built.
In Erlang, the syntax remains, but the FOL semantics are gone, and perhaps
that's what drives you to think that some of the syntax above is unnecessary
or odd. In truth, the syntax is the semantics of the program (which is,
itself, a proof). You could, of course, change the syntax to make the language
look more familiar- but then you'd lose the benefit of having a programming
language that has the syntax and semantics of FOL, which would be a shame.