Other Program Sections

Now that you're reasonably familiar with the clauses, predicates, domains, and goal sections of a Visual Prolog program, we'll tell you a little bit about some other commonly-used program sections: the facts section, the constants section, and the various global sections. This is just an introduction; as you work through the rest of the tutorials in this book you'll learn more about these sections and how to use them in your programs.

1. The Facts Section

A Visual Prolog program is a collection of facts and rules. Sometimes, while the program is running, you might want to update (change, remove, or add) some of the facts the program operates on. In such a case, the facts constitute a dynamic or internal database; it can change while the program is running. Visual Prolog includes a special section for declaring the facts in the program that are to be a part of the dynamic (or changing) database; this is the facts section.

The keyword facts declares the facts section. It is here that you declare the facts to be included in the dynamic facts section. Visual Prolog includes a number of built-in predicates that allow easy use of the dynamic facts section. The keyword facts is synonymous with database.

Chapter 8 provides a complete discussion of the facts section and the predicates used along with it.

2. The Constants Section

You can declare and use symbolic constants in your Visual Prolog programs. A constant declaration section is indicated by the keyword constants, followed by the declarations themselves, using the following syntax:

    <Id> =  <Macro definition>

<Id> is the name of your symbolic constant, and <Macro definition> is what you're assigning to that constant. Each <Macro definition> is terminated by a newline character, so there can only be one constant declaration per line. Constants declared in this way can then be referred to later in the program.

Consider the following program fragment:

Before compiling your program, Visual Prolog will replace each constant with the actual string to which it corresponds. For instance:

    ...,
    A = hundred*34, delay(A),
    setfillstyle(slash_fill, red),
    Circumf = pi*Diam,
    ...

will be handled by the compiler in exactly the same way as

    ...,
    A = (10*(10-1)+10)*34, delay(A),
    setfillstyle(4, 4),
    Circumf = 3.141592653*Diam,
    ...

There are a few restrictions on the use of symbolic constants:

The definition of a constant can't refer to itself. For example:

    my_number = 2*my_number/2 /* Is not allowed  */

will generate the error message Recursion in constant definition.

The system does not distinguish between upper-case and lower-case in a constants declaration. Consequently, when a constants identifier is used in the clauses section of a program, the first letter must be lower-case to avoid confusing constants with variables. So, for example, the following is a valid construction:

There can be several constants declaration sections in a program, but constants must be declared before they are used.

Declared constants are effective from their point of declaration to the end of the source file, and in any files included after the declaration. Constant identifiers can only be declared once. Multiple declarations of the same identifier will result in the error message This constant is already defined.

3. The Global Sections

Visual Prolog allows you to declare some domains, predicates, and clauses in your program to be global (rather than local); you do this by setting aside separate global domains, global predicates, and global facts sections at the top of your program. These global sections are discussed in the chapter 17.

4. The Compiler Directives

Visual Prolog provides several compiler directives you can add to your program to tell the compiler to treat your code in specified ways when compiling. You can also set most of the compiler directives from the Options | Project | Compiler Options menu item in the Visual Prolog system. Compiler directives are covered in detail in the chapter 17, but you'll want to know how to use a couple of them before you get to that chapter, so we introduce the basic ones here.

The include Directive

As you get more familiar with using Visual Prolog, you'll probably find that you use certain procedures over and over again in your programs. You can use the include directive to save yourself from having to type those procedures in again and again.

Here's an example of how you could use it:

You create a file (such as MYSTUFF.PRO) in which you declare your frequently-used predicates (using domains and predicates sections) and give the procedures defining those predicates in a clauses section.

You write the source text for the program that will make use of these procedures.

At a natural boundary in your source text, you place the line

When you compile your source text, Visual Prolog will compile the contents of MYSTUFF.PRO right into the final compiled product of your source text.

You can use the include directive to include practically any often-used text into your source text, and one included file can in turn include another (but a given file can only be included once in your program). The include directive can appear at any natural boundary in your source text. However, you must observe the restrictions on program structure when you include a file into your source text.

Summary

These are the ideas we've introduced in this chapter:

A Visual Prolog program has the following basic structure:

The clauses section is where you put the facts and rules that Visual Prolog will operate on when trying to satisfy the program's goal.

The predicates section is where you declare your predicates and the domains (types) of the arguments to your predicates. Predicate names must begin with a letter (preferably lower-case), followed by a sequence of letters, digits, and underscores, up to 250 characters long. You can't use spaces, the minus sign, asterisks, or slashes in predicate names. Predicate declarations are of the form

argument_type1, ..., argument_typeN are either standard domains or domains that you've declared in the domains section. Declaring the domain of an argument and defining the argument's type are the same thing.

The domains section is where you declare any nonstandard domains you're using for the arguments to your predicates. Domains in Prolog are like types in other languages. Visual Prolog's basic standard domains are char, byte, short, ushort, word, integer, unsigned, long, ulong, dword, real, string, and symbol; the more specialized standard domains are covered in other chapters. The basic domain declarations are of the form

The goal section is where you put your program's internal goal; this allows the program to run independent of the development environment. With an internal goal, Visual Prolog only searches for the first solution, and the values to which variables are bound are not displayed.

If you don't use an internal goal, you'll enter an external goal in the Dialog window at run time. With an external goal, Visual Prolog searches for all solutions, and displays the values to which variables are bound.

The arity of a predicate is the number of arguments that it takes; two predicates can have the same name but different arity. You must group a predicate's different arity versions together in both the predicates and clauses sections, but different arities are treated as completely different predicates.

Rules are of the form

The :- ("if") in Prolog should not be confused with the IF used in other languages; a Prolog rule is in the form of a then/if conditional, while IF statements in other languages are in the form of an if/then conditional.