- A set S is said to be inductively specified if it is the smallest set such that
- S contains a certain set S0.
- If certain elements are in S, then certain other elements are in S.
For each n > 0, let Let Sn be the set of elements in Sn-1 together with the elements that must be in S by applying 2 to elements in Sn-1.
Then Sn is a subset of S for n = 0, 1, ..., and S is the union of S0, S1, ... because this union is the smallest set satisfying conditions 1 and 2
Things can be proved about inductively defined sets by Mathematical Induction.
- Principle of Mathematical Induction
Let P(x) be a proposition about x, for x in an inductively defined set S. Then P(x) will be true for all x in S if
- (base case) P(x) is true for x in S0, the elements in S by rule 1, and
- (inductive case) If elements in B are in S whenever elements in A are in S by rule 2, then P(x) is true for x in B whenever P(x) is true for all x in A.
Proof.
Suppose these conditions hold but P(z) is false for some z in S.
There is a smallest n such that P(z) is false for some z in Sn.
Let z be such an element for this smallest n.Then n > 0 because P(x) is true for all x in S0 (base case).
By definition of n, P(x) is true for all x in Sn-1.
But then P(x) is true for all x in Sn (inductive case).
But z is in Sn and P(z) is false.
This contradiction implies P(x) must be true for all x in S.
Examples.
[list-of-numbers] ::= () | ([number] . [list-of-numbers])Then, with the above notation,
S0 = {()},We can use the inductive definition of list-of-numbers to define a recognizer,
S1 = {()} union {(n . ()) | n is a number}
S2 = {()} union {(n . ()) | n is a number} union {(n . (m . ())) | n, m are numbers}}
...
list-of-numbers?
(define list-of-numbers?
(lambda (x)
(if (null? x)
;; base case
#t
(if (pair? x)
;; inductive case
(and (number? (car x))
(list-of-numbers? (cdr x)))
#f))))
Then,
(list-of-numbers? datum) => #t if datum is in list-of-numbers,
=> #f, otherwise
because, by mathematical induction, the result is #t in the base case
where datum = () and in the inductive case where
datum = (n . lon), when n is
an integer and lon is in list-of-numbers, and the result is #f in all
other cases.
The literal representation contains only symbols and parentheses.[s-list] ::= () | ([symbol-expression] . [s-list]) [symbol-expression] ::= symbol | [s-list]
s-list?
(define s-list?
(lambda (x)
(or (null? x)
(and (pair? x)
(or (symbol? (car x))
(s-list? (car x)))
(s-list? (cdr x))))))
This procedure is correct by Mathematical Induction.(s-list? x) returns #t if x = () or if x is a pair
whose cdr is a symbol or an s-list and whose cdr is an s-list,
and #f otherwise. According to the BNF, this is exactly what has
to be checked.
E.g.,[bintree] ::= [number] | ([symbol] [bintree] [bintree])
1 (a 1 2) (a (b 1 (c 2)) 3) -- not a binary-tree (a (b 1 (c 2 3)) 4)
(binary-tree? x) => #t if x is a binary-tree, #f otherwise.
(define binary-tree?
(lambda (x)
(if (number? x)
#t
(and (list? x)
(= (length x) 3)
(symbol? (car x))
(binary-tree? (cadr x))
(binary-tree? (caddr x))))))
list? and length are built in.
(leaf-sum bt) => the sum of the leaf values, if bt is a
binary-tree.
(define leaf-sum
(lambda (tree)
(if (number? tree)
tree
(+ (leaf-sum (cadr tree)) (leaf-sum (caddr tree))))))
Trace of an example:
> (trace leaf-sum) (leaf-sum) > (leaf-sum '(a (b 1 (c 2 3)) 4)) |(leaf-sum (a (b 1 (c 2 3)) 4)) | (leaf-sum (b 1 (c 2 3))) | |(leaf-sum 1) | |1 | |(leaf-sum (c 2 3)) | | (leaf-sum 2) | | 2 | | (leaf-sum 3) | | 3 | |5 | 6 | (leaf-sum 4) | 4 |10 10 >
[expression] ::= [identifier]
| ([lambda]([identifier]) [expression])
| ([expression] [expression])
where [identifier] is any symbol but lambda.A straight-forward expression recognizer:
(define expression?
(lambda (x)
(cond ((and (symbol? x)
(not (eqv? x 'lambda)))
#t)
((and (pair? x)
(eqv? (car x) 'lambda)
(list? x)
(= (length x) 3)
(let ((formals (cadr x))
(body (caddr x)))
(and (pair? formals)
(symbol? (car formals))
(null? (cdr formals))
(expression? body))))
#t)
((and (list? x)
(= (length x) 2)
(expression? (car x))
(expression? (cadr x)))
#t)
(else #f))))
set! special form has been added.