![]() |
[Last
Update 2002.11.20]
Matthew Wronka <wronkm@rpi.edu> |
Unification, Variables and UnificationOk, 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 :
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:
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:
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.
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.
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. ExamplesConsider 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). Who=chris yes attends(wHo,compOrg). no attends(matt,What). What=chd yes attends(Geoff,What). Geoff=tina What=progLang yes attends(VaRiAbLe,proglang). no attends(VaRiAbLe,compOrg). VaRiAbLe=tim yes attends(TEMP,progLang). TEMP=tina yes 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=? tina,progLang yes Who=?,What=? chris,sd_d yes Who=?,What=? geoff,progLang yes Who=?,What=? matt,chd yes Who=?,What=? chris,progLang yes Who=?,What=? tim,compOrg yes Who=?,What=? chris,phil_film yes Previous: Facts Next: Rules |
|
||||||||||
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.