# SP18:Lecture 19 Inductive sets

We discussed how to build sets inductively, and introduced BNF notation. We also discussed inductively defined functions.

# Inductively defined sets

Over the next several weeks, we will be working with strings and machines that process strings. We could prove things about those machines by doing induction on the length of the strings: if was a property of a string , then we could prove "[[for all]] strings x, by doing an inductive proof of the statement "for all , if is a string of length , then P(x) holds". Since every string has a finite length, this would allow us to conclude for all .

This approach works well enough, but there are a few rough edges:

• we haven't defined strings, or proved that every string has a length.
• using induction forces us to state everything as a property of numbers, when it is more natural to think of our claims as properties of strings

Instead, we will use this opportunity to introduce a generalization of weak induction: structural induction. Structural induction gives us a technique for defining new objects by combining smaller objects, and for defining functions and proving facts about those objects and functions.

## Inductively defined sets

An inductively defined set is a set where the elements are constructed by a finite number of applications of a given set of rules for creating more complicated objects from simpler ones.

For example, the set of natural numbers is the set of elements defined inductively by the following rules:

• Rule 1: (We think of Z as standing for "zero")
• Rule 2: If then (S stands for "successor"; you can think of [/itex]S~n[/itex] it as representing "1 + n", although we will define + in terms of S instead of the other way around).

Thus the elements of are . You can then define as , as , and so on.

### BNF Notation

Backus-Naur form (or BNF) is a convenient syntax for writing down inductively defined sets.

BNF definitions give the name of the set being defined, and the list of rules defining the set in a compact form, separated by a vertical bar ().

A BNF definition looks like this:

The rules define the various ways of constructing objects in the set. Any uses of the variables on the left hand-side of any of the equations, should be replaced by already-constructed elements of the corresponding set.

The symbol is simply used to indicate that the definition is a BNF definition.

For example, the rules defining the natural numbers say that and for any . These would be written in BNF as follows:

Definition: natural number

The second rule says that you can form a new natural number by prepending an S onto an existing natural number.

### Example: strings

Definition: Σ*
The set of strings with characters in the alphabet is defined inductively by:

represents the empty string, while represents a string with 1 or more characters, starting with the shorter string and ending in the character .

For example, if , then

To add further detail, we can show that as follows:

By convention, we leave off the leading "ε" from non-empty strings. For example, we would write "001" instead of "ε001".

### Example: trees

The set of binary trees with integer nodes is defined inductively by

• Rule 1: The empty tree is in
• Rule 2: If and are trees, then the tree with value , left child and right child is in T.

We often think of trees pictorially; in this case we might give a BNF definition of as follows:

However, it is also useful to write trees symbolically; in this case we might define as follows:

Using the first definition, the following is an example tree:

Using the second definition, we would write this as

### Example: expressions

Mathematical expressions of various kinds are often defined inductively. For example, one might define a set of arithmetic expressions by the rules

Here, we are treating "+", "*", and "-" as uninterpreted symbols. For example, is an expression, formed from the substructures and using the "+" rule. Most programming languages have a similar inductive definition, allowing us to formally model programs.

## Inductively defined functions

Given an inductively defined set , we know that every element of is formed using one of the rules defining . Therefore, if we want to define a function with domain , it suffices to describe how to evaluate it for objects formed with each rule.

Moreover, we can think of forming the value of at the same time that we show that ; this means that for all of the substructures of (i.e. the "smaller" elements of that were used to construct ), we can assume that has already been defined, and use it in the definition of .

A function defined in this way is called an inductively defined function or a recursive function.

### Example: length

For example, we could define a function for computing the length of a string inductively:

Definition: len
Let be given inductively by and .

It is reasonable to use when defining because is a substructure of ; we must have already proved that before we prove that

We can then compute

When giving an inductive definition of a function , it is important to only use in the definition of if is a substructure of .

For example, the "function" given inductively by and is not well-defined. Indeed, try computing ; any choice of output satisfies the defining equations.

As a more insidious example, consider given by and . Here, it is perfectly reasonable to use in the definition of because is a substructure of , but is not ok, because is not necessarily a substructure of and we can only apply to substructures of in the definition of .

### Example: concatenation

We often want to concatenate two strings and : put one of them at the end of another. is not a string according to the inductive definition of Σ^*, but we can define a function that produces that string (which we will then abbreviate as ).

There are at least two options. We could use induction on the second argument:

Definition: concatenation
The function is given inductively by and .

We could also define inductively on the pair of arguments:

Definition: concatenation
The function is given inductively by , , , and .

Here, we have used in the definition of ; this is ok because can be considered a substructure of .

If and are substructures of and respectively, then , and can all be considered substructures of : they must all have been shown to be in before we could show that .

We might be tempted to use induction on the first argument, but we fail: how do we define in terms of and ? You might propose something like , but this is undefined for a number of reasons: (not ), so is undefined. Moreover, if we could just stick a string at the end of another string (in this case, appending to ), we wouldn't need to define ! Without first defining cat, we can only form strings using the two rules given in the inductive definition of .

We can inductively define familiar operations like addition and multiplication.

There are several alternatives: we could induct on the second argument, on the first argument, or on both arguments: