ArticleslgStudy

science

Syntax and semantics of logic programming

Syntax and semantics of logic programming is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Syntax and semantics of logic programming rather than just read about it. In short: Logic programming is a programming paradigm that includes languages based on formal logic, including Datalog and Prolog. This article describes the syntax and semantics of the purely declarative subset of these languages.

Syntax and semantics of logic programming — main illustration
Syntax and semantics of logic programming — illustration

Key takeaways

  • Syntax and semantics of logic programming belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Syntax and semantics of logic programming to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Syntax and semantics of logic programming from memory before moving on to harder problems.

Reference excerpt

Logic programming is a programming paradigm that includes languages based on formal logic, including Datalog and Prolog. This article describes the syntax and semantics of the purely declarative subset of these languages. Confusingly, the name "logic programming" also refers to a specific programming language that roughly corresponds to the declarative subset of Prolog. Unfortunately, the term must be used in both senses in this article. Declarative logic programs consist entirely of rules of the form

Each such rule can be read as an implication:

B 1 ∧ … ∧ B n → H {\displaystyle B_{1}\land \ldots \land B_{n}\rightarrow H}

meaning "If each B i {\displaystyle B_{i}} is true, then H {\displaystyle H} is true". Logic programs compute the set of facts that are implied by their rules. Many implementations of Datalog, Prolog, and related languages add procedural features such as Prolog's cut operator or extra-logical features such as a foreign function interface. The formal semantics of such extensions are beyond the scope of this article.

Datalog

Datalog is the simplest widely-studied logic programming language. There are three major definitions of the semantics of Datalog, and they are all equivalent. The syntax and semantics of other logic programming languages are extensions and generalizations of those of Datalog.

Syntax A Datalog program consists of a list of rules (Horn clauses). If constant and variable are two countable sets of constants and variables respectively and relation is a countable set of predicate symbols, then the following BNF grammar expresses the structure of a Datalog program:

Atoms are also referred to as literals. The atom to the left of the :- symbol is called the head of the rule; the atoms to the right are the body. Every Datalog program must satisfy the condition that every variable that appears in the head of a rule also appears in the body (this condition is sometimes called the range restriction). Rules with empty bodies are called facts. For example, the following rule is a fact:

Syntactic sugar Many implementations of logic programming extend the above grammar to allow writing facts without the :-, like so:

Many also allow writing 0-ary relations without parentheses, like so:

These are merely abbreviations (syntactic sugar); they have no impact on the semantics of the program.

Example The following program computes the relation path, which is the transitive closure of the relation edge.

Semantics There are three widely-used approaches to the semantics of Datalog programs: model-theoretic, fixed-point, and proof-theoretic. These three approaches can be proven to be equivalent. An atom is called ground if none of its subterms are variables. Intuitively, each of the semantics define the meaning of a program to be the set of all ground atoms that can be deduced from the rules of the program, starting from the facts.

Model theoretic

A rule is called ground if all of its atoms (head and body) are ground. A ground rule R2 is a ground instance of another rule R1 if R2 is the result of a substitution of constants for all the variables in R1. The Herbrand base of a Datalog program is the set of all ground atoms that can be made with the constants appearing in the program. An interpretation (also known as a database instance) is a subset of the Herbrand base. A ground atom is true in an interpretation I if it is an element of I. A rule is true in an interpretation I if for each ground instance of that rule, if all the atoms in the body are true in I, then the head of the rule is also true in I. A Herbrand model of a Datalog program P is an interpretation I of P which contains all the ground facts of P, and makes all of the rules of P true in I. Model-theoretic semantics state that the meaning of a Datalog program is its minimal Herbrand model (equivalently, the intersection of all its Herbrand models). For example, this program:

has this Herbrand universe: x, y, z and this Herbrand base: edge(x, x), edge(x, y), ..., edge(z, z), path(x, x), ..., path(z, z) and this minimal Herbrand model: edge(x, y), edge(y, z), path(x, y), path(y, z), path(x, z)

Fixed-point Let I be the set of interpretations of a Datalog program P, that is, I = P(H), where H is the Herbrand base of P and P is the powerset operator. The immediate consequence operator for P is the following map T from I to I: For each ground instance of each rule in P, if every clause in the body is in the input interpretation, then add the head of the ground instance to the output interpretation. This map T is monotonic with respect to the partial order given by subset inclusion on T. By the Knaster–Tarski theorem, this map has a least fixed point; by the Kleene fixed-point theorem the fixed point is the supremum of the chain T ( ∅ ) , T ( T ( ∅ ) ) , … , T n ( ∅ ) , … {\displaystyle T(\emptyset ),T(T(\emptyset )),\ldots ,T^{n}(\emptyset ),\ldots } . The least fixed point of M coincides with the minimal Herbrand model of the program. The fixpoint semantics suggest an algorithm for computing the minimal Herbrand model: Start with the set of ground facts in the program, then repeatedly add consequences of the rules until a fixpoint is reached. This algorithm is called naïve evaluation.

Proof-theoretic

Given a program P, a proof tree of a ground atom A is a tree with a root labeled by A, leaves labeled by ground atoms from the heads of facts in P, and branches with children A 1 , … , A n {\displaystyle A_{1},\ldots ,A_{n}} labeled by ground atoms G such that there exists a ground instance

… excerpt ends here. Continue reading the full article.

Illustrations

Syntax and semantics of logic programming: Proof tree showing the derivation of the ground atom path(x, z) from the program

edge(x, y).
edge(y, z).
path(A, B) :- 
  edge(A, B).
path(A, C) :- 
  path(A, B), 
  edge(B, C).
Proof tree showing the derivation of the ground atom path(x, z) from the program edge(x, y). edge(y, z). path(A, B) :- edge(A, B). path(A, C) :- path(A, B), edge(B, C).

Worked examples

Example 1 — a first encounter with Syntax and semantics of logic programming

Start with the simplest possible case. Write down what Syntax and semantics of logic programming claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Syntax and semantics of logic programming before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Syntax and semantics of logic programming ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Syntax and semantics of logic programming

In research
Syntax and semantics of logic programming appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Syntax and semantics of logic programming in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Syntax and semantics of logic programming is common in secondary-school and first-year university syllabi. It links to neighbouring topics Logic programming, Programming language syntax, so understanding it makes those chapters shorter.
In everyday life
Look for Syntax and semantics of logic programming outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Syntax and semantics of logic programming in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Syntax and semantics of logic programming means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Syntax and semantics of logic programming out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Syntax and semantics of logic programming in simple terms?

Logic programming is a programming paradigm that includes languages based on formal logic, including Datalog and Prolog. This article describes the syntax and semantics of the purely declarative subset of these languages.

Why does Syntax and semantics of logic programming matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Syntax and semantics of logic programming?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Syntax and semantics of logic programming.

Tags

  • Logic programming
  • Programming language syntax

Keep exploring