Computer Science Canada A Glimpse at Prolog |
Author: | wtd [ Sun Apr 10, 2005 3:29 am ] | ||||||||||||||||||||
Post subject: | A Glimpse at Prolog | ||||||||||||||||||||
So, I've brought to attention all kinds of languages. Let's look at one that's completely different. Prolog Prolog is a logic programming language. With it we don't spell out sequences of statements to solve a problem. Rather we build a database of known facts and rules and let the computer solve queries based on logical deduction. Think my using the word database in their was a coincidence? Aside from using dramatically different syntax, Prolog and SQL are not too distant cousins. A Simple Example Let's build a family tree.
These are known facts. Each line is a clause, terminated with a period. Now, let's establish some relationships. Each of these says "_ is the parent of _".
These are well and good, but what if we want to add "fatherof" and "motherof" relations? Should we write them out? Nope, we already have enough information.
This is saying: "F is the father of C if F is male and F is the parent of C". ":-" means "if", and "," means "and". ";" means "or". We'll see that one eventually. Any name starting with a capital letter is a variable. We can define "motherof" similary.
What about a sibling relationship?
Here we say: "X is a sibling of C (known child) if P is a parent of C and P is a parent of X and X is not the same as C". What about a grandparent relation?
"G is a grandparent of C if P is a parent of C and G is a parent of P." How do I work with this? I'm currently using GNU Prolog and it seems to work well. First save the code above in a file "fam.pl". Then run the interpreter and use the consult predicate to loadthe file.
Now, we can make a simple query.
Or use a variable for a more complex query.
Exiting the interpreter is easy.
|
Author: | Drakain Zeil [ Sun Apr 10, 2005 8:44 am ] |
Post subject: | |
I remember using prolog a few years back, is it that thing for AI? I might get back into it one day. |
Author: | wtd [ Sun Apr 10, 2005 2:29 pm ] | ||
Post subject: | |||
Another relatively simple example, this one showing a bit of output and recursion.
|