0

I am reading the book Types and Programming Languages by Benjamin C. Pierce. the author talk about deriving syntax for a language in section 3. Section 3.2.3 has the following content.

For each natural number i, define a set S1 as follows

S0 = Empty Set
S(i+1) = {true, false, 0} Union {succ t1, pred t1, iszero t1 | t1 in Si}
         Union {if t1 then t2 else t3 | t1, t2, t3 in Si}
Finally, let
S = Union of Si (starting with i = 0) 

Then the author says that from this we can derive that S0 is empty.S1 contains just the constants. S2 contains the constants plus the phrases that can be built with constants and just one succ, pred, iszero or if. What does that mean? How do you derive S2

Ashwin
  • 12,691
  • 31
  • 118
  • 190

1 Answers1

0
(1) S_0 = ∅

(2) S_{i+1} = {true, false, 0} 
              ∪ {succ t1, pred t1, iszero t1 | t1 in Si}
              ∪ {if t1 then t2 else t3 | t1, t2, t3 in S_i}

(3) S = ⋃ {S_i} (starting with i = 0)$

This is a way of describing the language inductively using a set builder notation. Each larger indexed S will contain sub-terms of smaller indexed S terms. Let's work it out to give you an idea.

We know that S_0 is an empty set so it doesn't contain any terms. S_1 is a set of constants.

S_1 = {true, false, 0}

By putting i = 1 in equation (2) we can generate S_2

S_2 = {true, false, 0} 
    ∪ {succ t1, pred t1, iszero t1 | t1 in S_1}
    ∪ {if t1 then t2 else t3 | t1, t2, t3 in S_1}

So S_2 would be a set containing terms:

S_2 = { true, false, 0, 
        succ true, pred true, iszero true, succ false, pred false, 
        iszero false,  succ zero, pred zero, iszero zero, 
        if true then true else true, if true then true else t3 false, 
        if true then true else t3 zero, if true then false else t3 true, 
        ..., if false then zero else zero, if zero then zero else zero}

Notice, the sub-terms of terms in S_2 can only be terms from S_1 i.e. constants. Hence there will only be one occurrence of succ, pred, iszero and if.

S_3 will contain the terms such that terms of S_2 and S_1 are its sub-terms. So it can have 1 or 2 occurrences of succ, pred, iszero and if.

Essentially you can think of t1 as a hole in the S_{i+1} template where you can plug in S_{i} terms. Finally, the complete language S is the union of all such S_i terms.

Apoorv
  • 259
  • 4
  • 14
  • Right! but the author says `S2 contains the constants plus the phrases that can be built with constants and just one succ, pred, iszero or if.` Why **..just one succ, pred, iszero or if** ? – Ashwin Nov 08 '19 at 01:37
  • That’s because subterms of terms of S_2 can only belong to S_1 i.e. constants. so S_2 will have only one succ, pred iszero or if, as you can see from the S_2 set I have explicitly written down. – Apoorv Nov 08 '19 at 04:04