The Wronk [Last Update 2002.11.20]

Matthew Wronka
<wronkm@rpi.edu>

Facts are Fun!

In Prolog, we have the notion of facts. Facts define properties or relations of and or between objects for the program to follow. They are similar to statements in english (or any other language you prefer). This allows them to be read easily in english. Here are some examples, note that the ?- is the standard Prolog prompt (think of a DOS prompt or a UNIX shell).

?- dark. /* it's dark */
?- big. /* it's big */

We can also have more descriptive facts if we so desire.

?- the_room_is_dark. /* the room is dark */
?- theRoomIsbig. /* the room is big */

Simple, no? How about these?

?- Dark.
?- 3four.

Nope, neither of these are facts. Why? Facts need to start with a lower case letter (and end with a '.'), nor can they use basic mathematical operators (+, *, /, -). Aside from that, any other combination of letters, numbers, and the underscore symbol ('_') can be used.

Which of the following facts are syntactically correct and well formed?


cars
cars.
Isthisafactidontknowmyself.
thenisthisafact.
2manyfaqs.

Did you find both of them?


Prolog, being a logical language, evaluates everything in terms of yes or no. So given a database of facts, you can make queries against it to evaluate statements.

john_has_a_car. /* john has a car */
sam_has_no_car. /* sam has no car */
john_has_no_money. /* john has no money */
sam_has_money. /* sam has money */

?- tim_isrich. /* is tim is rich? */
no /* aww, guess not :( */
?- john_has_a_car. /* does john have a car? */
yes /* sure does */
?- johnhasacar.
no /* nope, this reads the same as the above line, but it doesn't match anything in the database */
?- sam_has_money.
yes /* yes he does */

See? It isn't so bad, now try a practice problem.

Given the database:

this_is_the_best_tutorial_ever.
fun.
you_are_reading_this_faq.
happyYay.

Which of these queries will succeed?



?- this_sucks.
?- i_can_notread.
?- you_are_reading_this_faq.
?- this_is_the_best_tutorial_ever.
?- happyYay.

Did you find the three statements that succeeded?


Expanding on the previous, we have facts with arguments. Facts with arguments are very similar to facts, the difference is, facts with arguments are used to show relationships between objects.

happy(tim). /* tim is happy :) */
smart(mint,vanilla). /* mint and vanilla are smart */

Just keep in mind a few things. First, relations can be read in multiple ways sometimes. From the above, smart(mint,vanilla) could also be read as vanilla and mint are smart. As long as you maintain consistency, this should not be a problem. Relationships folow the same rules as facts, they need to begin with a lower case letter and can have any combination of letters, numbers, and underscores. All relations follow the form: relation(<argument1>,....,<argumentN>) or relation(<argument1>), where N is the number of arguments you want the relation to have. Now for an example.

inproglang(fred). /* fred is in proglang */
inproglang(mary). /* mary is in proglang */
workswith(fred,mary). /* fred works with mary */
workswith(lisa,ian). /* lisa works with ian */

?- inproglang(tim). /* is tim in proglang? */
no /* not in the context of the given database */
?- inproglang(fred). /* is fred in proglang? */
yes /* yep, see the database */
?- workswith(|154,ian). /* does fred work with |154? */
no /* nope, |154 is not the same as lisa */
?- workswith(fred,mary).
yes /* yep, see the database */


Given the database:
money(sarah,900).
money(alby,0).
money(forte,20).
money(xorp,150).
money(aiwa,10000).
money(i,mad).

Which of these queries will succeed?
?- money(i,mad).
?- money(alby,0).
?- money(alby,zero).
?- money(sarah,899).
?- money(forte,20)

Next: Unification

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

Call on God, but row away from the rocks.
		-- Indian proverb

Copyright ©2000-2002 Matthew Wronka. This page was last modfied Wed, 20 Nov 2002 18:14:48 -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