10-05-2012, 01:33 PM
Prolog A Tutorial Introduction
PrologIntro.pdf (Size: 235.33 KB / Downloads: 24)
Introduction
Logic programming is a programming paradigm based on mathematical logic. In this paradigm the programmer
specifies relationships among data values (this constitutes a logic program) and then poses queries to
the execution environment (usually an interactive interpreter) in order to see whether certain relationships
hold. Putting this in another way, a logic program, through explicit facts and rules, defines a base of knowledge
from which implicit knowledge can be extracted. This style of programming is popular for data base
interfaces, expert systems, and mathematical theorem provers. In this tutorial you will be introduced to
Prolog, the primary logic programming language, through the interactive SWI-Prolog system (interpreter).
You will notice that Prolog has some similarities to a functional programming language such as Hugs. A
functional program consists of a sequence of function definitions — a logic program consists of a sequence
of relation definitions. Both rely heavily on recursive definitions. The big difference is in the underlying
execution “engine” — i.e., the imperative parts of the languages. The execution engine of a functional
language evaluates an expression by converting it to an acyclic graph and then reducing the graph to a
normal form which represents the computed value. The Prolog execution environment, on the other hand,
doesn’t so much “compute” an answer, it “deduces” an answer from the relation definitions at hand.
EasingintoProlog
In this section you will be introduced to the basic features of the logic programming language Prolog. You will
see how to define a knowledge base in terms of facts (unconditionally true) and rules (truth is conditional),
how to load the knowledge base into the SWI-Prolog system, and how to query the system.
Logic Programming
Remember that all programming languages have both declarative (definitional) and imperative (computational)
components. Prolog is referred to as a declarative language because all program statements are
definitional. In particular, a Prolog program consists of facts and rules which serve to define relations (in
the mathematical sense) on sets of values. The imperative component of Prolog is its execution engine based
on unification and resolution, a mechanism for recursively extracting sets of data values implicit in the facts
and rules of a program. In this section you will be briefly introduced to each of these terms.
Facts and Rules
Everything in Prolog is defined in terms of two constructs: the fact and the rule. A fact is a Prolog statement
consisting simply of an identifier followed by an n-tuple of constants. The identifier is interpreted as the
name of a (mathematical) relation and the fact states that the specified n-tuple is in the relation. In Prolog
a relation identifier is referred to as a predicate; when a tuple of values is in a relation we say the tuple
satisfies the predicate.
As a simple example, consider the following Prolog program which defines a directed graph on five nodes
– this first example involves only facts.