0% found this document useful (0 votes)
4 views

Lecture_24_Foundations_of_Logic_Programming_Part_II

programming the program

Uploaded by

DA BEST
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture_24_Foundations_of_Logic_Programming_Part_II

programming the program

Uploaded by

DA BEST
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Further Programming Paradigms

Lecture 24: Foundations of Logic Programming - Part II, Logical Variables


and Unification

Lecturer: Alireza Tamaddoni-Nezhad


Original Slides by Prof. Paul Krause, University of Surrey

30
That’s all very nice, but how can we answer a query
using a logic program?

32
Logic Programming’s Computational Mechanism

• We will need to understand some


more foundational concepts:
• Unification;
• Logical Variables
• Substitution
• Most General Unifier
• Backtracking

• For full details, see Programming


Languages, Sections 12.3 & 12.4

33
Logical Variables

• Logical variables have some significant differences to variables in an


imperative language
• They can assume values from a predetermined set. However, their values are
not modifiable in the sense of imperative languages:
• A logic variable can be bound only once. That binding cannot be destroyed,
but it can be modified.
• A binding of X to a constant a cannot be replaced with a later binding to a
different constant b.
• But a logic variable can be partially defined (or undefined), to be completely
specified later
E.g. if X is bound to the term f(Y, Z), successive bindings of Y and Z will modify
the value of X:
• X = f(Y, Z) -> f(a, Z) -> f(a, g(W)) -> f(a, g(b))

34
Substitution

Definition:
A substitution is a function from variables to terms (such that the number of
variables which are not mapped to themselves is finite). A substitution θ is
denoted by
θ = {X1/t1, …, Xn/tn}

where X1, …, Xn are different variables, t1, …, tn are different terms and we
assume is ti different to Xi, for i = 1, …, n

• A pair Xi/ti is said to be a binding

35
Substitution vs. Assignment

The basic computational mechanism for the logic paradigm is evaluation of


equations of the form s = t, where s and t are terms (containing variables) and
“=” is a predicate symbol interpreted as syntactic equality over the set of all
ground terms

So, in a logic program, the equation X=a (a is constant) has as solution the
variable substitution {X/a}
• This substitution is also a solution to the equation a = X

However, assuming a binary function symbol +, consider the equation:


3 = 2 +1
• Given that this equation contains no variables, can it be solved as a logic
program?

36
Substitution vs. Assignment

The basic computational mechanism for the logic paradigm is evaluation of


equations of the form s = t, where s and t are terms (containing variables) and
“=” is a predicate symbol interpreted as syntactic equality over the set of all
ground terms

So, in a logic program, the equation X=a (a is constant) has as solution the
variable substitution {X/a}
• This substitution is also a solution to the equation a = X

However, assuming a binary function symbol +, consider the equation:


3=2+1
• Given that this equation contains no variables, can it be solved as a logic
program? Ans: No! The l.h.s and r.h.s differ syntactically

37
Unification

• From a logic programming perspective, the following equation is solvable:

f(X) = f(g(Y))
• It is solvable in the sense that application of the substitution θ = {X/g(Y)} to
both sides of the equation makes them syntactically equal
f(X)θ -> f(g(Y))

f(g(Y))θ -> f(g(Y))

Is the equation f(X) = f(g(X)) solvable?

38
Most General Unifier

• We say that θ = {X/g(Y)} is a unifier of the equation f(X) = f(g(Y))

• Note that θ` = {X/g(a), Y/a} is also a unifier of f(X) = f(g(Y))

• But we can change θ into θ` by applying the substitution {Y/a} to θ

• In that sense, θ is more general than θ`

• In solving a logic programming goal, we wish to find a most general unifier:

• This is a unifier for which there is no other unifier more general than it

39
The Computational Model

1. The only possible values, at least in the pure model, are terms over a given
signature.

2. Programs can have a declarative reading which is entirely logical, or a


procedural reading of an operational kind.

3. Computation works by instantiating the variables appearing in terms (and


therefore in goals) to other terms using the unification mechanism.

4. Control, which is entirely handled by the abstract machine (except for some
possible annotations in PROLOG) is based on the process of automatic
backtracking.

40

You might also like