![]() |
[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).
We can also have more descriptive facts if we so desire.
Simple, no? How about these?
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
ah ha! gotcha! there's no full stop (no '.')
[Hide]
cars.
yep
[Hide]
Isthisafactidontknowmyself.
nope, it starts with a capitalized letter
[Hide]
thenisthisafact.
sure is
[Hide]
2manyfaqs.
negative, starts with a number
[Hide]
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.
See? It isn't so bad, now try a practice problem. Given the database:
Which of these queries will succeed? ?- this_sucks.
no way! this_sucks was never stated as a fact!
[Hide]
?- i_can_notread.
false as well, was never stated as a fact!
[Hide]
?- you_are_reading_this_faq.
yep, you sure are
[Hide]
?- this_is_the_best_tutorial_ever.
you better believe it
[Hide]
?- happyYay.
this is also true, whee
[Hide]
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.
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.
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).
do i have mad money? -> yes i do (yay)
[Hide]
?- money(alby,0).
alby has 0 money? -> yep, no money at all
[Hide]
?- money(alby,zero).
alby has zero money? -> nope, zero is not the same as 0
[Hide]
?- money(sarah,899).
sarah has $899? -> nope, sarah doesn't have $899, she has $900.
[Hide]
?- money(forte,20)
forte has $20? -> yep
[Hide]
Next: Unification |
|
||||||||||
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.