Lessons/Hello World!

In this lesson we will just write some code disregarding the finer details.

The main.pro file contains other stuff, which we will currently disregard, and focus solely on the run clauses you just wrote.

First we have the keyword clauses, which simly indicates that what comes after are clauses. Clauses is where the actual running code is written.

In this text I will use words like "clause" and "predicate", which all have their origin in formal logic, but for the moment we will simply treat them as the names that happens to be chosen for things in a Prolog program.

clauses are used to define predicates, where a predicate is a Prolog routine/subroutine/function/procedure (dear child has many names)

This particular clause defines a predicate run which takes no arguments (indicated by the empty parentheses ).

After the :- token we have the code that will be executed when run is invoked/called.

In this case we call a predicate named write</vp> which is defined in the class</vp> named stdio</vp>. We will discuss classes later for now we will just consider stdio::write</vp> as "a long compound name".

We supply one argument, i.e. the string literal "Hello World!\n"</vp>, to the stdio::write</vp> predicate. Notice that \n in the string literal represents a line shift.

The clause is terminated by a period (i.e. .</vp>). Period is used as terminator for many kinds of entities in Visual Prolog.

The predicate run</vp> is called when the program is run, and acts as our entry point to the program. Actually, the real entry point is the goal</vp> a further down in the main.pro file, but that entry point is used to make varous initializations, corresponding clean-up, etc and will call the run</vp> predicate in the right spot, so run can act like your entry point.

So when the program is run the predicate run</vp> in main.pro will be called and this will (here) call stdio::write</vp> with the argument "Hello World!\n"</vp>, as a result Hello World! and a line shift will be written to the console window.

Actually, stdio::write</vp> writes to the standard output stream, which in a console program is initialized to be the console window. In a default GUI program the standard output stream will write to a message window, but the standard output stream is under programmer control, so what happens when you call stdio::write</vp> will depend of what has taken place in the program.

To summarize:


 * The actual running code is in clauses</vp>
 * <vp>predicates</vp> is the Visual Prolog name for routines/subroutines/functions/procedures.
 * Predicates are organized in classes, and predicates from other classes is referenced using <vp> :: </vp>.
 * The <vp>run</vp> predicate in <vp>main.pro</vp> is the (effective) entry to a program.
 * <vp>stdio::write</vp> can write to the standard output stream

Now let us extend the program with an extra predicate.

When you build the project you will get a warning stating that the predicate <vp>main::advancedHello</vp> is an unused local predicate. Which is completely correct; we haven't used it for anything. But let us "decipher" it before we go any further.

We notice that we again have a clause in a <vp>clauses</vp> section. The clause is for a predicate named <vp> advancedHello</vp> that takes one argument called <vp>Name</vp>. <vp>Name</vp> is a variable, a variable starts with an uppercase letter and will be green in the editor (variables can also start with an underscore, but we will discuss that later).

<vp>Name</vp> acts as a formal parameter representing the actual argument used in the call to <vp>advancedHello</vp>. In this clause we call <vp>stdio::writef</vp> which is a more advanced "writer" predicate. The first argument to <vp>writef</vp> is a format string, which contains a %-character, what happens when <vp>writef</vp> is called is that the actual value of <vp>Name</vp> is substituted into the format string in the place where the %-character is and then the resulting string is written to the standard output.

This time we also have a <vp>class predicates</vp> section containing a declaration of the <vp>advacedHello</vp> predicate. This perticular declaration declares <vp>advancedHello</vp> as a class predicate which takes one argument which must be a string.

In the predicate declaration the argument also have the name <vp>Name</vp> but in the declaration the name is mainly intended for documentation purposes. The variable name in the declaration and in the clauses can be different, or you can remove it completely. I recommend that you always have variable names here and that you use variable names that will make it clearer for the user of the predicate what the arguments represents.

What it means to be a class predicate and what else a predicate can be is discussed elsewhere/later. But before we leave the subject try this little experiement:

Now that we have the advancedHello predicate we can use it in our <vp>run</vp> predicate.

The clause should be understandable, I just want to highlight that we have used comma to separate the two invocations of <vp>advancedHello</vp>: comma" to separate, period to terminate. I will give a little prediction: Later when you are familiar with Visual Prolog you will from time to time make syntax errors and (the prediction) 9 out of 10 times the syntax error will concern comma and/or period'''.

To summarize:


 * Predicates must have a declaration (<vp>run</vp> is also declared we just havn't looked at its declaration).
 * Arguments in predicates declarations can be given names that clarify the meaning if the arguments.
 * <vp>stdio::writef</vp> writes text to the standard output stream using a format string.
 * calls are separated by comma and clauses (and predicate declarations) are terminated by period.
 * If I get a syntax error it is likely to be related to comma or period.
 * A certain other error message (can you recall it) probably means that I have forgotten to write <vp>class</vp> in front of <vp>predicates</vp>.