270 likes | 390 Views
Inductive Sets of Data. Programming Language Essentials 2nd edition Chapter 1.1 Recursively Specified Data. Inductive Specification. specific value is in the set. if some value is in the set, some other also is. S: smallest set of natural numbers with 0 in S if x in S then x+3 in S
E N D
Inductive Sets of Data • Programming Language Essentials • 2nd edition • Chapter 1.1 Recursively Specified Data
Inductive Specification • specific value is in the set. • if some value is in the set, some other also is. • S: smallest set of natural numbers with • 0 in S • if x in S then x+3 in S • M: multiples of 3 • smallest guarantees uniqueness
list-of-numbers? • L: smallest set of values with • empty list in L • if x in L and n a number, (n . x) in L • (define list-of-numbers? • (lambda (x) • (if (null? x) • #t • (if (number? (car x)) • (list-of-numbers? (cdr x)) • #f • ) ) ) )
list-of-numbers? • L: smallest set of values with • empty list in L • if x in L and n a number, (n . x) in L • (define list-of-numbers? • (lambda (x) • (if (null? x) • #t • (if (and (pair? x) (number? (car x))) • (list-of-numbers? (cdr x)) • #f • ) ) ) )
Backus-Naur Form • l-of-nums: '()' • l-of-nums: '(' 'Number' '.' l-of-nums ')' • grammar • nonterminals l-of-nums • terminals '(' 'Number’ ‘.’ ‘)’ • rules, productions l-of-nums: '()' • context-free • notations differ
Extended Backus-Naur Form • l-of-ns: '()' | '(' 'Number' '.' l-of-ns ')' • l-of-ns: '()' • : '(' 'Number' '.' l-of-ns ')' • l-of-ns: '(' 'Number'* ')' • parentheses for grouping • optional term? • zero or more term* • one or more term+ • separated {<term>}*(,)
Syntactic Derivation • list-of-numbers • => ( Number . list-of-numbers )
Syntactic Derivation • list-of-numbers • => ( Number . list-of-numbers ) • => ( 14 . list-of-numbers )
Syntactic Derivation • list-of-numbers • => ( Number . list-of-numbers ) • => ( 14 . list-of-numbers ) • => ( 14 . () )
Syntactic Derivation • list-of-numbers • => ( Number . list-of-numbers ) • => ( 14 . list-of-numbers ) • => ( 14 . () ) • order of substitution does not matter • done once only terminals remain • need to cheat about quoting terminals
s-list • s-list: '(' symbol-expression* ')' • symbol-expression: 'Symbol' | s-list • (a b c) • (an (((s-list)) (wth () lots) ((of) nests)))
bintree • bintree: 'Number' • | '(' 'Symbol' bintree bintree ')' • 1 • (foo 1 2) • (bar 1 (foo 1 2)) • (baz (bar 1 (foo 1 2)) (biz 4 5)) • search-tree: '()' • | '(' 'Key' search-tree search-tree ')' • needs restriction for key ordering
Scheme Data • list: '(' datum* ')' • dotted: '(' datum+ '.' datum ')' • vector: '#(' datum* ')' • datum: 'Number' | 'Symbol' | 'Boolean' • | 'String' | list | dotted | vector • what's missing?
Sample Derivation • list => ( datum datum datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum ) • => ( #t ( datum+ . datum ) datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum ) • => ( #t ( datum+ . datum ) datum ) • => ( #t ( Symbol . datum ) datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum ) • => ( #t ( datum+ . datum ) datum ) • => ( #t ( Symbol . datum ) datum ) • => ( #t ( foo . datum ) datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum ) • => ( #t ( datum+ . datum ) datum ) • => ( #t ( Symbol . datum ) datum ) • => ( #t ( foo . datum ) datum ) • => ( #t ( foo . list ) datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum ) • => ( #t ( datum+ . datum ) datum ) • => ( #t ( Symbol . datum ) datum ) • => ( #t ( foo . datum ) datum ) • => ( #t ( foo . list ) datum ) • => ( #t ( foo . () ) datum )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum ) • => ( #t ( datum+ . datum ) datum ) • => ( #t ( Symbol . datum ) datum ) • => ( #t ( foo . datum ) datum ) • => ( #t ( foo . list ) datum ) • => ( #t ( foo . () ) datum ) • => ( #t ( foo . () ) Number )
Sample Derivation • list => ( datum datum datum ) • => ( Boolean datum datum ) • => ( #t datum datum ) • => ( #t dotted datum ) • => ( #t ( datum+ . datum ) datum ) • => ( #t ( Symbol . datum ) datum ) • => ( #t ( foo . datum ) datum ) • => ( #t ( foo . list ) datum ) • => ( #t ( foo . () ) datum ) • => ( #t ( foo . () ) Number ) • => ( #t ( foo . () ) 3 )
Lambda Calculus • expr: 'Symbol' • | '(' 'lambda' '(' 'Symbol' ')' expr ')' • | '(' expr expr ')' • small language • variable references • function definition with single parameter • function call with one argument • http://www.cs.rit.edu/~ats/projects/oops/edu/doc/edu/rit/cs/oops/examples/Lambda.html
Proof by Induction • bintree: 'Number' • | '(' 'Symbol' bintree bintree ')' • has an odd number of nodes: • (0) Hypothesis: trees of size <= k have odd number of nodes. Show that this holds for any k. • (1) k=0: there are no such bintrees. (0) true. • assume (0) true up to some k. Look at tree of size k+1: bintree: 'Number' has one node. (0) true. | '(' 'Symbol' bintree bintree ')' has one plus size of two smaller tress, i.e., 1+odd+odd, nodes. (0) true.
Proof by Structural Induction • Strategy: • (0) some hypothesis. • (1) show (0) on a simple structure, i.e., on one without substructures. • (2) show (0) on a structure with substructures: if it is true on the substructures it is true on the composite.