Prolog Fast Manual


© Zhou Qingqing 2001
http://www.cs.toronto.edu/~zhouqq

Last modified: 2001-09-17



Read First (1 minute)

  1. Q: Who can benefit from this?

  2. A: If you have master 1 programming language, and you have 0% knowledge of how to programming with PROLOG. I can give you the push from 0%->1%.

  3. Q: Why do I write this ?

  4. A: Because when I wanted to learn this before I use the search engine to look for "prolog manual", "prolog tutorial" ... and I got "GNU prolog manual", "SWI prolog manual", long tutorial ... after reading I even don't know how to make the prolog code be compiled. This manual will fast and easily lead you to the PROLOG world.

  5. Q: Is this esp. for some PROLOG environment?

  6. A: Actually, if you use google to find out the information of PROLOG programming language, you will be dizzy with different version, "GNU", "SWI", "SICStus", "Amzi!" ... and the one on your lab's main frame. However, just like C/C++ with many versions, they have the same core part. The most unfriendly interface is the unix box PROLOG, so I will begin my course based on this PROLOG. I am sure every Prolog compiler will support them.

Let's begin (5 minute)

  1. First program

  2. Just as many language, we begin with "Hello world!":
    step1 >>> key in "prolog" after command prompt:

    qew.cs>prolog

    if you have the right to execute this program, you can enter it and you will see:

    | ?-

    step2 >>> key in "write('Hello world!')." and press return key. Don't forget the last full stop:

    | ?- write('Hello world!').
    Hello world!
    yes | ?-

    step3 >>> press "^C" :

    qew.cs>Prolog interruption (h for help)?

    if you press enter key now, it will show you the help message. If you press "^C" again, prolog will quit.

    So how about PROLOG, seems it is tender and not that difficult to use!

  3. Know a little about PROLOG

  4. There are 2 main parts in a PROLOG program, one is the facts, the other is rules. I don't want to give you the precise definition of them, but we can feel what are they from the example.
    Facts(Note: '%' is PROLOG's comment):

    good(zhou). % zhou is good

    man(zhou). % zhou is a man


    Rules:

    good(X):-man(X). % for all X, if X is a man, then X is good.

    Here, we pay attention to 2 points: (1) ':-' means the right part implicate the left part. (2) PROLOG regards all strings with capital initial as

  5. variable
  6. and else as a const
  7. identifier
  8. . For example, X is a variable and zhou is a const identifier. Copy the above 3 lines into a file and saved as "goodman". Then you can key in "consult(goodman)." after PROLOG command prompt:

    | ?- consult(goodman).

    % compiling file /a/11/zhouqq/goodman
    * Clauses for good/1 are not together in the source file
    * Approximate line: 5, file: '/a/11/zhouqq/goodman'
    % goodman compiled in module user, 0.010 sec 456 bytes

    yes
    | ?-

    'consult()'is a build-in command of PROLOG. It just load your file and compile it. When you see 'yes', that is to say PROLOG has accepted your program. So you can begin your query:

    | ?- good(zhou).

    yes
    | ?- good(wang).

    no
    | ?- good(zhang).

    no

    So, your program works!
    So far, we have learned how to enter/quit command line PROLOG environment; write/compile your first simple program and interact with it. This is the 1% of PROLOG. Now you can surf the Internet to find more about PROLOG you need!


Other links


Back