# 13.2 Building Blocks

 Content

The fundamental elements of Logikus are structures and variables. As a logic programmer, you will learn to weave structures and variables together into rules and then to combine rules to form logic programs.

#### 13.2.1 Structures

A structure is the basic data repository in a logic language. A structure is a functor associated with a group of terms. A functor can be a lowercase string, a quoted string, or a number. Some acceptable functors are

` jamesCagney  starred "Yankee Doodle Dandy" 1942 `

In addition to having a functor, each structure possesses a collection of zero or more terms. A structure with no terms is an atom. When a structure has terms, they must be structures and variables, separated by commas and enclosed in parentheses. For example, here is a valid structure:

` starred(jamesCagney, "Yankee Doodle Dandy", 1942) `

This structure models the fact that James Cagney starred in "Yankee Doodle Dandy," which debuted in 1942. This structure contains no variables, and so it is a fact. Its functor is starred , and it has three terms, each of which is an atom. For example, within the starred structure,

` jamesCagney `

is an atom. This structure's functor is jamesCagney , and it has no associated terms.

To write James Cagney's name with proper capitalization, you can put it in double quotes:

` starred("James Cagney", "Yankee Doodle Dandy", 1942) `

Numbers in Logikus must be atoms , and they can be integers or reals but not exponents or other descriptive forms. For example, the following is a valid atom:

` 1942.0 `

But the following expressions are not valid in Logikus:

` 19.42e2          // invalid - uses an exponent 3.14(symbol, pi) // invalid - nonatomic `

Structures can have nonatomic structures as terms. For example,

` person(      jim,     address(         "2240 A Street",         "Yourtown",         "YS"),     23) `

is a valid structure having three terms. Its middle term , the address structure, serves to group information for a single term in person . Practical logic programmers use nested structures sparingly because these structures can make a logic model harder to use.

#### 13.2.2 Variables

A Logikus variable is an object that has a name and can bind to structures or other variables. The Logikus parser distinguishes variables from structures using case, treating any string beginning with an uppercase letter as a variable. For example, valid variable names include

` X  Y Title WhoDoneIt `

Given the structure

` starred(jamesCagney, Title, Year) `

the Logikus parser creates the starred structure with three terms. Those terms are the atom jamesCagney and the variables Title and Year . The terms Title and Year are variables because they begin with capital letters .

You might like to match a structure such as this one with a structure such as

` starred(jamesCagney, "Yankee Doodle Dandy", 1942) `

so that you can extract a structure's data into variables. Logikus provides this kind of matching in a behavior called unification. You can use the Logikus development environment to see unification in action.

 Top

Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon