# Lecture 20: Inductive definitions

• We started by briefly reviewing hashing; I have added this discussion to the previous lecture's notes

• Inductively defined sets
• BNF notation
• examples: lists, trees, $$\mathbb{N}$$, logical formulae
• Strings ($$Σ^*$$)
• Inductively defined functions
• examples: length, concatenation

• Review Exercises:
• Give inductive definitions for the following sets: $$\mathbb{N}$$; the set of strings with alphabet $$Σ$$; the set of binary trees; the set of arithmetic expressions formed using addition, multiplication, exponentiation
• Give inductive definitions of the length of a string, the concatenation of two strings, the reverse of a string, the maximum element of a list of integers, the sum of two natural numbers, the product of two natural numbers, etc.
• Let $$B$$ be given by $b \in B ::= Z \mid D~b \mid SD~b$ Let $$f : B → \mathbb{N}$$ be given by $$f(Z) ::= 0$$, $$f(D~b) ::= 2f(b)$$ and $$f(SD~b) ::= 2f(b) + 1$$. List several elements of $$B$$. Compute $$f(SD~(D~(SD~Z)))$$. Is $$f$$ injective? Surjective?

## 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.

Examples:

• the set $$\mathbb{N}$$ of natural numbers is the set of elements defined by the following rules:
1. $$Z \in \mathbb{N}$$
2. If $$n \in \mathbb{N}$$ then $$Sn \in \mathbb{N}$$.

thus the elements of $$\mathbb{N}$$ are $$\{Z, SZ, SSZ, SSSZ, \dots\}$$. $$S$$ stands for successor. You can then define $$1$$ as $$SZ$$, $$2$$ as $$SSZ$$, and so on.

• the set $$\Sigma^{*}$$ of strings with characters in $$\Sigma$$ is defined by
1. $$\epsilon \in \Sigma^*$$
2. If $$a \in \Sigma$$ and $$x \in \Sigma^{*}$$ then $$xa \in \Sigma^*$$.

thus if $$\Sigma = \{0,1\}$$, then the elements of $$\Sigma^*$$ are $$\{ε, ε0, ε1, ε00, ε01, \dots, ε1010101, \dots\}$$. we usually leave off the $$ε$$ at the beginning of strings of length 1 or more.

• the set $$T$$ of binary trees with integers in the nodes is given by the rules
1. the empty tree (, written $$nil$$) is a tree
2. if $$t_1$$ and $$t_2$$ are trees, then , written $$node(a,t_1,t_2)$$) is a tree.

thus the elements of $$T$$ are things like the picture to the right (click for tex), which might be written textually as $$node(3,node(0,nil,nil),node(1,node(2,nil,nil),nil))$$

## BNF

Compact way of writing down inductively defined sets: BNF (Backus Naur Form)

Only the name of the set and the rules are written down; they are separated by a "::=", and the rules are separated by vertical bar ($$|$$).

Examples (from above):

• $$n \in \mathbb{N} ::= 0 \mid Sn$$

• $$x \in \Sigma^* ::= \epsilon \mid xa$$ where $$a \in \Sigma$$

• $$t \in T ::= nil \mid node(a,t_1,t_2)$$ where $$a \in Z$$

• (basic mathematical expresssions) \begin{aligned}e \in E &::= n \mid e_1 + e_2 \mid e_1 * e_2 \mid - e \mid e_1 / e_2 \\ n \in \mathbb{Z}\end{aligned}

Here, the variables to the left of the $$\in$$ indicate metavariables. When the same characters appear in the rules on the right-hand side of the $$::=$$, they indicate an arbitrary element of the set being defined. For example, the $$e_1$$ and $$e_2$$ in the $$e_1 + e_2$$ rule could be arbitrary elements of the set $$E$$, but $$+$$ is just the symbol $$+$$.

## Inductively defined functions

If $$X$$ is an inductively defined set, you can define a function from $$X$$ to $$Y$$ by defining the function on each of the types of elements of $$X$$; i.e. for each of the rules. In the inductive rules (i.e. the ones containing the metavariable being defined), you can assume the function is already defined on the subterms.

Examples:

• $$add2 : \mathbb{N} → \mathbb{N}$$ is given by $$add2(0) ::= SS0$$ and $$add2 (Sn) ::= S(add2(n))$$.

• $$plus : \mathbb{N} \times \mathbb{N} → \mathbb{N}$$ given by $$plus (0,n) ::= n$$ and $$plus (Sn, n') ::= S(plus(n,n'))$$. Note that we don't need to use induction on both of the inputs.

• $$len : Σ^* → \mathbb{N}$$ is given by $$len(ε) ::= 0$$ and $$len(xa) ::= 1 + len(x)$$.

• $$cat : Σ^* \times Σ^* → Σ^*$$ is given by $$cat(ε,ε) ::= ε$$, $$cat(xa,ε) ::= xa$$, $$cat(ε,xa) ::= xa$$ and $$cat(xa,yb) ::= cat(xa,y)b$$.