The Wronk [Last Update 2002.11.20]

Matthew Wronka
<wronkm@rpi.edu>

Unification, Variables and Unification

Ok, so now that you know how to use facts it'd be nice to do something more with them. How about the ability to query a database of facts to see what is and is not a fact? How do we do this?

SURVEY SAYS: UNIFICATION!! (at least a survey taken by anyone remotely awake from Prof. Schupp's ProgLang class)

Unification is a type of matching that Prolog will perform for us. Unification depends on Variable which are nothing without Unification, ok?

So, if we had a fact of :

am(alby,squirrel).

and we wanted to ask Prolog what alby was, we might try:
?- am(alby,what).
no

The reason Prolog returns no is because alby is not a what, alby is a squirrel. To have the previous query work the way we expect we need to use a variable. A variable in Prolog is signified by starting with a capital letter, so some variables are:
Who
WhY
WHERE
HoW

becaues they all start with capitals. So,
?- am(alby,What).
What=squirrel
yes

Prolog says that it found a fact about alby being something and it was a squirrel, so it returns yes.

The way Prolog achieved this was through unification, What unified with squirrel. Prolog goes through the database of facts looking for anything of the type is(alby,X). The first thing it find or unifies with is returned, if nothing unifies it returns no.

Using the previous fact, query for:
?- am(me,What).
no

Prolog can not find a fact about me.

Prolog will also only return the first fact that unifies, unless you specially redo the query before it finishes.
Consider the database:
am(prolog,awesome). %prolog is awesome
am(metroid,awesome). %metroid is awesome

when you ask Prolog:
?- am(What,awesome).
What=prolog
yes
?- am(What,awesome).
What=prolog
yes

But if before you finish the query (get to yes or no) you redo a query, Prolog will give you the next fact that unifies.
For instance:
?- am(What,awesome).
What=prolog;
What=metroid;
no

Now we see everything, but Prolog return no, this is because we kept asking and on the third search it couldn't find anything, hence no.

Unification and Variables and especially Unification are very important to Prolog and will show up in later lectures.

Examples

Consider the following database:

attends(tina,progLang).
attends(chris,sd_d).
attends(geoff,progLang).
attends(matt,chd).
attends(chris,progLang).
attends(tim,compOrg).
attends(chris,phil_film).

For each of the follow pick yes or no to whether they unify and write what you think the variable is bound to:


attends(Who,sd_d).
attends(wHo,compOrg).

attends(matt,What).

attends(Geoff,What).

attends(VaRiAbLe,proglang).

attends(VaRiAbLe,compOrg).

attends(TEMP,progLang).

Now, using the previous database, select what each variable is bound to at a given moment when Prolog is continually asked for more solutions to:


attends(Who,What).

        Who=?,What=?

        yes

        Who=?,What=?

        yes

        Who=?,What=?

        yes

        Who=?,What=?

        yes

        Who=?,What=?

        yes

        Who=?,What=?

        yes

        Who=?,What=?

        yes

Previous: Facts

Next: Rules

Home

Me
About Me
Class Schedule
Prospective Employers
Site
About
Front Page
PHP SysInfo
Texts
State of the World
Email Rules
Lasthope
Stuff
People
Projects
My Computers
Old Site
Links
Inflatable Code
The Register
Slashdot
Everything2
Wiretap
Google
CERT

A continuing flow of paper is sufficient to continue the flow of paper.
		-- Dyer

Copyright ©2000-2002 Matthew Wronka. This page was last modfied Wed, 20 Nov 2002 19:43:03 -0500.
Valid HTML 4.01! Valid CSS! Best viewed with any HTML 4.01 browser Bobby WorldWide Approved 508 Bobby WorldWide Approved AA
Powered by PHP Powered by Apache Powered by FreeBSD