CHAPTER 5    Simple and Compound Objects

 

So far we've only shown you a few kinds of Visual Prolog data objects, such as numbers, symbols, and strings. In this chapter we discuss the whole range of data objects that Visual Prolog can create, from simple to compound objects.

We also show the different types of data structures and data objects that a Visual Prolog program can contain. Because the standard domains do not cover some of the compound data structures, we explain how to declare these compound data structures in both the domains section and the predicates section of your programs.

Simple Data Objects

A simple data object is either a variable or a constant. Don't confuse this use of the word "constant" with the symbolic constants you define in the constants section of a program. What we mean here by a constant, is anything identifying an object not subject to variation, such as a character (a char), a number (an integral value or a real), or an atom (a symbol or string).

1. Variables as Data Objects

Variables, which we've discussed in chapter 2, must begin with an upper-case letter (A-Z) or an underscore (_). A single underscore represents an anonymous variable, which stands for a "don't care what it is" situation. In Prolog, a variable can bind with any legal Prolog argument or data object.

Prolog variables are local, not global. That is, if two clauses each contain a variable called X, these Xs are two distinct variables. They may get bound to each other if they happen to be brought together during unification, but ordinarily they have no effect on each other.

2. Constants as Data Objects

Constants include characters, numbers, and atoms. Again, don't confuse constants in this context with the symbolic constants defined in the constants section of a program. A constant's value is its name. That is, the constant 2 can only stand for the number 2, and the constant abracadabra can only stand for the symbol abracadabra.

Characters

Characters are char type. The printable characters (ASCII 32-127) are the digits 0-9, upper-case letters A-Z, lower-case letters a-z, and the punctuation and familiar TTY characters. Characters outside this range may not be portable between different platforms; in particular, characters less than ASCII 32 (space) are control characters, traditionally used by terminals and communication equipment.

A character constant is simply written as the character you want, enclosed by single quotes:

'a'         '3'

'*'         '{'

'W'         'A'

If, however, you want to specify a backslash or a single quote itself as the character, precede it by a backslash (¡¬):

'¡¬¡¬' backslash   '¡¬'' single quote.

There are a few characters that perform a special function, when preceded by the escape character:

'¡¬n'

Newline (linefeed)

'¡¬r'

Carriage return.

'¡¬t'

Tab (horizontal)

Character constants can also be written as their ASCII codes, preceded by the escape character, like this:

'¡¬225'        ß

'¡¬3'          %]

but the exact character displayed by more exotic ASCII values will vary depending on your video-card/terminal.

Numbers

Numbers are either from one of the integral domains (see Table 3.1 on page 25), or the real domain. Real numbers are stored in the IEEE standard format and range from 1e-308 to 1e308 (10-308 to 10+308). Examples are:

Integers

Real Numbers

3

3.

-77

34.96

32034

-32769

-10

4e27

0

-7.4e-296

Atoms

An atom is either a symbol or a string. The distinction between these is largely a question about machine-representation and implementation, and is generally not syntactically visible. When an atom is used as an argument in a predicate call, it is the declaration for the predicate that determines if that argument should be implemented as a string or a symbol.

Visual Prolog performs an automatic type conversion between the string domain and the symbol domain, so you can use symbol atoms for string domains and string atoms for the symbol domains. However, there is a loose convention stating that anything in double quotes should be considered a string, while anything not needing to be quoted to be syntactically valid is a symbol:

Symbol atoms are names starting with a lower-case letter, and containing only letters, digits, and underscores.

String atoms are bound within double quotes and can contain any combination of characters, except ASCII NULL (0, binary zero), which marks the end of the string.

Symbol Atoms

String Atoms

food

"Jesse James"

rick_Jones_2nd

"123 Pike street"

fred_Flintstone_1000_Bc_Rd_Bedrock

"jon"

a

"a"

new_york

"New York"

pdcProlog

"Visual Prolog, by Prolog Development Center"

As far as the string/symbol domain interchangeability goes, this distinction is not important. However, things such as predicate names and functors for compound objects, introduced below, must follow the syntactic conventions for symbols.