CHAPTER 3    Visual Prolog Programs

 

The syntax of Visual Prolog is designed to express knowledge about properties and relationships. You've already seen the basics of how this is done; in Chapter 2 you learned about clauses (facts and rules), predicates, variables, and goals.

Unlike other versions of Prolog, Visual Prolog is a typed Prolog compiler; you declare the types of the objects that each predicate applies to. The type declarations allow Visual Prolog programs to be compiled right down to native machine code, giving execution speeds similar to those of compiled C and pascal.

We discuss the four basic sections of a Visual Prolog program--where you declare and define the predicates and arguments, define rules, and specify the program's goal--in the first part of this chapter. In the second part of this chapter we take a closer look at declarations and rule syntax. Then, at the end of this chapter, we briefly introduce the other sections of a Visual Prolog program, including the facts, constants, and various global sections, and compiler directives.

Visual Prolog's Basic Program Sections

Generally, a Visual Prolog program includes four basic program sections. These are the clauses section, the predicates section, the domains section, and the goal section.

The clauses section is the heart of a Visual Prolog program; this 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. (You don't need to declare Visual Prolog's built-in predicates.)

The domains section is where you declare any domains you're using that aren't Visual Prolog's standard domains. (You don't need to declare standard domains.)

The goal section is where you put the starting goal for a Visual Prolog program.

1. The Clauses Section

The clauses section is where you put all the facts and rules that make up your program. Most of the discussion in Chapter 2 was centered around the clauses (facts and rules) in your programs; what they convey, how to write them, and so on.

If you understand what facts and rules are and how to write them in Prolog, you know what goes in the clauses section. Clauses for a given predicate must be placed together in the clauses section; a sequence of clauses defining a predicate is called a procedure.

When attempting to satisfy a goal, Visual Prolog will start at the top of the clauses section, looking at each fact and rule as it searches for a match. As Visual Prolog proceeds down through the clauses section, it places internal pointers next to each clause that matches the current subgoal. If that clause is not part of a logical path that leads to a solution, Visual Prolog returns to the set pointer and looks for another match (this is backtracking, which we mentioned in Chapter 2).

2. The Predicates Section

If you define your own predicate in the clauses section of a Visual Prolog program, you must declare it in a predicates section, or Visual Prolog won't know what you're talking about. When you declare a predicate, you tell Visual Prolog which domains the arguments of that predicate belong to.

Visual Prolog comes with a wealth of built-in predicates. You don't need to declare any of Visual Prolog's built-in predicates that you use in your program. The Visual Prolog on-line help gives a full explanation of the built-in predicates.

Facts and rules define predicates. The predicates section of the program simply lists each predicate, showing the types (domains) of its arguments. Although the clauses section is the heart of your program, Visual Prolog gets much of its efficiency from the fact that you also declare the types of objects (arguments) that your facts and rules refer to.

How to Declare User-Defined Predicates

A predicate declaration begins with the predicate name, followed by an open (left) parenthesis. After the predicate name and the open parenthesis come zero or more arguments to the predicate.

    predicateName(argument_type1, argument_type2, ..., argument_typeN)

Each argument type is followed by a comma, and the last argument type is followed by the closing (right) parenthesis. Note that, unlike the clauses in the clauses section of your program, a predicate declaration is not followed by a period. The argument types are either standard domains or domains that you've declared in the domains section.

Predicate Names

The name of a predicate must begin with a letter, followed by a sequence of letters, digits, and underscores. The case of the letters does not matter, but we strongly recommend using only a lower-case letter as the first letter in the predicate name. (Other versions of Prolog don't allow predicate names to begin with upper-case letters or underscores, and future versions of Visual Prolog might not, either.) Predicate names can be up to 250 characters long.

You can't use spaces, the minus sign, asterisks, slashes, or other non-alphanumeric characters in predicate names. Valid naming charactersnaming characters in Visual Prolog consist of the following:

All predicate names and arguments can consist of combinations of these characters, as long as you obey the rules for forming both predicate and argument names.

Below are a few examples of legal and illegal predicate names.

Legal Predicate Names

Illegal Predicate Names

fact

[fact]

is_a

*is_a*

has_a

has/a

patternCheckList

pattern-Check-List

choose_Menu_Item

choose Menu Item

predicateName

predicate<Name>

first_in_10

>first_in_10

Predicate Arguments

The arguments to the predicates must belong to known Visual Prolog domains. A domain can be a standard domain, or it can be one you declare in the domains section.

Examples

If you declare a predicate my_predicate(symbol, integer) in the predicates section, like this:

This program excerpt shows some more predicate and domain declarations:

This excerpt specifies the following information about these predicates and their arguments:

The predicate likes takes two arguments (person and activity), both of which belong to unique symbol domains (which means that their values are names rather than numbers).

The predicate parent takes two person arguments, where person is a symbol type.

The predicate can_buy takes two arguments, person and car, which are also both symbol types.

The predicate car takes five arguments: make and color are of unique symbol domains, while mileage, years_on_road, and cost are of unique integer domains.

The predicate green takes one argument, a symbol: there is no need to declare the argument's type, because it's of the standard domain symbol.

The predicate ranking takes two arguments, both of which belong to standard domains (symbol and integer), so there is no need to declare the argument types.

Chapter 5, "Simple and Compound Objects," gives more detail about domain declarations.

3. The Domains Section

In traditional Prolog there is only one type; the term. We have the same in Visual Prolog, but we are declaring what the domains of the arguments to the predicates actually are.

Domains enable you to give distinctive names to different kinds of data that would otherwise look alike. In a Visual Prolog program, objects in a relation (the arguments to a predicate) belong to domains; these can be pre-defined domains, or special domains that you specify.

The domains section serves two very useful purposes. First, you can give meaningful names to domains even if, internally, they are the same as domains that already exist. Second, special domain declarations are used to declare data structures that are not defined by the standard domains.

It is sometimes useful to declare a domain when you want to clarify portions of the predicates section. Declaring your own domains helps document the predicates that you define by giving a useful name to the argument type.

Examples

Here's an example to illustrate how declaring domains helps to document your predicates:

This next example program will yield a type error when run:

/* Program ch03e01.pro */

You might expect Visual Prolog to return

To further understand how you can use domain declarations to catch type errors, consider the following program example:

/* Program ch03e02.pro */

4. The Goal Section

Essentially, the goal section is the same as the body of a rule: it's simply a list of subgoals. There are two differences between the goal section and a rule:

The goal keyword is not followed by :-.

Visual Prolog automatically executes the goal when the program runs.

It's as if Visual Prolog makes a call to goal, and the program runs, trying to satisfy the body of the goal rule. If the subgoals in the goal section all succeed, then the program terminates successfully. If, while the program is running, a subgoal in the goal section fails, then the program is said to have failed. (Although, from an external point of view, there isn't necessarily any difference; the program simply terminates.)