Prolog: First Impressions (or a basic tutorial on the first lab session)
Yesterday I had my first artificial intelligence lab session on Prolog. It didn't start off very well. The instructions we were given didn't seem to have been tested and were largely useless. Thankfully one of the demonstrators came over to help and showed me the basics.
This blog post is part review, part first impressions, and part tutorial. It's purpose is to consolidate the things that I have learnt. Hopefully it will be useful to someone else out there :) Also note, if you haven't figured it out by now, I am a complete beginner with Prolog. There will be mistakes! If you spot one, please comment below so that I can fix it :)
My first impression of Prolog is that it is hard. It is very hard to learn. This isn't helped by the fact that the tools available to the new Prolog programmer are very out of date and feel exceptionally clunky. Perhaps a redesign is in order?
Anyway, I discovered at first that Prolog is very much like a detective. You give it facts and relationships, and then question it on the information you've given it (much like comprehension) using things called queries. Here's an example of a fact:
cat.
In the above, we are telling prolog that something called a cat
exists. In it's world now, a cat
is the only thing in existence. We can then ask it whether a cat
exists in the world in the query window by running the prolog file. If you are using the default editor that comes with Prolog, simply press CTRL+C
and then CTRL+B
. If you aren't, type prolog filename.pl
to launch a query window based on filename
.
?- cat.
true.
Prolog is telling us that a cat
exists, but we knew that already. Let's try asking it whether a dog
exists:
?- dog.
ERROR: toplevel: Undefined procedure: dog/0 (DWIM could not correct goal)
Oh dear! We get a nasty (over)complicated error! Thankfully, this error (in this case) is a really simple one. Prolog is basically telling us that it doesn't know what a dog
is, because a dog
doesn't exist in Prolog's world at the moment.
Getting Prolog to tell us whether something exists in it's world is nice, but it isn't terribly useful. Let's try something a little bit more complicated.
animal(cat).
The above tells prolog that a cat
is an animal
. For reference, the bit before the opening bracket is called a predicate I think. After executing the above, Prolog's world jsut got a bit more complicated. It now knows that there is something called a cat
, and that a cat
is an animal
. We can then ask Prolog if a cat
is an animal
like so:
?- animal(cat).
true.
Prolog tells us that yes, a cat is an animal. This is also nice, but still not amazingly clever. Let's write some more Prolog.
animal(cat).
animal(dog).
animal(cow).
pet(cat).
pet(dog).
food(milk).
food(bone).
likes(cat, milk).
likes(dog, bone).
The above looks much more complicated, but it's not as bad you might first think. In the above, we tell Prolog that a cat
, a dog
, and a cow
are animals. We also tell it that only a cat
and dog
are pets. In addition, we say that both milk
and a bone
are forms of food, and that a cat
likes milk
, and a dog
likes a bone
. We can then ask Prolog a bunch of questions on the above:
?- animal(cow).
true.
?- pet(cow).
false.
?- likes(cat, milk).
true.
?- likes(cat, bone).
false.
In the above, we ask prolog 4 questions:
- Is a cow an animal? Prolog answers that yes, a cow is in fact an animal (we told it so).
- Is a cow a pet? Prolog answers that no, a cow isn't a pet. It knows that because we told it that a cow is an animal, but we didn't tell it that a cow is a pet.
- Does a cat like milk? Prolog answers that yes, cats do like milk - because we told it so.
- Does a cat like a bone? Prolog answers that no, cats don't like bones, as we told it that cats like milk instead.
Prolog is starting to show how powerful it is, but I have a feeling that I haven't even scratched the surfaced yet. Next week, assuming I understand the next lab, I'll make another post about what I've learnt.