Inductive specifications
Inductive specifications and recursive programming
1. 0 E S
2. whenever x E S, x + 3 E S
0 3 6 9 12 ...
Inductive specification of "multiples of 3"
In general:
1. Some specific value is in S
2. If certain values are in S,
then certain other values are in S
Inductive specification of data types:
"List of Numbers"
1. ()
2. if L is a "ListofNumbers" and n is a number,
then the pair (n.L) is a "ListofNumbers"
()
(14)
(3 14)
...
BNF for inductive specifications
- especially useful to specify programming language
syntax --> compilers.
- theory of computing
::== ()
::== ( . )
OR
::== () | ( . )
terminals, Syntactic categories
more Shortcuts:
Kleene Star
::== ({}*) // 0 or more numbers
Kleene Plus
::== ({}+) // 1 or more numbers
Prove that a given data value is syntactic category X
Derivation:
(14.()) is a listofnumbers
=> (.)
=> (14.)
=> (14.())
Another way equally ok.
=> (.)
=> (.())
=> (14.())
Scheme example (language specification example):
using BNF
datum is an literal data representation
::== ({}*)
::== ({}+ . )
::== #({}*)
::== |
| |
| |
|
BNF rules are context free
programming languages are not
Prose accompanying BNF
provides context in language specification
Compilers use limited context senstive BNF rules
Recursive specification of programs
1. if n is 0, e(n,x) = 1
2. if n > 0, assume e(n-1,x) is known/well defined
then
e(n,x) = x * e(n-1,x)
(define e
(lambda (n x)
(if (zero? n)
1
(* x (e (- n 1) x)))))
When defining a program based on structural
induction, the structure of the program should
be patterned after the structure of the data.
(define listofnumbers?
::== () | ( . )
(define list-of-numbers?
(lambda (lst)
(if (null? lst)
#t
(if (pair? lst)
(if (number? (car lst))
(list-of-numbers? (cdr lst))
#f)
#f))))
Sushil Louis
Last modified: Mon Sep 13 09:46:55 PDT 1999