Unification & Prolog: ICS/CSE 141: Programming Languages
Unification & Prolog: ICS/CSE 141: Programming Languages
Chris Stork
cstork@ics.uci.edu
Overview
2. Unification
3. Prolog
Rules
We want to be able to say:
(and (parent ?x ?y) (male ?x)) ==> (father ?x ?y)
(and (parent ?y ?x) (male ?x)) ==> (son ?x ?y)
Rules as DB entries
(<- (father ?x ?y) ⇐= head of the rule
(and (parent ?x ?y) (male ?x))) ⇐= body of the rule
Backtracking (depth-first)
(test bt-ex
((query-infer ’(and (a) (e)))
(() ()))
)
Unification
Beyond Matching
Unification
Terms
Unification operates on terms, e.g.,
Terms consist of
• variables: x, y, z, . . . (our “logical variables”)
• function symbols: f, g, h, . . . (our “relations”)
• parethesis and comma
Terms are inductively defined as
• a variable
• if f is an n-ary function symbol and t1 , . . . tn are terms, then
f (t1 , . . . tn ) is a term
Constants (0-ary functions) are written as a, b, c, . . . (our “objects”
or (f))
Example: Unification
Examples of Unification:
• a=a ⇒ yes, []
• f (b) = f (b) ⇒ yes, []
• f (a) = f (b) ⇒ no
• f (x) = f (2a) ⇒ yes, [?x=a]
• f (x) = f (y) ⇒ yes, [?x=?y]
• f (x, y) = f (a) ⇒ no
• f (x, y) = f (a, x) ⇒ yes, [?x=a,?y=a]
A variable can only have one value for a given unification to
succeed.
Unification Algorithm
This is proven to terminate and find the most general unifier (MGU). (So if
we implement this we can be sure to do the “right thing”.)
More Examples
More Examples
Consider {f (x, a) = f (g(z), y), h(x, z) = h(u, d)}
• Use (1) on first to get {x = g(z), a = y, h(x, z) = h(u, d)}
• Use (5) with first to get
{x = g(z), a = y, h(g(z), z) = h(u, d)}
• Use (1) on third to get {x = g(z), a = y, g(z) = u, z = d}
• Use (5) with fourth to get
{x = g(d), a = y, g(d) = u, z = d}
• Use (4) on second to get
{x = g(d), y = a, g(d) = u, z = d}
• Use (4) on third to get {x = g(d), y = a, u = g(d), z = d}
• Nothing else applicable, unifies using
[x/g(d)], [y/a], [u/g(d)], [z/d]
Interesting Stuff
Prolog
Prolog
• arithmetic
• lists
• special control facilities, most notably cut
• Syntax:
– variable names: Prolog uses capitalized names for logical
variables
– facts written as:happy.
– implications as: happy :- moreFood. (called
clauses)
• we use Scheme lists instead of Prologs special [H|T] lists
Prolog interactive queries:
append([],Xs,Xs).
append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).
> append([1,2],A,[1,2,3,4])?
A=[3,4].
It’s not the same as Prolog!! Much has changed since then. You
only got a taste of what’s out there.
Check out:
• Constraint-based programming . . .
Functions are equalities, we just chose to read them one way! Take
any of our purly functional examples, e.g. app.
Pure functions, rules, and relations define, or better, declare
relationships!
11 Example: Unification
List of Slides
12 Unification Algorithm
1 Lecture 7
13 More Examples
1 Title
14 More Examples
2 Overview
3 Rules & Backtracking 15 Our Unification Implementation