scrambled
Book

Chapter 2 inductive definitions

Modified just now

Inductive definitions cannot be dispensed in the study of programming languages.

  • TLDR

    • Basic framework of inductive definitions.

    • Examples of inductive definitions.

    • Inductive definition = A set of Rules for deriving Judgments/Assertions of a variety of forms.

      • Judgment: A statement about one or more Abstract binding tree (ABT)s of some Sort.
      • The rules specify necessary and sufficient conditions for the validity of a judgment. -> Fully determine its meaning.

Judgments

  • TLDR: Judgment/Assertion about an Abstract binding tree (ABT).

  • Many forms of judgments:

    n natn is a natural numbern1+n2=nn is the sum of n1 and n2τ typeτ is a typee:τexpression e has type τevexpression e has value v\begin{aligned} n\ \text{nat} & & & \text{$n$ is a natural number}\\ n_1 + n_2 = n & & & \text{$n$ is the sum of $n_1$ and $n_2$}\\ \tau\ \text{type} & & & \text{$\tau$ is a type}\\ e : \tau & & & \text{expression $e$ has type $\tau$}\\ e \Downarrow v & & & \text{expression $e$ has value $v$} \end{aligned}

    • Personal notes

      • The notation e:τe : \tau is familiar.
      • eve \Downarrow v is maybe similar to Big-step/Natural operational semantics.
      • n1+n2=nn_1 + n_2 = n is not a boolean (Boolean Blindness).
      • n natn\ \text{nat} and τ type\tau\ \text{type} have the same structure but totally different meaning.
  • Judgment: A statement that one or more Abstract binding tree (ABT)s have a property (unary relation) or stand in some relation to one another.

    • The property/relation \to Judgment form.
    • The judgment that an object or objects have that property or stand in that relation \to An Instance of an inference rule.
  • Judgment form = Predicate.

    The objects constituting an instance are its subjects. \to Judgment form instance's subject.

  • The Judgment asserting that a Judgment form JJ holds of the Abstract binding tree (ABT) aa is written as a Ja\ J or J aJ\ a. \to Instance of the judgment form.

  • A general Judgment form can be written as J J\ - or  J-\ J.

  • When it is not important to stress the subject of the judgment, we write JJ to stand for an unspecified judgment, that is, an instance of some judgment form.

Inference rules

  • An Inductive definition of a Judgment form consists of a collection of Rules of the form:

    J1 J2  JkJ \displaystyle\frac{J_1\ J_2\ \dots\ J_k}{J}

    in which JJ and J1J_1, J2J_2, \dots, JkJ_k are all judgment of the form being defined.

    • The judgments above the line: Premises.

    • The judgment below the line: Conclusion.

    • No Premise \to Axiom.

      Otherwise, a Proper rule.

  • If all premises hold \to The conclusion hold.

    The reverse does not hold.

  • Example - Inductive definition of the judgment form  nat-\ \text{nat}

zero nat \displaystyle\frac{}{\text{zero}\ \text{nat}}

a natsucc(a) nat\displaystyle\frac{a\ \text{nat}}{\text{succ}(a)\ \text{nat}}

  • Example - Inductive definition of the judgment form  tree-\ \text{tree}

empty tree\displaystyle\frac{}{\text{empty}\ \text{tree}}

a treenode(a1;a2) tree\displaystyle \frac{a\ \text{tree}}{\text{node}(a_1;a_2)\ \text{tree}}

  • Example - Inductive definition of the judgment form  is -\ \text{is}\ -

zero is zero\displaystyle\frac{}{\text{zero}\ \text{is}\ \text{zero}}

a is bsucc(a) is succ(b)\displaystyle\frac{a\ \text{is}\ b}{\text{succ}(a)\ \text{is}\ \text{succ}(b)}

\Rightarrow A notational convention for specifying an infinite family of Rules by a finite number of patterns, or Rule schemes.

  • A collection of Rules is considered to define the strongest Judgment form that is closed under, or respects, those rules.

    • Closed under the rules = The rules are sufficient to show the validity of a judgment: JJ holds if there is a way to obtain it using the given rules.

    • Strongest judgment form = The rules are also necessary: JJ holds only if there is a way to obtain it by applying the rules.

      \to The sufficiency of the rules means that we may show that JJ holds by deriving it by composing rules.

      \to Their necessity means that we may reason about it using rule induction.

Derivations

  • To prove an inductively defined Judgment, it's sufficient to show a Derivation of it.

  • Derivation of a Judgment: A finite composition of rules, starting with axioms and ending with that judgment.

    \to A Derivation of JJ is evidence for the validity of an inductively defined judgment JJ.

  • Derivations are visualized as trees with the conclusion at the bottom and with the children of a node corresponding to a rule appearing above it as evidence for the premises of that rule.

    Formalization:

    • An inference rule: J1  JkJ\displaystyle\frac{J_1\ \dots\ J_k}{J}.

    • Derivations of its premises: 1,,k\triangledown_1, \dots, \triangledown_k.

      \to 1  kJ\displaystyle\frac{\triangledown_1\ \dots\ \triangledown_k}{J} is a derivation of JJ.

  • Two main methods for finding derivations:

    • Forward chaining/Bottom-up construction.

      Start with the axioms and work towards the desired conclusion.

      Idea: Forward chaining search maintains a set of derivable judgments and continually extends this set by adding to it the conclusion of any rules all of whose premises are in that set.

      Assume that all rules are considered at every stag:

      • Forward chaining would eventually find a derivation of any derivable judgment. \to Complete.

      • Forward chaining cannot decide when to stop & conclude that a judgment is not derivable.

        \to Need to understand the global properties of the rules.

        Do not take into account the end goal when deciding how to proceed at each step. \to Undirected.

    • Backward chaining/Top-down construction.

      Start with the desired conclusion and work backwards towards the axioms. \to Goal-directed.

      1. Initialization: The process begins with a queue containing only the final goal (the judgment to be derived).

      2. The loop:

        • A goal is removed from the queue.
        • The system searches for rules where the conclusion matches that goal.
        • The premises of those matching rules are added to the queue as new sub-goals.
      3. Success: The process terminates successfully when the queue is empty (all goals and sub-goals are satisfied).

        Same issues as Forward chaining.

Rule induction

  • An Inductive definition specifies the strongest Judgment form closed under a collection of rules.

    \to We can reason about them using Rule induction.

  • The principle of Rule induction states that to show that a property a a Pa\ \mathcal{P} holds whenever a a Ja\ J is Derivable, it is enough to show that P\mathcal{P} is closed under, or respects, the rules defining the judgment form JJ.

    More precisely, the property P\mathcal{P} respects the rule:

    a1 J  ak JJ \displaystyle\frac{a_1\ J \ \dots \ a_k\ J}{J}

    if P(a)\mathcal{P}(a) holds whenever P(a1),,P(ak)\mathcal{P}(a_1 ), \dots , P(a_k) do.

    \to The assumptions P(a1),,P(ak)\mathcal{P}(a_1), \dots , P(a_k) are called the Inductive hypothesis, and P(a)\mathcal{P}(a) is called the Inductive conclusion of the inference.

  • To show that P a\mathcal{P}\ a whenever a nata\ \text{nat}, it's sufficient to show that:

    • P(zero)\mathcal{P}(\text{zero}).

    • For every aa, if P(a)\mathcal{P}(a) then P(succ a)\mathcal{P}(\text{succ}\ a).

      \to The principle of Mathematical induction.

  • To show that P a\mathcal{P}\ a whenever a treea\ \text{tree}, it's sufficient to show that:

    • P(empty)\mathcal{P}(\text{empty}).

    • For every a1a_1 and a2a_2, if P(a1)\mathcal{P}(a_1) and P(a2)\mathcal{P}(a_2) then P(node(a1;a2))\mathcal{P}(\text{node}(a_1; a_2)).

      \to The principle of Tree induction.

Iterated and simultaneous inductive definition

  • Iterated inductive definition: One inductive definition builds on top of another.

    \to The premises of a rule J1  JkJ\displaystyle\frac{J_1\ \dots\ J_k}{J} may be instances of either a previously defined judgment form, or the judgment form being defined.

    Example: The  list-\ \text{list} Judgment form.

    nil list \displaystyle\frac{}{\text{nil}\ \text{list}}

a natb listcons(a;b) list\displaystyle\frac{a\ \text{nat} \quad b\ \text{list}}{\text{cons}(a; b)\ \text{list}}

  • Simultaneous inductive definition: A set of rules for deriving instances of several different judgment forms, any of which may appear as the premise of any rule.

    \to Those judgment forms are defined at once.

    Example: The  even-\ \text{even} and  odd-\ \text{odd} Judgment forms.

    zero even \displaystyle\frac{}{\text{zero}\ \text{even}}

a evensucc(a) odd\displaystyle\frac{a\ \text{even}}{\text{succ}(a)\ \text{odd}}

a oddsucc(a) even\displaystyle\frac{a\ \text{odd}}{\text{succ}(a)\ \text{even}}

\to To show simultaneously that P(a)\mathcal{P}(a) whenever a evena\ \text{even} and Q(b)\mathcal{Q}(b) whenever bb odd, it is enough to show the following:

  1. P(zero)\mathcal{P}(\text{zero}). 2. For every aa, if P(a)\mathcal{P}(a) then Q(succ(a))\mathcal{Q}(\text{succ}(a)). 3. For every aa, if Q(a)\mathcal{Q}(a) then P(succ(a))\mathcal{P}(\text{succ}(a)).

Defining functions by rules

  • Functions can be defined by:

    1. Providing the definition of its graph relating inputs to outputs.
    2. Then showing that the relation uniquely determines the outputs for given inputs.
  • Example: The function sum(a;b;c)\text{sum}(a;b;c) with the intended meaning of cc being the sum of aa and bb.

    b natsum(zero;b;b) \displaystyle\frac{b\ \text{nat}}{\text{sum}(\text{zero};b;b)}

    sum(a;b;c)sum(succ(a);b;succ(c)) \displaystyle\frac{\text{sum}(a;b;c)}{\text{sum}(\text{succ}(a);b;\text{succ}(c))}

Theorem. For every a nata\ \text{nat} and b natb\ \text{nat}, there exists a unique c natc\ \text{nat} such that sum(a;b;c)\text{sum}(a;b;c).

Proof

Two parts:

  • Existence: If a nata\ \text{nat} and b natb\ \text{nat}, then there exists c natc\ \text{nat} such that sum(a;b;c)\text{sum}(a;b;c).
  • Uniqueness: If sum(a;b;c)\text{sum}(a;b;c), and sum(a;b;c)\text{sum}(a;b;c'), then cc is cc'.

Exercises

Exercise 2.1

Problem statement. Give an inductive definition of the judgment max(m;n;p)\text{max}(m;n;p), where m natm\ \text{nat}, n natn\ \text{nat}, and p natp\ \text{nat}, with the meaning that pp is the larger of mm and nn. Prove that every mm and nn are related to a unique pp by this judgment.

Solution
  • Definition

n natmax(zero;n;n)(1)\displaystyle\frac{n\ \text{nat}}{\max(\text{zero};n;n)} \quad (1)

n natmax(n;zero;n)(2)\displaystyle\frac{n\ \text{nat}}{\max(n;\text{zero};n)}\quad (2)

max(m;n;p)max(succ(m);succ(n);succ(p))(3)\displaystyle\frac{\max(m;n;p)}{\max(\text{succ}(m); \text{succ}(n); \text{succ}(p))} \quad (3)

  • The judgment defines a function on mm and nn

    • Existence ()(*): For every m natm\ \text{nat} and n natn\ \text{nat}, there exists a pp such that max(m;n;p)\max(m;n;p).

      • If m=zerom = \text{zero} or n=zeron = \text{zero}, applying (1)(1) or (2)(2) suffices to show ()(*) holds in these cases.
      • If m=succ(m)m = \text{succ}(m') and n=succ(n)n = \text{succ}(n'), then with (3)(3), it's sufficient to show that ()(*) holds in the case m=mm = m' and n=nn = n'. By the induction hypothesis, this is trivial.
    • Uniqueness ()(**): For every m natm\ \text{nat} and n natn\ \text{nat}, if there exists pp and pp' such that max(m;n;p)\max(m;n;p) and max(m;n;p)\max(m;n;p'), then p=pp = p'.

      • If m=zerom = \text{zero} or n=zeron = \text{zero}, applying (1)(1) or (2)(2) is the only way to obtain such a pp. Therefore, ()(**) holds in these cases.
      • If m=succ(m)m = \text{succ}(m') and n=succ(n)n = \text{succ}(n'), then applying (3)(3) for the case m=mm = m' and n=nn = n' is the only way to obtain such a pp. By the induction hypothesis, this is trivial.

Exercise 2.2

Problem statement. Consider the following rules, which define the judgment hgt(t;n)\text{hgt}(t;n) stating that the binary tree tt has height nn.

hgt(empty;zero)\text{hgt}(\text{empty};\text{zero})

hgt(t1;n1)hgt(t2;n2)max(n1;n2;n)hgt(node(t1;t2);succ(n))\displaystyle\frac{\text{hgt}(t_1;n_1) \quad \text{hgt}(t_2;n_2) \quad \max(n_1;n_2;n)}{\text{hgt}(\text{node}(t_1;t_2); \text{succ}(n))}

Prove that the judgment hgt\text{hgt} defines a function from trees to natural numbers.

Solution
  • Existence ()(*): For every t treet\ \text{tree}, there exists hh such that hgt(t;h)\text{hgt}(t;h).
  • For t=emptyt = \text{empty}, this is trivial that n=zeron = \text{zero}, so ()(*) holds.
  • For t=node(t1;t2)t = \text{node}(t_1;t_2) such that t1 treet_1\ \text{tree} and t2 treet_2\ \text{tree}.

By the induction hypothesis, there exists n1 natn_1\ \text{nat} and n2 natn_2\ \text{nat} such that hgt(t1;n1)\text{hgt}(t_1;n_1) and hgt(t2;n2)\text{hgt}(t_2;n_2).

By Exercise 2.1, there exists n natn\ \text{nat} such that max(n1;n2;n)\max(n_1;n_2;n).

By definition, hgt(t;succ(n))\text{hgt}(t;\text{succ}(n)).

  • Uniqueness ()(**): For every t treet\ \text{tree}, if hgt(t;h)\text{hgt}(t;h) and hgt(t;h)\text{hgt}(t;h'), then h=hh = h'.

Similar to ()(*).

Exercise 2.3

Problem statement. Given an inductive definition of ordered variadic trees whose nodes have a finite, but variable, number of children with a specified left-to-right ordering among them. Your solution should consist of a simultaneous definition of two judgments:

  • t treet\ \text{tree}, stating that tt is a variadic tree.
  • f forestf\ \text{forest}, stating that ff is a "forest" (finite sequence) of variadic trees.
Solution

empty tree\displaystyle\frac{}{\text{empty}\ \text{tree}}

nil forest\displaystyle\frac{}{\text{nil}\ \text{forest}}

t treef forestcons(t;f) forest\displaystyle\frac{t\ \text{tree} \quad f\ \text{forest}}{\text{cons}(t;f)\ \text{forest}}

f forestnode(f) tree\displaystyle\frac{f\ \text{forest}}{\text{node}(f)\ \text{tree}}

Exercise 2.4

Problem statement. Give an inductive definition of the height of a variadic tree of the kind defined in Exercise 2.3.

  • Your definition should make use of an auxiliary judgment defining the height of a forest of variadic trees.
  • It will be defined simultaneously with the height of a variadic tree.

Show that the two judgments so defined each define a function.

Solution (incomplete)
  • Definition

tree_hgt(empty;zero)\displaystyle\frac{}{\text{tree\_hgt}(\text{empty};\text{zero})}

forest_hgt(nil;zero)\displaystyle\frac{}{\text{forest\_hgt}(\text{nil};\text{zero})}

tree_hgt(t;n)forest_hgt(f;m)forest_hgt(cons(t;f);max(n;m))\displaystyle\frac{\text{tree\_hgt}(t;n) \quad \text{forest\_hgt}(f;m)}{\text{forest\_hgt}(\text{cons}(t;f);\max(n;m))}

forest_hgt(f;n)tree_hgt(node(f);succ(n))\displaystyle\frac{\text{forest\_hgt}(f;n)}{\text{tree\_hgt}(\text{node}(f);\text{succ}(n))}

Exercise 2.5

Problem statement. Give an inductive definition of the binary natural numbers, which are either:

  • Zero
  • Twice a binary number
  • One more than twice a binary number

The size of such a representation is logarithmic, rather than linear, in the natural number it represents.

Solution

zero bin\displaystyle\frac{}{\text{zero}\ \text{bin}}

b bintwice(b) bin\displaystyle\frac{b\ \text{bin}}{\text{twice}(b)\ \text{bin}}

b bintwice_plus_one(b) bin\displaystyle\frac{b\ \text{bin}}{\text{twice\_plus\_one}(b)\ \text{bin}}

Exercise 2.6

Problem statement. Give an inductive definition of addition of binary natural numbers as defined in Exercise 2.5.

  • Hint: Proceed by analyzing both arguments to the addition, and make use of an auxiliary function to compute the successor of a binary number.
  • Hint: Alternatively, define both the sum and the sum-plus-one of two binary numbers mutually recursively.
Solution (incomplete)
  • Definition

sum(b1;b2;b3)sum(b2;b1;b3)\displaystyle\frac{\text{sum}(b_1;b_2;b_3)}{\text{sum}(b_2;b_1;b_3)}

sum_plus_one(b1;b2;b3)sum_plus_one(b2;b1;b3)\displaystyle\frac{\text{sum\_plus\_one}(b_1;b_2;b_3)}{\text{sum\_plus\_one}(b_2;b_1;b_3)}

sum(zero;zero;zero)\displaystyle\frac{}{\text{sum}(\text{zero};\text{zero};\text{zero})}

sum_plus_one(zero;zero;twice_plus_one(zero))\displaystyle\frac{}{\text{sum\_plus\_one}(\text{zero};\text{zero};\text{twice\_plus\_one}(\text{zero}))}

sum(b1;b2;b3)sum(twice(b1);twice(b2);twice(b3))\displaystyle\frac{\text{sum}(b_1;b_2;b_3)}{\text{sum}(\text{twice}(b_1);\text{twice}(b_2);\text{twice}(b_3))}

sum(b1;b2;b3)sum(twice_plus_one(b1);twice(b2);twice_plus_one(b3))\displaystyle\frac{\text{sum}(b_1;b_2;b_3)}{\text{sum}(\text{twice\_plus\_one}(b_1);\text{twice}(b_2);\text{twice\_plus\_one}(b_3))}

sum_plus_one(b1;b2;b3)sum(twice_plus_one(b2);twice_plus_one(b1);twice(b3))\displaystyle\frac{\text{sum\_plus\_one}(b_1;b_2;b_3)}{\text{sum}(\text{twice\_plus\_one}(b_2);\text{twice\_plus\_one}(b_1);\text{twice}(b_3))}

sum_plus_one(b1;b2;b3)sum_plus_one(twice_plus_one(b1);twice(b2);twice(b3))\displaystyle\frac{\text{sum\_plus\_one}(b_1;b_2;b_3)}{\text{sum\_plus\_one}(\text{twice\_plus\_one}(b_1);\text{twice}(b_2);\text{twice}(b_3))}

sum(b1;b2;b3)sum_plus_one(twice(b1);twice(b2);twice_plus_one(b3))\displaystyle\frac{\text{sum}(b_1;b_2;b_3)}{\text{sum\_plus\_one}(\text{twice}(b_1);\text{twice}(b_2);\text{twice\_plus\_one}(b_3))}

sum_plus_one(b1;b2;b3)sum_plus_one(twice_plus_one(b1);twice_plus_one(b2);twice_plus_one(b3))\displaystyle\frac{\text{sum\_plus\_one}(b_1;b_2;b_3)}{\text{sum\_plus\_one}(\text{twice\_plus\_one}(b_1);\text{twice\_plus\_one}(b_2);\text{twice\_plus\_one}(b_3))}