# 6.3 Variables

## What is a variable?

A variable is a piece of the computer's memory which can hold a value, and which we have given a name.

For example, the variable L might have the value Romeo, which is an atom.

The variable G1 might have the value [Cat]-ON->[Mat], which is a CG.

These are just examples.

## Bound and free variables

A variable's value can change as the program runs. This is known as binding the variable to different values.

A variable that is bound is a variable that has a value.

A variable is is free is a variable that does not have a value.

## Variable syntax

Variables have to follow a number of rules.

### Rules

A variable:

• Must consist of letters, underscores, or digits,

### Examples of variables

So for example, the following are all variables:

• A
• G
• _c3p0
• _my_CG

### Anti-examples

The following are not variables:

• AB (because the first two are letters)
• ABC (because the first two are letters)
• graph1 (because the first two are letters)
• A.C (because it does not consist solely of letters, underscores, or digits)
• 3PO (because it does not start with a letter or an underscore)

Thus variables have a specific syntax, which follows three simple rules.

## Writing CGs

When writing CGs, you can use variables as almost any part of a CG. This includes:

## Example

We have already seen many examples of variables being used in writing CGs.

Here is one more:

```// Type hierarchy
Entity > Animal, Act.
Animal > Cat, Dog.

// Catalog of instances
Act = Talk, Bark, Kick.
Cat = Garfield.
Dog = Odie.

// "A cat is talking to itself."
// Note how the same variable, "C" is used as the referent of
// both Cat concepts.  This means that they are coreferents.
gr(graph1, [Cat: C]<-AGNT-[Act: Talk]-RCPT->[Cat: C]).

// "Odie is barking at Garfield".
gr(graph2, [Dog: Odie]<-AGNT-[Act: Bark]-RCPT->[Cat: Garfield]).

// "Garfield is kicking Odie"
gr(graph3, [Cat: Garfield]<-AGNT-[Act: Kick]-PTNT->[Dog: Odie]).

// Rule: Animal A1 acts on Animal A2 with relation R
acts_on(A1, R, A2, L) :- gr(L, G), subsume([Animal: A1]<-AGNT-[Act]-R->[Animal: A2], G).
```

## Usage

If we now ask the following query:

`?- acts_on(Garfield, PTNT, Odie, L).`

`{L = graph3}`

This is because in graph3, Garfield acts on Odie with the relation PTNT.

Similarly, if we ask the following query:

`?- acts_on(Odie, RCPT, Garfield, L).`

`{L = graph2}`

This is because in graph2, Odie acts on Garfield with the relation RCPT.

## Summary

Thus variables are pieces of the computer's memory which can hold values, and which we have given a name.

Variables can be free or bound. A free variable does not have a value, whereas a bound variable does.

Variables must follow three simply syntax rules, or they are not variables.

You can use variables to represent almost any part of a CG.

PrevLite: 6.2 Structures, relationships and terms
NextLite: 6.4 Facts

Prev: 6.2 Structures, relationships and terms
Up: 6 Prolog for CG users
Next: 6.4 Facts