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.
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:
Who=chris yes
no
What=chd yes
Geoff=tina What=progLang yes
no
VaRiAbLe=tim yes
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:
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
--